-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (158 loc) · 4.52 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
var url = require('url');
var util = require('util');
var http = require('http');
var https = require('https');
var stream = require('stream');
var pathToRegexp = require('path-to-regexp');
var originHttpRequest = http.request;
var originHttpsRequest = https.request;
var mockconfigSource = {};
var latestRequestURL = null;
var latestRequestMethod = null;
util.inherits(OutgoingMessage, stream.Readable);
function OutgoingMessage () {
if (!(this instanceof OutgoingMessage))
return new OutgoingMessage();
stream.Readable.call(this);
http.OutgoingMessage.call(this);
}
//
// nothing
//
function noop () {}
//
// resolves option and then generate corresponding response object
//
function resolveResponse (options, callback) {
var resp = new OutgoingMessage();
resp.statusCode = 200;
resp.headers = {};
resp._read = noop;
resp.write = noop;
if ((options.port === 80 && options.protocol === 'http')
|| (options.port === 443 && options.protocol === 'https'))
options.port = false;
if (options.auth && !(typeof options.auth === 'string')) {
var str = options.auth.user + ':' + options.auth.pass;
options.auth = str;
}
options.pathname = options.path;
var requesturl = url.format(options);
var configSource;
var configs = Object.keys(mockconfigSource).reverse();
for (var i = 0; i < configs.length; i++) {
var prefix = configs[i];
if (urlMatch(requesturl, prefix)) {
configSource = mockconfigSource[prefix];
break;
}
}
if (!configSource)
return false;
else {
latestRequestURL = requesturl;
latestRequestMethod = options.method || 'GET';
}
resp.setTimeout = function () {
// For now, this function will never be called because of in test environment,
// but TODO: support timeout in config
};
resp.end = function () {
var err = configSource.error || null;
configSource.statusCode = configSource.statusCode || configSource.status;
if (configSource.statusCode)
resp.statusCode = configSource.statusCode;
if (configSource.headers || configSource.header)
for (var key in (configSource.headers || configSource.header))
resp.headers[key.toLowerCase()] = configSource.headers[key];
var body;
if (configSource.body) {
if (!resp.headers['content-type'] ||
resp.headers['content-type'].search('application/json') === 0)
body = JSON.stringify(configSource.body);
else if (Buffer.isBuffer(configSource.body))
body = configSource.body;
else
body = configSource.body.toString();
resp.headers['content-length'] = body.length;
} else {
body = '';
}
resp.push(body);
resp.push(null);
if (typeof callback === 'function')
callback(resp);
if (err)
resp.emit('error', err);
else
resp.emit('response', resp);
};
return resp;
}
//
// create request function for http/https
//
function requestor (type) {
return function (options, callback) {
var ret;
options.protocol = type;
if (process.env.NODE_ENV === 'test'
&& (ret = resolveResponse(options, callback))) {
return ret;
} else if (type === 'https') {
delete options.protocol;
return originHttpsRequest.call(https, options, callback);
} else {
delete options.protocol;
return originHttpRequest.call(http, options, callback);
}
}
}
// config mocks
function configMock (config) {
if (config)
for (var url in config)
mockconfigSource[url] = config[url];
}
//
// check request url match the config mock url
//
function urlMatch (requestUrl, mockUrl) {
if (requestUrl === mockUrl)
return true;
var parsedRequestUrl = url.parse(requestUrl);
var parsedMockUrl = url.parse(mockUrl);
if (parsedRequestUrl.host === parsedMockUrl.host &&
parsedRequestUrl.port === parsedMockUrl.port &&
parsedRequestUrl.auth === parsedMockUrl.auth &&
parsedRequestUrl.search === parsedMockUrl.search &&
parsedRequestUrl.protocol === parsedMockUrl.protocol &&
pathToRegexp(parsedMockUrl.path, [], {}).exec(parsedRequestUrl.path)) {
return true;
}
if (requestUrl.search(mockUrl) !== -1) {
return true;
}
return false;
}
// clear configs
function clear (url) {
if (url)
delete mockconfigSource[url];
else
mockconfigSource = {};
}
// inject on http/https
http.request = requestor('http');
https.request = requestor('https');
// exports
module.exports = {
config: configMock,
clear: clear,
get last () {
return {
method: latestRequestMethod,
url: latestRequestURL
};
}
};