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