public static <ExtractorType1, ExtractorType2> ContentResource template(final String template, final String name1, final RequestExtractor<ExtractorType1> extractor1, final String name2, final RequestExtractor<ExtractorType2> extractor2) { return template(text(checkNotNullOrEmpty(template, "Template should not be null")), checkNotNullOrEmpty(name1, "Template variable name should not be null"), checkNotNull(extractor1, "Template variable extractor should not be null"), checkNotNullOrEmpty(name2, "Template variable name should not be null"), checkNotNull(extractor2, "Template variable extractor should not be null")); }
private Resource getResource(final CookieContainer container) { if (container.isForTemplate()) { return template(container.getTemplate()); } return text(container.getValue()); }
public static ContentResource template(final ContentResource template, final String name1, final String value1, final String name2, final String value2) { return template(checkNotNull(template, "Template should not be null"), checkNotNullOrEmpty(name1, "Template variable name should not be null"), var(checkNotNullOrEmpty(value1, "Template variable value should not be null")), checkNotNullOrEmpty(name2, "Template variable name should not be null"), var(checkNotNullOrEmpty(value2, "Template variable value should not be null"))); }
@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_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_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_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_throw_exception_for_unknown_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 { HttpResponse response = helper.getResponse(remoteUrl("/template")); assertThat(response.getStatusLine().getStatusCode(), is(400)); } }); }
@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_generate_response_with_http_query() throws Exception { server.request(by(uri("/template"))).response(template("${req.uri} ${req.queries['foo']}")); running(server, new Runnable() { @Override public void run() throws Exception { String response = helper.get(remoteUrl("/template?foo=bar")); assertThat(response, is("/template bar")); } }); }
@Test public void should_send_get_request_to_target_on_complete_with_template() throws Exception { ResponseHandler handler = mock(ResponseHandler.class); server.request(by(uri("/target"))).response(handler); server.request(by(uri("/event"))).response("event").on(complete(get(template("${var}", "var", remoteUrl("/target"))))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/event")), is("event")); } }); verify(handler).writeToResponse(any(SessionContext.class)); }
@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_generate_response_with_many_extracted_variables() throws Exception { server.request(by(uri("/template"))).response(template("<#list seq as item>${item}</#list>", "seq", xpath("/request/parameters/id/text()"))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.postFile(remoteUrl("/template"), "foobar.xml"), is("12")); } }); }
@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_return_path_resource_with_template() throws Exception { server.request(by(uri("/template"))).response(pathResource(template("${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_with_two_variables_by_request_and_one_variable_is_plain_text() throws Exception { server.request(by(uri("/template"))).response(template("${foo} ${bar}", "foo", jsonPath("$.book.price"), "bar", var("bar"))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.postContent(remoteUrl("/template"), "{\"book\":{\"price\":\"2\"}}"), is("2 bar")); assertThat(helper.postContent(remoteUrl("/template"), "{\"book\":{\"price\":\"1\"}}"), is("1 bar")); } }); }
@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")); } }); }