Browse Source

added support for do_not_track header

pull/850/head
Stephen Hess 8 years ago
parent
commit
8a7ba3d8de
  1. 2
      controller/placeholder.js
  2. 69
      service/placeholder.js
  3. 87
      test/unit/controller/placeholder.js
  4. 68
      test/unit/service/placeholder.js

2
controller/placeholder.js

@ -56,7 +56,7 @@ function setup(placeholderService, should_execute) {
return next();
}
placeholderService.search(req.clean.text, req.clean.lang.iso6393, (err, results) => {
placeholderService.search(req.clean.text, req.clean.lang.iso6393, logging.isDNT(req), (err, results) => {
res.meta = {};
res.data = _.flatten(results.map((result) => {
return synthesizeDocs(result);

69
service/placeholder.js

@ -9,7 +9,7 @@ module.exports = function setup(url) {
logger.warn('placeholder service disabled');
return {
search: (text, lang, callback) => {
search: (text, lang, do_not_track, callback) => {
callback(`placeholder service disabled`);
}
};
@ -18,37 +18,46 @@ module.exports = function setup(url) {
logger.info(`using placeholder service at ${url}`);
return {
search: (text, lang, callback) => {
search: (text, lang, do_not_track, callback) => {
const requestUrl = `${url}/search?text=${text}&lang=${lang}`;
request
.get(requestUrl)
.on('response', (response) => {
// pipe the response thru bl which will accumulate the entire body
response.pipe(bl((err, data) => {
if (response.statusCode === 200) {
// parse and return w/o error unless response wasn't JSON
try {
const parsed = JSON.parse(data);
return callback(null, parsed);
}
catch (err) {
logger.error(`${encodeURI(requestUrl)} could not parse response: ${data}`);
return callback(`${encodeURI(requestUrl)} could not parse response: ${data}`);
}
}
else {
// otherwise there was a non-200 status so handle generically
logger.error(`${encodeURI(requestUrl)} returned status ${response.statusCode}: ${data}`);
return callback(`${encodeURI(requestUrl)} returned status ${response.statusCode}: ${data}`);
}
}));
})
.on('error', (err) => {
logger.error(JSON.stringify(err));
callback(err);
});
const options = {
method: 'GET',
url: requestUrl
};
if (do_not_track) {
options.headers = {
dnt: '1'
};
}
request(options).on('response', (response) => {
// pipe the response thru bl which will accumulate the entire body
response.pipe(bl((err, data) => {
if (response.statusCode === 200) {
// parse and return w/o error unless response wasn't JSON
try {
const parsed = JSON.parse(data);
return callback(null, parsed);
}
catch (err) {
logger.error(`${encodeURI(requestUrl)} could not parse response: ${data}`);
return callback(`${encodeURI(requestUrl)} could not parse response: ${data}`);
}
}
else {
// otherwise there was a non-200 status so handle generically
logger.error(`${encodeURI(requestUrl)} returned status ${response.statusCode}: ${data}`);
return callback(`${encodeURI(requestUrl)} returned status ${response.statusCode}: ${data}`);
}
}));
})
.on('error', (err) => {
logger.error(JSON.stringify(err));
callback(err);
});
}
};

87
test/unit/controller/placeholder.js

@ -49,7 +49,7 @@ module.exports.tests.success = function(test, common) {
let placeholderService_was_called = false;
const placeholderService = {
search: (text, language, callback) => {
search: (text, language, do_not_track, callback) => {
t.equals(text, 'query value');
t.equals(language, 'language value');
placeholderService_was_called = true;
@ -206,7 +206,7 @@ module.exports.tests.success = function(test, common) {
];
const placeholderService = {
search: (text, language, callback) => {
search: (text, language, do_not_track, callback) => {
t.equals(text, 'query value');
t.equals(language, 'language value');
placeholderService_was_called = true;
@ -366,6 +366,89 @@ module.exports.tests.success = function(test, common) {
};
module.exports.tests.do_not_track = function(test, common) {
test('do_not_track enabled should pass header with `true` value to service', (t) => {
let placeholderService_was_called = false;
const placeholderService = {
search: (text, language, do_not_track, callback) => {
t.ok(do_not_track, 'should be true');
placeholderService_was_called = true;
callback(null, []);
}
};
const should_execute = (req, res) => {
return true;
};
const controller = proxyquire('../../../controller/placeholder', {
'../helper/logging': {
isDNT: (req) => {
return true;
}
}
})(placeholderService, should_execute);
const req = {
clean: {
text: 'query value',
lang: {
iso6393: 'language value'
}
}
};
const res = { b: 2 };
controller(req, res, () => {
t.ok(placeholderService_was_called);
t.end();
});
});
test('do_not_track disabled should pass header with `false` value to service', (t) => {
let placeholderService_was_called = false;
const placeholderService = {
search: (text, language, do_not_track, callback) => {
t.notOk(do_not_track, 'should be false');
placeholderService_was_called = true;
callback(null, []);
}
};
const should_execute = (req, res) => {
return true;
};
const controller = proxyquire('../../../controller/placeholder', {
'../helper/logging': {
isDNT: (req) => {
return false;
}
}
})(placeholderService, should_execute);
const req = {
clean: {
text: 'query value',
lang: {
iso6393: 'language value'
}
}
};
const res = { b: 2 };
controller(req, res, () => {
t.ok(placeholderService_was_called);
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {

68
test/unit/service/placeholder.js

@ -26,7 +26,7 @@ module.exports.tests.do_nothing_service = (test, common) => {
'pelias-logger': logger
})();
service.search('search text', 'search lang', (err) => {
service.search('search text', 'search lang', false, (err) => {
t.deepEquals(logger.getWarnMessages(), [
'placeholder service disabled'
]);
@ -52,7 +52,7 @@ module.exports.tests.failure_conditions = (test, common) => {
'pelias-logger': logger
})(`http://localhost:${port}`);
service.search('search text', 'search lang', (err, results) => {
service.search('search text', 'search lang', false, (err, results) => {
t.equals(err.code, 'ECONNREFUSED');
t.notOk(results);
t.ok(logger.isErrorMessage(/ECONNREFUSED/), 'there should be a connection refused error message');
@ -79,7 +79,7 @@ module.exports.tests.failure_conditions = (test, common) => {
'pelias-logger': logger
})(`http://localhost:${port}`);
service.search('search text', 'search lang', (err, results) => {
service.search('search text', 'search lang', false, (err, results) => {
t.equals(err, `http://localhost:${port}/search?text=search%20text&lang=search%20lang returned status 400: a bad request was made`);
t.notOk(results);
t.ok(logger.isErrorMessage(`http://localhost:${port}/search?text=search%20text&lang=search%20lang ` +
@ -108,7 +108,7 @@ module.exports.tests.failure_conditions = (test, common) => {
'pelias-logger': logger
})(`http://localhost:${port}`);
service.search('search text', 'search lang', (err, results) => {
service.search('search text', 'search lang', false, (err, results) => {
t.equals(err, `http://localhost:${port}/search?text=search%20text&lang=search%20lang returned status 404: resource not found`);
t.notOk(results);
t.ok(logger.isErrorMessage(`http://localhost:${port}/search?text=search%20text&lang=search%20lang ` +
@ -136,7 +136,7 @@ module.exports.tests.failure_conditions = (test, common) => {
'pelias-logger': logger
})(`http://localhost:${port}`);
service.search('search text', 'search lang', (err, results) => {
service.search('search text', 'search lang', false, (err, results) => {
t.equals(err, `http://localhost:${port}/search?text=search%20text&lang=search%20lang ` +
`could not parse response: this is not parseable as JSON`);
t.notOk(results, 'should return undefined');
@ -170,7 +170,7 @@ module.exports.tests.success_conditions = (test, common) => {
'pelias-logger': logger
})(`http://localhost:${port}`);
service.search('search text', 'search lang', (err, results) => {
service.search('search text', 'search lang', false, (err, results) => {
t.notOk(err, 'should be no error');
t.deepEquals(results, [1, 2, 3]);
t.notOk(logger.hasErrorMessages());
@ -184,6 +184,62 @@ module.exports.tests.success_conditions = (test, common) => {
};
module.exports.tests.do_not_track_header = (test, common) => {
test('true do_not_track value should be passed in \'DNT\' header', (t) => {
const placeholderServer = express();
placeholderServer.get('/search', (req, res, next) => {
t.equals(req.headers.dnt, '1');
res.status(200).send([]);
});
const server = placeholderServer.listen();
const port = server.address().port;
const logger = require('pelias-mock-logger')();
const service = proxyquire('../../../service/placeholder', {
'pelias-logger': logger
})(`http://localhost:${port}`);
service.search('search text', 'search lang', true, (err, results) => {
t.deepEquals(results, []);
server.close();
});
t.end();
});
test('false do_not_track value should not pass a \'DNT\' header', (t) => {
[true, false].forEach((do_not_track) => {
const placeholderServer = express();
placeholderServer.get('/search', (req, res, next) => {
t.notOk(req.headers.hasOwnProperty('DNT'));
res.status(200).send([]);
});
const server = placeholderServer.listen();
const port = server.address().port;
const logger = require('pelias-mock-logger')();
const service = proxyquire('../../../service/placeholder', {
'pelias-logger': logger
})(`http://localhost:${port}`);
service.search('search text', 'search lang', false, (err, results) => {
t.deepEquals(results, []);
server.close();
});
});
t.end();
});
};
module.exports.all = (tape, common) => {
function test(name, testFunction) {
return tape(`SERVICE /placeholder ${name}`, testFunction);

Loading…
Cancel
Save