mirror of https://github.com/pelias/api.git
Stephen Hess
8 years ago
4 changed files with 0 additions and 417 deletions
@ -1,82 +0,0 @@
|
||||
const request = require('request'); |
||||
const bl = require('bl'); |
||||
const _ = require('lodash'); |
||||
|
||||
const logger = require( 'pelias-logger' ).get( 'placeholder' ); |
||||
|
||||
module.exports = function setup(url) { |
||||
if (_.isEmpty(url)) { |
||||
logger.warn('placeholder service disabled'); |
||||
|
||||
return { |
||||
search: (text, lang, do_not_track, callback) => { |
||||
callback(`placeholder service disabled`); |
||||
} |
||||
}; |
||||
|
||||
} |
||||
|
||||
logger.info(`using placeholder service at ${url}`); |
||||
return { |
||||
search: (text, lang, do_not_track, callback) => { |
||||
const requestUrl = `${url}/search`; |
||||
|
||||
const options = { |
||||
method: 'GET', |
||||
url: requestUrl, |
||||
qs: { |
||||
text: text, |
||||
lang: lang |
||||
} |
||||
}; |
||||
|
||||
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) { |
||||
if (do_not_track) { |
||||
logger.error(`${requestUrl} could not parse response: ${data}`); |
||||
return callback(`${requestUrl} could not parse response: ${data}`); |
||||
} else { |
||||
logger.error(`${response.request.href} could not parse response: ${data}`); |
||||
return callback(`${response.request.href} could not parse response: ${data}`); |
||||
} |
||||
|
||||
} |
||||
} |
||||
else { |
||||
// otherwise there was a non-200 status so handle generically
|
||||
if (do_not_track) { |
||||
logger.error(`${requestUrl} returned status ${response.statusCode}: ${data}`); |
||||
return callback(`${requestUrl} returned status ${response.statusCode}: ${data}`); |
||||
} else { |
||||
logger.error(`${response.request.href} returned status ${response.statusCode}: ${data}`); |
||||
return callback(`${response.request.href} returned status ${response.statusCode}: ${data}`); |
||||
} |
||||
|
||||
} |
||||
})); |
||||
|
||||
}) |
||||
.on('error', (err) => { |
||||
logger.error(`${requestUrl}: ${JSON.stringify(err)}`); |
||||
callback(err); |
||||
}); |
||||
|
||||
} |
||||
}; |
||||
|
||||
}; |
@ -1,333 +0,0 @@
|
||||
const proxyquire = require('proxyquire').noCallThru(); |
||||
const express = require('express'); |
||||
|
||||
const setup = require('../../../service/placeholder'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = (test, common) => { |
||||
test('valid interface', (t) => { |
||||
const logger = require('pelias-mock-logger')(); |
||||
|
||||
var service = proxyquire('../../../service/placeholder', { |
||||
'pelias-logger': logger |
||||
}); |
||||
|
||||
t.equal(typeof service, 'function', 'service is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.do_nothing_service = (test, common) => { |
||||
test('undefined url should return service that logs fact that placeholder service is not available', (t) => { |
||||
const logger = require('pelias-mock-logger')(); |
||||
|
||||
const service = proxyquire('../../../service/placeholder', { |
||||
'pelias-logger': logger |
||||
})(); |
||||
|
||||
service.search('search text', 'search lang', false, (err) => { |
||||
t.deepEquals(logger.getWarnMessages(), [ |
||||
'placeholder service disabled' |
||||
]); |
||||
t.equals(err, 'placeholder service disabled'); |
||||
t.end(); |
||||
}); |
||||
|
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.tests.failure_conditions = (test, common) => { |
||||
test('server returning error should log it and return no results', (t) => { |
||||
const server = express().listen(); |
||||
const port = server.address().port; |
||||
|
||||
// immediately close the server so to ensure an error response
|
||||
server.close(); |
||||
|
||||
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.equals(err.code, 'ECONNREFUSED'); |
||||
t.notOk(results); |
||||
t.ok(logger.isErrorMessage(/ECONNREFUSED/), 'there should be a connection refused error message'); |
||||
t.end(); |
||||
|
||||
server.close(); |
||||
|
||||
}); |
||||
|
||||
}); |
||||
|
||||
test('server returning error should log it sanitized and return no results', (t) => { |
||||
const server = express().listen(); |
||||
const port = server.address().port; |
||||
|
||||
// immediately close the server so to ensure an error response
|
||||
server.close(); |
||||
|
||||
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.equals(err.code, 'ECONNREFUSED'); |
||||
t.notOk(results); |
||||
t.ok(logger.isErrorMessage(/ECONNREFUSED/), 'there should be a connection refused error message'); |
||||
t.end(); |
||||
|
||||
server.close(); |
||||
|
||||
}); |
||||
|
||||
}); |
||||
|
||||
test('server returning non-200 response should log error and return no results', (t) => { |
||||
const placeholderServer = express(); |
||||
placeholderServer.get('/search', (req, res, next) => { |
||||
res.status(400).send('a bad request was made'); |
||||
}); |
||||
|
||||
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.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 ` + |
||||
`returned status 400: a bad request was made`)); |
||||
t.end(); |
||||
|
||||
server.close(); |
||||
|
||||
}); |
||||
|
||||
}); |
||||
|
||||
test('server returning non-200 response should log sanitized error when do_not_track and return no results', (t) => { |
||||
const placeholderServer = express(); |
||||
placeholderServer.get('/search', (req, res, next) => { |
||||
res.status(400).send('a bad request was made'); |
||||
}); |
||||
|
||||
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.equals(err, `http://localhost:${port}/search returned status 400: a bad request was made`); |
||||
t.notOk(results); |
||||
t.ok(logger.isErrorMessage(`http://localhost:${port}/search ` + |
||||
`returned status 400: a bad request was made`)); |
||||
t.end(); |
||||
|
||||
server.close(); |
||||
|
||||
}); |
||||
|
||||
}); |
||||
|
||||
test('server returning 404 statusCode should be treated the same as other statusCodes', (t) => { |
||||
// at one point placeholder treated 0 results as a 404 instead of just an unparseable input
|
||||
const placeholderServer = express(); |
||||
placeholderServer.get('/search', (req, res, next) => { |
||||
res.status(404).send('resource not found'); |
||||
}); |
||||
|
||||
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.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 ` + |
||||
`returned status 404: resource not found`)); |
||||
t.end(); |
||||
|
||||
server.close(); |
||||
|
||||
}); |
||||
|
||||
}); |
||||
|
||||
test('server returning 200 statusCode but with non-JSON response should log error and return undefined', (t) => { |
||||
const placeholderServer = express(); |
||||
placeholderServer.get('/search', (req, res, next) => { |
||||
res.status(200).send('this is not parseable as JSON'); |
||||
}); |
||||
|
||||
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.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'); |
||||
t.ok(logger.isErrorMessage(`http://localhost:${port}/search?text=search%20text&lang=search%20lang ` + |
||||
`could not parse response: this is not parseable as JSON`)); |
||||
t.end(); |
||||
|
||||
server.close(); |
||||
|
||||
}); |
||||
|
||||
}); |
||||
|
||||
test('server returning 200 statusCode but with non-JSON response should log sanitized error and return undefined', (t) => { |
||||
const placeholderServer = express(); |
||||
placeholderServer.get('/search', (req, res, next) => { |
||||
res.status(200).send('this is not parseable as JSON'); |
||||
}); |
||||
|
||||
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.equals(err, `http://localhost:${port}/search ` + |
||||
`could not parse response: this is not parseable as JSON`); |
||||
t.notOk(results, 'should return undefined'); |
||||
t.ok(logger.isErrorMessage(`http://localhost:${port}/search ` + |
||||
`could not parse response: this is not parseable as JSON`)); |
||||
t.end(); |
||||
|
||||
server.close(); |
||||
|
||||
}); |
||||
|
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.tests.success_conditions = (test, common) => { |
||||
test('server returning statusCode 200 should return no error and parsed output', (t) => { |
||||
const placeholderServer = express(); |
||||
placeholderServer.get('/search', (req, res, next) => { |
||||
if (req.query.text === 'search text' && req.query.lang === 'search lang') { |
||||
res.status(200).send('[1, 2, 3]'); |
||||
} |
||||
}); |
||||
|
||||
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.notOk(err, 'should be no error'); |
||||
t.deepEquals(results, [1, 2, 3]); |
||||
t.notOk(logger.hasErrorMessages()); |
||||
t.end(); |
||||
|
||||
server.close(); |
||||
|
||||
}); |
||||
|
||||
}); |
||||
|
||||
}; |
||||
|
||||
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); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue