mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
make sure cookie is never shared among multiple test users
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import superagent from 'superagent';
|
||||
import nconf from 'nconf';
|
||||
import { isEmpty, cloneDeep } from 'lodash';
|
||||
|
||||
const API_TEST_SERVER_PORT = nconf.get('PORT');
|
||||
let apiVersion;
|
||||
@@ -9,7 +10,9 @@ let apiVersion;
|
||||
// Sets up an abject that can make all REST requests
|
||||
// If a user is passed in, the uuid and api token of
|
||||
// the user are used to make the requests
|
||||
export function requester (user = {}, additionalSets) {
|
||||
export function requester (user = {}, additionalSets = {}) {
|
||||
additionalSets = cloneDeep(additionalSets); // cloning because it could be modified later to set cookie
|
||||
|
||||
return {
|
||||
get: _requestMaker(user, 'get', additionalSets),
|
||||
post: _requestMaker(user, 'post', additionalSets),
|
||||
@@ -22,11 +25,7 @@ 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) {
|
||||
function _requestMaker (user, method, additionalSets = {}) {
|
||||
if (!apiVersion) throw new Error('apiVersion not set');
|
||||
|
||||
return (route, send, query) => {
|
||||
@@ -40,12 +39,7 @@ 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) {
|
||||
if (!isEmpty(additionalSets)) {
|
||||
request.set(additionalSets);
|
||||
}
|
||||
|
||||
@@ -63,7 +57,7 @@ function _requestMaker (user, method, additionalSets) {
|
||||
|
||||
// if any cookies was sent, save it for the next request
|
||||
if (response.headers['set-cookie']) {
|
||||
cookie = response.headers['set-cookie'].map(cookieString => {
|
||||
additionalSets.cookie = response.headers['set-cookie'].map(cookieString => {
|
||||
return cookieString.split(';')[0];
|
||||
}).join('; ');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user