Expectations
test.NewResponseExpected defines the contract that must comply with an HTTP response.
It is possible to validate the status code, the headers, cookies and body. The omitted properties do not restrict the corresponding response.
Properties
| Property | Description |
|---|---|
| Status | HTTP status code expected. |
| Headers | Rules applied to headers. |
| Cookies | Rules applied to cookies. |
| Body | Rules applied to the body. |
Status
Status requires a specific status code.
expected := test.NewResponseExpected{Status: 200} The zero value omits this validation. ctx.Response are compared to the indicated value.
Headers
test.ExpectedHeaders provides four types of rule:
| Method | Behavior |
|---|---|
Equal(values) | It demands exactly the indicated headers and values. |
Contains(values) | It requires the indicated headers and allows additional headers. |
NotContains(values) | Reject the indicated headers and values. |
Custom(values) | Apply a different comparator to each header. |
expected := test.NewResponseExpected{
Headers: test.ExpectedHeaders.Contains(test.Map{
"Content-Type": "application/json; charset=utf-8",
}),
} The names of the headers are compared without depending on capital or lower capital.
Cookies
test.ExpectedCookies offers Equal(), Contains(), NotContains() and Custom() for the same purpose as the header rules.
expected := test.NewResponseExpected{
Cookies: test.ExpectedCookies.Contains(test.Map{
"language": "es",
}),
} The validation compares the name and value of each cookie returned.
Body
test.ExpectedBody includes the following rules:
| Method | Behavior |
|---|---|
Equal(content) | It requires the full text. |
NotEqual(content) | Reject the full text. |
Contains(patterns...) | It requires one or more fragments. |
NotContains(patterns...) | Reject one or more fragments. |
JSONEqual(value) | It demands the full JSON value. |
JSONNotEqual(value) | Reject the full JSON value. |
JSONContains(value) | It demands a JSON subset. |
JSONNotContains(value) | Reject a JSON subset. |
Snapshot(file) | Compare the body with a file. |
Custom(rules...) | It combines several body rules. |
expected := test.NewResponseExpected{
Status: 200,
Body: test.ExpectedBody.JSONContains(test.Map{
"name": "George",
}),
} JSONContains() allows additional properties in the response. snapshots are useful when a complete document stored in a file must be validated.
Custom Comparators
test.Expect provides reusable comparators for Custom():
| Method | Validation |
|---|---|
Equal(value) | The value exists and is the same. |
NotEqual(value) | The value is not the same. |
Contains(value) | The text contains the fragment. |
NotContains(value) | The text does not contain the fragment. |
Exists() | The value exists. |
NotExists() | The value does not exist. |
expected := test.NewResponseExpected{
Headers: test.ExpectedHeaders.Custom(map[string]test.ExpectedStringRule{
"Content-Type": test.Expect.Contains("application/json"),
"X-Request-ID": test.Expect.Exists(),
}),
} Integration with Go
The expectations implement specific interfaces for headers, cookies and bodies. Each factory returns a value that meets the corresponding interface and can be stored in a variable before building the contract.
JSON matchers decode the response before comparing their values. This prevents the order of properties or the numerical representation of JSON from converting equivalent responses into failures.
Best practices
- It only applies to aspects which are part of the public contract.
- Use
Contains()where the response may include legitimate additional data. - He prefers the JSON matchers for JSON responses.
- Use
Custom()when each field needs a different rule. - Book snapshots for full and stable content.