Merge branch '1.0.x'

This commit is contained in:
Andy Wilkinson
2016-02-15 12:25:49 +00:00
121 changed files with 1672 additions and 1529 deletions

View File

@@ -23,10 +23,14 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.RequestSpecification;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.IntegrationTest;
@@ -42,10 +46,6 @@ import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.RequestSpecification;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -189,8 +189,8 @@ public class RestAssuredRequestConverterTests {
@Test
public void objectBody() {
RequestSpecification requestSpec = RestAssured.given()
.body(new ObjectBody("bar")).port(this.port);
RequestSpecification requestSpec = RestAssured.given().body(new ObjectBody("bar"))
.port(this.port);
requestSpec.post();
OperationRequest request = this.factory
.convert((FilterableRequestSpecification) requestSpec);
@@ -203,8 +203,8 @@ public class RestAssuredRequestConverterTests {
.body(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }))
.port(this.port);
requestSpec.post();
this.thrown
.expectMessage(equalTo("Unsupported request content: java.io.ByteArrayInputStream"));
this.thrown.expectMessage(
equalTo("Unsupported request content: java.io.ByteArrayInputStream"));
this.factory.convert((FilterableRequestSpecification) requestSpec);
}

View File

@@ -19,18 +19,18 @@ package org.springframework.restdocs.restassured;
import java.util.List;
import java.util.Map;
import com.jayway.restassured.filter.FilterContext;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.FilterableResponseSpecification;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.snippet.WriterResolver;
import org.springframework.restdocs.templates.TemplateEngine;
import com.jayway.restassured.filter.FilterContext;
import com.jayway.restassured.specification.FilterableRequestSpecification;
import com.jayway.restassured.specification.FilterableResponseSpecification;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.hasEntry;
@@ -48,11 +48,14 @@ import static org.mockito.Mockito.verify;
public class RestAssuredRestDocumentationConfigurerTests {
@Rule
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("build");
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(
"build");
private final FilterableRequestSpecification requestSpec = mock(FilterableRequestSpecification.class);
private final FilterableRequestSpecification requestSpec = mock(
FilterableRequestSpecification.class);
private final FilterableResponseSpecification responseSpec = mock(FilterableResponseSpecification.class);
private final FilterableResponseSpecification responseSpec = mock(
FilterableResponseSpecification.class);
private final FilterContext filterContext = mock(FilterContext.class);
@@ -76,16 +79,11 @@ public class RestAssuredRestDocumentationConfigurerTests {
configurationCaptor.capture());
@SuppressWarnings("unchecked")
Map<String, Object> configuration = configurationCaptor.getValue();
assertThat(
configuration,
hasEntry(equalTo(TemplateEngine.class.getName()),
instanceOf(TemplateEngine.class)));
assertThat(
configuration,
hasEntry(equalTo(WriterResolver.class.getName()),
instanceOf(WriterResolver.class)));
assertThat(
configuration,
assertThat(configuration, hasEntry(equalTo(TemplateEngine.class.getName()),
instanceOf(TemplateEngine.class)));
assertThat(configuration, hasEntry(equalTo(WriterResolver.class.getName()),
instanceOf(WriterResolver.class)));
assertThat(configuration,
hasEntry(
equalTo(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS),
instanceOf(List.class)));

View File

@@ -24,9 +24,12 @@ import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.specification.RequestSpecification;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.IntegrationTest;
@@ -45,9 +48,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.specification.RequestSpecification;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
@@ -96,8 +96,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
@Test
public void defaultSnippetGeneration() {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("default")).get("/").then().statusCode(200);
assertExpectedSnippetFilesExist(new File("build/generated-snippets/default"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc");
@@ -106,8 +105,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
@Test
public void curlSnippetWithContent() throws Exception {
String contentType = "text/plain; charset=UTF-8";
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("curl-snippet-with-content")).accept("application/json")
.content("content").contentType(contentType).post("/").then()
.statusCode(200);
@@ -115,18 +113,16 @@ public class RestAssuredRestDocumentationIntegrationTests {
assertThat(
new File(
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(
codeBlock(asciidoctor(), "bash").content(
"$ curl 'http://localhost:" + this.port + "/' -i "
+ "-X POST -H 'Accept: application/json' "
+ "-H 'Content-Type: " + contentType + "' "
+ "-d 'content'"))));
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
.content("$ curl 'http://localhost:" + this.port + "/' -i "
+ "-X POST -H 'Accept: application/json' "
+ "-H 'Content-Type: " + contentType + "' "
+ "-d 'content'"))));
}
@Test
public void curlSnippetWithQueryStringOnPost() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("curl-snippet-with-query-string"))
.accept("application/json").param("foo", "bar").param("a", "alpha")
.post("/?foo=bar").then().statusCode(200);
@@ -134,19 +130,16 @@ public class RestAssuredRestDocumentationIntegrationTests {
assertThat(
new File(
"build/generated-snippets/curl-snippet-with-query-string/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(
codeBlock(asciidoctor(), "bash").content(
"$ curl " + "'http://localhost:" + this.port
+ "/?foo=bar' -i -X POST "
+ "-H 'Accept: application/json' "
+ "-H 'Content-Type: " + contentType + "' "
+ "-d 'a=alpha'"))));
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
.content("$ curl " + "'http://localhost:" + this.port
+ "/?foo=bar' -i -X POST "
+ "-H 'Accept: application/json' " + "-H 'Content-Type: "
+ contentType + "' " + "-d 'a=alpha'"))));
}
@Test
public void linksSnippet() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("links",
links(linkWithRel("rel").description("The description"))))
.accept("application/json").get("/").then().statusCode(200);
@@ -157,68 +150,62 @@ public class RestAssuredRestDocumentationIntegrationTests {
@Test
public void pathParametersSnippet() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
.filter(document(
"path-parameters",
pathParameters(parameterWithName("foo").description(
"The description")))).accept("application/json")
.get("/{foo}", "").then().statusCode(200);
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/path-parameters"), "http-request.adoc",
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("path-parameters",
pathParameters(
parameterWithName("foo").description("The description"))))
.accept("application/json").get("/{foo}", "").then().statusCode(200);
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/path-parameters"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "path-parameters.adoc");
}
@Test
public void requestParametersSnippet() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
.filter(document(
"request-parameters",
requestParameters(parameterWithName("foo").description(
"The description")))).accept("application/json")
.param("foo", "bar").get("/").then().statusCode(200);
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/request-parameters"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "request-parameters.adoc");
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("request-parameters",
requestParameters(
parameterWithName("foo").description("The description"))))
.accept("application/json").param("foo", "bar").get("/").then()
.statusCode(200);
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/request-parameters"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
"request-parameters.adoc");
}
@Test
public void requestFieldsSnippet() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
.filter(document("request-fields", requestFields(fieldWithPath("a")
.description("The description")))).accept("application/json")
.content("{\"a\":\"alpha\"}").post("/").then().statusCode(200);
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/request-fields"), "http-request.adoc",
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("request-fields",
requestFields(fieldWithPath("a").description("The description"))))
.accept("application/json").content("{\"a\":\"alpha\"}").post("/").then()
.statusCode(200);
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/request-fields"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "request-fields.adoc");
}
@Test
public void responseFieldsSnippet() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
.filter(document(
"response-fields",
responseFields(
fieldWithPath("a").description("The description"),
fieldWithPath("links").description(
"Links to other resources"))))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("response-fields",
responseFields(fieldWithPath("a").description("The description"),
fieldWithPath("links")
.description("Links to other resources"))))
.accept("application/json").get("/").then().statusCode(200);
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/response-fields"), "http-request.adoc",
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/response-fields"), "http-request.adoc",
"http-response.adoc", "curl-request.adoc", "response-fields.adoc");
}
@Test
public void parameterizedOutputDirectory() throws Exception {
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("{method-name}")).get("/").then().statusCode(200);
assertExpectedSnippetFilesExist(new File(
"build/generated-snippets/parameterized-output-directory"),
assertExpectedSnippetFilesExist(
new File("build/generated-snippets/parameterized-output-directory"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc");
}
@@ -244,73 +231,56 @@ public class RestAssuredRestDocumentationIntegrationTests {
@Test
public void preprocessedRequest() throws Exception {
Pattern pattern = Pattern.compile("(\"alpha\")");
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
.header("a", "alpha")
.header("b", "bravo")
.contentType("application/json")
.accept("application/json")
.content("{\"a\":\"alpha\"}")
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.header("a", "alpha").header("b", "bravo").contentType("application/json")
.accept("application/json").content("{\"a\":\"alpha\"}")
.filter(document("original-request"))
.filter(document(
"preprocessed-request",
preprocessRequest(
prettyPrint(),
.filter(document("preprocessed-request",
preprocessRequest(prettyPrint(),
removeHeaders("a", HttpHeaders.HOST,
HttpHeaders.CONTENT_LENGTH),
replacePattern(pattern, "\"<<beta>>\"")))).get("/")
.then().statusCode(200);
replacePattern(pattern, "\"<<beta>>\""))))
.get("/").then().statusCode(200);
assertThat(
new File("build/generated-snippets/original-request/http-request.adoc"),
is(snippet(asciidoctor())
.withContents(
httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("a", "alpha")
.header("b", "bravo")
.header("Accept",
MediaType.APPLICATION_JSON_VALUE)
.header("Content-Type",
"application/json; charset=UTF-8")
.header("Host", "localhost")
.header("Content-Length", "13")
.content("{\"a\":\"alpha\"}"))));
.withContents(httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("a", "alpha").header("b", "bravo")
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
.header("Content-Type", "application/json; charset=UTF-8")
.header("Host", "localhost")
.header("Content-Length", "13")
.content("{\"a\":\"alpha\"}"))));
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
assertThat(
new File(
"build/generated-snippets/preprocessed-request/http-request.adoc"),
is(snippet(asciidoctor())
.withContents(
httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("b", "bravo")
.header("Accept",
MediaType.APPLICATION_JSON_VALUE)
.header("Content-Type",
"application/json; charset=UTF-8")
.content(prettyPrinted))));
.withContents(httpRequest(asciidoctor(), RequestMethod.GET, "/")
.header("b", "bravo")
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
.header("Content-Type", "application/json; charset=UTF-8")
.content(prettyPrinted))));
}
@Test
public void preprocessedResponse() throws Exception {
Pattern pattern = Pattern.compile("(\"alpha\")");
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
given().port(this.port).filter(documentationConfiguration(this.restDocumentation))
.filter(document("original-response"))
.filter(document(
"preprocessed-response",
preprocessResponse(
prettyPrint(),
maskLinks(),
removeHeaders("a", "Transfer-Encoding", "Date", "Server"),
replacePattern(pattern, "\"<<beta>>\""), modifyUris()
.scheme("https").host("api.example.com")
.removePort()))).get("/").then().statusCode(200);
.filter(document("preprocessed-response", preprocessResponse(
prettyPrint(), maskLinks(),
removeHeaders("a", "Transfer-Encoding", "Date", "Server"),
replacePattern(pattern, "\"<<beta>>\""), modifyUris()
.scheme("https").host("api.example.com").removePort())))
.get("/").then().statusCode(200);
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\",%n \"links\" : "
+ "[ {%n \"rel\" : \"rel\",%n \"href\" : \"...\"%n } ]%n}");
assertThat(
new File(
"build/generated-snippets/preprocessed-response/http-response.adoc"),
is(snippet(asciidoctor()).withContents(
httpResponse(asciidoctor(), HttpStatus.OK)
is(snippet(asciidoctor())
.withContents(httpResponse(asciidoctor(), HttpStatus.OK)
.header("Foo", "https://api.example.com/foo/bar")
.header("Content-Type", "application/json;charset=UTF-8")
.header(HttpHeaders.CONTENT_LENGTH,
@@ -320,8 +290,8 @@ public class RestAssuredRestDocumentationIntegrationTests {
@Test
public void customSnippetTemplate() throws Exception {
ClassLoader classLoader = new URLClassLoader(new URL[] { new File(
"src/test/resources/custom-snippet-templates").toURI().toURL() },
ClassLoader classLoader = new URLClassLoader(new URL[] {
new File("src/test/resources/custom-snippet-templates").toURI().toURL() },
getClass().getClassLoader());
ClassLoader previous = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
@@ -334,8 +304,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
finally {
Thread.currentThread().setContextClassLoader(previous);
}
assertThat(new File(
"build/generated-snippets/custom-snippet-template/curl-request.adoc"),
assertThat(
new File(
"build/generated-snippets/custom-snippet-template/curl-request.adoc"),
is(snippet(asciidoctor()).withContents(equalTo("Custom curl request"))));
}

View File

@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
@@ -54,7 +55,8 @@ public class UriModifyingOperationPreprocessorTests {
this.preprocessor.scheme("https");
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithUri("http://localhost:12345"));
assertThat(processed.getUri(), is(equalTo(URI.create("https://localhost:12345"))));
assertThat(processed.getUri(),
is(equalTo(URI.create("https://localhost:12345"))));
}
@Test
@@ -114,7 +116,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriSchemeCanBeModified() {
this.preprocessor.scheme("https");
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345' should be used"));
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'https://localhost:12345' should be used")));
}
@@ -123,7 +126,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriHostCanBeModified() {
this.preprocessor.host("api.example.com");
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345' should be used"));
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://api.example.com:12345' should be used")));
}
@@ -132,7 +136,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriPortCanBeModified() {
this.preprocessor.port(23456);
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345' should be used"));
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost:23456' should be used")));
}
@@ -141,7 +146,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriPortCanBeRemoved() {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345' should be used"));
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost' should be used")));
}
@@ -150,17 +156,18 @@ public class UriModifyingOperationPreprocessorTests {
public void multipleRequestContentUrisCanBeModified() {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("Use 'http://localhost:12345' or 'https://localhost:23456' to access the service"));
assertThat(
new String(processed.getContent()),
is(equalTo("Use 'http://localhost' or 'https://localhost' to access the service")));
.preprocess(createRequestWithContent(
"Use 'http://localhost:12345' or 'https://localhost:23456' to access the service"));
assertThat(new String(processed.getContent()), is(equalTo(
"Use 'http://localhost' or 'https://localhost' to access the service")));
}
@Test
public void requestContentUriPathIsPreserved() {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345/foo/bar' should be used"));
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345/foo/bar' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost/foo/bar' should be used")));
}
@@ -169,7 +176,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriQueryIsPreserved() {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345?foo=bar' should be used"));
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345?foo=bar' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost?foo=bar' should be used")));
}
@@ -178,7 +186,8 @@ public class UriModifyingOperationPreprocessorTests {
public void requestContentUriAnchorIsPreserved() {
this.preprocessor.removePort();
OperationRequest processed = this.preprocessor
.preprocess(createRequestWithContent("The uri 'http://localhost:12345#foo' should be used"));
.preprocess(createRequestWithContent(
"The uri 'http://localhost:12345#foo' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost#foo' should be used")));
}
@@ -187,7 +196,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriSchemeCanBeModified() {
this.preprocessor.scheme("https");
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345' should be used"));
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'https://localhost:12345' should be used")));
}
@@ -196,7 +206,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriHostCanBeModified() {
this.preprocessor.host("api.example.com");
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345' should be used"));
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://api.example.com:12345' should be used")));
}
@@ -205,7 +216,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriPortCanBeModified() {
this.preprocessor.port(23456);
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345' should be used"));
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost:23456' should be used")));
}
@@ -214,7 +226,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriPortCanBeRemoved() {
this.preprocessor.removePort();
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345' should be used"));
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost' should be used")));
}
@@ -223,17 +236,18 @@ public class UriModifyingOperationPreprocessorTests {
public void multipleResponseContentUrisCanBeModified() {
this.preprocessor.removePort();
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("Use 'http://localhost:12345' or 'https://localhost:23456' to access the service"));
assertThat(
new String(processed.getContent()),
is(equalTo("Use 'http://localhost' or 'https://localhost' to access the service")));
.preprocess(createResponseWithContent(
"Use 'http://localhost:12345' or 'https://localhost:23456' to access the service"));
assertThat(new String(processed.getContent()), is(equalTo(
"Use 'http://localhost' or 'https://localhost' to access the service")));
}
@Test
public void responseContentUriPathIsPreserved() {
this.preprocessor.removePort();
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345/foo/bar' should be used"));
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345/foo/bar' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost/foo/bar' should be used")));
}
@@ -242,7 +256,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriQueryIsPreserved() {
this.preprocessor.removePort();
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345?foo=bar' should be used"));
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345?foo=bar' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost?foo=bar' should be used")));
}
@@ -251,7 +266,8 @@ public class UriModifyingOperationPreprocessorTests {
public void responseContentUriAnchorIsPreserved() {
this.preprocessor.removePort();
OperationResponse processed = this.preprocessor
.preprocess(createResponseWithContent("The uri 'http://localhost:12345#foo' should be used"));
.preprocess(createResponseWithContent(
"The uri 'http://localhost:12345#foo' should be used"));
assertThat(new String(processed.getContent()),
is(equalTo("The uri 'http://localhost#foo' should be used")));
}
@@ -276,19 +292,17 @@ public class UriModifyingOperationPreprocessorTests {
@Test
public void urisInRequestPartHeadersCanBeModified() {
OperationRequest processed = this.preprocessor.host("api.example.com")
.preprocess(
createRequestWithPartWithHeader("Foo", "http://locahost:12345"));
OperationRequest processed = this.preprocessor.host("api.example.com").preprocess(
createRequestWithPartWithHeader("Foo", "http://locahost:12345"));
assertThat(processed.getParts().iterator().next().getHeaders().getFirst("Foo"),
is(equalTo("http://api.example.com:12345")));
}
@Test
public void urisInRequestPartContentCanBeModified() {
OperationRequest processed = this.preprocessor
.host("api.example.com")
.preprocess(
createRequestWithPartWithContent("The uri 'http://localhost:12345' should be used"));
OperationRequest processed = this.preprocessor.host("api.example.com")
.preprocess(createRequestWithPartWithContent(
"The uri 'http://localhost:12345' should be used"));
assertThat(new String(processed.getParts().iterator().next().getContent()),
is(equalTo("The uri 'http://api.example.com:12345' should be used")));
}
@@ -317,16 +331,16 @@ public class UriModifyingOperationPreprocessorTests {
HttpHeaders headers = new HttpHeaders();
headers.add(name, value);
return this.requestFactory.create(URI.create("http://localhost"), HttpMethod.GET,
new byte[0], new HttpHeaders(), new Parameters(), Arrays
.asList(new OperationRequestPartFactory().create("part",
"fileName", new byte[0], headers)));
new byte[0], new HttpHeaders(), new Parameters(),
Arrays.asList(new OperationRequestPartFactory().create("part", "fileName",
new byte[0], headers)));
}
private OperationRequest createRequestWithPartWithContent(String content) {
return this.requestFactory.create(URI.create("http://localhost"), HttpMethod.GET,
new byte[0], new HttpHeaders(), new Parameters(), Arrays
.asList(new OperationRequestPartFactory().create("part",
"fileName", content.getBytes(), new HttpHeaders())));
new byte[0], new HttpHeaders(), new Parameters(),
Arrays.asList(new OperationRequestPartFactory().create("part", "fileName",
content.getBytes(), new HttpHeaders())));
}
private OperationResponse createResponseWithContent(String content) {