@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_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_return_expected_response_based_on_specified_request_with_text_api() throws Exception { server.request(by(text("foo"))).response(text("bar")); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postContent(root(), "foo"), is("bar")); } }); }
@Test public void should_return_content_one_by_one() throws Exception { server.request(by(uri("/foo"))).response(seq("bar", "blah")); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/foo")), is("bar")); assertThat(helper.get(remoteUrl("/foo")), is("blah")); assertThat(helper.get(remoteUrl("/foo")), is("blah")); } }); }
@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_patch_with_any_id() throws Exception { server.resource("targets", patch(anyId()).response(with(text("patch result"))) ); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.patchForResponse(remoteUrl("/targets/1"), "result"), is("patch result")); } }); }
@Test public void should_exist_header() throws Exception { server.request(exist(header("foo"))).response("header"); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.getWithHeader(root(), of("foo", "bar")), is("header")); assertThat(helper.getWithHeader(root(), of("foo", "blah")), is("header")); } }); }
@Test public void should_no_exception_form_get_request() throws Exception { server.request(eq(form("password"), "hello")).response("foobar"); server.response("foobar"); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(root()), is("foobar")); } }); }
@Test public void should_contain_for_resource() throws Exception { server.request(contain(header("foo"), "bar")).response("bar"); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.getWithHeader(root(), of("foo", "Abar")), is("bar")); assertThat(helper.getWithHeader(root(), of("foo", "barA")), is("bar")); } }); }
@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_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_return_content_based_on_xpath_existing() throws Exception { server.request(exist(xpath("/request/parameters/id/text()"))).response("foo"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postFile(root(), "foo.xml"), is("foo")); } }); } }
@Test public void should_reply_404_for_unknown_resource() throws Exception { server.resource("targets", get("2").response(with(text("hello")))); running(server, new Runnable() { @Override public void run() throws Exception { org.apache.http.HttpResponse response = helper.getResponse(remoteUrl("/targets/1")); assertThat(response.getStatusLine().getStatusCode(), is(404)); } }); }
@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_match_exact_json_with_resource() throws Exception { final String jsonContent = "{\"foo\":\"bar\"}"; server.request(by(json(jsonContent))).response("foo"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postContent(root(), jsonContent), is("foo")); } }); }
@Test public void should_patch() throws Exception { server.resource("targets", patch("1").response(with(text("patch result"))) ); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.patchForResponse(remoteUrl("/targets/1"), "result"), is("patch result")); } }); }