Skip to content

Commit

Permalink
Merge 76ef4f5 into 11d270b
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Jun 29, 2017
2 parents 11d270b + 76ef4f5 commit c61aa8e
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 7 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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
],
"license": "MIT",
"engines": {
"node" : ">=6.0.0"
"node": ">=6.0.0"
},
"engineStrict": true,
"dependencies": {
"chai": "^4.0.2",
"lodash": "^4.17.4"
"lodash": "^4.17.4",
"request": "^2.81.0"
},
"devDependencies": {
"coveralls": "^2.13.1",
Expand Down
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 @@ -49,6 +49,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 @@ -76,8 +84,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
60 changes: 58 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ajv-keywords@^1.0.0:
version "1.5.1"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-1.5.1.tgz#314dd0a4b3368fad3dfcdc54ede6171b886daf3c"

ajv@^4.7.0:
ajv@^4.7.0, ajv@^4.9.1:
version "4.11.8"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-4.11.8.tgz#82ffb02b29e662ae53bdc20af15947706739c536"
dependencies:
Expand Down Expand Up @@ -411,6 +411,10 @@ caseless@~0.11.0:
version "0.11.0"
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.11.0.tgz#715b96ea9841593cc33067923f5ec60ebda4f7d7"

caseless@~0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"

catharsis@~0.8.8:
version "0.8.8"
resolved "https://registry.yarnpkg.com/catharsis/-/catharsis-0.8.8.tgz#693479f43aac549d806bd73e924cd0d944951a06"
Expand Down Expand Up @@ -1148,6 +1152,10 @@ handlebars@^4.0.3:
optionalDependencies:
uglify-js "^2.6"

har-schema@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/har-schema/-/har-schema-1.0.5.tgz#d263135f43307c02c602afc8fe95970c0151369e"

har-validator@~2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/har-validator/-/har-validator-2.0.6.tgz#cdcbc08188265ad119b6a5a7c8ab70eecfb5d27d"
Expand All @@ -1157,6 +1165,13 @@ har-validator@~2.0.6:
is-my-json-valid "^2.12.4"
pinkie-promise "^2.0.0"

har-validator@~4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/har-validator/-/har-validator-4.2.1.tgz#33481d0f1bbff600dd203d75812a6a5fba002e2a"
dependencies:
ajv "^4.9.1"
har-schema "^1.0.5"

has-ansi@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91"
Expand Down Expand Up @@ -2158,6 +2173,10 @@ pathval@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0"

performance-now@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-0.2.0.tgz#33ef30c5c77d4ea21c5a53869d91b56d8f2555e5"

pify@^2.0.0, pify@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
Expand Down Expand Up @@ -2223,6 +2242,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 Expand Up @@ -2311,6 +2334,33 @@ request@2.79.0, request@^2.79.0:
tunnel-agent "~0.4.1"
uuid "^3.0.0"

request@^2.81.0:
version "2.81.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.81.0.tgz#c6928946a0e06c5f8d6f8a9333469ffda46298a0"
dependencies:
aws-sign2 "~0.6.0"
aws4 "^1.2.1"
caseless "~0.12.0"
combined-stream "~1.0.5"
extend "~3.0.0"
forever-agent "~0.6.1"
form-data "~2.1.1"
har-validator "~4.2.1"
hawk "~3.1.3"
http-signature "~1.1.0"
is-typedarray "~1.0.0"
isstream "~0.1.2"
json-stringify-safe "~5.0.1"
mime-types "~2.1.7"
oauth-sign "~0.8.1"
performance-now "^0.2.0"
qs "~6.4.0"
safe-buffer "^5.0.1"
stringstream "~0.0.4"
tough-cookie "~2.3.0"
tunnel-agent "^0.6.0"
uuid "^3.0.0"

require-directory@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"
Expand Down Expand Up @@ -2381,7 +2431,7 @@ rx-lite@*, rx-lite@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/rx-lite/-/rx-lite-4.0.8.tgz#0b1e11af8bc44836f04a6407e92da42467b79444"

safe-buffer@~5.1.0:
safe-buffer@^5.0.1, safe-buffer@~5.1.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"

Expand Down Expand Up @@ -2670,6 +2720,12 @@ tryit@^1.0.1:
version "1.0.3"
resolved "https://registry.yarnpkg.com/tryit/-/tryit-1.0.3.tgz#393be730a9446fd1ead6da59a014308f36c289cb"

tunnel-agent@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd"
dependencies:
safe-buffer "^5.0.1"

tunnel-agent@~0.4.1:
version "0.4.3"
resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.4.3.tgz#6373db76909fe570e08d73583365ed828a74eeeb"
Expand Down

0 comments on commit c61aa8e

Please sign in to comment.