forked from floatdrop/express-request-id
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
76 lines (62 loc) · 1.89 KB
/
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
import test from 'ava';
import express from 'express';
import request from 'supertest';
import {validate} from 'uuid';
import requestID from './index.js';
function errorHandler(t) {
return (error, _request, _response, next) => {
t.fail(error.message);
next();
};
}
test('sets request id', async t => {
const app = express();
app.use(requestID());
app.get('/', (request, response, _next) => {
t.true(validate(request.id));
response.send('OK');
});
app.use(errorHandler(t));
const response = await request(app).get('/').expect(200, 'OK');
t.true(validate(response.get('X-Request-Id')));
});
test('preserves old request id', async t => {
const app = express();
app.use(requestID());
app.get('/', (request, response, _next) => {
t.is(request.id, 'MyID');
response.send('OK');
});
app.use(errorHandler(t));
await request(app).get('/').set('X-Request-Id', 'MyID').expect(200, 'OK');
});
test('setHeader option', async t => {
const app = express();
app.use(requestID({setHeader: false}));
app.get('/', (_request, response, _next) => {
response.send('OK');
});
app.use(errorHandler(t));
const response = await request(app).get('/').set('X-Request-Id', 'MyID').expect(200, 'OK');
t.is(response.get('X-Request-Id'), undefined);
});
test('headerName option', async t => {
const app = express();
app.use(requestID({headerName: 'X-My-Request-Id'}));
app.get('/', (_request, response, _next) => {
response.send('OK');
});
app.use(errorHandler(t));
const response = await request(app).get('/').set('X-My-Request-Id', 'MyID').expect(200, 'OK');
t.is(response.get('X-My-Request-Id'), 'MyID');
});
test('generator option', async t => {
const app = express();
app.use(requestID({generator: _request => 'ID'}));
app.get('/', (request, response, _next) => {
t.is(request.id, 'ID');
response.send('OK');
});
app.use(errorHandler(t));
await request(app).get('/').expect(200, 'OK');
});