@Override public RequestMatcher apply(final RequestMatcher target) { return and(requestMatcher, target); } }
public final RequestMatcher getRequestMatcher(final RestIdMatcher resourceName) { RequestMatcher rootMatcher = getBaseRequestMatcher(resourceName); if (this.matcher.isPresent()) { return and(rootMatcher, this.matcher.get()); } return rootMatcher; }
private HttpResponseSetting requestByHttpMethod(final HttpMethod method, final RequestMatcher matcher) { return request(and(by(method(method)), matcher)); }
@Test(expected = HttpResponseException.class) public void should_throw_exception_even_if_match_one_of_conditions() throws Exception { server.request(and(by("foo"), by(uri("/foo")))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { helper.get(remoteUrl("/foo")); } }); }
@Test public void should_send_post_request_to_target_on_complete_with_template_content() throws Exception { ResponseHandler handler = mock(ResponseHandler.class); server.request(and(by(uri("/target")), by("content"))).response(handler); server.request(by(uri("/event"))).response("event").on(complete(post(remoteUrl("/target"), template("${req.headers['foo']}")))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.getWithHeader(remoteUrl("/event"), ImmutableMultimap.of("foo", "content")), is("event")); } }); verify(handler).writeToResponse(any(SessionContext.class)); }
@Test public void should_return_expected_response_for_multiple_specified_query() throws Exception { server.request(and(by(uri("/foo")), eq(query("param"), "blah"))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/foo?param=multiple¶m=blah")), is("bar")); } }); }
@Test public void should_send_post_request_to_target_on_complete() throws Exception { ResponseHandler handler = mock(ResponseHandler.class); server.request(and(by(uri("/target")), by("content"))).response(handler); server.request(by(uri("/event"))).response("event").on(complete(post(remoteUrl("/target"), text("content")))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/event")), is("event")); } }); verify(handler).writeToResponse(any(SessionContext.class)); }
@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_return_expected_response_for_specified_query() throws Exception { server.request(and(by(uri("/foo")), eq(query("param"), "blah"))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/foo?param=blah")), 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_return_expected_response_for_empty_query() throws Exception { server.request(and(by(uri("/foo")), eq(query("param"), ""))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/foo?param")), 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_send_post_request_to_target_on_complete_with_resource_url() throws Exception { ResponseHandler handler = mock(ResponseHandler.class); server.request(and(by(uri("/target")), by("content"))).response(handler); server.request(by(uri("/event"))).response("event").on(complete(post(text(remoteUrl("/target")), text("content")))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/event")), is("event")); } }); verify(handler).writeToResponse(any(SessionContext.class)); }
@Test public void should_match_request_based_on_multiple_matchers() throws Exception { server.request(and(by("foo"), by(uri("/foo")))).response(text("bar")); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postContent(remoteUrl("/foo"), "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_eq_multiple_header_with_same_name() throws Exception { server.request(and(eq(header("foo"), "bar")), eq(header("foo"), "bar2")) .response("blah"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.getWithHeader(root(), of("foo", "bar", "foo", "bar2")), is("blah")); } }); }
@Test public void should_proxy_with_request_header() throws Exception { server.request(and(by(uri("/target")), eq(header("foo"), "foo"))).response("foo_proxy"); server.request(and(by(uri("/target")), eq(header("bar"), "bar"))).response("bar_proxy"); server.request(by(uri("/proxy"))).response(proxy(remoteUrl("/target"))); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.getWithHeader(remoteUrl("/proxy"), of("foo", "foo")), is("foo_proxy")); } }); }
@Test(expected = HttpResponseException.class) public void should_throw_exception_without_match_and_matcher() throws Exception { server = httpServer(port(), request(eq(header("foo"), "bar"))); server.request(and(by(uri("/foo")), eq(header("header"), "blah"))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { helper.getWithHeader(remoteUrl("/foo"), of("header", "blah")); } }); } }
@Test public void should_match_request_based_on_and_matcher() throws Exception { server = httpServer(port(), request(eq(header("foo"), "bar"))); server.request(and(by(uri("/foo")), eq(header("header"), "blah"))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.getWithHeader(remoteUrl("/foo"), of("foo", "bar", "header", "blah")), is("bar")); } }); }
@Test public void should_generate_response_with_cookie() throws Exception { server.request(and(by(uri("/cookie")), eq(cookie("templateLoggedIn"), "true"))).response(template("${req.cookies['templateLoggedIn']}")); server.request(by(uri("/cookie"))).response(cookie("templateLoggedIn", "true"), status(302)); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.getForStatus(remoteUrl("/cookie")), is(302)); assertThat(helper.get(remoteUrl("/cookie")), is("true")); } }); }