forked from koajs/logger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
155 lines (141 loc) · 3.95 KB
/
test.js
File metadata and controls
155 lines (141 loc) · 3.95 KB
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
/**
* test cases
*/
// test tools
var chai = require('chai');
var sinon = require('sinon');
var sc = require('sinon-chai');
var request = require('supertest');
chai.use(sc);
var expect = chai.expect;
// test subjects
var chalk = require('chalk');
var app = require('./test-server');
var log, sandbox;
describe('koa-logger', function() {
beforeEach(function() {
sandbox = sinon.sandbox.create();
log = sandbox.spy(console, 'log');
});
afterEach(function() {
sandbox.restore();
});
it('should log a request', function(done) {
request(app.listen()).get('/200').expect(200, 'hello world', function() {
expect(log).to.have.been.called;
done();
});
});
it('should log a request with correct method and url', function(done) {
request(app.listen()).head('/200').expect(200, function() {
expect(log).to.have.been.calledWith(' ' + chalk.gray('<--')
+ ' ' + chalk.bold('%s')
+ ' ' + chalk.gray('%s'),
'HEAD',
'/200');
done();
});
});
it('should log a response', function(done) {
request(app.listen()).get('/200').expect(200, function() {
expect(log).to.have.been.calledTwice;
done();
});
});
it('should log a 200 response', function(done) {
request(app.listen()).get('/200').expect(200, function() {
expect(log).to.have.been.calledWith(' ' + chalk.gray('-->')
+ ' ' + chalk.bold('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.green('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.gray('%s'),
'GET',
'/200',
200,
sinon.match.any,
'11b');
done();
});
});
it('should log a 301 response', function(done) {
request(app.listen()).get('/301').expect(301, function() {
expect(log).to.have.been.calledWith(' ' + chalk.gray('-->')
+ ' ' + chalk.bold('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.cyan('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.gray('%s'),
'GET',
'/301',
301,
sinon.match.any,
'-');
done();
});
});
it('should log a 304 response', function(done) {
request(app.listen()).get('/304').expect(304, function() {
expect(log).to.have.been.calledWith(' ' + chalk.gray('-->')
+ ' ' + chalk.bold('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.cyan('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.gray('%s'),
'GET',
'/304',
304,
sinon.match.any,
'');
done();
});
});
it('should log a 404 response', function(done) {
request(app.listen()).get('/404').expect(404, function() {
expect(log).to.have.been.calledWith(' ' + chalk.gray('-->')
+ ' ' + chalk.bold('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.yellow('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.gray('%s'),
'GET',
'/404',
404,
sinon.match.any,
'9b');
done();
});
});
it('should log a 500 response', function(done) {
request(app.listen()).get('/500').expect(500, function() {
expect(log).to.have.been.calledWith(' ' + chalk.gray('-->')
+ ' ' + chalk.bold('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.red('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.gray('%s'),
'GET',
'/500',
500,
sinon.match.any,
'12b');
done();
});
});
it('should log middleware error', function(done) {
request(app.listen()).get('/error').expect(500, function() {
expect(log).to.have.been.calledWith(' ' + chalk.red('xxx')
+ ' ' + chalk.bold('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.red('%s')
+ ' ' + chalk.gray('%s')
+ ' ' + chalk.gray('%s'),
'GET',
'/error',
500,
sinon.match.any,
'-');
done();
});
});
});