Skip to content

Commit

Permalink
Adding tests using axios-mock-adapter & fixes
Browse files Browse the repository at this point in the history
* 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
rootd00d committed Feb 13, 2019
1 parent 75bf603 commit dca0096
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 23 deletions.
9 changes: 7 additions & 2 deletions client/client.js
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);
});
}
2 changes: 1 addition & 1 deletion client/config.js
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/'
};
13 changes: 11 additions & 2 deletions client/output.js
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;
};
15 changes: 15 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"axios": "^0.18.0",
"debug": "^4.1.1",
"jest": "^24.1.0"
},
"devDependencies": {
"axios-mock-adapter": "^1.16.0"
}
}
21 changes: 9 additions & 12 deletions client/request.js
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);
});
});
};
8 changes: 4 additions & 4 deletions client/tests/output.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ var output = require('../output');

describe('test client output functionality', () => {
test('rejects non-numeric values', (done) => {
expect(output('asdf','foo')).toBe(false);
expect(output({ number: 'asdf', result: true })).toBe(false);
done();
});

test('rejects non-boolean values', (done) => {
expect(output('7','foo')).toBe(false);
expect(output({ number: '7', result: 'foo' })).toBe(false);
done();
});

test('accepts when numeric and boolean', (done) => {
expect(output('7', true)).toBe(true);
expect(output({ number: '7', result: true })).toBe(true);
done();
});

});
41 changes: 41 additions & 0 deletions client/tests/request.test.js
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();
});
});
});
5 changes: 3 additions & 2 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var express = require('express');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var jsbn = require('jsbn');
var config = require('./config');
var busywait = require('./busywait');

var app = express();
Expand All @@ -12,10 +13,10 @@ app.use(cookieParser());

app.get('/primes/:number', (req, res, next) => {
var bi = new jsbn.BigInteger(req.params.number);
if (process.env.SLOW_DOWN) {
if (config.slowDown) {
busywait(Math.floor((Math.random() * 1000) + 500));
}
res.json({ result: bi.isProbablePrime() });
res.json({ number: bi.toString(), result: bi.isProbablePrime() });
next();
});

Expand Down
3 changes: 3 additions & 0 deletions server/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'slowDown': process.env.SLOW_DOWN || false
};

0 comments on commit dca0096

Please sign in to comment.