Spring REST Docs - Update URIs in Response Snippets Generated From REST Assured Tests

1k views Asked by At

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)

https://docs.spring.io/spring-restdocs/docs/current/api/org/springframework/restdocs/restassured3/RestAssuredRestDocumentation.html#document-java.lang.String-org.springframework.restdocs.operation.preprocess.OperationRequestPreprocessor-org.springframework.restdocs.operation.preprocess.OperationResponsePreprocessor-org.springframework.restdocs.snippet.Snippet...-

Once I made that change everything worked as expected. Cheers!

2

There are 2 answers

1
Andy Wilkinson On BEST ANSWER

Yes, there is. From the documentation:

modifyUris on RestAssuredPreprocessors 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.

1
Kim Young Han On

The code below would be helpful.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.RestDocsMockMvcConfigurationCustomizer;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.restdocs.operation.preprocess.UriModifyingOperationPreprocessor;

import static org.springframework.restdocs.operation.preprocess.Preprocessors.modifyUris;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;

@TestConfiguration
public class RestDocsConfiguration {

    @Autowired
    TestProperties testProperties;

    @Bean
    public RestDocsMockMvcConfigurationCustomizer restDocsMockMvcConfigurationCustomizer() {
        UriModifyingOperationPreprocessor uriPreprocessor = modifyUris()
                .scheme(testProperties.getScheme())
                .host(testProperties.getHost())
                .removePort();

        return configurer -> configurer.operationPreprocessors()
                .withRequestDefaults(prettyPrint(), uriPreprocessor)
                .withResponseDefaults(prettyPrint(), uriPreprocessor);
    }

}