0

TL;DR: I commented on this issue and got asked to open a new ticket, but then realized this is more of a question as Spring RestDocs provides a way to achieve what I want (ignoring unimportant headers in contracts) with operation preprocessor. So here we are, on our friendly SoF

The problem is I am trying to generate contracts starting from a RestDocs test (using RestAssured and junit5 if it matters). Test setup (in Kotlin) looks like:

private val defaultDocument = document("{method_name}", SpringCloudContractRestDocs.dslContract())
lateinit var spec: RequestSpecification
@BeforeEach
internal fun setUp(restDocumentationContextProvider: RestDocumentationContextProvider) {
    RestAssured.port = port
    spec = RequestSpecBuilder()
        .setConfig(
            RestAssuredConfig.config()
                .objectMapperConfig(
                    ObjectMapperConfig.objectMapperConfig()
                        .jackson2ObjectMapperFactory { _, _ -> mapper }
                )
        )
        .addFilter(defaultDocument)
        .addFilter(ResponseLoggingFilter())
        .log(LogDetail.ALL)
        .build()
}

where mapper and port are injected as Spring beans.

The server generates a Date header, which is the time when the response is generated. This is done automatically by Spring WebMvc (I think) and I don't care at all for that header. However, the Date header causes stub generation to fail, as I decided to use Spring Cloud Contracts in a polyglot world approach to generate and upload stub to maven repository, because now the server generates a different date.

As I point out here, the ContractDslSnippet does not seem to provide a way to ignore unimportant headers and/or to add matchers (which would still an open question).

The (short) list of questions:

  • How can I filter out unimportant headers from generated contracts?
  • Can I add custom matchers for headers, like I can do for body?
ThanksForAllTheFish
  • 7,101
  • 5
  • 35
  • 54
  • Spring MVC doesn’t add a `Date` header. It’s probably coming from whatever you’re using as the HTTP server. See https://stackoverflow.com/a/19292132/1384297, for example. – Andy Wilkinson Oct 30 '18 at 23:55

1 Answers1

0

How to remove unimportant header, using Spring RestDocs preprocessors:

private val defaultDocument = document("{method_name}", SpringCloudContractRestDocs.dslContract())
lateinit var spec: RequestSpecification
@BeforeEach
internal fun setUp(restDocumentationContextProvider: RestDocumentationContextProvider) {
    RestAssured.port = port
    spec = RequestSpecBuilder()
        .setConfig(
            RestAssuredConfig.config()
                .objectMapperConfig(
                    ObjectMapperConfig.objectMapperConfig()
                        .jackson2ObjectMapperFactory { _, _ -> mapper }
                )
        )
        .addFilter(
            documentationConfiguration(restDocumentationContextProvider)
                .operationPreprocessors()
                .withResponseDefaults(Preprocessors.removeMatchingHeaders("Date"))
        )
        .addFilter(defaultDocument)
        .addFilter(ResponseLoggingFilter())
        .log(LogDetail.ALL)
        .build()
}

The important part is adding a new filter (the first one), which takes care of configuring Spring RestDocs to remove the Date from all its snippets, including the contract ones.

How to add custom matchers, using the default SpringCloudContractRestDocs.dslContract(): I don't think it is actually possible right now, but might be wrong here (glad if somebody can chime in and correct me in case)

ThanksForAllTheFish
  • 7,101
  • 5
  • 35
  • 54