Skip to content

Commit 538bb72

Browse files
author
Ammar Dodin
committed
💚 add unit tests for lib/requestwrapper.ts
1 parent d678f32 commit 538bb72

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

test/unit/test.requestWrapper.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const createRequest = require('../../lib/requestwrapper').createRequest;
2+
const formatError = require('../../lib/requestwrapper').formatErrorIfExists;
3+
const assert = require('assert');
4+
const isStream = require('isstream');
5+
6+
describe('requestwrapper', () => {
7+
it('should emit error stream on missing parameters when callback is undefined', () => {
8+
const parameters = {
9+
options: {
10+
url: '/stuff/',
11+
qs: { fake: 'fake' }
12+
},
13+
requiredParams: ['fake_param'],
14+
defaultOptions: { url: 'more'}
15+
};
16+
assert(isStream(createRequest(parameters, '')));
17+
});
18+
});
19+
20+
describe('formatError', () => {
21+
it('should check for error in response', () => {
22+
const _error = undefined;
23+
const _response = { statusCode: 401 };
24+
const _body = 'fake body';
25+
const cb = (err, body, res) => {
26+
assert.equal(body, null);
27+
assert(err instanceof Error);
28+
assert.equal(
29+
err.message,
30+
'Unauthorized: Access is denied due to invalid credentials.'
31+
);
32+
};
33+
const formatted = formatError(cb);
34+
formatted(_error, _response, _body);
35+
});
36+
37+
it('should check for error in body with error_code', () => {
38+
const _error = undefined;
39+
const _response = {};
40+
const _body = { error_code: '666', fake_key: 'fake_value' };
41+
const cb = (err, body, res) => {
42+
assert.equal(body, null);
43+
assert(err instanceof Error);
44+
assert.equal(err.code, _body.error_code);
45+
assert.equal(err.fake_key, _body.fake_key);
46+
};
47+
const formatted = formatError(cb);
48+
formatted(_error, _response, _body);
49+
});
50+
51+
it('should check for error in body with error', () => {
52+
const _error = undefined;
53+
const _response = {};
54+
const _body = {
55+
error: { description: 'fake description' },
56+
fake_key: 'fake_value'
57+
};
58+
const cb = (err, body, res) => {
59+
assert.equal(body, null);
60+
assert(err instanceof Error);
61+
assert.equal(err.description, 'fake description');
62+
assert.equal(err.fake_key, 'fake_value');
63+
};
64+
const formatted = formatError(cb);
65+
formatted(_error, _response, _body);
66+
});
67+
68+
it('should check for error in body with error.error', () => {
69+
const _error = undefined;
70+
const _response = {};
71+
const _body = {
72+
error: { error: { error: 'fake description' } },
73+
fake_key: 'fake_value'
74+
};
75+
const cb = (err, body, res) => {
76+
assert.equal(body, null);
77+
assert(err instanceof Error);
78+
assert.equal(err.error, JSON.stringify({ error: 'fake description' }));
79+
assert.equal(err.fake_key, 'fake_value');
80+
};
81+
const formatted = formatError(cb);
82+
formatted(_error, _response, _body);
83+
});
84+
85+
it('should check for error if its not null', () => {
86+
const _error = { message: 'fake error' };
87+
const _response = {};
88+
const _body = {};
89+
const cb = (err, body, res) => {
90+
assert(err instanceof Error);
91+
assert.equal(err.message, _error.message);
92+
};
93+
const formatted = formatError(cb);
94+
formatted(_error, _response, _body);
95+
});
96+
97+
it('should format error for invalid api keys', () => {
98+
const _error = undefined;
99+
const _response = { statusMessage: 'invalid-api-key' };
100+
const _body = {};
101+
const cb = (err, body, res) => {
102+
assert.equal(err.error, _response.statusMessage);
103+
assert.equal(err.code, 401);
104+
};
105+
const formatted = formatError(cb);
106+
formatted(_error, _response, _body);
107+
});
108+
});

0 commit comments

Comments
 (0)