Skip to content

Commit

Permalink
feat(httpApi): minor improve for httpDefinitions and fix uri for make…
Browse files Browse the repository at this point in the history
…Request
  • Loading branch information
tduyng committed Jan 14, 2022
1 parent 4f31a2e commit 27fabae
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 10 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ Scenario Outline: Fetching <key> API endpoint from root endpoint
| public_gists_url |
```

You can also pick a field from response headers.

```gherkin
Scenario: Setting json body from .json fixture file
And set request json body from json_file
When I POST https://examples.com/users
Then response status code should be 201
And I pick response header location as location
And I clear request body
And I GET {{location}}
And response status code should be 200
```

#### Using cookies

Cookies are disabled by default, but you've got the ability to enable/disable the feature using a gherkin `Given` expression.
Expand Down Expand Up @@ -813,7 +826,7 @@ Given:
- /^(?:I )?set request multipart body from (.+)$/
- /^(?:I )?clear request body$/
- /^(?:I )?set request query$/
- /^(?:I )?pick response json (.+) as (.+)$/
- /^(?:I )?pick response (json|header) (.+) as (.+)$/
- /^(?:I )?enable cookies$/
- /^(?:I )?disable cookies$/
- /^(?:I )?set cookie from (.+)$/
Expand Down
15 changes: 15 additions & 0 deletions doc/README.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,21 @@ Scenario Outline: Fetching <key> API endpoint from root endpoint
```
<%={{ }}=%>

You can also pick a field from response headers.

{{=<% %>=}}
```gherkin
Scenario: Setting json body from .json fixture file
And set request json body from json_file
When I POST https://examples.com/users
Then response status code should be 201
And I pick response header location as location
And I clear request body
And I GET {{location}}
And response status code should be 200
```
<%={{ }}=%>

#### Using cookies

Cookies are disabled by default, but you've got the ability to enable/disable the feature using a gherkin `Given` expression.
Expand Down
12 changes: 12 additions & 0 deletions examples/features/http_api/fixtures/fixtures.feature
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ Feature: Using fixtures with http API extension
And set request form body from json_00
When I POST http://fake.io/users/json
Then response status code should be 200

@json @header
Scenario: Setting json body from .json fixture file
Given I mock http call to forward request body for path /users
And set request json body from json_00
When I POST http://fake.io/users
Then response status code should be 200
And I pick response header location as location
And I clear request body
Given I mock GET http call to forward request body for path /users/1
And I GET {{location}}
And response status code should be 200
12 changes: 10 additions & 2 deletions examples/support/http_api/fixtures.steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ const { Given, Then } = require('@cucumber/cucumber')
const nock = require('nock')
const { expect } = require('chai')

Given(/^I mock http call to forward request body for path (.+)$/, function (path) {
nock('http://fake.io')
Given(/^I mock (?:(POST|GET) )?http call to forward request body for path (.+)$/, function (method,path) {
if(method !== 'GET') {
nock('http://fake.io')
.post(path)
.reply(200, (uri, requestBody) => requestBody)
.defaultReplyHeaders({location: 'http://fake.io/users/1'})
return
}

nock('http://fake.io')
.get(path)
.reply(200 )
})

Then(/^response should match url encoded snapshot (.+)$/, function (snapshotId) {
Expand Down
6 changes: 6 additions & 0 deletions src/extensions/http_api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ class HttpApiClient {
*/
makeRequest(method, path, baseUrl) {
return new Promise((resolve, reject) => {
if (/https?:\/\//.test(path)) {
const url = new URL(path)
path = path.replace(url.origin, '')
baseUrl = url.origin
}

const options = {
baseUrl: baseUrl,
uri: path,
Expand Down
9 changes: 6 additions & 3 deletions src/extensions/http_api/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,14 @@ exports.install = ({ baseUrl = '' } = {}) => {
this.httpApiClient.setQuery(Cast.object(this.state.populateObject(step.rowsHash())))
})

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

this.state.set(key, _.get(body, path))
this.state.set(key, _.get(data, path))
})

/**
Expand Down
10 changes: 6 additions & 4 deletions tests/extensions/http_api/definitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,15 @@ test('do not follow redirect', () => {
expect(clientMock.httpApiClient.setFollowRedirect).toHaveBeenCalledWith(false)
})

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

const def = context.getDefinitionByMatcher('pick response json')
const def = context.getDefinitionByMatcher('pick response (json|header) (.+) as (.+)')
def.shouldNotMatch('I pick response json as ')
def.shouldMatch('I pick response json key as value', ['key', 'value'])
def.shouldMatch('pick response json key as value', ['key', 'value'])
def.shouldMatch('I pick response json key as value', ['json', 'key', 'value'])
def.shouldMatch('pick response json key as value', ['json', 'key', 'value'])
def.shouldMatch('I pick response header key as value', ['header', 'key', 'value'])
def.shouldMatch('pick response header key as value', ['header', 'key', 'value'])
})

test('enable cookies', () => {
Expand Down

0 comments on commit 27fabae

Please sign in to comment.