-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
83 lines (65 loc) · 2.02 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
// Mocks http.ServerResponse
module.exports = MockServerResponse;
var Transform = require('stream').Transform,
util = require('util'),
STATUS_CODES = require('http').STATUS_CODES;
function MockServerResponse(finish) {
Transform.call(this);
this.statusCode = 200;
this.statusMessage = STATUS_CODES[this.statusCode];
this._header = this._headers = {};
this._onEnd = finish;
this._responseData = []
this.finished = false;
}
util.inherits(MockServerResponse, Transform);
MockServerResponse.prototype._transform = function(chunk, encoding, next) {
this.push(chunk);
this._responseData.push(chunk)
next();
};
MockServerResponse.prototype.setHeader = function(name, value) {
this._headers[name.toLowerCase()] = value;
};
MockServerResponse.prototype.getHeader = function(name) {
return this._headers[name.toLowerCase()];
};
MockServerResponse.prototype.getHeaders = function() {
return this._headers;
};
MockServerResponse.prototype.removeHeader = function(name) {
delete this._headers[name.toLowerCase()];
};
MockServerResponse.prototype.writeHead = function(statusCode, reason, headers) {
if (arguments.length == 2 && typeof arguments[1] !== 'string') {
headers = reason;
reason = undefined;
}
this.statusCode = statusCode;
this.statusMessage = reason || STATUS_CODES[statusCode] || 'unknown';
if (headers) {
for (var name in headers) {
this.setHeader(name, headers[name]);
}
}
};
MockServerResponse.prototype._getString = function() {
return Buffer.concat(this._responseData).toString();
};
MockServerResponse.prototype._getJSON = function() {
return JSON.parse(this._getString());
};
MockServerResponse.prototype.end = function() {
Transform.prototype.end.apply(this, arguments);
this.finished = true;
if (this._onEnd !== undefined) {
this._onEnd.call(this);
}
}
/* Not implemented:
MockServerResponse.prototype.writeContinue()
MockServerResponse.prototype.setTimeout(msecs, callback)
MockServerResponse.prototype.headersSent
MockServerResponse.prototype.sendDate
MockServerResponse.prototype.addTrailers(headers)
*/