Skip to content

Commit 27fabae

Browse files
committed
feat(httpApi): minor improve for httpDefinitions and fix uri for makeRequest
1 parent 4f31a2e commit 27fabae

File tree

7 files changed

+69
-10
lines changed

7 files changed

+69
-10
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,19 @@ Scenario Outline: Fetching <key> API endpoint from root endpoint
227227
| public_gists_url |
228228
```
229229

230+
You can also pick a field from response headers.
231+
232+
```gherkin
233+
Scenario: Setting json body from .json fixture file
234+
And set request json body from json_file
235+
When I POST https://examples.com/users
236+
Then response status code should be 201
237+
And I pick response header location as location
238+
And I clear request body
239+
And I GET {{location}}
240+
And response status code should be 200
241+
```
242+
230243
#### Using cookies
231244

232245
Cookies are disabled by default, but you've got the ability to enable/disable the feature using a gherkin `Given` expression.
@@ -813,7 +826,7 @@ Given:
813826
- /^(?:I )?set request multipart body from (.+)$/
814827
- /^(?:I )?clear request body$/
815828
- /^(?:I )?set request query$/
816-
- /^(?:I )?pick response json (.+) as (.+)$/
829+
- /^(?:I )?pick response (json|header) (.+) as (.+)$/
817830
- /^(?:I )?enable cookies$/
818831
- /^(?:I )?disable cookies$/
819832
- /^(?:I )?set cookie from (.+)$/

doc/README.tpl.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,21 @@ Scenario Outline: Fetching <key> API endpoint from root endpoint
231231
```
232232
<%={{ }}=%>
233233

234+
You can also pick a field from response headers.
235+
236+
{{=<% %>=}}
237+
```gherkin
238+
Scenario: Setting json body from .json fixture file
239+
And set request json body from json_file
240+
When I POST https://examples.com/users
241+
Then response status code should be 201
242+
And I pick response header location as location
243+
And I clear request body
244+
And I GET {{location}}
245+
And response status code should be 200
246+
```
247+
<%={{ }}=%>
248+
234249
#### Using cookies
235250

236251
Cookies are disabled by default, but you've got the ability to enable/disable the feature using a gherkin `Given` expression.

examples/features/http_api/fixtures/fixtures.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,15 @@ Feature: Using fixtures with http API extension
7070
And set request form body from json_00
7171
When I POST http://fake.io/users/json
7272
Then response status code should be 200
73+
74+
@json @header
75+
Scenario: Setting json body from .json fixture file
76+
Given I mock http call to forward request body for path /users
77+
And set request json body from json_00
78+
When I POST http://fake.io/users
79+
Then response status code should be 200
80+
And I pick response header location as location
81+
And I clear request body
82+
Given I mock GET http call to forward request body for path /users/1
83+
And I GET {{location}}
84+
And response status code should be 200

examples/support/http_api/fixtures.steps.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ const { Given, Then } = require('@cucumber/cucumber')
55
const nock = require('nock')
66
const { expect } = require('chai')
77

8-
Given(/^I mock http call to forward request body for path (.+)$/, function (path) {
9-
nock('http://fake.io')
8+
Given(/^I mock (?:(POST|GET) )?http call to forward request body for path (.+)$/, function (method,path) {
9+
if(method !== 'GET') {
10+
nock('http://fake.io')
1011
.post(path)
1112
.reply(200, (uri, requestBody) => requestBody)
13+
.defaultReplyHeaders({location: 'http://fake.io/users/1'})
14+
return
15+
}
16+
17+
nock('http://fake.io')
18+
.get(path)
19+
.reply(200 )
1220
})
1321

1422
Then(/^response should match url encoded snapshot (.+)$/, function (snapshotId) {

src/extensions/http_api/client.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ class HttpApiClient {
219219
*/
220220
makeRequest(method, path, baseUrl) {
221221
return new Promise((resolve, reject) => {
222+
if (/https?:\/\//.test(path)) {
223+
const url = new URL(path)
224+
path = path.replace(url.origin, '')
225+
baseUrl = url.origin
226+
}
227+
222228
const options = {
223229
baseUrl: baseUrl,
224230
uri: path,

src/extensions/http_api/definitions.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,14 @@ exports.install = ({ baseUrl = '' } = {}) => {
125125
this.httpApiClient.setQuery(Cast.object(this.state.populateObject(step.rowsHash())))
126126
})
127127

128-
Given(/^(?:I )?pick response json (.+) as (.+)$/, function (path, key) {
128+
/**
129+
* Pick a value from previous json response or header and set it to state
130+
*/
131+
Given(/^(?:I )?pick response (json|header) (.+) as (.+)$/, function (dataSource, path, key) {
129132
const response = this.httpApiClient.getResponse()
130-
const body = response.body
133+
let data = dataSource !== 'header' ? response.body : response.headers
131134

132-
this.state.set(key, _.get(body, path))
135+
this.state.set(key, _.get(data, path))
133136
})
134137

135138
/**

tests/extensions/http_api/definitions.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,15 @@ test('do not follow redirect', () => {
264264
expect(clientMock.httpApiClient.setFollowRedirect).toHaveBeenCalledWith(false)
265265
})
266266

267-
test('pick response json property', () => {
267+
test('pick response json|header property', () => {
268268
const context = helper.getContext() // Extension context
269269

270-
const def = context.getDefinitionByMatcher('pick response json')
270+
const def = context.getDefinitionByMatcher('pick response (json|header) (.+) as (.+)')
271271
def.shouldNotMatch('I pick response json as ')
272-
def.shouldMatch('I pick response json key as value', ['key', 'value'])
273-
def.shouldMatch('pick response json key as value', ['key', 'value'])
272+
def.shouldMatch('I pick response json key as value', ['json', 'key', 'value'])
273+
def.shouldMatch('pick response json key as value', ['json', 'key', 'value'])
274+
def.shouldMatch('I pick response header key as value', ['header', 'key', 'value'])
275+
def.shouldMatch('pick response header key as value', ['header', 'key', 'value'])
274276
})
275277

276278
test('enable cookies', () => {

0 commit comments

Comments
 (0)