Skip to content

Commit

Permalink
feat(cookies): init 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 38615d4 commit f7f8caf
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
10 changes: 10 additions & 0 deletions examples/features/http_api/cookies.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@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
38 changes: 37 additions & 1 deletion src/extensions/http_api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ let bodyType = null
let headers = null
let query = null

let cookieJar = null

// RESPONSE INFORMATION
let response = null
let responseCookies = null

/**
* Resets the client.
Expand All @@ -28,7 +31,11 @@ exports.reset = () => {
bodyType = null
headers = null
query = null

cookieJar = null

response = null
responseCookies = null
}

/**
Expand Down Expand Up @@ -80,6 +87,26 @@ exports.setHeader = (key, value) => {
headers[key] = value
}

exports.enableCookieJar = () => {
cookieJar = request.jar()
}

exports.disableCookieJar = () => {
cookieJar = null
}

exports.setCookie = (key, value) => {
headers = headers || {}
headers[key] = value
}

exports.getCookie = key => {
if (responseCookies === null) return null
if (responseCookies[key] === undefined) return null

return responseCookies[key]
}

/**
* Returns the latest collected response.
*/
Expand All @@ -102,7 +129,8 @@ exports.makeRequest = (method, path, baseUrl) => {
uri: path,
method,
qs: query || {},
headers
headers,
jar: cookieJar
}

if (body !== null) {
Expand All @@ -125,6 +153,14 @@ exports.makeRequest = (method, path, baseUrl) => {
}

response = _response

if (cookieJar !== null) {
responseCookies = {}
cookieJar.getCookies(`${baseUrl}${path}`).forEach(cookie => {
responseCookies[cookie.key] = cookie
})
}

resolve()
})
})
Expand Down
50 changes: 48 additions & 2 deletions src/extensions/http_api/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ module.exports = ({ baseUrl = '' } = {}) => ({ Given, When, Then }) => {
this.state.set(key, _.get(body, path))
})

Given(/^(?:I )?enable cookies$/, function() {
this.httpApiClient.enableCookieJar()
})

Given(/^(?:I )?disable cookies$/, function() {
this.httpApiClient.disableCookieJar()
})

/**
* Resetting the client's state
*/
Expand Down Expand Up @@ -96,8 +104,46 @@ module.exports = ({ baseUrl = '' } = {}) => ({ Given, When, Then }) => {
*/
Then(/^(?:I )?should receive a ([1-5][0-9][0-9]) HTTP status code$/, function(statusCode) {
const httpResponse = this.httpApiClient.getResponse()
expect(httpResponse).to.not.be.empty
expect(httpResponse.statusCode).to.equal(Number(statusCode))
expect(httpResponse, 'Response is empty').to.not.be.empty
expect(httpResponse.statusCode, `Expected status code to be: ${statusCode}, but found: ${httpResponse.statusCode}`).to.equal(
Number(statusCode)
)
})

/**
* Checking response cookie exists
*/
Then(/^response should have a (.*) cookie$/, function(key) {
const cookie = this.httpApiClient.getCookie(key)
expect(cookie, `No cookie found for key ${key}`).to.not.be.null
})

/**
* Checking response cookie secure
*/
Then(/^response (.*) cookie should (not )?be secure$/, function(key, flag) {
const cookie = this.httpApiClient.getCookie(key)
expect(cookie, `No cookie found for key ${key}`).to.not.be.null

if (flag === undefined) {
expect(cookie.secure, `Cookie ${key} is not secure`).to.be.true
} else {
expect(cookie.secure, `Cookie ${key} is secure`).to.be.false
}
})

/**
* Checking response cookie httpOnly
*/
Then(/^response (.*) cookie should (not )?be http only/, function(key, flag) {
const cookie = this.httpApiClient.getCookie(key)
expect(cookie, `No cookie found for key ${key}`).to.not.be.null

if (flag === undefined) {
expect(cookie.httpOnly, `Cookie ${key} is not http only`).to.be.true
} else {
expect(cookie.httpOnly, `Cookie ${key} is http only`).to.be.false
}
})

/**
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,10 @@ qs@~6.3.0:
version "6.3.2"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.3.2.tgz#e75bd5f6e268122a2a0e0bda630b2550c166502c"

qs@~6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233"

randomatic@^1.1.3:
version "1.1.7"
resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-1.1.7.tgz#c7abe9cc8b87c0baa876b19fde83fd464797e38c"
Expand Down

0 comments on commit f7f8caf

Please sign in to comment.