/
middleware.js
187 lines (137 loc) · 4.83 KB
/
middleware.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
"use strict";
var EventEmitter = require("events").EventEmitter,
metricsd = require("../lib/metricsd"),
expect = require("chai").expect;
describe("metrics.middleware", function() {
var metrics;
var _send;
var req;
var res;
var next;
beforeEach(function() {
metrics = metricsd();
// allow metrics._send() to be mocked
_send = metrics._send;
req = {};
res = new EventEmitter();
next = function() {};
});
afterEach(function() {
metrics._send = _send;
_send = undefined;
req = undefined;
res = undefined;
next = undefined;
});
describe(".Measure", function() {
var event;
var metric;
beforeEach(function() {
event = "finished";
metric = "requestTime";
});
afterEach(function() {
event = undefined;
metric = undefined;
});
it("should return Express-compatible middleware", function(done) {
var fn = metrics.middleware.Measure(event, metric);
fn(req, res, done);
});
it("should register an event handler for the specified event name", function(done) {
var fn = metrics.middleware.Measure(event, metric);
fn(req, res, function() {
expect(res.listeners(event)).to.have.length(1);
done();
});
});
it("should update the named histogram when the specified event fires", function(done) {
var fn = metrics.middleware.Measure(event, metric);
fn(req, res, next);
metrics._send = function(str) {
expect(str).to.match(new RegExp(metric + ":\\d+\\|h"));
done();
};
res.emit(event);
});
it("should treat the 'name' parameter as a name lookup if it's a function", function(done) {
var fn = metrics.middleware.Measure(event, function(req, res, eventName, elapsedTime) {
expect(eventName).to.equal(event);
return metric;
});
fn(req, res, next);
metrics._send = function(str) {
expect(str).to.match(new RegExp(metric + ":\\d+\\|h"));
done();
};
res.emit(event);
});
it("should handle lookups returning multiple names", function(done) {
var metricNames = [metric, "test.requestTime"];
var fn = metrics.middleware.Measure(event, function(req, res, eventName, elapsedTime) {
return metricNames;
});
fn(req, res, next);
var sent = 0;
metrics._send = function(str) {
expect(str).to.match(new RegExp(metricNames[sent++] + ":\\d+\\|h"));
if (sent === 2) {
done();
}
};
res.emit(event);
});
});
describe(".TimeFilter", function() {
var metric;
beforeEach(function() {
metric = "providedMiddleware";
});
afterEach(function() {
metric = undefined;
});
it("should be Express-compatible middleware", function(done) {
var fn = metrics.middleware.TimeFilter(metric, function(req, res, next) {
next();
});
fn(req, res, function() {
done();
});
});
it("should call the provided filter", function(done) {
var fn = metrics.middleware.TimeFilter(metric, function(req, res, next) {
done();
});
fn(req, res, next);
});
it("should measure the time that the provided filter takes", function(done) {
var fn = metrics.middleware.TimeFilter(metric, function(req, res, next) {
next();
});
metrics._send = function(str) {
expect(str).to.match(new RegExp(metric + ":\\d+\\|h"));
done();
};
fn(req, res, next);
});
});
describe(".Timer", function() {
it("should be Express-compatible middleware", function(done) {
metrics.middleware.Timer(req, res, done);
});
it("should add a 'timer' property to the provided request", function(done) {
metrics.middleware.Timer(req, res, function() {
expect(req.timer).to.be.an.instanceof(metrics.Timer);
done();
});
});
it("should leave an existing 'timer' property alone", function(done) {
var timer = {};
req.timer = timer;
metrics.middleware.Timer(req, res, function() {
expect(req.timer).to.equal(timer);
done();
});
});
});
});