fix tests that require session authentication

This commit is contained in:
Matteo Pagliazzi
2016-02-20 21:02:46 +01:00
parent 2ed2fb5d06
commit 62ebde3186
6 changed files with 27 additions and 5 deletions

View File

@@ -22,6 +22,10 @@ requester.setApiVersion = (version) => {
apiVersion = version;
};
// save the last cookie so that it's resent with every request
// should be safe since every time a user is generated this will be overwritten
let cookie;
function _requestMaker (user, method, additionalSets) {
if (!apiVersion) throw new Error('apiVersion not set');
@@ -36,6 +40,11 @@ function _requestMaker (user, method, additionalSets) {
.set('x-api-key', user.apiToken);
}
// if we previously saved a cookie, send it along the request
if (cookie) {
request.set('Cookie', cookie);
}
if (additionalSets) {
request.set(additionalSets);
}
@@ -52,6 +61,13 @@ function _requestMaker (user, method, additionalSets) {
reject(parsedError);
}
// if any cookies was sent, save it for the next request
if (response.headers['set-cookie']) {
cookie = response.headers['set-cookie'].map(cookieString => {
return cookieString.split(';')[0];
}).join('; ');
}
let contentType = response.headers['content-type'] || '';
resolve(contentType.indexOf('json') !== -1 ? response.body : response.text);
});