public static ContentResource template(final String template, final ImmutableMap<String, ? extends RequestExtractor<?>> variables) { return template(text(checkNotNull(template, "Template should not be null")), checkNotNull(variables, "Template variable should not be null")); }
public static <T> ContentResource template(final String template, final String name, final RequestExtractor<T> extractor) { return template(text(checkNotNullOrEmpty(template, "Template should not be null")), checkNotNullOrEmpty(name, "Template variable name should not be null"), checkNotNull(extractor, "Template variable extractor should not be null")); }
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()); }
private Resource asResource(final String name, final FileContainer fileContainer) { Optional<Charset> charset = fileContainer.getCharset(); String text = fileContainer.getName().getText(); return asResource(name, text(text), charset); }
public static ContentResource template(final String template, final String name, final String value) { return template(text(checkNotNullOrEmpty(template, "Template should not be null")), checkNotNullOrEmpty(name, "Template variable name should not be null"), checkNotNullOrEmpty(value, "Template variable value should not be null")); }
private Resource getResource(final TextContainer container) { if (container.isForTemplate()) { return template(container.getText()); } return text(container.getText()); }
@Test(expected = IllegalArgumentException.class) public void should_throw_exception_when_file_does_not_exist() { ClasspathFileResourceReader reader = new ClasspathFileResourceReader(text("unknown.response"), Optional.<Charset>absent()); reader.readFor(Optional.<Request>absent()); } }
public ContentResource asResource() { if (isRawText()) { return text(this.text); } if (isForTemplate()) { return asTemplateResource(); } return invokeTarget(getMethodName(), this.text, ContentResource.class); }
@Test public void should_return_class_path_file_content() { FileResourceReader reader = new FileResourceReader(text(new File("src/test/resources/foo.response").getPath())); assertThat(reader.readFor(Optional.<Request>absent()).toString(), is("foo.response")); }
@Test public void should_return_expected_response_based_on_specified_request_with_text_api() throws Exception { server.request(by(text("foo"))).response(text("bar")); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.postContent(root(), "foo"), is("bar")); } }); }
public static ContentResource template(final String template, final String name1, final String value1, final String name2, final String value2) { return template(text(checkNotNullOrEmpty(template, "Template should not be null")), checkNotNullOrEmpty(name1, "Template variable name should not be null"), checkNotNullOrEmpty(value1, "Template variable value should not be null"), checkNotNullOrEmpty(name2, "Template variable name should not be null"), checkNotNullOrEmpty(value2, "Template variable value should not be null")); }
@Test public void should_return_class_path_file_content() { ClasspathFileResourceReader reader = new ClasspathFileResourceReader(text("foo.response"), Optional.<Charset>absent()); assertThat(reader.readFor(Optional.<Request>absent()).toString(), is("foo.response")); }
@Test public void should_return_expected_response_with_text_api() throws Exception { server.response(text("foo")); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(root()), is("foo")); } }); }
@Test public void should_match_xml_with_resource() throws Exception { server.request(xml(text("<request><parameters><id>1</id></parameters></request>"))).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_false_for_empty_content() { XmlRequestMatcher unitUnderTest = new XmlRequestMatcher(text("<request><parameters><id>1</id></parameters></request>")); HttpRequest request = DefaultHttpRequest.builder().withContent("").build(); assertThat(unitUnderTest.match(request), is(false)); } }
@Test public void should_reply_404_for_unknown_resource() throws Exception { server.resource("targets", get("2").response(with(text("hello")))); running(server, new Runnable() { @Override public void run() throws Exception { org.apache.http.HttpResponse response = helper.getResponse(remoteUrl("/targets/1")); assertThat(response.getStatusLine().getStatusCode(), is(404)); } }); }
@Test public void should_patch() throws Exception { server.resource("targets", patch("1").response(with(text("patch result"))) ); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.patchForResponse(remoteUrl("/targets/1"), "result"), is("patch result")); } }); }
@Test public void should_patch_with_any_id() throws Exception { server.resource("targets", patch(anyId()).response(with(text("patch result"))) ); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.patchForResponse(remoteUrl("/targets/1"), "result"), is("patch result")); } }); }
@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")); } }); }