@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")); } }); }
private HttpResponseSetting bindToSession(final HttpServer server) { if (isMount()) { return server.mount(mount.getDir(), to(mount.getUri()), mount.getMountPredicates()) .response(mount.getResponseHandler()); } if (isProxy()) { if (proxy.hasUrl()) { throw new IllegalArgumentException("It's not allowed to have URL in proxy from server"); } return server.proxy(proxy.getProxyConfig(), proxy.getFailover()); } if (isAnyResponse()) { return server.response(getResponseHandler()); } HttpResponseSetting targetRequest = server.request(getRequestMatcher()); if (isRedirectResponse()) { return targetRequest.redirectTo(this.redirectTo.asResource()); } return targetRequest.response(getResponseHandler()); }
@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")); } }); }
@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_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_have_context_even_if_there_is_no_context_configured() throws Exception { server.response("foo"); running(server, new Runnable() { @Override public void run() throws Exception { String content = helper.get(remoteUrl("/context")); assertThat(content, is("foo")); } }); }
@Before("@http") public void mock() { server.server() .post(and( by(uri("/not_json")), by("text"), .get(and( by(uri("/cookie")), eq(cookie("key"), "value"))) .get(by(uri("/users/list"))) .response(json( newArrayList( .get(by(uri("/users/me"))) .response(json(of("username", "jakim", "age", 18, "iq", 80.0))); .get(by(uri("/getnull"))) .response(text("{\"notexist\":null}")); .get(or( and( by(uri("/users")), .post(by(uri("/empty_request"))) .response(status(201)) .response(json(of("name", "someone"))); .post(and(
@Test public void should_failover_with_many_response_content() throws Exception { server.get(by(uri("/target"))).response("get_proxy"); server.post(and(by(uri("/target")), by("proxy"))).response("post_proxy"); final File tempFile = tempFolder.newFile(); server.request(by(uri("/proxy"))).response(proxy(remoteUrl("/target"), failover(tempFile.getAbsolutePath()))); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/proxy")), is("get_proxy")); assertThat(helper.postContent(remoteUrl("/proxy"), "proxy"), is("post_proxy")); String failoverContent = Files.toString(tempFile, Charset.defaultCharset()); assertThat(failoverContent, containsString("get_proxy")); assertThat(failoverContent, containsString("post_proxy")); } }); }
@Test(expected = HttpResponseException.class) public void should_return_bad_request_for_nonexistence_file() throws Exception { server.mount(MOUNT_DIR, to("/dir")); running(server, new Runnable() { @Override public void run() throws IOException { helper.get(remoteUrl("/dir/unknown.response")); } }); }
@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_failover_with_response_content() throws Exception { server.post(and(by(uri("/target")), by("proxy"))).response("proxy"); final File tempFile = tempFolder.newFile(); server.request(by(uri("/proxy"))).response(proxy(remoteUrl("/target"), failover(tempFile.getAbsolutePath()))); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postContent(remoteUrl("/proxy"), "proxy"), is("proxy")); assertThat(Files.toString(tempFile, Charset.defaultCharset()), containsString("proxy")); } }); }
@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_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")); } }); }
@Override public void run() throws Exception { assertThat(mergedServer.port(), is(12306)); } });
@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(expected = HttpResponseException.class) public void should_not_proxy_url_for_unmatching_url_for_batch_proxy_from_server() throws Exception { server.proxy(from("/proxy").to(remoteUrl("/target"))); running(server, new Runnable() { @Override public void run() throws Exception { helper.get(remoteUrl("/proxy1/1")); } }); }
@Before("@variables or @code") public void mockForVariableTests() { server.server() .get(by(uri("/not_important"))) .response(json(of( "name", "panda", "age", 18, "iq", 80.0 ))); server.server() .get(by(uri("/simple_response"))) .response(text("SIMPLE_RESPONSE")); server.server() .post(and( by(uri("/users")), eq(jsonPath("$.name"), "someone"))) .response(status(201)) .response(json(of("user", "someone_created"))); server.start(); }
@Test public void should_match_put_method() throws Exception { server.put(by("foo")).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { Request request = Request.Put(root()).bodyByteArray("foo".getBytes()); assertThat(helper.executeAsString(request), is("bar")); } }); }
@Test public void should_match_delete_method() throws Exception { server.delete(by(uri("/foo"))).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(expected = HttpResponseException.class) public void should_throw_exception_without_context_for_any_response_handler() throws Exception { server.response("foo"); running(server, new Runnable() { @Override public void run() throws Exception { helper.get(root()); } }); } }