@@ -25,62 +25,40 @@ export function constructURLFromRequest(request: { protocol: string; path: strin
2525}
2626
2727export function updateRequestUrl ( ctx : IContext , apiMock : ApiMock ) {
28- if ( ! apiMock . updateUrl ) {
28+ if ( ! apiMock . updateUrl || ! ctx . clientToProxyRequest || ! ctx . proxyToServerRequestOptions ) {
2929 return ;
3030 }
3131
32- if ( ctx . clientToProxyRequest ?. headers . host && ctx . proxyToServerRequestOptions ?. host ) {
33- let request = ctx . clientToProxyRequest ;
34- const originalUrl = constructURLFromRequest ( {
35- host : request . headers . host ! ,
36- path : request . url ! ,
37- protocol : ctx . isSSL ? 'https://' : 'http://' ,
38- } ) ;
39-
40- let matchers = _ . isArray ( apiMock . updateUrl ) ? apiMock . updateUrl : [ apiMock . updateUrl ] ;
41- let updatedUrlString = originalUrl . toString ( ) ;
42- for ( let matcher of matchers ) {
43- updatedUrlString = updatedUrlString . replace (
44- parseRegex ( matcher . pattern as string ) ,
45- matcher . replaceWith
46- ) ;
47- }
32+ const { headers, url } = ctx . clientToProxyRequest ;
33+ const protocol = ctx . isSSL ? 'https://' : 'http://' ;
34+ const originalUrl = constructURLFromRequest ( {
35+ host : headers . host ! ,
36+ path : url ! ,
37+ protocol,
38+ } ) ;
4839
49- const updatedUrl = new URL ( updatedUrlString ) ;
50- ctx . proxyToServerRequestOptions . host = updatedUrl . hostname ;
51- ctx . proxyToServerRequestOptions . path = `${ updatedUrl . pathname } ${ updatedUrl . search } ` ;
52- ctx . proxyToServerRequestOptions . port = updatedUrl . port || ctx . proxyToServerRequestOptions . port ;
53- }
40+ const updateUrlMatchers = _ . castArray ( apiMock . updateUrl ) ;
41+ const updatedUrlString = updateUrlMatchers . reduce ( ( current , matcher ) => {
42+ return current . replace ( parseRegex ( matcher . pattern as string ) , matcher . replaceWith ) ;
43+ } , originalUrl . toString ( ) ) ;
44+
45+ const updatedUrl = new URL ( updatedUrlString ) ;
46+ ctx . proxyToServerRequestOptions . host = updatedUrl . hostname ;
47+ ctx . proxyToServerRequestOptions . path = `${ updatedUrl . pathname } ${ updatedUrl . search } ` ;
48+ ctx . proxyToServerRequestOptions . port = updatedUrl . port || ctx . proxyToServerRequestOptions . port ;
5449}
5550
5651export function updateRequestHeaders ( ctx : IContext , apiMock : ApiMock ) {
57- if ( ! apiMock . headers ) {
52+ if ( ! apiMock . headers || ! ctx . proxyToServerRequestOptions ) {
5853 return ;
5954 }
6055
61- let headersToBeAdded : Record < string , string > = { } ;
62- let headersToBeDeleted = [ ] ;
63-
64- if ( apiMock . headers . hasOwnProperty ( 'add' ) || apiMock . headers . hasOwnProperty ( 'remove' ) ) {
65- if ( ! _ . isNil ( apiMock . headers . add ) && typeof apiMock . headers . add === 'object' ) {
66- headersToBeAdded = apiMock . headers . add || ( { } as any ) ;
67- }
68-
69- if ( ! _ . isNil ( apiMock . headers . remove ) && _ . isArray ( apiMock . headers . remove ) ) {
70- headersToBeDeleted = apiMock . headers . remove ;
71- }
72- } else {
73- headersToBeAdded = apiMock . headers as any ;
56+ const { headers } = ctx . proxyToServerRequestOptions ;
57+ if ( apiMock . headers . add ) {
58+ Object . assign ( headers , apiMock . headers . add ) ;
7459 }
75-
76- if ( ctx . proxyToServerRequestOptions ) {
77- Object . keys ( headersToBeAdded ) . forEach ( ( key ) => {
78- ctx . proxyToServerRequestOptions ! . headers [ key ] = headersToBeAdded [ key ] ;
79- } ) ;
80-
81- headersToBeDeleted . forEach ( ( header ) => {
82- delete ctx . proxyToServerRequestOptions ! . headers [ header ] ;
83- } ) ;
60+ if ( apiMock . headers . remove && Array . isArray ( apiMock . headers . remove ) ) {
61+ apiMock . headers . remove . forEach ( ( header ) => delete headers [ header ] ) ;
8462 }
8563}
8664
@@ -89,14 +67,15 @@ export function updateRequestBody(ctx: IContext, apiMock: ApiMock) {
8967 return ;
9068 }
9169
92- const requestBodyChunks : Array < Buffer > = [ ] ;
70+ const requestBodyChunks : Buffer [ ] = [ ] ;
9371 ctx . onRequestData ( ( ctx : IContext , chunk : Buffer , callback : OnRequestDataCallback ) => {
9472 requestBodyChunks . push ( chunk ) ;
9573 callback ( null , undefined ) ;
9674 } ) ;
9775 ctx . onRequestEnd ( ( ctx : IContext , callback : OnRequestDataCallback ) => {
9876 const originalBody = Buffer . concat ( requestBodyChunks ) . toString ( 'utf-8' ) ;
99- ctx . proxyToServerRequest ?. write ( apiMock . postBody || originalBody ) ;
77+ const body = apiMock . postBody || originalBody ;
78+ ctx . proxyToServerRequest ?. write ( body ) ;
10079 callback ( ) ;
10180 } ) ;
10281}
@@ -228,24 +207,11 @@ export function compileApiMock(mocks: Array<ApiMock>) {
228207}
229208
230209function parseMockHeader ( header ?: HttpHeader ) {
231- let add = { } ,
232- remove : string [ ] = [ ] ;
233-
234- if ( header ) {
235- const parsedHeader = parseJson ( header ) ;
236- if ( typeof parsedHeader === 'object' ) {
237- if ( parsedHeader && parsedHeader . add ) {
238- add = parsedHeader . add ;
239- } else if ( parsedHeader && parsedHeader . remove ) {
240- remove = parsedHeader . remove ;
241- } else {
242- add = parsedHeader ;
243- }
244- }
245- }
210+ if ( ! header ) return { add : { } , remove : [ ] } ;
211+ const parsedHeader = typeof header === 'string' ? parseJson ( header ) : header ;
246212
247213 return {
248- add,
249- remove,
214+ add : parsedHeader ?. add ?? { } ,
215+ remove : parsedHeader ?. remove ?? [ ] ,
250216 } ;
251217}
0 commit comments