-
Notifications
You must be signed in to change notification settings - Fork 0
/
vendorRelated.test.js
190 lines (159 loc) · 6.51 KB
/
vendorRelated.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const { expect } = require('chai');
const http = require('http');
const faker = require('faker');
const lolex = require('lolex');
const doRequestWrapper = require('./helpers/doRequest');
const { TYPE_KOA, TYPE_EXPRESS, TYPE_OPTIONS } = require('../src/consts');
describe('Vendor hopper', () => {
TYPE_OPTIONS.forEach(type => {
describe(type, () => {
const doRequest = doRequestWrapper(type);
it('should use default options and return fields', async () => {
const entry = await doRequest();
expect(entry).to.have.all.keys([
'responseTime',
'status',
'ip',
'method',
'url',
'contentLength',
'contentType',
'host',
'headers',
]);
});
it('should call resolver default interpreterFields', async () => {
const resolver = fieldInterpreters => {
expect(fieldInterpreters).to.have.all.keys([
'status',
'ip',
'method',
'url',
'contentLength',
'contentType',
'host',
'headers',
]);
return {};
};
await doRequest({ resolver });
});
it('should call resolver with correct args', async () => {
const resolver = (fieldInterpreters, ...args) => {
if (type === TYPE_KOA) {
expect(args).to.have.lengthOf(1);
const ctx = args[0];
expect(ctx.req).to.be.an.instanceOf(http.IncomingMessage);
expect(ctx.res).to.be.an.instanceOf(http.ServerResponse);
} else if (type === TYPE_EXPRESS) {
expect(args).to.have.lengthOf(2);
expect(args[0]).to.be.an.instanceOf(http.IncomingMessage);
expect(args[1]).to.be.an.instanceOf(http.ServerResponse);
}
return {};
};
await doRequest({ resolver });
});
it('should have some response data in handler', async () => {
const entry = await doRequest();
expect(entry).to.have.property('status', 200);
expect(entry).to.have.property('method', 'GET');
});
it('should call handler without any fields when defaultFields is false', async () => {
const entry = await doRequest({ defaultFields: false });
expect(entry).to.have.all.keys(['responseTime']);
});
it('should record responseTime based on default options', async () => {
const fakeTime = faker.random.number();
const responseTime = 2000;
const clock = lolex.install({ now: fakeTime, toFake: ['Date'] });
const afterMiddleware = (...args) => {
clock.tick(responseTime);
args[args.length - 1]();
};
const entry = await doRequest({ afterMiddleware });
expect(entry).to.have.own.property('responseTime', responseTime);
expect(entry).to.not.have.own.property('requestTime');
clock.uninstall();
});
it('should record responseTime when option passed', async () => {
const fakeTime = faker.random.number();
const responseTime = 2000;
const clock = lolex.install({ now: fakeTime, toFake: ['Date'] });
const afterMiddleware = (...args) => {
clock.tick(responseTime);
args[args.length - 1]();
};
const entry = await doRequest({ afterMiddleware, timestamps: { responseTime: true } });
expect(entry).to.have.own.property('responseTime', responseTime);
clock.uninstall();
});
it('should record requestTime when option passed', async () => {
const fakeTime = faker.random.number();
const clock = lolex.install({ now: fakeTime, toFake: ['Date'] });
const entry = await doRequest({ timestamps: { requestTime: true } });
expect(entry).to.have.own.property('requestTime', fakeTime);
clock.uninstall();
});
it('should record both times when option passed', async () => {
const fakeTime = faker.random.number();
const responseTime = 2000;
const clock = lolex.install({ now: fakeTime, toFake: ['Date'] });
const afterMiddleware = (...args) => {
clock.tick(responseTime);
args[args.length - 1]();
};
const entry = await doRequest({ afterMiddleware, timestamps: true });
expect(entry).to.have.own.property('requestTime', fakeTime);
expect(entry).to.have.own.property('responseTime', responseTime);
clock.uninstall();
});
it('should not record times if option disabled', async () => {
const entry = await doRequest({ timestamps: false });
expect(entry).to.not.have.own.property('requestTime');
expect(entry).to.not.have.own.property('responseTime');
});
it('should still call handler when error is handled', async () => {
const afterMiddleware = (...args) => {
const error = new Error('FAIL');
if (type === TYPE_KOA) {
throw error;
} else if (type === TYPE_EXPRESS) {
args[args.length - 1](error);
}
};
const entry = await doRequest({ status: 500, afterMiddleware });
expect(entry.status).to.equal(500);
});
it('should immediattely invoke koa handler before finishing request', async () => {
const entry = await doRequest({ immediate: true });
expect(entry).to.not.have.property('responseTime');
});
it('should call handler without response data', async () => {
const entry = await doRequest({ immediate: true });
expect(entry).to.not.have.property('contentLength');
});
it('should ignore based result from function', async () => {
const ignore = () => true;
const entry = await doRequest({ ignore });
expect(entry).to.equal(undefined);
});
it('should pass relevant fields to ignore function', async () => {
const ignore = (...args) => {
if (type === TYPE_KOA) {
expect(args).to.have.lengthOf(1);
expect(args[0].req).to.be.an.instanceOf(http.IncomingMessage);
expect(args[0].res).to.be.an.instanceOf(http.ServerResponse);
} else if (type === TYPE_EXPRESS) {
expect(args).to.have.lengthOf(2);
expect(args[0]).to.be.an.instanceOf(http.IncomingMessage);
expect(args[1]).to.be.an.instanceOf(http.ServerResponse);
}
return true;
};
const entry = await doRequest({ ignore });
expect(entry).to.equal(undefined);
});
});
});
});