private RequestMatcher createRequestMatcherFromValue(final String name, final Object value) { if ("json".equalsIgnoreCase(name)) { return by(Moco.json(value)); } if (Map.class.isInstance(value)) { return createCompositeMatcher(name, castToMap(value)); } if (TextContainer.class.isInstance(value)) { return createSingleTextMatcher(name, TextContainer.class.cast(value)); } throw new IllegalArgumentException("unknown configuration :" + value); }
@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_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_post_method() throws Exception { server.post(by("foo")).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postContent(root(), "foo"), is("bar")); } }); }
@Test public void should_config_context() throws Exception { server.get(by(uri("/foo"))).response("foo"); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/context/foo")), is("foo")); } }); }
@Test public void should_return_expected_response_based_on_path_resource() throws Exception { server.request(by(pathResource("foo.request"))).response("foo"); running(server, new Runnable() { @Override public void run() throws Exception { URL resource = Resources.getResource("foo.request"); InputStream stream = resource.openStream(); assertThat(helper.postStream(root(), stream), is("foo")); } }); }
@Test public void should_generate_response_from_file_with_variable_map() throws Exception { server.request(by(uri("/template"))).response(template(file("src/test/resources/var.template"), of("var", jsonPath("$.book.price")))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.postContent(remoteUrl("/template"), "{\"book\":{\"price\":\"2\"}}"), is("2")); assertThat(helper.postContent(remoteUrl("/template"), "{\"book\":{\"price\":\"1\"}}"), is("1")); } }); }
@Test(expected = VerificationException.class) public void should_fail_to_verify_while_expectation_can_not_be_met() throws Exception { httpServer(port(), hit); hit.verify(by(uri("/foo")), times(1)); }
@Test public void should_match_request_based_on_not_matcher() throws Exception { server.request(not(by(uri("/foo")))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/bar")), is("bar")); } }); }
@Test public void should_match_extreme_big_request() throws Exception { server.request(by(times("a", 1025))).response(line("long_a")); running(server, new Runnable() { @Override public void run() throws Exception { helper.connect(); assertThat(helper.send(times("a", 1025)), is("long_a")); helper.close(); } }); }
@Test public void should_generate_response_with_http_header() throws Exception { server.request(by(uri("/template"))).response(template("${req.headers['foo']}")); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.getWithHeader(remoteUrl("/template"), ImmutableMultimap.of("foo", "bar")), is("bar")); } }); }
@Test public void should_generate_response_with_uri() throws Exception { server.request(by(uri("/template"))).response(template("${req.uri}")); running(server, new Runnable() { @Override public void run() throws IOException { String response = helper.get(remoteUrl("/template")); assertThat(response, is("/template")); } }); }
@Test public void should_return_json() throws Exception { server.request(by(uri("/template"))).response(template("${req.json.code} ${req.json.message}")); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postContent(remoteUrl("/template"), "{\n\t\"code\":1,\n\t\"message\":\"message\"\n}"), is("1 message")); } }); }
@Test public void should_generate_response_with_form() throws Exception { server.request(by(uri("/template"))).response(template("${req.forms['name']}")); running(server, new Runnable() { @Override public void run() throws IOException { Request request = Request.Post(remoteUrl("/template")).bodyForm(new BasicNameValuePair("name", "dreamhead")); assertThat(helper.executeAsString(request), is("dreamhead")); } }); }
@Test public void should_generate_response_with_two_variables() throws Exception { server.request(by(uri("/template"))).response(template("${foo} ${bar}", "foo", "ANOTHER", "bar", "TEMPLATE")); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/template")), is("ANOTHER TEMPLATE")); } }); }
@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_verify_unexpected_request_without_unexpected_request() throws Exception { final HttpServer server = httpServer(port(), hit); server.get(by(uri("/foo"))).response("bar"); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/foo")), is("bar")); } }); hit.verify(unexpected(), never()); }
@Test(expected = HttpResponseException.class) public void should_throw_exception_without_global_matcher() throws Exception { server = httpServer(port(), request(eq(header("foo"), "bar"))); server.request(by(uri("/global-request"))).response("blah"); running(server, new Runnable() { @Override public void run() throws Exception { String result = helper.get(remoteUrl("/global-request")); assertThat(result, is("blah")); } }); }
@Test public void should_match_request_based_on_not_matcher() throws Exception { server = httpServer(port(), request(eq(header("foo"), "bar"))); server.request(not(by(uri("/foo")))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.getWithHeader(remoteUrl("/bar"), of("foo", "bar")), is("bar")); } }); }
@Test(expected = HttpResponseException.class) public void should_throw_exception_without_match_not() throws Exception { server = httpServer(port(), request(eq(header("foo"), "bar"))); server.request(not(by(uri("/foo")))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { helper.get(remoteUrl("/bar")); } }); }