|
| 1 | +import { IncomingMessage } from 'http'; |
1 | 2 | /* eslint-disable @typescript-eslint/ban-types */ |
2 | 3 | import http from 'http'; |
3 | 4 | import { Socket } from 'net'; |
4 | 5 | import { inherits } from 'util'; |
5 | 6 | import Bypass from '../../common/bypass'; |
6 | | -import simpleRequest from '../../common/request'; |
| 7 | +import simpleRequest, { parseResponseBody } from '../../common/request'; |
7 | 8 | import { getQuery } from '../../common/utils'; |
8 | 9 | import { HTTPStatusCodes } from '../../config'; |
9 | 10 | import MockItem from '../../mocker/mock-item'; |
10 | 11 | import Mocker from '../../mocker/mocker'; |
11 | | -import { ClientRequestOptions, ClientRequestType, RequestInfo } from '../../types'; |
| 12 | +import { ClientRequestOptions, ClientRequestType, OriginalResponse, RequestInfo } from '../../types'; |
12 | 13 | import { RemoteResponse } from './../../types'; |
13 | 14 |
|
14 | 15 | /** |
@@ -93,7 +94,12 @@ function ClientRequest( |
93 | 94 | return this; |
94 | 95 | }; |
95 | 96 |
|
96 | | - this.setOriginalRequestInfo = (nativeReqestMethod: Function, nativeRequestArgs: unknown[]) => { |
| 97 | + this.setOriginalRequestInfo = ( |
| 98 | + getOrRequest: 'get' | 'request', |
| 99 | + nativeReqestMethod: Function, |
| 100 | + nativeRequestArgs: unknown[] |
| 101 | + ) => { |
| 102 | + this.nativeReqestName = getOrRequest; // get or request |
97 | 103 | this.nativeReqestMethod = nativeReqestMethod; |
98 | 104 | this.nativeRequestArgs = nativeRequestArgs; |
99 | 105 | }; |
@@ -205,6 +211,12 @@ function ClientRequest( |
205 | 211 | body: method === 'GET' ? undefined : this.bufferToString(this.requestBody) |
206 | 212 | }; |
207 | 213 |
|
| 214 | + requestInfo.doOriginalCall = async (): Promise<OriginalResponse> => { |
| 215 | + const res = await this.getOriginalResponse(); |
| 216 | + requestInfo.doOriginalCall = undefined; |
| 217 | + return res; |
| 218 | + }; |
| 219 | + |
208 | 220 | let remoteResponse: RemoteResponse | null = null; |
209 | 221 | const remoteInfo = mockItem?.getRemoteInfo(url); |
210 | 222 | if (remoteInfo) { |
@@ -310,6 +322,46 @@ function ClientRequest( |
310 | 322 | return this.nativeInstance; |
311 | 323 | }; |
312 | 324 |
|
| 325 | + this.getOriginalResponse = (): Promise<OriginalResponse> => { |
| 326 | + const callback = this.nativeRequestArgs[this.nativeRequestArgs.length - 1]; |
| 327 | + |
| 328 | + const defaultResponse = { |
| 329 | + status: null, |
| 330 | + headers: {}, |
| 331 | + responseText: null, |
| 332 | + responseJson: null, |
| 333 | + responseBuffer: null, |
| 334 | + responseBlob: null, |
| 335 | + error: null, |
| 336 | + }; |
| 337 | + return new Promise((resolve) => { |
| 338 | + const newCallback = (res: IncomingMessage) => { |
| 339 | + parseResponseBody(res).then(data => { |
| 340 | + resolve(data); |
| 341 | + }).catch(err => { |
| 342 | + resolve({ ...defaultResponse, error: err }); |
| 343 | + }); |
| 344 | + if (typeof callback === 'function') { |
| 345 | + callback(res); |
| 346 | + } |
| 347 | + }; |
| 348 | + const callbackIndex = typeof callback === 'function' |
| 349 | + ? this.nativeRequestArgs.length - 1 |
| 350 | + : this.nativeRequestArgs.length; |
| 351 | + |
| 352 | + this.nativeRequestArgs[callbackIndex] = newCallback; |
| 353 | + |
| 354 | + // do original call |
| 355 | + const req = this.nativeReqestMethod(...this.nativeRequestArgs); |
| 356 | + req.on('error', (err: Error) => { |
| 357 | + resolve({ ...defaultResponse, error: err }); |
| 358 | + }); |
| 359 | + if (this.nativeReqestName = 'get') { |
| 360 | + req.end(); |
| 361 | + } |
| 362 | + }); |
| 363 | + }; |
| 364 | + |
313 | 365 | /** |
314 | 366 | * https://nodejs.org/api/http.html#http_request_end_data_encodingcallback |
315 | 367 | * |
|
0 commit comments