Skip to content

Commit 9fea2e3

Browse files
authored
Make location header fetching case-insensitive (#83)
1 parent fde2e7c commit 9fea2e3

4 files changed

Lines changed: 11 additions & 9 deletions

File tree

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,15 +368,17 @@ The headers are available as `responseHeaders`. However, this can be tricky to u
368368
in the form `Map<String, List<String>>`, and it preserves the case of the returned header names even though they should
369369
be treated as case insensitive. Because of this, there is a shortcut `header` which matches the header name
370370
case-insensitively and matches any value of that key. In the example below, the first 3 are equivalent but the 4th
371-
fails:
371+
fails. There is a new function which helps with this problem as the last example shows:
372372
```gherkin
373373
* match header Content-Type contains 'text/turtle'
374374
* match header content-type contains 'text/turtle'
375-
* responseHeaders['Content-Type'][0] contains 'text/turtle'
376-
* responseHeaders['content-type'][0] contains 'text/turtle' # fails as responseHeaders['content-type'] returns null
375+
* match responseHeaders['Content-Type'][0] contains 'text/turtle'
376+
* match responseHeaders['content-type'][0] contains 'text/turtle' # fails as responseHeaders['content-type'] returns null
377+
* match karate.response.header('content-type') contains 'text/turtle'
377378
```
378379
Note that it is safer to use `contains` instead of `==` in this case since the header value may contain an encoding
379380
element such as `; charset=UTF-8`.
381+
You can also get an array of all the values of a header with `karate.response.headerValues()`.
380382
381383
Using the `responseStatus` variable as an alternative to `status` was mentioned earlier.
382384

protocol/cors/preflight-requests.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Feature: Server must implement the CORS protocol for preflight requests
5050
And header Access-Control-Request-Headers = 'X-CUSTOM, Content-Type'
5151
When method OPTIONS
5252
Then match [301, 308] contains responseStatus
53-
* def location = responseHeaders['Location'][0]
53+
* def location = karate.response.headerValues('location')[0]
5454

5555
Given url location
5656
And header Origin = 'https://tester'

protocol/cors/preflight.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Feature: Server must support HTTP OPTIONS for CORS preflight requests
2727
And header Access-Control-Request-Headers = 'X-CUSTOM, Content-Type'
2828
When method OPTIONS
2929
Then match [301, 308] contains responseStatus
30-
* def location = responseHeaders['Location'][0]
30+
* def location = karate.response.headerValues('location')[0]
3131

3232
Given url location
3333
And header Origin = 'https://tester'

protocol/writing-resource/slash-semantics-exclude.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Feature: With and without trailing slash cannot co-exist
6060
And header Link = '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"'
6161
When method POST
6262
Then assert responseStatus >= 200 && responseStatus < 300
63-
And def childContainerUrl = responseHeaders['Location'][0]
63+
And def childContainerUrl = karate.response.headerValues('location')[0]
6464
And assert childContainerUrl.endsWith('/')
6565
6666
# confirm there is no non-container resource with the same URI
@@ -87,7 +87,7 @@ Feature: With and without trailing slash cannot co-exist
8787
And request 'Hello'
8888
When method POST
8989
# this should either succeed (without using the slug) or fail as a conflict
90-
Then assert (responseStatus >= 200 && responseStatus < 300 && responseHeaders['Location'][0] != resourceUrl) || [409, 415].includes(responseStatus)
90+
Then assert (responseStatus >= 200 && responseStatus < 300 && karate.response.headerValues('location')[0] != resourceUrl) || [409, 415].includes(responseStatus)
9191
9292
Scenario: POST resource, then try container with same name
9393
Given url testContainer.url
@@ -96,7 +96,7 @@ Feature: With and without trailing slash cannot co-exist
9696
And request 'Hello'
9797
When method POST
9898
Then assert responseStatus >= 200 && responseStatus < 300
99-
And def resourceUrl = responseHeaders['Location'][0]
99+
And def resourceUrl = karate.response.headerValues('location')[0]
100100
And assert !resourceUrl.endsWith('/')
101101
102102
# confirm there is no container with the same URI
@@ -122,6 +122,6 @@ Feature: With and without trailing slash cannot co-exist
122122
And header Link = '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"'
123123
When method POST
124124
# this should either succeed (without using the slug) or fail as a conflict
125-
Then assert (responseStatus >= 200 && responseStatus < 300 && responseHeaders['Location'][0] != resourceUrl + '/') || [409, 415].includes(responseStatus)
125+
Then assert (responseStatus >= 200 && responseStatus < 300 && karate.response.headerValues('location')[0] != resourceUrl + '/') || [409, 415].includes(responseStatus)
126126
127127
# TODO: Evil test to check various suffices.

0 commit comments

Comments
 (0)