|
1 | 1 | import { MockConfig } from './types'; |
2 | | -import { Proxy as HttpProxy, IContext, IProxyOptions, ErrorCallback } from 'http-mitm-proxy'; |
| 2 | +import { Proxy as HttpProxy, IContext, IProxyOptions } from 'http-mitm-proxy'; |
3 | 3 | import { v4 as uuid } from 'uuid'; |
4 | | -import http from 'http'; |
5 | 4 | import { |
6 | 5 | addDefaultMocks, |
7 | 6 | compileMockConfig, |
8 | | - constructURLFromRequest, |
9 | | - matchHttpMethod, |
10 | | - matchUrl, |
11 | | - updateRequestBody, |
12 | | - updateRequestHeaders, |
13 | | - updateRequestUrl, |
14 | | - updateResponseBody, |
| 7 | + constructURLFromHttpRequest, |
| 8 | + doesHttpMethodMatch, |
| 9 | + doesUrlMatch, |
| 10 | + modifyRequestBody, |
| 11 | + modifyRequestHeaders, |
| 12 | + modifyRequestUrl, |
| 13 | + modifyResponseBody, |
15 | 14 | } from './utils/proxy'; |
16 | 15 | import ResponseDecoder from './response-decoder'; |
17 | 16 | import { Mock } from './mock'; |
18 | 17 |
|
19 | | -export type ProxyOptions = { |
| 18 | +export interface ProxyOptions { |
20 | 19 | deviceUDID: string; |
21 | 20 | sessionId: string; |
22 | 21 | certificatePath: string; |
23 | 22 | port: number; |
24 | 23 | ip: string; |
25 | | -}; |
| 24 | +} |
26 | 25 |
|
27 | 26 | export class Proxy { |
28 | | - private started: boolean = false; |
29 | | - private mocks: Map<string, Mock> = new Map(); |
30 | | - private httpProxy!: HttpProxy; |
| 27 | + private _started = false; |
| 28 | + private readonly mocks = new Map<string, Mock>(); |
| 29 | + private readonly httpProxy: HttpProxy; |
| 30 | + |
| 31 | + public isStarted(): boolean { |
| 32 | + return this._started; |
| 33 | + } |
31 | 34 |
|
32 | | - constructor(private options: ProxyOptions) { |
| 35 | + constructor(private readonly options: ProxyOptions) { |
33 | 36 | this.httpProxy = new HttpProxy(); |
34 | 37 | addDefaultMocks(this); |
35 | 38 | } |
36 | 39 |
|
37 | | - public getPort() { |
| 40 | + public get port(): number { |
38 | 41 | return this.options.port; |
39 | 42 | } |
40 | 43 |
|
41 | | - public getIp() { |
| 44 | + public get ip(): string { |
42 | 45 | return this.options.ip; |
43 | 46 | } |
44 | 47 |
|
45 | | - public getDeviceUDID() { |
| 48 | + public get deviceUDID(): string { |
46 | 49 | return this.options.deviceUDID; |
47 | 50 | } |
48 | 51 |
|
49 | | - public getCertificatePath() { |
| 52 | + public get certificatePath(): string { |
50 | 53 | return this.options.certificatePath; |
51 | 54 | } |
52 | 55 |
|
53 | | - public addMock(mockConfig: MockConfig) { |
54 | | - const id = uuid(); |
55 | | - this.mocks.set(id, new Mock(id, mockConfig)); |
56 | | - return id; |
57 | | - } |
| 56 | + public async start(): Promise<boolean> { |
| 57 | + if (this._started) return true; |
58 | 58 |
|
59 | | - public removeMock(id: string) { |
60 | | - this.mocks.delete(id); |
61 | | - } |
62 | | - |
63 | | - public isStarted() { |
64 | | - return this.started; |
65 | | - } |
66 | | - |
67 | | - public async start() { |
68 | | - if (this.isStarted()) { |
69 | | - return this.isStarted(); |
70 | | - } |
71 | 59 | const proxyOptions: IProxyOptions = { |
72 | | - port: this.options.port, |
73 | | - sslCaDir: this.options.certificatePath, |
74 | | - host: '::', |
| 60 | + port: this.port, |
| 61 | + sslCaDir: this.certificatePath, |
| 62 | + host: '::', // IPv6 any |
| 63 | + forceSNI: true, |
75 | 64 | }; |
76 | 65 |
|
77 | | - this.httpProxy.onRequest(this._onMockApiRequest.bind(this)); |
| 66 | + this.httpProxy.onRequest(this.handleMockApiRequest.bind(this)); |
78 | 67 |
|
79 | 68 | await new Promise((resolve) => { |
80 | | - this.httpProxy.listen({ ...proxyOptions, forceSNI: true }, () => { |
81 | | - this.started = true; |
| 69 | + this.httpProxy.listen(proxyOptions, () => { |
| 70 | + this._started = true; |
82 | 71 | resolve(true); |
83 | 72 | }); |
84 | 73 | }); |
| 74 | + |
| 75 | + return true; |
85 | 76 | } |
86 | 77 |
|
87 | | - public async stop() { |
| 78 | + public async stop(): Promise<void> { |
88 | 79 | this.httpProxy.close(); |
89 | 80 | } |
90 | 81 |
|
91 | | - private async _onMockApiRequest(ctx: IContext, next: ErrorCallback) { |
92 | | - const matchedMocks = await this.getMatchingMock(ctx); |
| 82 | + public addMock(mockConfig: MockConfig): string { |
| 83 | + const id = uuid(); |
| 84 | + this.mocks.set(id, new Mock(id, mockConfig)); |
| 85 | + return id; |
| 86 | + } |
| 87 | + |
| 88 | + public removeMock(id: string): void { |
| 89 | + this.mocks.delete(id); |
| 90 | + } |
| 91 | + |
| 92 | + private async handleMockApiRequest(ctx: IContext, next: () => void): Promise<void> { |
| 93 | + const matchedMocks = await this.findMatchingMocks(ctx); |
93 | 94 | if (matchedMocks.length) { |
94 | 95 | const compiledMock = compileMockConfig(matchedMocks); |
95 | | - this.performMock(ctx, compiledMock, next); |
| 96 | + this.performMockResponse(ctx, compiledMock, next); |
96 | 97 | } else { |
97 | 98 | next(); |
98 | 99 | } |
99 | 100 | } |
100 | 101 |
|
101 | | - private async getMatchingMock(ctx: IContext) { |
102 | | - let request: http.IncomingMessage = ctx.clientToProxyRequest; |
103 | | - if (!request.headers?.host) { |
| 102 | + private async findMatchingMocks(ctx: IContext): Promise<MockConfig[]> { |
| 103 | + const request = ctx.clientToProxyRequest; |
| 104 | + if (!request.headers?.host || !request.url) { |
104 | 105 | return []; |
105 | 106 | } |
106 | | - const url = constructURLFromRequest({ |
107 | | - host: request.headers.host!, |
108 | | - path: request.url!, |
| 107 | + |
| 108 | + const url = constructURLFromHttpRequest({ |
| 109 | + host: request.headers.host, |
| 110 | + path: request.url, |
109 | 111 | protocol: ctx.isSSL ? 'https://' : 'http://', |
110 | 112 | }).toString(); |
111 | 113 |
|
112 | | - const matchedMocks = []; |
113 | | - for (let [id, mock] of this.mocks.entries()) { |
| 114 | + const matchedMocks: MockConfig[] = []; |
| 115 | + for (const mock of this.mocks.values()) { |
114 | 116 | const config = mock.getConfig(); |
115 | | - if (matchUrl(config.url, url) && matchHttpMethod(request, config.method)) { |
| 117 | + if (doesUrlMatch(config.url, url) && doesHttpMethodMatch(request, config.method)) { |
116 | 118 | matchedMocks.push(config); |
117 | 119 | } |
118 | 120 | } |
119 | 121 |
|
120 | 122 | return matchedMocks; |
121 | 123 | } |
122 | 124 |
|
123 | | - private performMock(ctx: IContext, mockConfig: MockConfig, callback: ErrorCallback) { |
| 125 | + private performMockResponse(ctx: IContext, mockConfig: MockConfig, next: () => void): void { |
124 | 126 | ctx.use(ResponseDecoder); |
125 | 127 |
|
126 | | - this._updateClientRequest(ctx, mockConfig); |
127 | | - this._updateClientResponse(ctx, mockConfig, callback); |
| 128 | + this.modifyClientRequest(ctx, mockConfig); |
| 129 | + this.modifyClientResponse(ctx, mockConfig, next); |
128 | 130 | } |
129 | 131 |
|
130 | | - private _updateClientRequest(ctx: IContext, mockConfig: MockConfig) { |
131 | | - updateRequestUrl(ctx, mockConfig); |
132 | | - updateRequestHeaders(ctx, mockConfig); |
133 | | - updateRequestBody(ctx, mockConfig); |
| 132 | + private modifyClientRequest(ctx: IContext, mockConfig: MockConfig): void { |
| 133 | + modifyRequestUrl(ctx, mockConfig); |
| 134 | + modifyRequestHeaders(ctx, mockConfig); |
| 135 | + modifyRequestBody(ctx, mockConfig); |
134 | 136 | } |
135 | 137 |
|
136 | | - private _updateClientResponse(ctx: IContext, mockConfig: MockConfig, next: ErrorCallback) { |
| 138 | + private modifyClientResponse(ctx: IContext, mockConfig: MockConfig, next: () => void): void { |
137 | 139 | if (mockConfig.statusCode && mockConfig.responseBody) { |
138 | 140 | ctx.proxyToClientResponse.writeHead(mockConfig.statusCode); |
139 | 141 | ctx.proxyToClientResponse.end(mockConfig.responseBody); |
140 | | - return; |
| 142 | + } else { |
| 143 | + modifyResponseBody(ctx, mockConfig); |
| 144 | + next(); |
141 | 145 | } |
142 | | - |
143 | | - updateResponseBody(ctx, mockConfig); |
144 | | - next(); |
145 | 146 | } |
146 | 147 | } |
0 commit comments