/
index.js
245 lines (241 loc) · 8.8 KB
/
index.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var should = require('should');
var connect = require('connect');
var request = require('request');
var JsonStatus = require('../index');
var FakeResponse = function(){
this.status = null;
this.ended = false;
this.headers = {};
this.body = '';
this.writeHead = function(status){
this.status = status;
};
this.setHeader = function(name, value){
this.headers[name] = value;
};
this.write = function(out){
this.body += out || '';
};
this.end = function(out){
this.ended = true;
this.body += out || '';
};
};
describe("JsonStatus", function(){
describe("#internalServerError", function(){
it ("sets the status to 500 when the detail is a simple object", function(done){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
var expected = {type : 500,
message : 'Internal Server Error',
detail : {}};
responder.on('error', function(data){
data.type.should.equal(expected.type);
data.message.should.equal(expected.message);
data.detail.should.equal(expected.detail);
done();
});
responder.internalServerError(expected.detail);
fakeRes.status.should.equal(expected.type);
fakeRes.ended.should.equal(true);
fakeRes.headers['Content-Type'].should.equal("application/json; charset=utf-8");
var response = JSON.parse(fakeRes.body);
response.error.should.eql(expected);
});
it ("sets the status to 500 when the detail is a 'circular' object", function(done){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
var circular = {};
circular.circular = circular; // this is circular, in case that's not obvious
var expected = {type : 500,
message : 'Internal Server Error',
detail : '{ circular: [Circular] }'};
responder.on('error', function(data){
data.type.should.equal(expected.type);
data.message.should.equal(expected.message);
data.detail.should.equal(expected.detail);
done();
});
responder.internalServerError(circular);
fakeRes.status.should.equal(expected.type);
fakeRes.ended.should.equal(true);
fakeRes.headers['Content-Type'].should.equal("application/json; charset=utf-8");
var response = JSON.parse(fakeRes.body);
response.error.should.eql(expected);
});
});
it ("emits error events in the case of errors", function(done){
var server = connect.createServer();
var responseSent = false;
var errorEventSent = false;
var middleware = JsonStatus.connectMiddleware('status', function(data){
should.exist(data.req);
should.exist(data.res);
data.type.should.equal(500);
data.message.should.equal('Internal Server Error');
data.detail.should.equal('some error!');
if (responseSent){
return done();
}
errorEventSent = true;
});
server.use(middleware);
server.use(function(req, res){
res.status.internalServerError("some error!");
});
server.listen(8082, function(){
request('http://localhost:8082/', function(err, res, body){
res.statusCode.should.equal(500);
JSON.parse(body).should.eql(
{"error":{"type":500,
"message":"Internal Server Error",
"detail":"some error!"}});
if (errorEventSent){
return done();
}
responseSent = true;
});
});
});
describe("#badRequest", function(){
it ("sets the status to 400", function(done){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
var expected = {type : 400,
message : 'Bad Request',
detail : 'bad request'};
responder.on('error', function(data){
data.type.should.equal(expected.type);
data.message.should.equal(expected.message);
data.detail.should.equal(expected.detail);
done();
});
responder.badRequest(expected.detail);
fakeRes.status.should.equal(expected.type);
fakeRes.ended.should.equal(true);
fakeRes.headers['Content-Type'].should.equal("application/json; charset=utf-8");
var response = JSON.parse(fakeRes.body);
response.error.should.eql(expected);
});
});
describe("#accepted", function(){
it ("sets the status to 202", function(){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
responder.accepted();
fakeRes.status.should.equal(202);
fakeRes.ended.should.equal(true);
});
});
describe("#noContent", function(){
it ("sets the status to 204", function(){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
responder.noContent();
fakeRes.status.should.equal(204);
fakeRes.ended.should.equal(true);
});
});
describe("#resetContent", function(){
it ("sets the status to 205", function(){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
responder.resetContent();
fakeRes.status.should.equal(205);
fakeRes.ended.should.equal(true);
});
});
describe("#movedPermanently", function(){
it ("sets the Location header and sets the status to 301", function(){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
responder.movedPermanently("SOMEURL");
fakeRes.headers.Location.should.equal("SOMEURL");
fakeRes.status.should.equal(301);
fakeRes.ended.should.equal(true);
});
});
describe("#OPTIONS", function(){
it ("sets the Allow header and sets the status to 200", function(){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
responder.OPTIONS(["GET", "POST"]);
fakeRes.headers.Allow.should.equal("GET,POST");
fakeRes.status.should.equal(200);
fakeRes.ended.should.equal(true);
JSON.parse(fakeRes.body)['allowed methods'].should.eql(["GET", "POST"]);
});
});
describe("#found", function(){
it ("sets the Location header and sets the status to 302", function(){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
responder.found("SOMEURL");
fakeRes.headers.Location.should.equal("SOMEURL");
fakeRes.status.should.equal(302);
fakeRes.ended.should.equal(true);
});
});
describe("#redirect", function(){
it ("sets the Location header and sets the status to 302", function(){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
responder.redirect("SOMEURL");
fakeRes.headers.Location.should.equal("SOMEURL");
fakeRes.status.should.equal(302);
fakeRes.ended.should.equal(true);
});
});
describe("#created", function(){
it ("sets the Location header and sets the status to 201", function(){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
responder.created("SOMEURL");
fakeRes.headers.Location.should.equal("SOMEURL");
should.not.exist(fakeRes.headers['content-type']);
// no content-type when there's no body!
fakeRes.status.should.equal(201);
fakeRes.ended.should.equal(true);
});
it ("accepts an optional data parameter", function(){
var fakeRes = new FakeResponse();
var responder = new JsonStatus({}, fakeRes);
responder.created("SOMEURL", {data: "data"});
fakeRes.headers.Location.should.equal("SOMEURL");
fakeRes.headers['content-type'].should.equal("application/json");
fakeRes.status.should.equal(201);
fakeRes.ended.should.equal(true);
JSON.parse(fakeRes.body).data.should.eql("data");
});
});
describe("connectMiddleware", function(){
it ("adds a 'status' object to the response object", function(done){
var server = connect.createServer();
server.use(JsonStatus.connectMiddleware());
server.use(function(req, res){
res.status.accepted();
});
server.listen(8081, function(){
request('http://localhost:8081/', function(err, res, body){
res.statusCode.should.equal(202);
body.should.equal('');
done();
});
});
});
it ("allows you to set the name of the 'status' object", function(done){
var server = connect.createServer();
server.use(JsonStatus.connectMiddleware("ohyeah"));
server.use(function(req, res){
res.ohyeah.accepted();
});
server.listen(8079, function(){
request('http://localhost:8079/', function(err, res, body){
res.statusCode.should.equal(202);
body.should.equal('');
done();
});
});
});
});
});