Spring Cloud Contract for provider - setting optional header

534 views Asked by At

I've created a contract on a provider side:

Contract.make { 
    request {
        method 'GET'
        url('/cars/car?id=3')
        headers {
            header(accept(), "application/hal+json")
            header(SOME OTHER HEADER)
        }
    }
    response {
        ...
    }
}

Unfortunately one of my customers do not send request with "header(SOME OTHER HEADER)". My question is how can I mark "header(SOME OTHER HEADER)" as optional?

2

There are 2 answers

0
PRF On

I'm experiencing the same issue here. I need to include Access-Control-Allow-Origin header to the generated stub but if I add it to the contract the tests begins to fail becouse of:

Expecting:
 <null>
to be equal to:
 <"\"Access-Control-Allow-Origin\": \"*\"">

even if I annotate the RestController class with @CrossOrigin(value = "*")

0
Marcin Grzejszczak On

You can have the value optional. Not the header itself. If the header is optional then create two separate contracts. One with the header and one without it.