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); } }
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); } }
if (response.getStatusCode() != null && response.getStatusCode() == BAD_REQUEST.code()) { throw new IllegalArgumentException(response.getBodyAsString());
@Override public void serialize(HttpResponse httpResponse, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject(); if (httpResponse.getStatusCode() != null) { jgen.writeObjectField("statusCode", httpResponse.getStatusCode());
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(")");
@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\"]")); } }
@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\"")); } }
@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\"")); }
/** * 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; } }
@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\"")); }
@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")); }
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)); } }
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); } }
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); } }
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); } }
if (response.getStatusCode() != null && response.getStatusCode() == BAD_REQUEST.code()) { throw new IllegalArgumentException(response.getBodyAsString());
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(")");
/** * 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; } }
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)); } }
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); } }