-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding tests using axios-mock-adapter & fixes
* Adding axios-mock-adapter to client project * Apparently, axios-mock-adapter doesn't give access to the 'request' data in the promise resolve callback -- this seems to require having the server actually return the queried number, rather than having that context preserved locally in the client. Ouch. (See: ctimmerm/axios-mock-adapter#104) * Server now returns both the primality and the number in question * Wrapping request logic to marshall HTTP response back to client
- Loading branch information
Showing
10 changed files
with
97 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
const request = require('./request'); | ||
const output = require('./output'); | ||
|
||
for (var i = 0; i < 1000; i++) { | ||
request(i); | ||
for (var i = 10000000000000; i < 10000000001000; i++) { | ||
request(i).then((response) => { | ||
output(response); | ||
}).catch((error) => { | ||
console.error(error); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = { | ||
'serverURL': 'http://localhost:3000/primes/' | ||
'serverURL': process.env.SERVER_URL || 'http://localhost:3000/primes/' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
module.exports = (query, result) => { | ||
console.log(`(${query}) -> ${result}`); | ||
module.exports = (response) => { | ||
if (isNaN(response.number)) { | ||
console.error('Query was non-numeric'); | ||
return false; | ||
} | ||
if (typeof(response.result) != 'boolean') { | ||
console.error('Result was non-boolean'); | ||
return false; | ||
} | ||
console.log(`(${response.number}) -> ${response.result}`); | ||
return true; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,14 @@ | ||
const config = require('./config'); | ||
const output = require('./output'); | ||
const axios = require('axios'); | ||
|
||
const instance = axios.create({ | ||
baseURL: process.env.SERVER_URL || config.serverURL | ||
}); | ||
|
||
module.exports = (number) => { | ||
instance.get(`${number}`) | ||
.then((response) => { | ||
output(response.request.path.split('/').pop(), response.data.result); | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
}); | ||
return new Promise((resolve, reject) => { | ||
axios.get(config.serverURL + `${number}`) | ||
.then((response) => { | ||
resolve(response.data); | ||
}) | ||
.catch((error) => { | ||
reject(error); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
var axios = require('axios'); | ||
var adapter = require('axios-mock-adapter'); | ||
var config = require('../config'); | ||
var request = require('../request'); | ||
var mock = new adapter(axios); | ||
|
||
describe('handle request responses', () => { | ||
test('handle mock data', (done) => { | ||
|
||
mock.onGet(config.serverURL + '7').reply(200, { number: 7, result: true }); | ||
|
||
request(7).then((response) => { | ||
expect(response.number).toBe(7); | ||
expect(response.result).toBe(true); | ||
mock.restore(); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('handle network error', (done) => { | ||
|
||
mock.onGet(config.serverURL + '7').networkError(); | ||
|
||
return request(7).then(() => {}, error => { | ||
expect(error.code).toBe('ECONNREFUSED'); | ||
mock.restore(); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('handle network timeout', (done) => { | ||
|
||
mock.onGet(config.serverURL + '7').timeout(); | ||
|
||
return request(7).then(() => {}, error => { | ||
expect(error.code).toBe('ECONNREFUSED'); | ||
mock.restore(); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
'slowDown': process.env.SLOW_DOWN || false | ||
}; |