本文整理了Java中org.mockserver.model.HttpResponse.getStatusCode()
方法的一些代码示例,展示了HttpResponse.getStatusCode()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpResponse.getStatusCode()
方法的具体详情如下:
包路径:org.mockserver.model.HttpResponse
类名称:HttpResponse
方法名:getStatusCode
暂无
代码示例来源:origin: jamesdbloom/mockserver
private void setStatusCode(HttpResponse httpResponse, HttpServletResponse httpServletResponse) {
int statusCode = httpResponse.getStatusCode() != null ? httpResponse.getStatusCode() : 200;
if (httpResponse.getReasonPhrase() != null) {
httpServletResponse.setStatus(statusCode, httpResponse.getReasonPhrase());
} else {
httpServletResponse.setStatus(statusCode);
}
}
代码示例来源:origin: jamesdbloom/mockserver
private HttpResponseStatus getStatus(HttpResponse response) {
int statusCode = response.getStatusCode() != null ? response.getStatusCode() : 200;
if (!StringUtils.isEmpty(response.getReasonPhrase())) {
return new HttpResponseStatus(statusCode, response.getReasonPhrase());
} else {
return HttpResponseStatus.valueOf(statusCode);
}
}
代码示例来源:origin: jamesdbloom/mockserver
if (response.getStatusCode() != null &&
response.getStatusCode() == BAD_REQUEST.code()) {
throw new IllegalArgumentException(response.getBodyAsString());
代码示例来源:origin: jamesdbloom/mockserver
@Override
public void serialize(HttpResponse httpResponse, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeStartObject();
if (httpResponse.getStatusCode() != null) {
jgen.writeObjectField("statusCode", httpResponse.getStatusCode());
代码示例来源:origin: jamesdbloom/mockserver
if (httpResponse != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append("response()");
if (httpResponse.getStatusCode() != null) {
appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output).append(".withStatusCode(").append(httpResponse.getStatusCode()).append(")");
代码示例来源:origin: jamesdbloom/mockserver
@Test
public void shouldReturnErrorForInvalidRequestToVerifySequence() throws Exception {
// when
org.mockserver.model.HttpResponse httpResponse = new NettyHttpClient(clientEventLoopGroup, null).sendRequest(
request()
.withMethod("PUT")
.withHeader(HOST.toString(), "localhost:" + getProxyPort())
.withPath(addContextToPath("mockserver/verifySequence"))
.withBody("{" + NEW_LINE +
" \"httpRequest\": {" + NEW_LINE +
" \"path\": false" + NEW_LINE +
" }," + NEW_LINE +
" \"httpRequest\": {" + NEW_LINE +
" \"path\": 10" + NEW_LINE +
" }" + NEW_LINE +
"}")
).get(10, TimeUnit.SECONDS);
// then
assertThat(httpResponse.getStatusCode(), Is.is(400));
assertThat(httpResponse.getBodyAsString(), Is.is("1 error:" + NEW_LINE +
" - object instance has properties which are not allowed by the schema: [\"httpRequest\"]"));
}
}
代码示例来源:origin: jamesdbloom/mockserver
@Test
public void shouldReturnErrorForInvalidRequest() throws Exception {
// when
HttpResponse httpResponse = httpClient.sendRequest(
request()
.withMethod("PUT")
.withHeader(HOST.toString(), "localhost:" + this.getServerPort())
.withPath(addContextToPath("mockserver/clear"))
.withBody("{" + NEW_LINE +
" \"path\" : 500," + NEW_LINE +
" \"method\" : true," + NEW_LINE +
" \"keepAlive\" : \"false\"" + NEW_LINE +
" }")
).get(10, TimeUnit.SECONDS);
// then
assertThat(httpResponse.getStatusCode(), is(400));
assertThat(httpResponse.getBodyAsString(), is("3 errors:" + NEW_LINE +
" - instance type (string) does not match any allowed primitive type (allowed: [\"boolean\"]) for field \"/keepAlive\"" + NEW_LINE +
" - instance type (boolean) does not match any allowed primitive type (allowed: [\"string\"]) for field \"/method\"" + NEW_LINE +
" - instance type (integer) does not match any allowed primitive type (allowed: [\"string\"]) for field \"/path\""));
}
}
代码示例来源:origin: jamesdbloom/mockserver
@Test
public void shouldReturnErrorForInvalidRequestToVerify() throws Exception {
// when
org.mockserver.model.HttpResponse httpResponse = new NettyHttpClient(clientEventLoopGroup, null).sendRequest(
request()
.withMethod("PUT")
.withHeader(HOST.toString(), "localhost:" + getProxyPort())
.withPath(addContextToPath("mockserver/verify"))
.withBody("{" + NEW_LINE +
" \"httpRequest\": {" + NEW_LINE +
" \"path\": \"/simple\"" + NEW_LINE +
" }, " + NEW_LINE +
" \"times\": 1" + NEW_LINE +
"}")
).get(10, TimeUnit.SECONDS);
// then
assertThat(httpResponse.getStatusCode(), Is.is(400));
assertThat(httpResponse.getBodyAsString(), Is.is("1 error:" + NEW_LINE +
" - instance type (integer) does not match any allowed primitive type (allowed: [\"object\"]) for field \"/times\""));
}
代码示例来源:origin: jamesdbloom/mockserver
/**
* Returns whether server MockServer is running, by polling the MockServer a configurable amount of times
*/
public boolean isRunning(int attempts, long timeout, TimeUnit timeUnit) {
try {
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("status")));
if (httpResponse.getStatusCode() == HttpStatusCode.OK_200.code()) {
return true;
} else if (attempts == 0) {
return false;
} else {
try {
timeUnit.sleep(timeout);
} catch (InterruptedException e) {
// ignore interrupted exception
}
return isRunning(attempts - 1, timeout, timeUnit);
}
} catch (SocketConnectionException sce) {
return false;
}
}
代码示例来源:origin: jamesdbloom/mockserver
@Test
public void shouldReturnErrorForInvalidRequestToClear() throws Exception {
// when
org.mockserver.model.HttpResponse httpResponse = new NettyHttpClient(clientEventLoopGroup, null).sendRequest(
request()
.withMethod("PUT")
.withHeader(HOST.toString(), "localhost:" + getProxyPort())
.withPath(addContextToPath("mockserver/clear"))
.withBody("{" + NEW_LINE +
" \"path\" : 500," + NEW_LINE +
" \"method\" : true," + NEW_LINE +
" \"keepAlive\" : \"false\"" + NEW_LINE +
" }")
).get(10, TimeUnit.SECONDS);
// then
assertThat(httpResponse.getStatusCode(), Is.is(400));
assertThat(httpResponse.getBodyAsString(), Is.is("3 errors:" + NEW_LINE +
" - instance type (string) does not match any allowed primitive type (allowed: [\"boolean\"]) for field \"/keepAlive\"" + NEW_LINE +
" - instance type (boolean) does not match any allowed primitive type (allowed: [\"string\"]) for field \"/method\"" + NEW_LINE +
" - instance type (integer) does not match any allowed primitive type (allowed: [\"string\"]) for field \"/path\""));
}
代码示例来源:origin: jamesdbloom/mockserver
@Test
public void shouldReturnErrorForInvalidExpectation() throws Exception {
// when
HttpResponse httpResponse = httpClient.sendRequest(
request()
.withMethod("PUT")
.withHeader(HOST.toString(), "localhost:" + this.getServerPort())
.withPath(addContextToPath("mockserver/expectation"))
.withBody("{" + NEW_LINE +
" \"httpRequest\" : {" + NEW_LINE +
" \"path\" : \"/path_one\"" + NEW_LINE +
" }," + NEW_LINE +
" \"incorrectField\" : {" + NEW_LINE +
" \"body\" : \"some_body_one\"" + NEW_LINE +
" }," + NEW_LINE +
" \"times\" : {" + NEW_LINE +
" \"remainingTimes\" : 1" + NEW_LINE +
" }," + NEW_LINE +
" \"timeToLive\" : {" + NEW_LINE +
" \"unlimited\" : true" + NEW_LINE +
" }" + NEW_LINE +
"}")
).get(10, TimeUnit.SECONDS);
// then
assertThat(httpResponse.getStatusCode(), is(400));
assertThat(httpResponse.getBodyAsString(), is("2 errors:" + NEW_LINE +
" - object instance has properties which are not allowed by the schema: [\"incorrectField\"]" + NEW_LINE +
" - oneOf of the following must be specified [\"httpResponse\", \"httpResponseTemplate\", \"httpResponseObjectCallback\", \"httpResponseClassCallback\", \"httpForward\", \"httpForwardTemplate\", \"httpForwardObjectCallback\", \"httpForwardClassCallback\", \"httpOverrideForwardedRequest\", \"httpError\"] but 0 found"));
}
代码示例来源:origin: jamesdbloom/mockserver
void sendExpectation(Expectation expectation) {
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("expectation")).withBody(expectation != null ? expectationSerializer.serialize(expectation) : "", StandardCharsets.UTF_8));
if (httpResponse != null && httpResponse.getStatusCode() != 201) {
throw new ClientException(formatLogMessage("error:{}while submitted expectation:{}", httpResponse.getBody(), expectation));
}
}
代码示例来源:origin: jamesdbloom/mockserver
public HttpResponseDTO(HttpResponse httpResponse) {
if (httpResponse != null) {
statusCode = httpResponse.getStatusCode();
reasonPhrase = httpResponse.getReasonPhrase();
body = BodyWithContentTypeDTO.createDTO(httpResponse.getBody());
headers = httpResponse.getHeaders();
cookies = httpResponse.getCookies();
delay = (httpResponse.getDelay() != null ? new DelayDTO(httpResponse.getDelay()) : null);
connectionOptions = (httpResponse.getConnectionOptions() != null ? new ConnectionOptionsDTO(httpResponse.getConnectionOptions()) : null);
}
}
代码示例来源:origin: org.mock-server/mockserver-core
private void setStatusCode(HttpResponse httpResponse, HttpServletResponse httpServletResponse) {
int statusCode = httpResponse.getStatusCode() != null ? httpResponse.getStatusCode() : 200;
if (httpResponse.getReasonPhrase() != null) {
httpServletResponse.setStatus(statusCode, httpResponse.getReasonPhrase());
} else {
httpServletResponse.setStatus(statusCode);
}
}
代码示例来源:origin: org.mock-server/mockserver-core
private HttpResponseStatus getStatus(HttpResponse response) {
int statusCode = response.getStatusCode() != null ? response.getStatusCode() : 200;
if (!StringUtils.isEmpty(response.getReasonPhrase())) {
return new HttpResponseStatus(statusCode, response.getReasonPhrase());
} else {
return HttpResponseStatus.valueOf(statusCode);
}
}
代码示例来源:origin: org.mock-server/mockserver-client-java
if (response.getStatusCode() != null &&
response.getStatusCode() == BAD_REQUEST.code()) {
throw new IllegalArgumentException(response.getBodyAsString());
代码示例来源:origin: org.mock-server/mockserver-core
if (httpResponse != null) {
appendNewLineAndIndent(numberOfSpacesToIndent * INDENT_SIZE, output).append("response()");
if (httpResponse.getStatusCode() != null) {
appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output).append(".withStatusCode(").append(httpResponse.getStatusCode()).append(")");
代码示例来源:origin: org.mock-server/mockserver-client-java
/**
* Returns whether server MockServer is running, by polling the MockServer a configurable amount of times
*/
public boolean isRunning(int attempts, long timeout, TimeUnit timeUnit) {
try {
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("status")));
if (httpResponse.getStatusCode() == HttpStatusCode.OK_200.code()) {
return true;
} else if (attempts == 0) {
return false;
} else {
try {
timeUnit.sleep(timeout);
} catch (InterruptedException e) {
// ignore interrupted exception
}
return isRunning(attempts - 1, timeout, timeUnit);
}
} catch (SocketConnectionException sce) {
return false;
}
}
代码示例来源:origin: org.mock-server/mockserver-client-java
void sendExpectation(Expectation expectation) {
HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("expectation")).withBody(expectation != null ? expectationSerializer.serialize(expectation) : "", StandardCharsets.UTF_8));
if (httpResponse != null && httpResponse.getStatusCode() != 201) {
throw new ClientException(formatLogMessage("error:{}while submitted expectation:{}", httpResponse.getBody(), expectation));
}
}
代码示例来源:origin: org.mock-server/mockserver-core
public HttpResponseDTO(HttpResponse httpResponse) {
if (httpResponse != null) {
statusCode = httpResponse.getStatusCode();
reasonPhrase = httpResponse.getReasonPhrase();
body = BodyWithContentTypeDTO.createDTO(httpResponse.getBody());
headers = httpResponse.getHeaders();
cookies = httpResponse.getCookies();
delay = (httpResponse.getDelay() != null ? new DelayDTO(httpResponse.getDelay()) : null);
connectionOptions = (httpResponse.getConnectionOptions() != null ? new ConnectionOptionsDTO(httpResponse.getConnectionOptions()) : null);
}
}
内容来源于网络,如有侵权,请联系作者删除!