@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_return_content_based_on_xpath() throws Exception { server.request(eq(xpath("/request/parameters/id/text()"), "1")).response("foo"); server.request(eq(xpath("/request/parameters/id/text()"), "2")).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postFile(root(), "foo.xml"), is("foo")); assertThat(helper.postFile(root(), "bar.xml"), is("bar")); } }); }
@Test public void should_match_form_value() throws Exception { server.post(eq(form("name"), "dreamhead")).response("foobar"); running(server, new Runnable() { @Override public void run() throws Exception { org.apache.http.client.fluent.Request request = Post(root()).bodyForm(new BasicNameValuePair("name", "dreamhead")); assertThat(helper.executeAsString(request), is("foobar")); } }); }
@Test(expected = HttpResponseException.class) public void should_not_return_anything_for_unknown_xpath() throws Exception { server.request(eq(xpath("/response/parameters/id/text()"), "3")).response("foo"); running(server, new Runnable() { @Override public void run() throws Exception { helper.postFile(root(), "foo.xml"); } }); }
@Test public void should_eq_header() throws Exception { server.request(eq(header("foo"), "bar")).response("blah"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.getWithHeader(root(), of("foo", "bar")), is("blah")); } }); }
@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_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_match_global_header_with_any_response() throws Exception { server = httpServer(port(), request(eq(header("foo"), "bar"))); server.response("blah"); running(server, new Runnable() { @Override public void run() throws Exception { String result = helper.getWithHeader(root(), of("foo", "bar")); assertThat(result, is("blah")); } }); }
@Test public void should_match_form_value_with_charset() throws Exception { server = httpServer(port(), log()); server.post(eq(form("name"), "表单")).response("foobar"); running(server, new Runnable() { @Override public void run() throws Exception { org.apache.http.client.fluent.Request request = Post(root()).bodyForm(of(new BasicNameValuePair("name", "表单")), Charset.forName("GBK")); assertThat(helper.executeAsString(request), is("foobar")); } }); }
@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_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(expected = HttpResponseException.class) public void should_throw_exception_without_match_mount() throws Exception { final String MOUNT_DIR = "src/test/resources/test"; server = httpServer(port(), request(eq(header("foo"), "bar"))); server.mount(MOUNT_DIR, to("/dir")); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/dir/dir.response")), is("response from dir")); } }); }
@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_set_and_recognize_cookie() throws Exception { server.request(eq(cookie("loggedIn"), "true")).response(status(200)); server.response(cookie("loggedIn", "true"), status(302)); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.getForStatus(root()), is(302)); assertThat(helper.getForStatus(root()), is(200)); } }); }
@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_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_global_header() 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.getWithHeader(remoteUrl("/global-request"), of("foo", "bar")); assertThat(result, is("blah")); } }); }
@Test(expected = HttpResponseException.class) public void should_throw_exception_without_global_matcher_for_exist() throws Exception { server = httpServer(port(), request(eq(header("foo"), "bar"))); server.request(exist(header("blah"))).response("header"); running(server, new Runnable() { @Override public void run() throws Exception { helper.getWithHeader(root(), of("blah", "any")); } }); }
@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")); } }); }