Skip to content

Commit

Permalink
feat(cookies): improve cookies support for http API extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte authored and Raphaël Benitte committed Jul 5, 2017
1 parent 8956e0d commit 2bd5bca
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ yarn run examples -- --tags @cli
[npm-url]: https://www.npmjs.com/package/@ekino/veggies
[travis-image]: https://img.shields.io/travis/ekino/veggies.svg?style=flat-square
[travis-url]: https://travis-ci.org/ekino/veggies
[prettier-image]: https://img.shields.io/badge/styled_with-prettier-ff69b4.svg?style=flat-square
[prettier-image]: https://img.shields.io/badge/styled_with-prettier-44cc11.svg?style=flat-square
[prettier-url]: https://github.com/prettier/prettier
[coverage-image]: https://img.shields.io/coveralls/ekino/veggies/master.svg?style=flat-square
[coverage-url]: https://coveralls.io/github/ekino/veggies?branch=master
31 changes: 24 additions & 7 deletions examples/features/http_api/cookies.feature
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
@http_api @cookies
Feature: GitHub API

Scenario: Using GitHub API
Given enable cookies
When I GET https://twitter.com/
Then I should receive a 200 HTTP status code
And response should have a guest_id cookie
And response guest_id cookie should not be secure
And response guest_id cookie should not be http only
# Scenario: Using GitHub API
# Given enable cookies
# When I GET https://twitter.com/
# Then I should receive a 200 HTTP status code
# And response should have a guest_id cookie
# And response guest_id cookie should not be secure
# And response guest_id cookie should not be http only

Scenario: test
Given I enable cookies
And set request headers
| Content-Type | application/x-www-form-urlencoded; charset=utf-8 |
| Accept | application/json |
And I set request form body
| grant_type | ekino |
| google_authorization_code | 4/nXXR1hDzzqi7qjOE5qF5YFmibifiQ_IzULvUovajtrY |
| client_id | 4be37f59-0143-470d-a0fb-514f63da5d70 |
When I POST http://localhost:8070/api/v1/proxy/auth
And I dump response body
Given I clear request body
#Then I should receive a 200 HTTP status code
When I GET http://localhost:8070/api/v1/proxy/verify
And I dump response body
Then I should receive a 200 HTTP status code
27 changes: 27 additions & 0 deletions src/extensions/http_api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ exports.setFormBody = payload => {
body = payload
}

/**
* Clears current request body
*/
exports.clearBody = () => {
body = null
bodyType = null
}

/**
* Sets request query parameters.
*
Expand Down Expand Up @@ -87,14 +95,33 @@ exports.setHeader = (key, value) => {
headers[key] = value
}

/**
* Clears current request headers.
*/
exports.clearHeaders = () => {
headers = null
}

/**
* Enables cookie jar.
*/
exports.enableCookies = () => {
cookieJar = request.jar()
}

/**
* Disables cookie jar.
*/
exports.disableCookies = () => {
cookieJar = null
}

/**
* Sets a cookie.
*
* @param {string} key - Cookie key
* @param {string} value - Cookie value
*/
exports.setCookie = (key, value) => {
headers = headers || {}
headers[key] = value
Expand Down
14 changes: 14 additions & 0 deletions src/extensions/http_api/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ module.exports = ({ baseUrl = '' } = {}) => ({ Given, When, Then }) => {
this.httpApiClient.setHeader(key, Cast.value(this.state.populate(value)))
})

/**
* Clearing headers
*/
Given(/^(?:I )?clear request headers/, function() {
this.httpApiClient.clearHeaders()
})

/**
* Setting json payload
*/
Expand Down Expand Up @@ -55,6 +62,13 @@ module.exports = ({ baseUrl = '' } = {}) => ({ Given, When, Then }) => {
})
})

/**
* Clearing body
*/
Given(/^(?:I )?clear request body$/, function() {
this.httpApiClient.clearBody()
})

/**
* Setting query parameters
*/
Expand Down
34 changes: 30 additions & 4 deletions tests/extensions/http_api/definitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ test('set a single request header', () => {
expect(clientMock.httpApiClient.setHeader).toHaveBeenCalledWith('Accept', 'test')
})

test('clear request headers', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('clear request headers')
def.shouldHaveType('Given')
def.shouldMatch('I clear request headers')
def.shouldMatch('clear request headers')

const clientMock = { httpApiClient: { clearHeaders: jest.fn() } }
def.exec(clientMock)
expect(clientMock.httpApiClient.clearHeaders).toHaveBeenCalled()
})

test('set request json body', () => {
const context = helper.define(definitions)

Expand Down Expand Up @@ -99,13 +112,13 @@ test('set request form body from fixture file', () => {
def.shouldMatch('I set request form body from fixture')
def.shouldMatch('set request form body from fixture')

const fixture = {
const fixture = {
is_active: 'true',
id: '2'
id: '2'
}
const worldMock = {
httpApiClient: { setFormBody: jest.fn() },
fixtures: { load: jest.fn(() => Promise.resolve(fixture)) }
httpApiClient: {setFormBody: jest.fn()},
fixtures: {load: jest.fn(() => Promise.resolve(fixture))}
}

return def.exec(worldMock, 'fixture').then(() => {
Expand All @@ -114,6 +127,19 @@ test('set request form body from fixture file', () => {
})
})

test('clear request body', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('clear request body')
def.shouldHaveType('Given')
def.shouldMatch('I clear request body')
def.shouldMatch('clear request body')

const clientMock = { httpApiClient: { clearBody: jest.fn() } }
def.exec(clientMock)
expect(clientMock.httpApiClient.clearBody).toHaveBeenCalled()
})

test('set request query', () => {
const context = helper.define(definitions)

Expand Down

0 comments on commit 2bd5bca

Please sign in to comment.