When using REST Assured in conjunction with REST Docs, I have an issue to where requests have their port updated, but all the HATEOAS links in the response point to whatever address the test ran on.
From the REST Docs documentation, I see how to update the request using a preprocessor:
.addFilter(document("{class-name}/{method-name}/{step}", preprocessRequest(
modifyUris().scheme("http")
.host("localhost")
.port(9999),
removeHeaders("Accept"))))
but cannot find if there is support to modify the port int the response. For instance, when I want to set the port in configuration to 9999:
curl-request.adoc: (This is good: localhost:9999)
$ curl 'localhost:9999/request/data' -i
response-body.adoc: (I would like to change localhost:51123 to localhost:9999)
{
"_links" : {
"requests" : {
"href" : "localhost:51123/request/data/requests{?page,size,sort,projection}",
"templated" : true
},
"users" : {
"href" : "localhost:51123/request/data/users{?projection}",
"templated" : true
},
"profile" : {
"href" : "localhost:51123/request/data/profile"
}
}
}
Is there any accepted way using REST Docs or REST Assured to modify the content of the response? I am supposing I could create a @AfterClass method to parse-update those resources but am hoping for something cleaner.
[Follow-Up] I accepted the answer from Andy W. below, but wanted to provide additional info for anyone who ever has the same issue -
My problem was that I was trying to add the document filter twice:
.addFilters(Arrays.asList(
document("{class-name}/{method-name}/{step}",
preprocessRequest(modifyUris().scheme("http")
.host("localhost")
.port(9999))),
document("{class-name}/{method-name}/{step}",
preprocessResponse(modifyUris().scheme("http")
.host("localhost")
.port(9999))))
vs. calling the document method with the parameters:
RestDocumentationFilter document(String identifier, OperationRequestPreprocessor requestPreprocessor, OperationResponsePreprocessor responsePreprocessor, Snippet... snippets)
Once I made that change everything worked as expected. Cheers!
Yes, there is. From the documentation:
modifyUris
onRestAssuredPreprocessors
can be used to modify any URIs in a request or a response. When using REST Assured, this allows you to customize the URIs that appear in the documentation while testing a local instance of the service.