@Test public void should_redirect_to_expected_url() throws Exception { server.get(by(uri("/"))).response("foo"); server.get(by(uri("/redirectTo"))).redirectTo(root()); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/redirectTo")), is("foo")); } }); }
@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(expected = HttpResponseException.class) public void should_not_response_for_get_while_http_method_is_not_get() throws Exception { server.get(by(uri("/foo"))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { helper.postContent(remoteUrl("/foo"), ""); } }); }
@Test public void should_match_get_method() throws Exception { server.get(by(uri("/foo"))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/foo")), is("bar")); } }); }
@Test public void should_proxy_a_batch_of_urls() throws Exception { server.get(by(uri("/target/1"))).response("target_1"); server.get(by(uri("/target/2"))).response("target_2"); server.get(match(uri("/proxy/.*"))).response(proxy(from("/proxy").to(remoteUrl("/target")))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/proxy/1")), is("target_1")); assertThat(helper.get(remoteUrl("/proxy/2")), is("target_2")); } }); }
@Test public void should_batch_proxy_from_server() throws Exception { server.get(by(uri("/target/1"))).response("target_1"); server.get(by(uri("/target/2"))).response("target_2"); server.proxy(from("/proxy").to(remoteUrl("/target"))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/proxy/1")), is("target_1")); assertThat(helper.get(remoteUrl("/proxy/2")), is("target_2")); } }); }
@Test public void should_download_attachment() throws Exception { server.get(by(uri("/"))).response(attachment("foo.txt", file("src/test/resources/foo.response"))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/")), is("foo.response")); } }); } }
@Test public void should_redirect_for_any_response() throws Exception { server.get(by(uri("/"))).response("foo"); server.redirectTo(root()); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/redirectTo")), is("foo")); } }); }
@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 public void should_work_with_file_resource_url() throws Exception { server.get(by(uri("/target"))).response("get_proxy"); server.request(by(uri("/proxy"))).response(proxy(file("src/test/resources/remote_url.resource"))); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/proxy")), is("get_proxy")); } }); }
@Test public void should_verify_expected_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(by(uri("/foo")), times(1)); }
@Test public void should_work_with_template() throws Exception { server.get(by(uri("/target"))).response("get_proxy"); server.request(by(uri("/proxy"))).response(proxy(template("http://localhost:12306/${var}", "var", "target"))); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/proxy")), is("get_proxy")); } }); }
@Test public void should_verify_expected_request_for_once() 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(by(uri("/foo")), once()); }
@Test public void should_verify_expected_request_for_at_most() 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")); assertThat(helper.get(remoteUrl("/foo")), is("bar")); } }); hit.verify(by(uri("/foo")), atMost(2)); }
@Test public void should_verify_expected_request_for_at_least() 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")); assertThat(helper.get(remoteUrl("/foo")), is("bar")); } }); hit.verify(by(uri("/foo")), atLeast(1)); }
@Test(expected = VerificationException.class) public void should_fail_to_verify_at_most_expected_request_while_expectation_can_not_be_met() 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")); assertThat(helper.get(remoteUrl("/foo")), is("bar")); } }); hit.verify(by(uri("/foo")), atMost(1)); }
@Test public void should_verify_expected_request_for_between() 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")); assertThat(helper.get(remoteUrl("/foo")), is("bar")); assertThat(helper.get(remoteUrl("/foo")), is("bar")); } }); hit.verify(by(uri("/foo")), between(1, 3)); }
@Test public void should_verify_expected_request_and_log_at_same_time() throws Exception { final HttpServer server = httpServer(port(), hit, log()); 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(by(uri("/foo")), times(1)); }
@Test public void should_verify_expected_request_and_log_at_same_time_for_https() throws Exception { final HttpServer server = httpsServer(port(), DEFAULT_CERTIFICATE, hit, log()); server.get(by(uri("/foo"))).response("bar"); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteHttpsUrl("/foo")), is("bar")); } }); hit.verify(by(uri("/foo")), times(1)); } }
@Test public void should_return_redirect_with_template() throws Exception { server.get(by(uri("/"))).response("foo"); server.request(by(uri("/redirectTemplate"))).redirectTo(template("${var}", "var", root())); server.redirectTo(template("${var}", "var", root())); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/redirectTemplate")), is("foo")); assertThat(helper.get(remoteUrl("/anything")), is("foo")); } }); }