|
| 1 | +# Consumer Tests |
| 2 | + |
| 3 | +## Contract Testing Process (HTTP) |
| 4 | + |
| 5 | +Pact is a consumer-driven contract testing tool, which is a fancy way of saying that the API `Consumer` writes a test to set out its assumptions and needs of its API `Provider`(s). By unit testing our API client with Pact, it will produce a `contract` that we can share to our `Provider` to confirm these assumptions and prevent breaking changes. |
| 6 | + |
| 7 | +The process looks like this: |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +1. The consumer writes a unit test of its behaviour using a Mock provided by Pact |
| 12 | +1. Pact writes the interactions into a contract file (as a JSON document) |
| 13 | +1. The consumer publishes the contract to a broker (or shares the file in some other way) |
| 14 | +1. Pact retrieves the contracts and replays the requests against a locally running provider |
| 15 | +1. The provider should stub out its dependencies during a Pact test, to ensure tests are fast and more deterministic. |
| 16 | + |
| 17 | +In this document, we will cover steps 1-3. |
| 18 | + |
| 19 | +## Consumer package |
| 20 | + |
| 21 | +The consumer interface is in the package: `github.com/pact-foundation/pact-go/v2/consumer`. |
| 22 | + |
| 23 | +The two primary interfaces are `NewV2Pact` and `NewV3Pact`. If your provider is also V3 compatible, you can use the V3 variant, otherwise you should stick with V2. |
| 24 | + |
| 25 | +## Writing a Consumer test |
| 26 | + |
| 27 | +The purpose of a Pact test is to unit test the API Client of the consumer. |
| 28 | + |
| 29 | +In this example, we are going to be testing our Product API client, responsible for communicating with the `ProductAPI` over HTTP. It currently has a single method `GetProduct(id)` that will return a `*Product`. |
| 30 | + |
| 31 | +Pact tests have a few key properties. We'll demonstrate a common example using the 3A `Arrange/Act/Assert` pattern. |
| 32 | + |
| 33 | +Here is a sequence diagram that shows how a consumer test works: |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +### Example |
| 38 | + |
| 39 | +```golang |
| 40 | +func TestProductAPIClient(t *testing.T) { |
| 41 | + // Specify the two applications in the integration we are testing |
| 42 | + // NOTE: this can usually be extracted out of the individual test for re-use) |
| 43 | + mockProvider, err := NewV2Pact(MockHTTPProviderConfig{ |
| 44 | + Consumer: "ProductAPIConsumer", |
| 45 | + Provider: "ProductAPI", |
| 46 | + }) |
| 47 | + assert.NoError(t, err) |
| 48 | + |
| 49 | + // Arrange: Setup our expected interactions |
| 50 | + mockProvider. |
| 51 | + AddInteraction(). |
| 52 | + Given("A Product with ID 10 exists"). |
| 53 | + UponReceiving("A request for Product 10"). |
| 54 | + WithRequest("GET", S("/product/10")). |
| 55 | + WillRespondWith(200). |
| 56 | + WithBodyMatch(&Product{}) |
| 57 | + |
| 58 | + // Act: test our API client behaves correctly |
| 59 | + err = mockProvider.ExecuteTest(func(config MockServerConfig) error { |
| 60 | + // Initialise the API client and point it at the Pact mock server |
| 61 | + // Pact spins up a dedicated mock server for each test |
| 62 | + client := newClient(config.Host, config.Port) |
| 63 | + |
| 64 | + // Execute the API client |
| 65 | + product, err := client.GetProduct("10") |
| 66 | + |
| 67 | + // Assert: check the result |
| 68 | + assert.NoError(t, err) |
| 69 | + assert.Equal(t, 10, product.ID) |
| 70 | + |
| 71 | + return err |
| 72 | + }) |
| 73 | + assert.NoError(t, err) |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## Matching |
| 78 | + |
| 79 | +In addition to matching on exact values, there are a number of useful matching functions |
| 80 | +in the `matching` package that can increase expressiveness of your tests and reduce brittle |
| 81 | +test cases. |
| 82 | + |
| 83 | +Rather than use hard-coded values which must then be present on the Provider side, |
| 84 | +you can use regular expressions and type matches on objects and arrays to validate the |
| 85 | +structure of your APIs. |
| 86 | + |
| 87 | +Matchers can be used on the `Body`, `Headers`, `Path` and `Query` fields of the request, |
| 88 | +and the `Body` and `Headers` on the response. |
| 89 | + |
| 90 | +_NOTE: Some matchers are only compatible with the V3 interface, and must not be used with a V2 Pact. Your test will panic if this is attempted_ |
| 91 | + |
| 92 | +| Matcher | Min. Compatibility | Description | |
| 93 | +| ---------------------------------- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 94 | +| `Like(content)` | V2 | Tells Pact that the value itself is not important, as long as the element _type_ (valid JSON number, string, object etc.) itself matches. | |
| 95 | +| `Term(example, matcher)` | V2 | Tells Pact that the value should match using a given regular expression, using `example` in mock responses. `example` must be a string. | |
| 96 | +| `EachLike(content, min)` | V2 | Tells Pact that the value should be an array type, consisting of elements like those passed in. `min` must be >= 1. `content` may be any valid JSON value: e.g. strings, numbers and objects. | |
| 97 | +| `Equality(content)` | V3 | Matchers cascade, equality resets the matching process back to exact values | |
| 98 | +| `Includes(content)` | V3 | Checks if the given string is contained by the actual value | |
| 99 | +| `FromProviderState(expr, example)` | V3 | Marks an item as to be dynamically injected from the provider state during provider verification | |
| 100 | +| `EachKeyLike(key, template)` | V3 | Object where the key itself is ignored, but the value template must match. Useful for dynamic keys. | |
| 101 | +| `ArrayContaining(variants)` | V3 | Allows heterogenous items to be matched within a list. Unlike EachLike which must be an array with elements of the same shape, ArrayContaining allows objects of different types and shapes. Useful for hypermedia responses such as Siron, HAL and JSONAPI | |
| 102 | +| `ArrayMinMaxLike(min, max` | V3 | Like EachLike except has a bounds on the max and the min | |
| 103 | +| `ArrayMaxLike` | V3 | Like EachLike except has a bounds on the max | |
| 104 | +| `DateGenerated` | V3 | Matches a cross platform formatted date, and generates a current date during verification | |
| 105 | +| `TimeGenerated` | V3 | Matches a cross platform formatted date, and generates a current time during verification | |
| 106 | +| `DateTimeGenerated` | V3 | Matches a cross platform formatted datetime, and generates a current datetime during verification | |
| 107 | + |
| 108 | +### Matching by regular expression |
| 109 | + |
| 110 | +_Example:_ |
| 111 | + |
| 112 | +Here is a more complex example that shows how all 3 terms can be used together: |
| 113 | + |
| 114 | +```go |
| 115 | + body := |
| 116 | + Like(map[string]interface{}{ |
| 117 | + "response": map[string]interface{}{ |
| 118 | + "name": Like("Billy"), |
| 119 | + "type": Term("admin", "admin|user|guest"), |
| 120 | + "items": EachLike("cat", 2) |
| 121 | + }, |
| 122 | + }) |
| 123 | +``` |
| 124 | + |
| 125 | +This example will result in a response body from the mock server that looks like: |
| 126 | + |
| 127 | +```json |
| 128 | +{ |
| 129 | + "response": { |
| 130 | + "name": "Billy", |
| 131 | + "type": "admin", |
| 132 | + "items": ["cat", "cat"] |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +### Match common formats |
| 138 | + |
| 139 | +| method | Min. Compatibility | description | |
| 140 | +| --------------- | ------------------ | ----------------------------------------------------------------------------------------------- | |
| 141 | +| `Identifier()` | V2 | Match an ID (e.g. 42) | |
| 142 | +| `Integer()` | V3 | Match all numbers that are integers (both ints and longs) | |
| 143 | +| `Decimal()` | V3 | Match all real numbers (floating point and decimal) | |
| 144 | +| `HexValue()` | V2 | Match all hexadecimal encoded strings | |
| 145 | +| `Date()` | V2 | Match string containing basic ISO8601 dates (e.g. 2016-01-01) | |
| 146 | +| `Timestamp()` | V2 | Match a string containing an RFC3339 formatted timestamp (e.g. Mon, 31 Oct 2016 15:21:41 -0400) | |
| 147 | +| `Time()` | V2 | Match string containing times in ISO date format (e.g. T22:44:30.652Z) | |
| 148 | +| `IPv4Address()` | V2 | Match string containing IP4 formatted address | |
| 149 | +| `IPv6Address()` | V2 | Match string containing IP6 formatted address | |
| 150 | +| `UUID()` | V2 | Match strings containing UUIDs | |
| 151 | + |
| 152 | +### Auto-generate matchers from struct tags |
| 153 | + |
| 154 | +Furthermore, if you isolate your Data Transfer Objects (DTOs) to an adapters package so that they exactly reflect the interface between you and your provider, then you can leverage `WithBodyMatch(object)` option to auto-generate the expected response body in your contract tests. Under the hood, it recursively traverses the DTO struct and uses `Term, Like, and EachLike` to create the contract. |
| 155 | + |
| 156 | +This saves the trouble of declaring the contract by hand. It also maintains one source of truth. To change the consumer-provider interface, you only have to update your DTO struct and the contract will automatically follow suit. |
| 157 | + |
| 158 | +_Example:_ |
| 159 | + |
| 160 | +```go |
| 161 | +type DTO struct { |
| 162 | + ID string `json:"id"` |
| 163 | + Title string `json:"title"` |
| 164 | + Tags []string `json:"tags" pact:"min=2"` |
| 165 | + Date string `json:"date" pact:"example=2000-01-01,regex=^\\d{4}-\\d{2}-\\d{2}$"` |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +then specifying a response body is as simple as: |
| 170 | + |
| 171 | +```go |
| 172 | + // Set up our expected interactions. |
| 173 | + pact. |
| 174 | + AddInteraction(). |
| 175 | + ... |
| 176 | + WithBodyMatch(DTO{}), // That's it!!! |
| 177 | +``` |
| 178 | + |
| 179 | +The `pact` struct tags shown above are optional. By default, it asserts that the JSON shape matches the struct and that the field types match. |
| 180 | + |
| 181 | +## Publishing Pacts to a Broker |
| 182 | + |
| 183 | +We recommend publishing the contracts to a Pact Broker (or https://pactflow.io) using the [CLI Tools]()https://docs.pact.io/implementation_guides/cli/#pact-cli. |
| 184 | + |
| 185 | +[Read more](https://docs.pact.io/pact_broker/publishing_and_retrieving_pacts/) about publishing pacts. |
0 commit comments