Skip to content

Feature Request - support dynamic responses based on ID in request #110

@YOU54F

Description

@YOU54F

Feature Request - support dynamic responses based on ID in request

Why? - one of the key features of pact-stub-service, is its ability to be used in integration / e2e tests. Allowing dynamic responses based on a user provided ID in the request body would allow it to stub real world scenarios in which a client requests an operation on a specific resource and the provider returns a reference to that resource in the request body. A consumer would validate this resource reference to ensure that any operations based on the response is for the correct resource.

This feature request would bring pact stub service closer to other API mocking tools such as wiremock (see http://wiremock.org/docs/response-templating/).

Current state of play :-

I have a pact that will accept http://localhost:8084/request/path/1

and will return

{
    "id": "1",
    "status": "LOOSE_MATCH"
}

It is setup with a term matcher on the request path

            path: term({
              generate: requestPath,
              matcher: "/request/path/(?![2]$)\\d+"
            })

so that it will match anything that isn’t http://localhost:8084/request/path/2

I want to set it up so that when I perform the request http://localhost:8084/request/path/123, the response body will return the ID in the url that was matched

{
    "id": "123",
    "status": "LOOSE_MATCH"
}

My pact test is here
https://github.com/YOU54F/jest-pact-typescript/blob/requestPathExample/src/pact/client/requestPathMatching.pacttest.ts

and my generated pact is here
https://github.com/YOU54F/jest-pact-typescript/blob/requestPathExample/docker/pact-stub-service/pacts/test-consumer-request-path-provider.json

Full example in a PR :- YOU54F/template-jest-pact-typescript#101

Instructions to run example

1. `git clone [email protected]:YOU54F/jest-pact-typescript.git`
2. `git checkout requestPathExample`
3. `yarn install`
4. `yarn run pact-test`
5. `cd docker/pact-stub-service`
6. `make copy-pacts`
7. `cd ../docker`
8. `docker-compose up`

My initial thoughts

I wonder if, similar to the verifier where you can override the response body, if we do a similar thing within the stub service, without having to mess with the contracts themselves.

Response from Matt -

That’s probably the approach that would be taken, or at the very least, it would be authored separately to the test itself and appended to the contract. Could even be a separate file that contained predicates/stub logic

Comments initially raised in slack
https://pact-foundation.slack.com/archives/C9VBGLUM9/p1559955411019400

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions