/** * Clear all expectations and logs that match the http * * @param httpRequest the http request that is matched against when deciding whether to clear each expectation if null all expectations are cleared */ public MockServerClient clear(HttpRequest httpRequest) { sendRequest(request().withMethod("PUT").withPath(calculatePath("clear")).withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", StandardCharsets.UTF_8)); return clientClass.cast(this); }
@Override public HttpRequest handle(HttpRequest httpRequest) { return request() .withHeader("Host", "localhost:" + httpRequest.getFirstHeader("x-echo-server-port")) .withHeader("x-test", httpRequest.getFirstHeader("x-test")) .withBody("some_overridden_body") .withSecure(httpRequest.isSecure()); } }
public HttpRequest update(HttpRequest replaceRequest) { if (!Strings.isNullOrEmpty(replaceRequest.getMethod().getValue())) { withMethod(replaceRequest.getMethod()); } if (!Strings.isNullOrEmpty(replaceRequest.getPath().getValue())) { withPath(replaceRequest.getPath()); } for (Header header : replaceRequest.getHeaderList()) { getHeaders().replaceEntry(header); } for (Cookie cookie : replaceRequest.getCookieList()) { withCookie(cookie); } for (Parameter parameter : replaceRequest.getQueryStringParameterList()) { getQueryStringParameters().replaceEntry(parameter); } if (replaceRequest.getBody() != null) { withBody(replaceRequest.getBody()); } if (replaceRequest.isSecure() != null) { withSecure(replaceRequest.isSecure()); } if (replaceRequest.isKeepAlive() != null) { withKeepAlive(replaceRequest.isKeepAlive()); } return this; } }
public HttpRequestDTO(HttpRequest httpRequest, Boolean not) { super(not); if (httpRequest != null) { method = httpRequest.getMethod(); path = httpRequest.getPath(); headers = httpRequest.getHeaders(); cookies = httpRequest.getCookies(); queryStringParameters = httpRequest.getQueryStringParameters(); body = BodyDTO.createDTO(httpRequest.getBody()); keepAlive = httpRequest.isKeepAlive(); secure = httpRequest.isSecure(); } }
public HttpRequest clone() { return not(request(), not) .withMethod(method) .withPath(path) .withQueryStringParameters(getQueryStringParameters().clone()) .withBody(body) .withHeaders(getHeaders().clone()) .withCookies(getCookies().clone()) .withKeepAlive(keepAlive) .withSecure(secure); }
/** * Reset MockServer by clearing all expectations */ public MockServerClient reset() { MockServerEventBus.getInstance().publish(EventType.RESET); sendRequest(request().withMethod("PUT").withPath(calculatePath("reset"))); return clientClass.cast(this); }
request() .withPath(calculatePath("some_path")) .withBody("some_request_body"), makeRequest( request() .withPath(calculatePath("some_path")) .withHeader("name", "value") .withBody("some_request_body"), headersToIgnore) ); .withBody("some_request_body"), makeRequest( request() .withSecure(true) .withPath(calculatePath("some_path")) .withHeader("name", "value") .withBody("some_request_body"), headersToIgnore) );
request() .withPath(calculatePath("callback")) request() .withPath(calculatePath("callback")) .withMethod("POST") .withHeaders( header("X-Test", "test_headers_and_body") .withBody("an_example_body_http"), headersToIgnore) ); assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(0).getBody().getValue(), "an_example_body_http"); assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(0).getPath().getValue(), calculatePath("callback")); request() .withSecure(true) .withPath(calculatePath("callback")) .withMethod("POST") .withHeaders( header("X-Test", "test_headers_and_body") .withBody("an_example_body_https"), headersToIgnore assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(1).getBody().getValue(), "an_example_body_https"); assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(1).getPath().getValue(), calculatePath("callback"));
request() .withMethod("POST") .withPath(calculatePath("some_pathRequest")) .withQueryStringParameters( param("queryStringParameterOneName", "queryStringParameterOneValueOne", "queryStringParameterOneValueTwo"), param("queryStringParameterTwoName", "queryStringParameterTwoValue") .withBody("some_bodyRequest") .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request() .withMethod("POST") .withPath(calculatePath("some_pathRequest")) .withQueryStringParameters( param("queryStringParameterOneName", "OTHERqueryStringParameterOneValueOne", "queryStringParameterOneValueTwo"), param("queryStringParameterTwoName", "queryStringParameterTwoValue") .withBody("some_bodyRequest") .withHeaders(header("headerNameRequest", "headerValueRequest")) .withCookies(cookie("cookieNameRequest", "cookieValueRequest")), headersToIgnore) );
public HttpRequest buildObject() { return new HttpRequest() .withMethod(method) .withPath(path) .withQueryStringParameters(queryStringParameters) .withBody((body != null ? Not.not(body.buildObject(), body.getNot()) : null)) .withHeaders(headers) .withCookies(cookies) .withSecure(secure) .withKeepAlive(keepAlive); }
protected HttpResponse makeRequest(HttpRequest httpRequest, Collection<String> headersToIgnore) { try { boolean isSsl = httpRequest.isSecure() != null && httpRequest.isSecure(); int port = (isSsl ? getServerSecurePort() : getServerPort()); httpRequest.withPath(addContextToPath(httpRequest.getPath().getValue())); httpRequest.withHeader(HOST.toString(), "localhost:" + port); boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0; HttpResponse httpResponse = httpClient.sendRequest(httpRequest, new InetSocketAddress("localhost", port))
@Override public Long call() throws Exception { long start = System.currentTimeMillis(); makeRequest(request("/slow"), Collections.<String>emptySet()); return System.currentTimeMillis() - start; } });
@Override public HttpResponse call() throws Exception { return httpClient.sendRequest( request(addContextToPath(calculatePath("delayed"))) .withHeader(HOST.toString(), "localhost:" + getServerPort()) ).get(10, TimeUnit.SECONDS); } });
@Test public void shouldReturnResponseByMatchingStringBody() throws IOException, URISyntaxException { // when getMockServerClient() .when( request() .withBody( exact("some_random_body") ), Times.exactly(2) ) .respond( response() .withBody("some_string_body_response") ); // then HttpClient httpClient = createHttpClient(); HttpPost request = new HttpPost( new URIBuilder() .setScheme("http") .setHost("localhost") .setPort(getServerPort()) .setPath(addContextToPath("some_path")) .build() ); request.setEntity(new StringEntity("some_random_body")); HttpResponse response = httpClient.execute(request); assertThat(new String(EntityUtils.toByteArray(response.getEntity()), UTF_8), is("some_string_body_response")); assertThat(response.getStatusLine().getStatusCode(), is(OK_200.code())); }
@Override public void serialize(HttpRequest httpRequest, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject(); if (httpRequest.getNot() != null && httpRequest.getNot()) { jgen.writeBooleanField("not", httpRequest.getNot()); if (httpRequest.getMethod() != null && !Strings.isNullOrEmpty(httpRequest.getMethod().getValue())) { jgen.writeObjectField("method", httpRequest.getMethod()); if (httpRequest.getPath() != null && !Strings.isNullOrEmpty(httpRequest.getPath().getValue())) { jgen.writeObjectField("path", httpRequest.getPath()); if (httpRequest.getQueryStringParameterList() != null && !httpRequest.getQueryStringParameterList().isEmpty()) { jgen.writeObjectField("queryStringParameters", httpRequest.getQueryStringParameters()); if (httpRequest.getHeaderList() != null && !httpRequest.getHeaderList().isEmpty()) { jgen.writeObjectField("headers", httpRequest.getHeaders()); if (httpRequest.getCookieList() != null && !httpRequest.getCookieList().isEmpty()) { jgen.writeObjectField("cookies", httpRequest.getCookies()); if (httpRequest.isKeepAlive() != null) { jgen.writeBooleanField("keepAlive", httpRequest.isKeepAlive()); if (httpRequest.isSecure() != null) { jgen.writeBooleanField("secure", httpRequest.isSecure()); if (httpRequest.getBody() != null && !Strings.isNullOrEmpty(String.valueOf(httpRequest.getBody().getValue()))) { jgen.writeObjectField("body", httpRequest.getBody());
private void setMethod(HttpRequest httpRequest, HttpServletRequest httpServletRequest) { httpRequest.withMethod(httpServletRequest.getMethod()); }
public HttpRequestTemplateObject(HttpRequest httpRequest) { if (httpRequest != null) { method = httpRequest.getMethod().getValue(); path = httpRequest.getPath().getValue(); for (Header header : httpRequest.getHeaderList()) { headers.put(header.getName().getValue(), Lists.transform(header.getValues(), new Function<NottableString, String>() { public String apply(NottableString input) { return input.getValue(); } })); } for (Cookie cookie : httpRequest.getCookieList()) { cookies.put(cookie.getName().getValue(), cookie.getValue().getValue()); } for (Parameter parameter : httpRequest.getQueryStringParameterList()) { queryStringParameters.put(parameter.getName().getValue(), Lists.transform(parameter.getValues(), new Function<NottableString, String>() { public String apply(NottableString input) { return input.getValue(); } })); } body = BodyDTO.createDTO(httpRequest.getBody()); keepAlive = httpRequest.isKeepAlive(); secure = httpRequest.isSecure(); } }
/** * The path to match on such as "/some_mocked_path" any servlet context path is ignored for matching and should not be specified here * regex values are also supported such as ".*_path", see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html * for full details of the supported regex syntax * * @param path the path such as "/some_mocked_path" or a regex */ public HttpRequest withPath(String path) { withPath(string(path)); return this; }