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. 19
      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(); 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.meta = {};
res.data = _.flatten(results.map((result) => { res.data = _.flatten(results.map((result) => {
return synthesizeDocs(result); return synthesizeDocs(result);

19
service/placeholder.js

@ -9,7 +9,7 @@ module.exports = function setup(url) {
logger.warn('placeholder service disabled'); logger.warn('placeholder service disabled');
return { return {
search: (text, lang, callback) => { search: (text, lang, do_not_track, callback) => {
callback(`placeholder service disabled`); callback(`placeholder service disabled`);
} }
}; };
@ -18,12 +18,21 @@ module.exports = function setup(url) {
logger.info(`using placeholder service at ${url}`); logger.info(`using placeholder service at ${url}`);
return { return {
search: (text, lang, callback) => { search: (text, lang, do_not_track, callback) => {
const requestUrl = `${url}/search?text=${text}&lang=${lang}`; const requestUrl = `${url}/search?text=${text}&lang=${lang}`;
request const options = {
.get(requestUrl) method: 'GET',
.on('response', (response) => { 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 // pipe the response thru bl which will accumulate the entire body
response.pipe(bl((err, data) => { response.pipe(bl((err, data) => {
if (response.statusCode === 200) { if (response.statusCode === 200) {

87
test/unit/controller/placeholder.js

@ -49,7 +49,7 @@ module.exports.tests.success = function(test, common) {
let placeholderService_was_called = false; let placeholderService_was_called = false;
const placeholderService = { const placeholderService = {
search: (text, language, callback) => { search: (text, language, do_not_track, callback) => {
t.equals(text, 'query value'); t.equals(text, 'query value');
t.equals(language, 'language value'); t.equals(language, 'language value');
placeholderService_was_called = true; placeholderService_was_called = true;
@ -206,7 +206,7 @@ module.exports.tests.success = function(test, common) {
]; ];
const placeholderService = { const placeholderService = {
search: (text, language, callback) => { search: (text, language, do_not_track, callback) => {
t.equals(text, 'query value'); t.equals(text, 'query value');
t.equals(language, 'language value'); t.equals(language, 'language value');
placeholderService_was_called = true; 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) { module.exports.all = function (tape, common) {
function test(name, testFunction) { 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 'pelias-logger': logger
})(); })();
service.search('search text', 'search lang', (err) => { service.search('search text', 'search lang', false, (err) => {
t.deepEquals(logger.getWarnMessages(), [ t.deepEquals(logger.getWarnMessages(), [
'placeholder service disabled' 'placeholder service disabled'
]); ]);
@ -52,7 +52,7 @@ module.exports.tests.failure_conditions = (test, common) => {
'pelias-logger': logger 'pelias-logger': logger
})(`http://localhost:${port}`); })(`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.equals(err.code, 'ECONNREFUSED');
t.notOk(results); t.notOk(results);
t.ok(logger.isErrorMessage(/ECONNREFUSED/), 'there should be a connection refused error message'); 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 'pelias-logger': logger
})(`http://localhost:${port}`); })(`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.equals(err, `http://localhost:${port}/search?text=search%20text&lang=search%20lang returned status 400: a bad request was made`);
t.notOk(results); t.notOk(results);
t.ok(logger.isErrorMessage(`http://localhost:${port}/search?text=search%20text&lang=search%20lang ` + 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 'pelias-logger': logger
})(`http://localhost:${port}`); })(`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.equals(err, `http://localhost:${port}/search?text=search%20text&lang=search%20lang returned status 404: resource not found`);
t.notOk(results); t.notOk(results);
t.ok(logger.isErrorMessage(`http://localhost:${port}/search?text=search%20text&lang=search%20lang ` + 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 'pelias-logger': logger
})(`http://localhost:${port}`); })(`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 ` + t.equals(err, `http://localhost:${port}/search?text=search%20text&lang=search%20lang ` +
`could not parse response: this is not parseable as JSON`); `could not parse response: this is not parseable as JSON`);
t.notOk(results, 'should return undefined'); t.notOk(results, 'should return undefined');
@ -170,7 +170,7 @@ module.exports.tests.success_conditions = (test, common) => {
'pelias-logger': logger 'pelias-logger': logger
})(`http://localhost:${port}`); })(`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.notOk(err, 'should be no error');
t.deepEquals(results, [1, 2, 3]); t.deepEquals(results, [1, 2, 3]);
t.notOk(logger.hasErrorMessages()); 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) => { module.exports.all = (tape, common) => {
function test(name, testFunction) { function test(name, testFunction) {
return tape(`SERVICE /placeholder ${name}`, testFunction); return tape(`SERVICE /placeholder ${name}`, testFunction);

Loading…
Cancel
Save