Skip to content

Conversation

@srenauld
Copy link

This MR adds the ability to conditionally respond to an API call in a way other than by passing Prefer headers. In practice, it enables a developer to use examples described in an API blueprint to create a feature-rich, scenario-based API mock.

Each request is now conditionally checked for the following:

  • If a Body is present, this Body is compared with the request body (this also takes into account JSON, if headers are set to the right content-type)
  • If a Header is present, this is compared with the request header

If a Request matches both Body and Header, api-mock returns the associated response.

If nothing matched, by specification, api-mock returns the last defined response.

If a Prefer header is passed, it is taken into account as usual and the behaviour remains unchanged.

Unit-tests are updated to reflect this, with a coverage of 100%.

Seb Renauld added 5 commits February 19, 2015 18:04
This addition to api-mock allows conditional matching of request bodies in examples. In its simplest form,
it will determine which request payload (body+header combo) matches the request received through express, and
return the response payload from it.
If it fails to find one, it returns the last defined payload.

In addition to this, it still fully honours Prefer header, providing backward-compatibility with the original
api-mock.

Use this if you would like to define routes such as

# /v5/test{?foo,bar}

## Test [GET]

+ Request

    + Body

         {
             foo: bar
         }

+ Response 200 (application/json)

    +
    + Body

        { baz: true}

+ Response 404 (application/json)
This UT covers the new methods to build an express handler aggregate from
multiple payloads, and to conditionally match headers and body returns.
- Now also mixes in the route parameters defined in express
- Now also properly handles the Prefer header again
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 239bb2f on srenauld:issue-body-mapping into 698898d on localmed:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling ccf25e7 on srenauld:issue-body-mapping into 698898d on localmed:master.

@ecordell
Copy link
Contributor

The code looks great and I appreciate the solid tests along with it.

If nothing matched, by specification, api-mock returns the last defined response.

Can you explain the reasoning behind this? I'm not sure why this would be a desirable default behavior (and in particular I'm having trouble seeing how it helps "create a feature-rich, scenario-based API mock").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants