@ -0,0 +1,14 @@ | |||||
{ | |||||
"name": "@smokeball-dev/javascript-developer-test", | |||||
"version": "2.0.1", | |||||
"description": "", | |||||
"main": "./index.js", | |||||
"author": "Smokeball Development Team", | |||||
"license": "MIT", | |||||
"scripts": { | |||||
"test": "jest" | |||||
}, | |||||
"dependencies": { | |||||
"jest": "25.1.0" | |||||
} | |||||
} |
@ -0,0 +1,9 @@ | |||||
const { getUrlP } = require('./mock-http-interface'); | |||||
const getUrls = async (urls) => { | |||||
// TODO. | |||||
}; | |||||
module.exports = { | |||||
getUrls, | |||||
}; |
@ -0,0 +1,37 @@ | |||||
const { getUrls } = require('./get-urls'); | |||||
const urls = [ | |||||
'http://www.smokeballdev.com/url1', | |||||
'http://www.smokeballdev.com/url2', | |||||
'http://www.smokeballdev.com/url3', | |||||
'http://www.smokeballdev.com/url4', | |||||
]; | |||||
test('expect no throws', () => { | |||||
expect.assertions(1); | |||||
expect(async () => results = await getUrls(urls)).not.toThrow(); | |||||
}); | |||||
test('responses to be correct', async () => { | |||||
expect.assertions(5); | |||||
const results = await getUrls(urls); | |||||
expect(results.length).toBe(4); | |||||
expect(results[0]).toEqual({ 'Arnie Quote': 'Get to the chopper' }); | |||||
expect(results[1]).toEqual({ 'Arnie Quote': 'MY NAME IS NOT QUAID' }); | |||||
expect(results[2]).toEqual({ 'Arnie Quote': `What's wrong with Wolfie?` }); | |||||
expect(results[3]).toEqual({ 'FAILURE': 'Your request has been terminated' }); | |||||
}); | |||||
test('code to be executed in less than 400ms', async () => { | |||||
expect.assertions(2); | |||||
const startTime = process.hrtime(); | |||||
await getUrls(urls); | |||||
const [ seconds, nanos ] = process.hrtime(startTime); | |||||
expect(seconds).toBe(0); | |||||
expect(nanos / 1000 / 1000).toBeLessThan(400); | |||||
}); |
@ -0,0 +1,34 @@ | |||||
'use strict'; | |||||
const urlPrefix = `http://www.smokeballdev.com`; | |||||
const urlToResponseLookup = { | |||||
[`${urlPrefix}/url1`]: 'Get to the chopper', | |||||
[`${urlPrefix}/url2`]: 'MY NAME IS NOT QUAID', | |||||
[`${urlPrefix}/url3`]: `What's wrong with Wolfie?`, | |||||
}; | |||||
const slowHttpRequestMockP = (url) => new Promise((resolve, reject) => { | |||||
setTimeout(() => { | |||||
const responseData = urlToResponseLookup[url]; | |||||
if (responseData) { | |||||
resolve(responseData); | |||||
} else { | |||||
reject(new Error('Your request has been terminated')); | |||||
} | |||||
}, 200); | |||||
}); | |||||
const getUrlP = async (url) => { | |||||
try { | |||||
const message = await slowHttpRequestMockP(url); | |||||
return { status: 200, body: JSON.stringify({ message }) }; | |||||
} | |||||
catch (err) { | |||||
return { status: 500, body: JSON.stringify({ message: err.message }) }; | |||||
} | |||||
}; | |||||
module.exports = { | |||||
getUrlP, | |||||
}; |