@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")); } }); }
@Test public void should_config_file_root() throws Exception { server.response(file("foo.response")); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(root()), is("foo.response")); } }); }
@Test public void should_return_expected_response_with_content_api() throws Exception { server.response(with("foo")); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(root()), is("foo")); } }); }
@Test public void should_return_excepted_version_with_version_api() throws Exception { server.response(version(VERSION_1_0)); running(server, new Runnable() { @Override public void run() throws IOException { ProtocolVersion version = helper.getResponse(root()).getProtocolVersion(); assertThat(version.getMajor(), is(1)); assertThat(version.getMinor(), is(0)); } }); }
@Test public void should_return_template_from_file_root() throws Exception { server.response(template(file("foo.response"))); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(root()), is("foo.response")); } }); }
@Test public void should_return_specified_content_type() throws Exception { server.response(with("foo"), header(HttpHeaders.CONTENT_TYPE, "text/html")); running(server, new Runnable() { @Override public void run() throws Exception { Header header = helper.getResponse(root()).getFirstHeader(HttpHeaders.CONTENT_TYPE); assertThat(header.getValue(), is("text/html")); } }); }
@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_starts_with_for_resource() throws Exception { server.request(startsWith(header("foo"), "bar")).response("bar"); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.getWithHeader(root(), of("foo", "barA")), is("bar")); assertThat(helper.getWithHeader(root(), of("foo", "barB")), is("bar")); } }); }
@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 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_config_handler_correctly_other_side_while_merging() throws Exception { httpServer = httpServer(12306, fileRoot("src/test/resources")); httpServer.response(file("foo.response")); HttpServer mergedServer = ((ActualHttpServer) httpServer).mergeServer((ActualHttpServer) anotherServer); running(mergedServer, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(root()), is("foo.response")); } }); }
@Test public void should_merge_http_server_with_different_port() throws Exception { httpServer = httpServer(12306, context("/foo")); anotherServer = httpServer(12307, context("/bar")); final HttpServer mergedServer = ((ActualHttpServer) anotherServer).mergeServer((ActualHttpServer) httpServer); running(mergedServer, new Runnable() { @Override public void run() throws Exception { assertThat(mergedServer.port(), is(12307)); } }); }
@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_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_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_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_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")); } }); }