1- import { MockConfig } from './types' ;
1+ import { MockConfig , RequestInfo , SniffConfig } from './types' ;
22import { Proxy as HttpProxy , IContext , IProxyOptions } from 'http-mitm-proxy' ;
33import { v4 as uuid } from 'uuid' ;
44import {
@@ -15,6 +15,8 @@ import {
1515import ResponseDecoder from './response-decoder' ;
1616import { Mock } from './mock' ;
1717import { RequestInterceptor } from './interceptor' ;
18+ import { ApiSniffer } from './api-sniffer' ;
19+ import _ from 'lodash' ;
1820
1921export interface ProxyOptions {
2022 deviceUDID : string ;
@@ -27,6 +29,8 @@ export interface ProxyOptions {
2729export class Proxy {
2830 private _started = false ;
2931 private readonly mocks = new Map < string , Mock > ( ) ;
32+ private readonly sniffers = new Map < string , ApiSniffer > ( ) ;
33+
3034 private readonly httpProxy : HttpProxy ;
3135
3236 public isStarted ( ) : boolean {
@@ -66,9 +70,9 @@ export class Proxy {
6670
6771 this . httpProxy . onRequest (
6872 RequestInterceptor ( ( requestData : any ) => {
69- // console.log('****** REQUEST **********');
70- // console.log(JSON.stringify( requestData, null, 2) );
71- // console.log('****************************');
73+ for ( const sniffer of this . sniffers . values ( ) ) {
74+ sniffer . onApiRequest ( requestData ) ;
75+ }
7276 } )
7377 ) ;
7478 this . httpProxy . onRequest ( this . handleMockApiRequest . bind ( this ) ) ;
@@ -97,6 +101,33 @@ export class Proxy {
97101 this . mocks . delete ( id ) ;
98102 }
99103
104+ public enableMock ( id : string ) : void {
105+ this . mocks . get ( id ) ?. setEnableStatus ( true ) ;
106+ }
107+
108+ public disableMock ( id : string ) : void {
109+ this . mocks . get ( id ) ?. setEnableStatus ( false ) ;
110+ }
111+
112+ public addSniffer ( sniffConfg : SniffConfig ) : string {
113+ const id = uuid ( ) ;
114+ this . sniffers . set ( id , new ApiSniffer ( id , sniffConfg ) ) ;
115+ return id ;
116+ }
117+
118+ public removeSniffer ( id ?: string ) : RequestInfo [ ] {
119+ const _sniffers = [ ...this . sniffers . values ( ) ] ;
120+ if ( id && ! _ . isNil ( this . sniffers . get ( id ) ) ) {
121+ _sniffers . push ( this . sniffers . get ( id ) ! ) ;
122+ }
123+ const apiRequests = _sniffers . reduce ( ( acc , sniffer ) => {
124+ acc . push ( ...sniffer . getRequests ( ) ) ;
125+ return acc ;
126+ } , [ ] as RequestInfo [ ] ) ;
127+ _sniffers . forEach ( ( sniffer ) => this . sniffers . delete ( sniffer . getId ( ) ) ) ;
128+ return apiRequests ;
129+ }
130+
100131 private async handleMockApiRequest ( ctx : IContext , next : ( ) => void ) : Promise < void > {
101132 const matchedMocks = await this . findMatchingMocks ( ctx ) ;
102133 if ( matchedMocks . length ) {
@@ -122,7 +153,11 @@ export class Proxy {
122153 const matchedMocks : MockConfig [ ] = [ ] ;
123154 for ( const mock of this . mocks . values ( ) ) {
124155 const config = mock . getConfig ( ) ;
125- if ( doesUrlMatch ( config . url , url ) && doesHttpMethodMatch ( request , config . method ) ) {
156+ if (
157+ mock . isEnabled ( ) &&
158+ doesUrlMatch ( config . url , url ) &&
159+ doesHttpMethodMatch ( request , config . method )
160+ ) {
126161 matchedMocks . push ( config ) ;
127162 }
128163 }
0 commit comments