You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+75-27
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,7 @@ Read [Getting started with Pact] for more information for beginners.
43
43
-[Provider API Testing](#provider-api-testing)
44
44
-[Verification Options](#verification-options)
45
45
-[API with Provider States](#api-with-provider-states)
46
-
-[API with Authorization](#api-with-authorization)
46
+
-[Modify Requests Prior to Verification (Request Filters)](#modify-requests-prior-to-verification-request-filters)
47
47
-[Publishing Pacts to a Broker](#publishing-pacts-to-a-broker)
48
48
-[Publishing options](#publishing-options)
49
49
-[Publishing Verification Results to a Pact Broker](#publishing-verification-results-to-a-pact-broker)
@@ -157,9 +157,6 @@ The first step is to create a test for your API Consumer. The example below uses
157
157
Check out the `examples` folder for examples with Karma Jasmine, Mocha and Jest. The example below is taken from the [integration spec](https://github.com/pact-foundation/pact-js/blob/master/src/pact.integration.spec.ts).
158
158
159
159
```javascript
160
-
/**
161
-
* The following example is for Pact version 5
162
-
*/
163
160
constpath=require("path")
164
161
constchai=require("chai")
165
162
const { Pact } =require("@pact-foundation/pact")
@@ -275,47 +272,98 @@ new Verifier().verifyProvider(opts).then(function () {
|`provider`| true | string | Name of the Provider. Required. |
282
-
|`pactUrls`| true | array of strings | Array of local Pact file paths or HTTP-based URLs (e.g. from a broker). Required if not using a Broker. |
283
-
|`pactBrokerUrl`| false | string | URL of the Pact Broker to retrieve pacts from. Required if not using pactUrls. |
284
-
|`tags`| false | array of strings | Array of tags, used to filter pacts from the Broker. Optional. |
285
-
|`providerStatesSetupUrl`| false | string | Optional URL to call with a POST request for each `providerState` defined in a pact (see below for more info). |
|`publishVerificationResult`| false | boolean | Publish verification result to Broker | boolean |
289
-
|`providerVersion`| false | string | Provider version, required to publish verification results to a broker |
290
-
|`customProviderHeaders`| false | array of strings | Header(s) to add to provider state set up and pact verification re`quests`. eg 'Authorization: Basic cGFjdDpwYWN0'.Broker. Optional otherwise. |
291
-
|`timeout`| false | number | The duration in ms we should wait to confirm verification process was successful. Defaults to 30000, Optional. |
|`provider`| true | string | Name of the Provider. Required. |
279
+
|`pactUrls`| true | array of strings | Array of local Pact file paths or HTTP-based URLs (e.g. from a broker). Required if not using a Broker. |
280
+
|`pactBrokerUrl`| false | string | URL of the Pact Broker to retrieve pacts from. Required if not using pactUrls. |
281
+
|`tags`| false | array of strings | Array of tags, used to filter pacts from the Broker. |
282
+
|`providerStatesSetupUrl`| false | string | DEPRECATED (see `stateHandlers`). URL to call with a POST request for each `providerState` defined in a pact (see below for more info). |
|`publishVerificationResult`| false | boolean | Publish verification result to Broker | boolean |
286
+
|`providerVersion`| false | string | Provider version, required to publish verification results to a broker |
287
+
|`customProviderHeaders`| false | array of strings | Header(s) to add to any requests to the provider service. eg `Authorization: Basic cGFjdDpwYWN0`. All interactions will receive the header. See `requestFilter` for when more flexiblility is required in modifying the request to the provider. |
288
+
|`timeout`| false | number | The duration in ms we should wait to confirm verification process was successful. Defaults to 30000. |
289
+
|`requestFilter`| false | object | An Express middleware handler (See https://expressjs.com/en/guide/writing-middleware.html) to modify requests and responses from the provider. See below for more details. |
290
+
|`stateHandlers`| false | object | Provider state handlers. A map of `string` -> `() => Promise`, where each string is the state to setup, and the function is used to configure the state in the Provider. See below for detail. |
292
291
293
292
That's it! Read more about [Verifying Pacts](https://docs.pact.io/getting_started/verifying_pacts).
294
293
295
294
#### API with Provider States
296
295
297
-
If you have defined any `state`s in your consumer tests, the `Verifier` can put the provider into the right state right before submitting a request (for example, the provider can use the state to mock away certain database queries). To support this, the provider must provide an extra API endpoint that accepts the query parameters `consumer` and `state` and returns an HTTP `200`.
296
+
If you have defined any `state`s in your consumer tests, the `Verifier` can put the provider into the right state prior to sending the request. For example, the provider can use the state to mock away certain database queries. To support this, set up a handler for each `state` using hooks on the `stateHandlers` property. Here is an example from our [e2e suite](https://github.com/pact-foundation/pact-js/blob/master/examples/e2e/test/provider.spec.js):
298
297
299
-
You can then configure this endpoint as `providerStatesSetupUrl` in the options passed into `Verifier.verifyProvider()`. If this option is not configured, the `Verifier` will ignore the provider states defined in the pact.
298
+
```js
299
+
let opts = {
300
+
...
301
+
stateHandlers: {
302
+
"Has no animals": () => {
303
+
animalRepository.clear()
304
+
returnPromise.resolve(`Animals removed from the db`)
305
+
},
306
+
"Has some animals": () => {
307
+
importData()
308
+
returnPromise.resolve(`Animals added to the db`)
309
+
},
310
+
"Has an animal with ID 1": () => {
311
+
importData()
312
+
returnPromise.resolve(`Animals added to the db`)
313
+
}
314
+
}
315
+
}
300
316
301
-
See this [Provider](https://github.com/pact-foundation/pact-js/blob/master/examples/e2e/test/provider.spec.js) for a working example, or read more about [Provider States](https://docs.pact.io/getting_started/provider_states).
As you can see, for each state ("Has no animals", ...), we configure the local datastore differently. If this option is not configured, the `Verifier` will ignore the provider states defined in the pact and log a warning.
304
321
305
-
Sometimes you may need to add things to the requests that can't be persisted in a pact file. Examples of these would be authentication tokens, which have a small life span. e.g. an OAuth bearer token: `Authorization: Bearer 0b79bab50daca910b000d4f1a2b675d604257e42`.
322
+
Read more about [Provider States](https://docs.pact.io/getting_started/provider_states).
306
323
307
-
For this case, we have a facility that should be carefully used during verification - the ability to specificy custom headers to be sent during provider verification. The flag to achieve this is `customProviderHeaders`.
324
+
#### Modify Requests Prior to Verification (Request Filters)
308
325
309
-
For example, to have two headers sent as part of the verification request, modify the `verifyProvider` options as per below:
326
+
Sometimes you may need to add things to the requests that can't be persisted in a pact file. Examples of these are authentication tokens with a small life span. e.g. an OAuth bearer token: `Authorization: Bearer 0b79bab50daca910b000d4f1a2b675d604257e42`.
327
+
328
+
For these cases, we have two facilities that should be carefully used during verification:
329
+
330
+
1. the ability to specify custom headers to be sent during provider verification. The flag to achieve this is `customProviderHeaders`.
331
+
1. the ability to modify a request/response and modify the payload. The flag to achieve this is `requestFilter`.
332
+
333
+
**Example API with Authorization**
334
+
335
+
For example, to have an `Authorization` bearer token header sent as part of the verification request, set the `verifyProvider` options as per below:
310
336
311
337
```js
338
+
let token
312
339
let opts = {
313
340
provider:'Animal Profile Service',
314
341
...
315
-
customProviderHeaders: ['Authorization: Bearer e5e5e5e5e5e5e5', 'SomeSpecialHeader: some specialvalue']
342
+
stateHandlers: {
343
+
"is authenticated": () => {
344
+
token ="1234"
345
+
Promise.resolve(`Valid bearer token generated`)
346
+
},
347
+
"is not authenticated": () => {
348
+
token =""
349
+
Promise.resolve(`Expired bearer token generated`)
350
+
}
351
+
},
352
+
353
+
// this middleware is executed for each request, allowing `token` to change between invocations
354
+
// it is common to pair this with `stateHandlers` as per above, that can set/expire the token
355
+
// for different test cases
356
+
requestFilter: (req, res, next) => {
357
+
req.headers["Authorization"] =`Bearer: ${token}`
358
+
next()
359
+
},
360
+
361
+
// This header will always be sent for each and every request, and can't be dynamic
362
+
// (i.e. passing a variable instead of the bearer token)
0 commit comments