Skip to content

Commit da8b8cd

Browse files
authored
Merge pull request #3 from fred1268/release-1.1.2
Release 1.1.2
2 parents 562d682 + 1d624bc commit da8b8cd

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ A test file looks like the following:
159159
"expected": {
160160
"statuscode": 200
161161
}
162+
},
163+
{
164+
"name": "121007",
165+
"server": "hackernews",
166+
"method": "GET",
167+
"endpoint": "/v0/item/121007.json",
168+
"expected": {
169+
"statuscode": 200,
170+
"response": "\\s+[wW]eight"
171+
}
162172
}
163173
]
164174
}
@@ -186,7 +196,7 @@ A test file contains an array of tests, each of them containing:
186196

187197
- `statuscode` (mandatory): the expected status code returned by the endpoint (200, 401, 403, etc.)
188198

189-
- `response` (default none): the expected payload returned by the endpoint. This field is optional
199+
- `response` (default none): the expected payload returned by the endpoint.
190200

191201
> Please note that `payload` and `response` can be either a string (including json, as shown in 121004), or `@file` (as shown in 121005) or even a `@custom_filename.json` (as shown in doesnotwork). This is useful if you prefer to separate the test from its `payload` or expected `response` (for instance, it is handy if the `payload` or `response` are complex JSON structs that you can easily copy and paste from somewhere else, or simply prefer to avoid escaping double quotes). However, keeping the names for `payload` and `response` like `test_name.payload.json`and `test_name.expected.json` is still a good practice.
192202
@@ -209,7 +219,7 @@ As we saw earlier, for each test, you will have to define the expected response.
209219
- if the response is a non-JSON string:
210220
- the response is compared to `expected` and success or failure is reported
211221

212-
> Please note that, in the case of non-JSON responses, you can use the `%` character to match start, end or part of the response, pretty much like in SQL. For instance, expected responses like `"%test"`, `"test%"` and `"%test%"` will match test at the beginning, end or as part of the returned response respectively.
222+
> Please note that, in the case of non-JSON responses, you can use regular expressions (see test 121007). In that case, make sure the `expected.response` field is set to a proper, compilable, regular expression. Be mindful that you will need to escape the `\ (backslash)` character using `\\`. For instance `\s+[wW]eight` will be written `\\s+[wW]eight`, in order to match one or more whitespace characters, followed by weight or Weight.
213223
214224
## Running okapi :giraffe:
215225

@@ -227,9 +237,9 @@ where options are one or more of the following:
227237

228238
- `--file-parallel` (default no): run the test files in parallel (instead of the tests themselves)
229239

230-
- `--file` (default none): only run the specified test file
240+
- `--file`, `-f` (default none): only run the specified test file
231241

232-
- `--test` (default none): only run the specified standalone test
242+
- `--test`, `-t` (default none): only run the specified standalone test
233243

234244
- `--timeout` (default 30s): set a default timeout for all HTTP requests
235245

assets/tests/hackernews.items.test.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"method": "GET",
5656
"endpoint": "/v0/item/121007.json",
5757
"expected": {
58-
"statuscode": 200
58+
"statuscode": 200,
59+
"response": "\\s+[wW]eight"
5960
}
6061
},
6162
{

testing/internal/json/json.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@ import (
44
"encoding/json"
55
"errors"
66
"reflect"
7+
"regexp"
78
"strings"
89
)
910

1011
var ErrJSONMismatched error = errors.New("json mismatched")
1112

1213
func compareStrings(wanted, got string) error {
13-
realWanted := strings.Trim(wanted, "%")
14-
if strings.HasPrefix(wanted, "%") && strings.HasSuffix(wanted, "%") && !strings.Contains(got, realWanted) {
15-
return ErrJSONMismatched
16-
} else if strings.HasPrefix(wanted, "%") && !strings.HasPrefix(got, realWanted) {
17-
return ErrJSONMismatched
18-
} else if strings.HasSuffix(wanted, "%") && !strings.HasSuffix(got, realWanted) {
19-
return ErrJSONMismatched
20-
} else if wanted != got {
14+
matched, err := regexp.MatchString(wanted, got)
15+
if err != nil {
16+
return err
17+
}
18+
if !matched {
2119
return ErrJSONMismatched
2220
}
2321
return nil

0 commit comments

Comments
 (0)