1 | 'use strict'
|
2 |
|
3 | const debug = require('debug')('nock.request_overrider')
|
4 | const {
|
5 | IncomingMessage,
|
6 | ClientRequest,
|
7 | request: originalHttpRequest,
|
8 | } = require('http')
|
9 | const { request: originalHttpsRequest } = require('https')
|
10 | const propagate = require('propagate')
|
11 | const common = require('./common')
|
12 | const globalEmitter = require('./global_emitter')
|
13 | const Socket = require('./socket')
|
14 | const { playbackInterceptor } = require('./playback_interceptor')
|
15 |
|
16 | function socketOnClose(req) {
|
17 | debug('socket close')
|
18 |
|
19 | if (!req.res && !req.socket._hadError) {
|
20 |
|
21 |
|
22 | req.socket._hadError = true
|
23 | const err = new Error('socket hang up')
|
24 | err.code = 'ECONNRESET'
|
25 | req.emit('error', err)
|
26 | }
|
27 | req.emit('close')
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | class InterceptedRequestRouter {
|
36 | constructor({ req, options, interceptors }) {
|
37 | this.req = req
|
38 | this.options = {
|
39 |
|
40 |
|
41 | ...options,
|
42 |
|
43 | headers: common.headersFieldNamesToLowerCase(options.headers || {}),
|
44 | }
|
45 | this.interceptors = interceptors
|
46 |
|
47 | this.socket = new Socket(options)
|
48 |
|
49 |
|
50 |
|
51 | if (options.timeout) {
|
52 | this.socket.setTimeout(options.timeout)
|
53 | }
|
54 |
|
55 | this.response = new IncomingMessage(this.socket)
|
56 | this.requestBodyBuffers = []
|
57 | this.playbackStarted = false
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | this.readyToStartPlaybackOnSocketEvent = false
|
63 |
|
64 | this.attachToReq()
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | process.nextTick(() => this.connectSocket())
|
70 | }
|
71 |
|
72 | attachToReq() {
|
73 | const { req, options } = this
|
74 |
|
75 | for (const [name, val] of Object.entries(options.headers)) {
|
76 | req.setHeader(name.toLowerCase(), val)
|
77 | }
|
78 |
|
79 | if (options.auth && !options.headers.authorization) {
|
80 | req.setHeader(
|
81 |
|
82 | 'authorization',
|
83 | `Basic ${Buffer.from(options.auth).toString('base64')}`
|
84 | )
|
85 | }
|
86 |
|
87 | req.path = options.path
|
88 | req.method = options.method
|
89 |
|
90 | req.write = (...args) => this.handleWrite(...args)
|
91 | req.end = (...args) => this.handleEnd(...args)
|
92 | req.flushHeaders = (...args) => this.handleFlushHeaders(...args)
|
93 |
|
94 |
|
95 | if (options.headers.expect === '100-continue') {
|
96 | common.setImmediate(() => {
|
97 | debug('continue')
|
98 | req.emit('continue')
|
99 | })
|
100 | }
|
101 | }
|
102 |
|
103 | connectSocket() {
|
104 | const { req, socket } = this
|
105 |
|
106 |
|
107 | if (req.destroyed || req.aborted) {
|
108 | return
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | req.socket = req.connection = socket
|
116 |
|
117 | propagate(['error', 'timeout'], socket, req)
|
118 | socket.on('close', () => socketOnClose(req))
|
119 |
|
120 | socket.connecting = false
|
121 | req.emit('socket', socket)
|
122 |
|
123 |
|
124 | socket.emit('connect')
|
125 |
|
126 |
|
127 | if (socket.authorized) {
|
128 | socket.emit('secureConnect')
|
129 | }
|
130 |
|
131 | if (this.readyToStartPlaybackOnSocketEvent) {
|
132 | this.maybeStartPlayback()
|
133 | }
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 | handleWrite(buffer, encoding, callback) {
|
139 | debug('request write')
|
140 | const { req } = this
|
141 |
|
142 | if (req.finished) {
|
143 | const err = new Error('write after end')
|
144 | err.code = 'ERR_STREAM_WRITE_AFTER_END'
|
145 | process.nextTick(() => req.emit('error', err))
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | return true
|
151 | }
|
152 |
|
153 | if (req.socket && req.socket.destroyed) {
|
154 | return false
|
155 | }
|
156 |
|
157 | if (!buffer || buffer.length === 0) {
|
158 | return true
|
159 | }
|
160 |
|
161 | if (!Buffer.isBuffer(buffer)) {
|
162 | buffer = Buffer.from(buffer, encoding)
|
163 | }
|
164 | this.requestBodyBuffers.push(buffer)
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | if (typeof callback === 'function') {
|
171 | callback()
|
172 | }
|
173 |
|
174 | common.setImmediate(function () {
|
175 | req.emit('drain')
|
176 | })
|
177 |
|
178 | return false
|
179 | }
|
180 |
|
181 | handleEnd(chunk, encoding, callback) {
|
182 | debug('request end')
|
183 | const { req } = this
|
184 |
|
185 |
|
186 | if (typeof chunk === 'function') {
|
187 | callback = chunk
|
188 | chunk = null
|
189 | } else if (typeof encoding === 'function') {
|
190 | callback = encoding
|
191 | encoding = null
|
192 | }
|
193 |
|
194 | if (typeof callback === 'function') {
|
195 | req.once('finish', callback)
|
196 | }
|
197 |
|
198 | if (chunk) {
|
199 | req.write(chunk, encoding)
|
200 | }
|
201 | req.finished = true
|
202 | this.maybeStartPlayback()
|
203 |
|
204 | return req
|
205 | }
|
206 |
|
207 | handleFlushHeaders() {
|
208 | debug('request flushHeaders')
|
209 | this.maybeStartPlayback()
|
210 | }
|
211 |
|
212 | |
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | setHostHeaderUsingInterceptor(interceptor) {
|
219 | const { req, options } = this
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | const HOST_HEADER = 'host'
|
225 | if (interceptor.__nock_filteredScope && interceptor.__nock_scopeHost) {
|
226 | options.headers[HOST_HEADER] = interceptor.__nock_scopeHost
|
227 | req.setHeader(HOST_HEADER, interceptor.__nock_scopeHost)
|
228 | } else {
|
229 |
|
230 |
|
231 | if (options.host && !req.getHeader(HOST_HEADER)) {
|
232 | let hostHeader = options.host
|
233 |
|
234 | if (options.port === 80 || options.port === 443) {
|
235 | hostHeader = hostHeader.split(':')[0]
|
236 | }
|
237 |
|
238 | req.setHeader(HOST_HEADER, hostHeader)
|
239 | }
|
240 | }
|
241 | }
|
242 |
|
243 | maybeStartPlayback() {
|
244 | const { req, socket, playbackStarted } = this
|
245 |
|
246 |
|
247 |
|
248 | if (socket.connecting) {
|
249 | this.readyToStartPlaybackOnSocketEvent = true
|
250 | return
|
251 | }
|
252 |
|
253 |
|
254 | if (!req.destroyed && !req.aborted && !playbackStarted) {
|
255 | this.startPlayback()
|
256 | }
|
257 | }
|
258 |
|
259 | startPlayback() {
|
260 | debug('ending')
|
261 | this.playbackStarted = true
|
262 |
|
263 | const { req, response, socket, options, interceptors } = this
|
264 |
|
265 | Object.assign(options, {
|
266 |
|
267 |
|
268 | path: req.path,
|
269 |
|
270 |
|
271 |
|
272 | headers: req.getHeaders(),
|
273 |
|
274 | protocol: `${options.proto}:`,
|
275 | })
|
276 |
|
277 | interceptors.forEach(interceptor => {
|
278 | this.setHostHeaderUsingInterceptor(interceptor)
|
279 | })
|
280 |
|
281 | const requestBodyBuffer = Buffer.concat(this.requestBodyBuffers)
|
282 |
|
283 |
|
284 | const requestBodyIsUtf8Representable = common.isUtf8Representable(
|
285 | requestBodyBuffer
|
286 | )
|
287 | const requestBodyString = requestBodyBuffer.toString(
|
288 | requestBodyIsUtf8Representable ? 'utf8' : 'hex'
|
289 | )
|
290 |
|
291 | const matchedInterceptor = interceptors.find(i =>
|
292 | i.match(req, options, requestBodyString)
|
293 | )
|
294 |
|
295 | if (matchedInterceptor) {
|
296 | matchedInterceptor.scope.logger(
|
297 | 'interceptor identified, starting mocking'
|
298 | )
|
299 |
|
300 | matchedInterceptor.markConsumed()
|
301 |
|
302 |
|
303 |
|
304 | req.emit('finish')
|
305 |
|
306 | playbackInterceptor({
|
307 | req,
|
308 | socket,
|
309 | options,
|
310 | requestBodyString,
|
311 | requestBodyIsUtf8Representable,
|
312 | response,
|
313 | interceptor: matchedInterceptor,
|
314 | })
|
315 | } else {
|
316 | globalEmitter.emit('no match', req, options, requestBodyString)
|
317 |
|
318 |
|
319 | const allowUnmocked = interceptors.some(
|
320 | i => i.matchHostName(options) && i.options.allowUnmocked
|
321 | )
|
322 |
|
323 | if (allowUnmocked && req instanceof ClientRequest) {
|
324 | const newReq =
|
325 | options.proto === 'https'
|
326 | ? originalHttpsRequest(options)
|
327 | : originalHttpRequest(options)
|
328 |
|
329 | propagate(newReq, req)
|
330 |
|
331 | newReq.end(requestBodyBuffer)
|
332 | } else {
|
333 | const reqStr = common.stringifyRequest(options, requestBodyString)
|
334 | const err = new Error(`Nock: No match for request ${reqStr}`)
|
335 | err.code = 'ERR_NOCK_NO_MATCH'
|
336 | err.statusCode = err.status = 404
|
337 | req.destroy(err)
|
338 | }
|
339 | }
|
340 | }
|
341 | }
|
342 |
|
343 | module.exports = { InterceptedRequestRouter }
|