Skip to content

Commit e20141e

Browse files
committed
Sync.
1 parent de3a9b0 commit e20141e

3 files changed

Lines changed: 77 additions & 13 deletions

File tree

sites/hurl.dev/_docs/asserting-response.md

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ jsonpath "$.cats[0].name" == "Felix"
2525
jsonpath "$.cats[0].lives" == 9
2626
```
2727

28+
Body responses can be encoded by server (see [`Content-Encoding` HTTP header]) but asserts in Hurl files are not
29+
affected by this content compression. All body asserts (`body`, `bytes`, `sha256` etc...) work _after_ content decoding.
30+
Finally, body text asserts (`body`, `jsonpath`, `xpath` etc...) are also decoded to strings based on [`Content-Type` header]
31+
so these asserts can be written with usual strings.
32+
2833
## Implicit asserts
2934

3035
### Version - Status
@@ -245,7 +250,8 @@ In this case, the XPath query `string(//article/@data-visible)` returns a string
245250
string.
246251

247252
The predicate function `==` can be used with string, numbers or booleans; `startWith` and `contains` can only
248-
be used with strings and bytes, while `matches` only works on string. If a query returns a number, using a `matches` predicate will cause a runner error.
253+
be used with strings and bytes, while `matches` only works on string. If a query returns a number, using a `matches`
254+
predicate will cause a runner error.
249255

250256
```hurl
251257
# A really well tested web page...
@@ -383,10 +389,8 @@ cookie "LSID[SameSite]" == "Lax"
383389
384390
### Body assert
385391

386-
Check the value of the received HTTP response body when decoded as a string.
387-
Body assert consists of the keyword `body` followed by a predicate function and
388-
value. The encoding used to decode the body is based on the `charset` value in the
389-
`Content-Type` header response.
392+
Check the value of the received HTTP response body when decoded as a string. Body assert consists of the keyword `body`
393+
followed by a predicate function and value.
390394

391395
```hurl
392396
GET https://example.org
@@ -395,18 +399,23 @@ HTTP 200
395399
body contains "<h1>Welcome!</h1>"
396400
```
397401

402+
The encoding used to decode the response body bytes to a string is based on the `charset` value in the `Content-Type`
403+
header response.
404+
398405
```hurl
399406
# Our HTML response is encoded with GB 2312 (see https://en.wikipedia.org/wiki/GB_2312)
400407
GET https://example.org/cn
401408
HTTP 200
402409
[Asserts]
403410
header "Content-Type" == "text/html; charset=gb2312"
411+
# bytes of the response, without any text decoding:
404412
bytes contains hex,c4e3bac3cac0bde7; # 你好世界 encoded in GB 2312
413+
# text of the response, decoded with GB 2312:
405414
body contains "你好世界"
406415
```
407416

408-
If the `Content-Type` doesn't include any encoding hint, a [`decode` filter] can be used to explicitly decode the body response
409-
bytes.
417+
If the `Content-Type` response header doesn't include any encoding hint, a [`decode` filter] can be used to explicitly
418+
decode the response body bytes.
410419

411420
```hurl
412421
# Our HTML response is encoded using GB 2312.
@@ -420,10 +429,31 @@ bytes contains hex,c4e3bac3cac0bde7; # 你好世界 encoded in GB2312
420429
bytes decode "gb2312" contains "你好世界"
421430
```
422431

432+
Body asserts are automatically decompressed based on the value of `Content-Encoding` response header. So,
433+
whatever is the response compression (`gzip`, `brotli`) etc... asserts values don't depend on the content encoding.
434+
435+
```hurl
436+
# Request a gzipped reponse, the `body` asserts works with ungzipped response
437+
GET https://example.org
438+
Accept-Encoding: gzip
439+
HTTP 200
440+
[Asserts]
441+
header "Content-Encoding" == "gzip"
442+
body contains "<h1>Welcome!</h1>"
443+
444+
# Without content encoding, asserts remains identical
445+
GET https://example.org
446+
HTTP 200
447+
[Asserts]
448+
header "Content-Encoding" not exists
449+
body contains "<h1>Welcome!</h1>"
450+
```
451+
452+
423453
### Bytes assert
424454

425-
Check the value of the received HTTP response body as a bytestream. Body assert
426-
consists of the keyword `bytes` followed by a predicate function and value.
455+
Check the value of the received HTTP response body as a bytestream. Body assert consists of the keyword `bytes`
456+
followed by a predicate function and value.
427457

428458
```hurl
429459
GET https://example.org/data.bin
@@ -434,6 +464,10 @@ bytes count == 12424
434464
header "Content-Length" == "12424"
435465
```
436466

467+
Like `body` assert, `bytes` assert works _after_ content encoding decompression (so the predicates values are not
468+
affected by `Content-Encoding` response header value).
469+
470+
437471
### XPath assert
438472

439473
Check the value of a [XPath] query on the received HTTP body decoded as a string (using the `charset` value in the
@@ -610,6 +644,26 @@ HTTP 200
610644
sha256 == hex,039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81;
611645
```
612646

647+
Like `body` assert, `sha256` assert works _after_ content encoding decompression (so the predicates values are not
648+
affected by `Content-Encoding` response header). For instance, if we have a resource `a.txt` on a server with a
649+
given hash `abcdef`, `sha256` value is not affected by `Content-Encoding`:
650+
651+
```hurl
652+
# Without content encoding compression:
653+
GET https://example.org/a.txt
654+
HTTP 200
655+
[Asserts]
656+
sha256 == hex,abcdef;
657+
658+
# With content encoding compression:
659+
GET https://example.org/a.txt
660+
Accept-Encoding: brotli
661+
HTTP 200
662+
[Asserts]
663+
header "Content-Encoding" == "brotli"
664+
sha256 == hex,abcdef;
665+
```
666+
613667
### MD5 assert
614668

615669
Check response body [MD5] hash.
@@ -621,6 +675,8 @@ HTTP 200
621675
md5 == hex,ed076287532e86365e841e92bfc50d8c;
622676
```
623677

678+
Like `sha256` asserts, `md5` assert works _after_ content encoding decompression (so the predicates values are not
679+
affected by `Content-Encoding` response header)
624680

625681
### Variable assert
626682

@@ -672,6 +728,11 @@ with <code>&#96;&#96;&#96;</code>. For a precise byte control of the response bo
672728
a [Base64] encoded string or an input file can be used to describe exactly
673729
the body byte content to check.
674730

731+
Like explicit [`body` assert], the body section is automatically decompressed based on the value of `Content-Encoding`
732+
response header. So, whatever is the response compression (`gzip`, `brotli`, etc...) body section doesn't depend on
733+
the content encoding. For textual body sections (JSON, XML, multiline, etc...), content is also decoded to string, based
734+
on the value of `Content-Type` response header.
735+
675736
### JSON body
676737

677738
{% raw %}
@@ -842,3 +903,6 @@ of all file nodes.
842903
[`decode` filter]: {% link _docs/filters.md %}#decode
843904
[headers implicit asserts]: #headers
844905
[RFC 3339]: https://www.rfc-editor.org/rfc/rfc3339
906+
[`Content-Encoding` HTTP header]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
907+
[`Content-Type` header]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
908+
[`body` assert]: #body-assert

sites/hurl.dev/_docs/frequently-asked-questions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ but also as a test tool for HTTP sessions, or even as documentation.
2525
Having a text based [file format] is another advantage. The Hurl format is simple,
2626
focused on the HTTP domain, can serve as documentation and can be read or written by non-technical people.
2727

28-
For instance posting JSON data with Hurl can be done with this simple file:
28+
For instance putting JSON data with Hurl can be done with this simple file:
2929

3030
```
31-
POST http://localhost:3000/api/login
31+
PUT http://localhost:3000/api/login
3232
{
3333
"username": "xyz",
3434
"password": "xyz"
@@ -39,7 +39,7 @@ With [curl]:
3939

4040
```
4141
curl --header "Content-Type: application/json" \
42-
--request POST \
42+
--request PUT \
4343
--data '{"username": "xyz","password": "xyz"}' \
4444
http://localhost:3000/api/login
4545
```

sites/hurl.dev/_docs/manual.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ will follow a redirection only for the second entry.
170170
| <a href="#cookie" id="cookie"><code>-b, --cookie &lt;FILE&gt;</code></a> | Read cookies from FILE (using the Netscape cookie file format).<br><br>Combined with [`-c, --cookie-jar`](#cookie-jar), you can simulate a cookie storage between successive Hurl runs.<br><br>This is a cli-only option.<br> |
171171
| <a href="#cookie-jar" id="cookie-jar"><code>-c, --cookie-jar &lt;FILE&gt;</code></a> | Write cookies to FILE after running the session (only for one session).<br>The file will be written using the Netscape cookie file format.<br><br>Combined with [`-b, --cookie`](#cookie), you can simulate a cookie storage between successive Hurl runs.<br><br>This is a cli-only option.<br> |
172172
| <a href="#curl" id="curl"><code>--curl &lt;FILE&gt;</code></a> | Export each request to a list of curl commands.<br><br>This is a cli-only option.<br> |
173-
| <a href="#delay" id="delay"><code>--delay &lt;MILLISECONDS&gt;</code></a> | Sets delay before each request (aka sleep). The delay is not applied to requests that have been retried because of [`--retry`](#retry). See [`--retry-interval`](#retry-interval) to space retried requests.<br><br>You can specify time units in the delay expression. Set Hurl to use a delay of 2 seconds with `--delay 2s` or set it to 500 milliseconds with `--delay 500ms`. No spaces allowed.<br> |
173+
| <a href="#delay" id="delay"><code>--delay &lt;MILLISECONDS&gt;</code></a> | Sets delay before each request (aka sleep). The delay is not applied to requests that have been retried because of [`--retry`](#retry). See [`--retry-interval`](#retry-interval) to space retried requests.<br><br>You can specify time units in the delay expression. Set Hurl to use a delay of 2 seconds with `--delay 2s` or set it to 500 milliseconds with `--delay 500ms`. No spaces allowed.<br> |
174174
| <a href="#error-format" id="error-format"><code>--error-format &lt;FORMAT&gt;</code></a> | Control the format of error message (short by default or long)<br><br>This is a cli-only option.<br> |
175175
| <a href="#file-root" id="file-root"><code>--file-root &lt;DIR&gt;</code></a> | Set root directory to import files in Hurl. This is used for files in multipart form data, request body and response output.<br>When it is not explicitly defined, files are relative to the Hurl file's directory.<br><br>This is a cli-only option.<br> |
176176
| <a href="#from-entry" id="from-entry"><code>--from-entry &lt;ENTRY_NUMBER&gt;</code></a> | Execute Hurl file from ENTRY_NUMBER (starting at 1).<br><br>This is a cli-only option.<br> |

0 commit comments

Comments
 (0)