@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_proxy_with_playback() throws Exception { server.request(by(uri("/target"))).response("proxy"); final File file = tempFolder.newFile(); server.request(by(uri("/proxy_playback"))).response(proxy(remoteUrl("/target"), playback(file.getAbsolutePath()))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/proxy_playback")), is("proxy")); } }); }
@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_return_path_resource_with_template_and_charset() throws Exception { server.request(by(uri("/template"))).response(pathResource(template("${var}", "var", "gbk.response"), Charset.forName("GBK"))); running(server, new Runnable() { @Override public void run() throws Exception { URL resource = Resources.getResource("gbk.response"); InputStream stream = resource.openStream(); assertThat(helper.getAsBytes(remoteUrl("/template")), is(ByteStreams.toByteArray(stream))); } }); }
@Test(expected = IllegalArgumentException.class) public void should_throw_exception_for_reserved_name_as_variable_nem() { server.request(by(uri("/template"))).response(template("${random}", "random", "bar")); } }
@Test public void should_generate_response_with_http_version() throws Exception { server.request(by(uri("/template"))).response(template("${req.version}")); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.getWithVersion(remoteUrl("/template"), HttpVersion.HTTP_1_0), is("HTTP/1.0")); } }); }
@Test public void should_generate_response_with_variable() throws Exception { server.request(by(uri("/template"))).response(template("${var}", "var", "TEMPLATE")); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/template")), is("TEMPLATE")); } }); }
@Test public void should_generate_response_with_now() throws Exception { server.request(by(uri("/template"))).response(template("${now('yyyy-MM-dd')}")); running(server, new Runnable() { @Override public void run() throws Exception { Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); assertThat(helper.get(remoteUrl("/template")), is(format.format(date))); } }); }
@Test public void should_throw_exception_for_now_without_format() throws Exception { server.request(by(uri("/template"))).response(template("${now()}")); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse response = helper.getResponse(remoteUrl("/template")); assertThat(response.getStatusLine().getStatusCode(), is(400)); } }); }
@Test public void should_generate_response_with_form() throws Exception { server.request(by(uri("/template"))).response(template("${req.forms['name']}")); running(server, new Runnable() { @Override public void run() throws IOException { Request request = Request.Post(remoteUrl("/template")).bodyForm(new BasicNameValuePair("name", "dreamhead")); assertThat(helper.executeAsString(request), is("dreamhead")); } }); }
@Test public void should_generate_response_with_two_variables() throws Exception { server.request(by(uri("/template"))).response(template("${foo} ${bar}", "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_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 public void should_match_request_based_on_either_matcher() throws Exception { server.request(or(by("foo"), by(uri("/foo")))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/foo")), is("bar")); assertThat(helper.postContent(remoteUrl("/foo"), "foo"), is("bar")); } }); }
@Test public void should_generate_response_from_file_with_variable() throws Exception { server.request(by(uri("/template"))).response(template(file("src/test/resources/var.template"), "var", "TEMPLATE")); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/template")), is("TEMPLATE")); } }); }
@Test public void should_generate_response_with_variable_by_request() throws Exception { server.request(by(uri("/template"))).response(template("${foo}", "foo", 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_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_generate_response_from_file_with_variable_by_request() throws Exception { server.request(by(uri("/template"))).response(template(file("src/test/resources/var.template"), "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_multiple_matchers() throws Exception { server.request(and(by("foo"), by(uri("/foo")))).response(text("bar")); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postContent(remoteUrl("/foo"), "foo"), is("bar")); } }); }
@Test public void should_match_with_xml() throws Exception { server = httpServer(port(), request(by(uri("/path")))); server.request(xml("<request><parameters><id>1</id></parameters></request>")).response("foo"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postFile(remoteUrl("/path"), "foo.xml"), is("foo")); } }); }
@Test(expected = HttpResponseException.class) public void should_throw_exception_without_match_json() throws Exception { server = httpServer(port(), request(by(uri("/path")))); final String jsonContent = "{\"foo\":\"bar\"}"; server.request(by(json(jsonContent))).response("foo"); running(server, new Runnable() { @Override public void run() throws IOException { helper.postContent(root(), jsonContent); } }); }