bitter-apple
Advanced tools
Comparing version 1.8.3 to 1.9.0
/* jslint node: true */ | ||
'use strict'; | ||
module.exports = function() { | ||
this.Given(/^I send the cookie (\S+)$/, function(cookieName, callback) { | ||
if(this.bitterapple.sendCookie(cookieName)) { | ||
callback(); | ||
} | ||
else { | ||
callback(new Error('Cannot send unknown cookie ' + cookieName)); | ||
} | ||
}); | ||
this.Given(/^I clear the cookie (\S+)$/, function(cookieName, callback) { | ||
this.bitterapple.clearCookie(cookieName); | ||
callback(); | ||
}); | ||
this.Given(/^I set (.*) header to (\S+)$/, function(headerName, headerValue, callback) { | ||
@@ -6,0 +19,0 @@ this.bitterapple.addRequestHeader(headerName, headerValue); |
@@ -13,2 +13,3 @@ /* jslint node: true */ | ||
var globalVariables = {}; | ||
var cookies = []; | ||
@@ -87,3 +88,3 @@ var ATTRIBUTE = 2; | ||
self.httpResponse = response; | ||
processResponse(self, response); | ||
callback(null, response); | ||
@@ -94,2 +95,50 @@ }); | ||
/** | ||
* Memorizes the server response | ||
*/ | ||
function processResponse(self, response) { | ||
self.httpResponse = response; | ||
if(response.headers.hasOwnProperty("set-cookie")) { | ||
for (var i = 0; i < response.headers["set-cookie"].length; i++) { | ||
parseCookies(response.headers["set-cookie"][i]); | ||
} | ||
} | ||
} | ||
/** | ||
* Decodes a raw cookie and store its value in the cookies global var | ||
*/ | ||
function parseCookies(rawCookie) { | ||
rawCookie && rawCookie.split(';').forEach(function( cookie ) { | ||
var parts = cookie.split('='); | ||
cookies[parts.shift().trim()] = decodeURI(parts.join('=')); | ||
}); | ||
} | ||
/** | ||
* Adds a received cookie to headers | ||
*/ | ||
BitterApple.prototype.sendCookie = function(cookieName) { | ||
if(!cookies.hasOwnProperty(cookieName)) { | ||
return false; | ||
} | ||
if(!this.headers.hasOwnProperty('Cookie')) { | ||
this.headers.Cookie = []; | ||
} | ||
this.headers.Cookie.push(cookieName + '=' + cookies[cookieName] + ';'); | ||
return true; | ||
}; | ||
BitterApple.prototype.clearCookie = function(cookieName) { | ||
var suffix = cookieName + '='; | ||
for (var i = 0; i < this.headers.Cookie.length; i++) { | ||
if (this.headers.Cookie[i].startsWith(suffix)) { | ||
this.headers.Cookie.splice(i, 1); | ||
break; | ||
} | ||
} | ||
return true; | ||
}; | ||
BitterApple.prototype.post = function(resource, callback) { | ||
@@ -110,3 +159,3 @@ resource = replaceVariables(resource, this.scenarioVariables); | ||
self.httpResponse = response; | ||
processResponse(self, response); | ||
callback(null, response); | ||
@@ -131,3 +180,3 @@ }); | ||
self.httpResponse = response; | ||
processResponse(self, response); | ||
callback(null, response); | ||
@@ -152,3 +201,3 @@ }); | ||
self.httpResponse = response; | ||
processResponse(self, response); | ||
callback(null, response); | ||
@@ -173,3 +222,3 @@ }); | ||
self.httpResponse = response; | ||
processResponse(self, response); | ||
callback(null, response); | ||
@@ -289,3 +338,3 @@ }); | ||
BitterApple.prototype.storeValueOfResponseBodyPathInGlobalScope = function(path, variableName) { | ||
var value = evaluatePath(path, this.getResponseObject().body); | ||
var value = evaluatePath(path, this.getResponseObject().body)[0]; | ||
this.setGlobalVariable(variableName, value); | ||
@@ -302,2 +351,8 @@ }; | ||
BitterApple.prototype.evaluatePath = function(path, content){ | ||
return evaluatePath(path, content); | ||
}; | ||
exports.BitterApple = BitterApple; | ||
@@ -340,3 +395,3 @@ | ||
endIndex = startIndex + variableValue.length; | ||
} | ||
} | ||
resource = replaceScopeVariables(resource, scope, endIndex + 1); | ||
@@ -343,0 +398,0 @@ } |
{ | ||
"name": "bitter-apple", | ||
"version": "1.8.3", | ||
"version": "1.9.0", | ||
"description": "Collection of utility functions and a gherkin framework for WEB API integration testing based on cucumber.js", | ||
@@ -5,0 +5,0 @@ "main": "bitter-apple.js", |
@@ -250,2 +250,4 @@ # bitterapple - REST API integration testing framework with cucumber.js | ||
I set (.*) header to global variable (.*) | ||
I send the cookie (.*) | ||
I clear the cookie (.*) | ||
@@ -273,3 +275,3 @@ WHEN: | ||
(.*) | ||
""" | ||
""" | ||
@@ -276,0 +278,0 @@ THEN: |
35241
655
315