-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
155 lines (134 loc) · 4.42 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
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
'use strict';
var assume = require('assume');
var morgan = require('morgan');
var json = require('./');
//
// A simple mock "morgan" object which returns deterministic
// output from the defined functions.
//
var mock = {
method: function () { return 'method' },
url: function () { return 'url' },
status: function () { return 'status' },
res: function (req, res, arg) { return ['res', arg].join(' ') },
'response-time': function () { return 'response-time' },
};
//
// Invalid argument message that morgan-json outputs.
//
var invalidMsg = 'argument format must be a string or an object';
describe('morgan-json', function () {
it('format string of all tokens', function () {
var compiled = json(':method :url :status :res[content-length] :response-time');
var output = compiled(mock);
assume(output).deep.equals(JSON.stringify({
method: 'method',
url: 'url',
status: 'status',
res: 'res content-length',
'response-time': 'response-time'
}));
});
it('format string of all tokens (with trailers)', function () {
var compiled = json(':method :url :status :res[content-length] bytes :response-time ms');
var output = compiled(mock);
assume(output).deep.equals(JSON.stringify({
method: 'method',
url: 'url',
status: 'status',
res: 'res content-length bytes',
'response-time': 'response-time ms'
}));
});
it('format object of all single tokens (no trailers)', function () {
var compiled = json({
method: ':method',
url: ':url',
status: ':status',
'response-time': ':response-time',
length: ':res[content-length]'
});
var output = compiled(mock);
assume(output).deep.equals(JSON.stringify({
method: 'method',
url: 'url',
status: 'status',
'response-time': 'response-time',
length: 'res content-length'
}))
});
it('format object with multiple tokens', function () {
var compiled = json({
short: ':method :url :status',
'response-time': ':response-time',
length: ':res[content-length]'
});
var output = compiled(mock);
assume(output).deep.equals(JSON.stringify({
short: 'method url status',
'response-time': 'response-time',
length: 'res content-length'
}));
});
it('format object of all tokens (with trailers)', function () {
var compiled = json({
method: 'GET :method',
url: '-> /:url',
status: 'Code :status',
'response-time': ':response-time ms',
length: ':res[content-length]'
});
var output = compiled(mock);
assume(output).deep.equals(JSON.stringify({
method: 'GET method',
url: '-> /url',
status: 'Code status',
'response-time': 'response-time ms',
length: 'res content-length'
}));
});
describe('{ stringify: false }', function () {
it('format object returns an object', function () {
var compiled = json({
short: ':method :url :status',
'response-time': ':response-time',
length: ':res[content-length]'
}, { stringify: false });
var output = compiled(mock);
assume(output).is.an('object');
assume(output).deep.equals({
short: 'method url status',
'response-time': 'response-time',
length: 'res content-length'
});
});
it('format string returns an object', function () {
var compiled = json(':method :url :status', { stringify: false });
var output = compiled(mock);
assume(output).is.an('object');
assume(output).deep.equals({
method: 'method',
url: 'url',
status: 'status'
});
});
});
describe('Invalid arguments', function () {
it('throws with null', function () {
assume(function () { json(null); }).throws(invalidMsg);
});
it('throws with Boolean', function () {
assume(function () { json(false); }).throws(invalidMsg);
assume(function () { json(true); }).throws(invalidMsg);
});
it('throws with Number', function () {
assume(function () { json(0); }).throws(invalidMsg);
assume(function () { json(1); }).throws(invalidMsg);
assume(function () { json(Number.MAX_VALUE); }).throws(invalidMsg);
assume(function () { json(Number.POSITIVE_INFINITY); }).throws(invalidMsg);
});
it('throws with empty string', function () {
assume(function () { json(''); }).throws('argument format string must not be empty');
});
});
});