public static MocoJunitRunner jsonHttpsRunner(final int port, final String filename, final HttpsCertificate certificate) { checkArgument(port > 0, "Port must be greater than zero"); checkNotNullOrEmpty(filename, "Filename should not be null"); checkNotNull(certificate, "Certificate should not be null"); return jsonHttpsRunner(port, file(filename), certificate); }
@Test public void should_config_handler_correctly_while_merging() throws Exception { httpServer = httpServer(12306, fileRoot("src/test/resources")); httpServer.response(file("foo.response")); HttpServer mergedServer = ((ActualHttpServer) anotherServer).mergeServer((ActualHttpServer) httpServer); running(mergedServer, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(root()), is("foo.response")); } }); }
public static MocoJunitRunner jsonSocketRunner(final int port, final String filename) { checkArgument(port > 0, "Port must be greater than zero"); checkNotNullOrEmpty(filename, "Filename should not be null"); return jsonSocketRunner(port, file(filename)); }
@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_return_expected_response_from_file_with_charset() throws Exception { server.response(file("src/test/resources/gbk.response", Charset.forName("GBK"))); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.getAsBytes(root()), is(toByteArray(new File("src/test/resources/gbk.response")))); } }); }
@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_from_file() throws Exception { server.response(file("src/test/resources/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() throws Exception { final SocketServer server = jsonSocketServer(port(), file("src/test/resources/base.json")); running(server, new Runnable() { @Override public void run() throws Exception { helper.connect(); assertThat(helper.send("foo", 3), is("bar")); assertThat(helper.send("anything", 4), is("blah")); helper.close(); } }); }
@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_expected_response() throws Exception { final HttpServer server = jsonHttpsServer(port(), file("src/test/resources/foo.json"), DEFAULT_CERTIFICATE); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(httpsRoot()), is("foo")); } }); }
@Test public void should_return_expected_response() throws Exception { final HttpServer server = jsonHttpServer(port(), file("src/test/resources/foo.json")); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(root()), is("foo")); } }); }
@Test public void should_match_exact_xml() throws Exception { server.request(xml(file("src/test/resources/foo.xml"))).response("foo"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postFile(root(), "foo.xml"), is("foo")); } }); }
@Test public void should_return_file_with_template_and_charset() throws Exception { server.request(by(uri("/template"))).response(file(template("src/test/resources/${var}", "var", "gbk.response"), Charset.forName("GBK"))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.getAsBytes(remoteUrl("/template")), is(toByteArray(new File("src/test/resources/gbk.response")))); } }); }
@Test public void should_return_template_header_from_file_root() throws Exception { server.response(header("foo", template(file("foo.response")))); running(server, new Runnable() { @Override public void run() throws IOException { org.apache.http.HttpResponse response = helper.getResponse(root()); Header header = response.getFirstHeader("foo"); assertThat(header.getValue(), is("foo.response")); } }); }
@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_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_generate_response_from_file_with_two_variables() throws Exception { server.request(by(uri("/template"))).response(template(file("src/test/resources/two_vars.template"), "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_return_file_with_template() throws Exception { server.request(by(uri("/template"))).response(file(template("src/test/resources/${var}", "var", "foo.response"))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/template")), is("foo.response")); } }); }
@Test public void should_generate_response_from_file() throws Exception { server.request(by(uri("/template"))).response(template(file("src/test/resources/foo.template"))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/template")), is("GET")); } }); }
@Test public void should_generate_response_from_file_with_two_variables_by_request() throws Exception { server.request(by(uri("/template"))).response(template(file("src/test/resources/two_vars.template"), "foo", jsonPath("$.book.price"), "bar", jsonPath("$.book.price"))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.postContent(remoteUrl("/template"), "{\"book\":{\"price\":\"2\"}}"), is("2 2")); assertThat(helper.postContent(remoteUrl("/template"), "{\"book\":{\"price\":\"1\"}}"), is("1 1")); } }); }