From 79375616d54aca4aa40482571d0020517cc7ecb5 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 30 Sep 2015 16:31:56 +0200 Subject: [PATCH] Default host to empty string when header is not found A String object is required to not fail on `.match` interface. --- lib/cartodb/models/cdb_request.js | 2 +- test/unit/cartodb/cdb_request.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/cartodb/models/cdb_request.js b/lib/cartodb/models/cdb_request.js index 27c101f0..6650dd08 100644 --- a/lib/cartodb/models/cdb_request.js +++ b/lib/cartodb/models/cdb_request.js @@ -8,7 +8,7 @@ module.exports = CdbRequest; CdbRequest.prototype.userByReq = function(req) { - var host = req.headers.host; + var host = req.headers.host || ''; if (req.params.user) { return req.params.user; } diff --git a/test/unit/cartodb/cdb_request.test.js b/test/unit/cartodb/cdb_request.test.js index 04b2c1b9..2d89d25c 100644 --- a/test/unit/cartodb/cdb_request.test.js +++ b/test/unit/cartodb/cdb_request.test.js @@ -57,4 +57,28 @@ describe('req2params', function() { assert.equal(user, undefined); }); + + it('should not fail for undefined host header', function() { + var userFromHostConfig = global.environment.user_from_host; + global.environment.user_from_host = null; + + var cdbRequest = new CdbRequest(); + var user = cdbRequest.userByReq(createRequest(undefined)); + + global.environment.user_from_host = userFromHostConfig; + + assert.equal(user, undefined); + }); + + it('should not fail for null host header', function() { + var userFromHostConfig = global.environment.user_from_host; + global.environment.user_from_host = null; + + var cdbRequest = new CdbRequest(); + var user = cdbRequest.userByReq(createRequest(null)); + + global.environment.user_from_host = userFromHostConfig; + + assert.equal(user, undefined); + }); });