private HttpResponseSetting requestByHttpMethod(final HttpMethod method, final RequestMatcher matcher) { return request(and(by(method(method)), matcher)); }
@Test public void should_match_method() throws Exception { server.request(match(method("get|post"))).response("bar"); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/bar/foo")), is("bar")); assertThat(helper.postContent(remoteUrl("/blah/foo"), "content"), is("bar")); } }); }
@Test public void should_match_get_method_by_method_api() throws Exception { server.request(and(by(uri("/foo")), by(method("get")))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/foo")), is("bar")); } }); }
@Test public void should_match_put_method_via_api() throws Exception { server.request(and(by(uri("/foo")), by(method("put")))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { Request request = Request.Put(remoteUrl("/foo")); assertThat(helper.executeAsString(request), is("bar")); } }); }
@Test public void should_match_get_method_by_method_api_with_http_method() throws Exception { server.request(and(by(uri("/foo")), by(method(HttpMethod.GET)))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/foo")), is("bar")); } }); }
@Test public void should_match_delete_method_via_api() throws Exception { server.request(and(by(uri("/foo")), by(method("delete")))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { Request request = Request.Delete(remoteUrl("/foo")); String response = helper.executeAsString(request); assertThat(response, is("bar")); } }); }
@Test public void should_proxy_with_request_method() throws Exception { server.get(by(uri("/target"))).response("get_proxy"); server.post(and(by(uri("/target")), by("proxy"))).response("post_proxy"); server.request(and(by(uri("/target")), by(method("put")), by("proxy"))).response("put_proxy"); server.request(and(by(uri("/target")), by(method("delete")))).response("delete_proxy"); server.request(and(by(uri("/target")), by(method("head")))).response(status(200)); server.request(and(by(uri("/target")), by(method("options")))).response("options_proxy"); server.request(and(by(uri("/target")), by(method("trace")))).response("trace_proxy"); server.request(by(uri("/proxy"))).response(proxy(remoteUrl("/target"))); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/proxy")), is("get_proxy")); assertThat(helper.postContent(remoteUrl("/proxy"), "proxy"), is("post_proxy")); Request putRequest = Request.Put(remoteUrl("/proxy")).bodyString("proxy", ContentType.DEFAULT_TEXT); assertThat(helper.executeAsString(putRequest), is("put_proxy")); Request deleteRequest = Request.Delete(remoteUrl("/proxy")); assertThat(helper.executeAsString(deleteRequest), is("delete_proxy")); Request headRequest = Request.Head(remoteUrl("/proxy")); StatusLine headStatusLine = helper.execute(headRequest).getStatusLine(); assertThat(headStatusLine.getStatusCode(), is(200)); Request optionsRequest = Request.Options(remoteUrl("/proxy")); assertThat(helper.executeAsString(optionsRequest), is("options_proxy")); Request traceRequest = Request.Trace(remoteUrl("/proxy")); assertThat(helper.executeAsString(traceRequest), is("trace_proxy")); } }); }
@Test public void can_execute_a_HEAD_request() throws Exception { server.request(and(by(uri("/")), by(method(HEAD)))).response("foo"); running(server, () -> assertThat( http.execute(Request.head(BASE_URL)), instanceOf(Response.Success.class) )); }
@Test public void can_execute_a_GET_request() throws Exception { server.request(and(by(uri("/")), by(method(GET)))).response("foo"); running(server, () -> assertThat( http.execute(Request.get(BASE_URL)).orElseThrow(BriefcaseException::new), containsString("foo") )); }
@Test public void can_handle_5xx_errors() throws Exception { server.request(and(by(uri("/")), by(method(GET)))).response(status(500)); running(server, () -> assertThat( http.execute(Request.get(BASE_URL)), instanceOf(Response.ServerError.class) )); }
@Test public void can_handle_4xx_errors() throws Exception { server.request(and(by(uri("/")), by(method(GET)))).response(status(400)); running(server, () -> assertThat( http.execute(Request.get(BASE_URL)), instanceOf(Response.ClientError.class) )); }
@Test public void can_handle_3xx_errors() throws Exception { server.request(and(by(uri("/")), by(method(GET)))).response(status(302)); running(server, () -> assertThat( http.execute(Request.get(BASE_URL)), instanceOf(Response.Redirection.class) )); } }
@Test public void can_handle_authorization() throws Exception { // We'll respond 401 if no Authorization header is received server.request(and( by(uri("/")), by(method(GET)), not(exist(header("Authorization"))) )).response( status(401), // We include a Digest challenge header on the response header("WWW-Authenticate", "Digest realm=\"Aggregate ODK Aggregate\", qop=\"auth\", nonce=\"MTUzNjA1NzY1MjI2NDoxNjIwYTZmMmJkYTNiNGRkMTA2MjI4ZjM4YjE0ZDIyMA==\"") ); // We'll respond 200 if we get a valid Authorization header server.request(and( by(uri("/")), by(method(GET)), // The Authorization header should include the expected username contain(header("Authorization"), "username=\"username\""), // The Authorization header should include the expected realm (from the challenge) contain(header("Authorization"), "realm=\"Aggregate ODK Aggregate\""), // The Authorization header should include the expected nonce (from the challenge) contain(header("Authorization"), "nonce=\"MTUzNjA1NzY1MjI2NDoxNjIwYTZmMmJkYTNiNGRkMTA2MjI4ZjM4YjE0ZDIyMA==\""), // The Authorization header should include the challenge response match(header("Authorization"), ".+response=\"[a-z0-9]+\".+") )).response("foo"); running(server, () -> assertThat( http.execute(Request.get(BASE_URL, Credentials.from("username", "password"))), instanceOf(Response.Success.class) )); }
.request(and( by(uri("/users/20")), by(method("PATCH")) )) .response(json(of("username", "lj"))); .request(and( by(uri("/users")), by(method("HEAD")) )) .response( .request(and( by(uri("/users")), by(method("OPTIONS")) )) .response( .request(and( by(uri("/users")), by(method("TRACE")) )).response(header("Content-Type", "message/http")); .request(and( by(uri("/mock_login")), by(method("POST")) )) .response(