-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathrequest.js
More file actions
executable file
·168 lines (117 loc) · 4.46 KB
/
request.js
File metadata and controls
executable file
·168 lines (117 loc) · 4.46 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
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
const Events = require('events');
const Stream = require('stream');
const Url = require('url');
const Symbols = require('./symbols');
const internals = {};
exports = module.exports = internals.Request = class extends Stream.Readable {
constructor(options) {
super({
emitClose: !!(options.simulate?.close),
autoDestroy: true // This is the default in node 14+
});
// options: method, url, payload, headers, remoteAddress
let url = options.url;
if (typeof url === 'object') {
url = Url.format(url);
}
const uri = Url.parse(url);
this.url = uri.path;
this.httpVersion = '1.1';
this.method = (options.method ? options.method.toUpperCase() : 'GET');
this.headers = {};
const headers = options.headers ?? {};
const fields = Object.keys(headers);
fields.forEach((field) => {
this.headers[field.toLowerCase()] = headers[field];
});
this.headers['user-agent'] = this.headers['user-agent'] ?? 'shot';
const hostHeaderFromUri = function () {
if (uri.port) {
return uri.host;
}
if (uri.protocol) {
return uri.hostname + (uri.protocol === 'https:' ? ':443' : ':80');
}
return null;
};
this.headers.host = this.headers.host ?? hostHeaderFromUri() ?? options.authority ?? 'localhost:80';
// NOTE connection is deprecated in favor of socket as of node v13
this.socket = this.connection = new internals.MockSocket(options);
let payload = options.payload ?? null;
if (payload &&
typeof payload !== 'string' &&
!(payload instanceof Stream) &&
!Buffer.isBuffer(payload)) {
payload = JSON.stringify(payload);
this.headers['content-type'] = this.headers['content-type'] || 'application/json';
}
// Set the content-length for the corresponding payload if none set
if (payload &&
!(payload instanceof Stream) &&
!this.headers.hasOwnProperty('content-length')) {
this.headers['content-length'] = (Buffer.isBuffer(payload) ? payload.length : Buffer.byteLength(payload)).toString();
}
// Use _shot namespace to avoid collision with Node
this._shot = {
payload,
isDone: false,
simulate: options.simulate ?? {}
};
return this;
}
prepare(next) {
if (this._shot.payload instanceof Stream === false) {
return next();
}
const chunks = [];
this._shot.payload.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
this._shot.payload.on('end', () => {
const payload = Buffer.concat(chunks);
this.headers['content-length'] = this.headers['content-length'] || payload.length;
this._shot.payload = payload;
return next();
});
}
_read(size) {
setImmediate(() => {
if (this._shot.isDone) {
/* $lab:coverage:off$ */
if (this._shot.simulate.end !== false) { // 'end' defaults to true
this.push(null);
}
/* $lab:coverage:on$ */
return;
}
this._shot.isDone = true;
if (this._shot.payload) {
if (this._shot.simulate.split) {
this.push(this._shot.payload.slice(0, 1));
this.push(this._shot.payload.slice(1));
}
else {
this.push(this._shot.payload);
}
}
if (this._shot.simulate.error) {
this.destroy(new Error('Simulated'));
}
else if (this._shot.simulate.end !== false) { // 'end' defaults to true
this.push(null);
}
else if (this._shot.simulate.close) { // manually close (out of spec)
this.emit('close');
}
});
}
};
internals.Request.prototype[Symbols.injection] = true;
internals.MockSocket = class MockSocket extends Events.EventEmitter {
constructor({ remoteAddress }) {
super();
this.remoteAddress = remoteAddress ?? '127.0.0.1';
}
// Net.Socket APIs used by hapi
end() {}
setTimeout() {}
};