Bohdan Petrenko
Bohdan Petrenko

Reputation: 1175

Reference request query parameter from response in spring cloud contract

How to reference request query parameter from response in spring cloud contract tests.

Lets say I have a following code:

Contract.make {
    description("Contract description")
    request {
        method 'GET'
        urlPath('/some/url') {
            queryParameters {
                parameter 'user_id': $(consumer(~/\d+/), producer(111))
                parameter 'session_id': $(consumer(~/\d+/), producer(222))
                parameter 'segment_ids': $(consumer(~/"\[?(\d|,\s*)*\]?/), producer([1, 2, 3]))
            }
        }
    }
    response {
        status 200
        body([1, 2, 3])
        headers {
            contentType applicationJsonUtf8()
        }
    }
}

And I want to replace [1, 2, 3] in the response body with segment_ids value from the request

Upvotes: 0

Views: 765

Answers (1)

Bohdan Petrenko
Bohdan Petrenko

Reputation: 1175

Use fromRequest().query("request_param") doc

In my case:

$(consumer(fromRequest().query("segment_ids")), producer(~/"\[?(\d|,\s*)*\]?/))

And a full answer

Contract.make {
    priority 1
    description("Should return available segments for pachinko-game service")
    request {
        method 'GET'
        urlPath('/segmentation-api/evaluation') {
            queryParameters {
                parameter 'user_id': $(consumer(~/\d+/), producer(111))
                parameter 'session_id': $(consumer(~/\d+/), producer(222))
                parameter 'segment_ids': $(consumer(~/"\[?(\d|,\s*)*\]?/), producer([1, 2, 3]))
            }
        }
    }
    response {
        status 200
        body($(consumer(fromRequest().query("segment_ids")), producer(~/"\[?(\d|,\s*)*\]?/)))
        headers {
            contentType applicationJsonUtf8()
        }
    }
}

Upvotes: 3

Related Questions