Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent REST helper from mutating default headers #789

Merged
merged 1 commit into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/helper/REST.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class REST extends Helper {
endpoint: ''
};
this.options = Object.assign(this.options, config);
headers = this.options.defaultHeaders;
headers = Object.assign({}, this.options.defaultHeaders);
}

static _checkRequirements() {
Expand Down Expand Up @@ -68,7 +68,7 @@ class REST extends Helper {

_cleanRequestHeaders() {
if (this.options.resetHeaders) {
headers = this.options.defaultHeaders;
headers = Object.assign({}, this.options.defaultHeaders);
}
}

Expand Down
45 changes: 31 additions & 14 deletions test/rest/REST_test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
'use strict';
const TestHelper = require('../support/TestHelper');
let REST = require('../../lib/helper/REST');
let should = require('chai').should();
let api_url = TestHelper.jsonServerUrl();
let assert = require('assert');
let path = require('path');
let fs = require('fs');
let fileExists = require('../../lib/utils').fileExists;
let AssertionFailedError = require('../../lib/assert/error');
let expectError = require('../../lib/utils').test.expectError;
const REST = require('../../lib/helper/REST');
const api_url = TestHelper.jsonServerUrl();
const path = require('path');
const fs = require('fs');
require('chai').should();
let I;
let dbFile = path.join(__dirname, '/../data/rest/db.json');
const dbFile = path.join(__dirname, '/../data/rest/db.json');
require('co-mocha')(require('mocha'));

const data = {
Expand All @@ -24,7 +20,7 @@ const data = {
"user": {
"name": "davert"
}
}
};

describe('REST', function () {

Expand All @@ -42,7 +38,7 @@ describe('REST', function () {
fs.writeFileSync(dbFile, JSON.stringify(data));
} catch (err) {}
setTimeout(done, 700);
})
});

describe('basic requests', function() {
it('should send GET requests', () => {
Expand Down Expand Up @@ -77,7 +73,7 @@ describe('REST', function () {
});

describe('headers', function() {
it ('should send request headers', () => {
it('should send request headers', () => {
return I.sendGetRequest('/headers', { 'Content-Type': 'application/json'}).then((resp) => {
resp.body.should.have.property('content-type');
resp.body['content-type'].should.eql('application/json');
Expand All @@ -87,7 +83,7 @@ describe('REST', function () {
});
});

it ('should set request headers', function*() {
it('should set request headers', function*() {
I.haveRequestHeaders({HTTP_X_REQUESTED_WITH: 'xmlhttprequest'});
yield I.sendGetRequest('/headers', { 'Content-Type': 'application/json'}).then((resp) => {
resp.body.should.have.property('content-type');
Expand All @@ -99,8 +95,29 @@ describe('REST', function () {
resp.body.should.have.property('http_x_requested_with');
resp.body['http_x_requested_with'].should.eql('xmlhttprequest');
});
});

it("should reset headers when flag is set", function*() {
const defaultHeaders = {
'X-Test': 'test'
};
const iResetHeaders = new REST({
endpoint: api_url,
defaultHeaders: Object.assign({}, defaultHeaders),
resetHeaders: true
});
const requestHeaders = {
'X-Request-Header': 'header'
};

iResetHeaders.haveRequestHeaders(requestHeaders);
yield iResetHeaders.sendGetRequest('/headers');

const expectedHeaders = Object.assign({}, defaultHeaders, {'content-length': 0});

const response = yield iResetHeaders.sendGetRequest('/headers');

response.request.headers.should.eql(expectedHeaders);
});
});

Expand Down