1- import http from " http" ;
2- import { Helpers } from " @secret-agent/testing" ;
3- import HttpProxyAgent from " http-proxy-agent" ;
4- import { AddressInfo } from " net" ;
5- import WebSocket from "ws" ;
6- import Url from " url" ;
7- import { createPromise } from " @secret-agent/commons/utils" ;
8- import HttpRequestHandler from " ../handlers/HttpRequestHandler" ;
9- import RequestSession from " ../handlers/RequestSession" ;
10- import MitmServer from " ../lib/MitmProxy" ;
11- import HeadersHandler from " ../handlers/HeadersHandler" ;
12- import HttpUpgradeHandler from " ../handlers/HttpUpgradeHandler" ;
13- import { parseRawHeaders } from " ../lib/Utils" ;
1+ import http from ' http' ;
2+ import { Helpers } from ' @secret-agent/testing' ;
3+ import HttpProxyAgent from ' http-proxy-agent' ;
4+ import { AddressInfo } from ' net' ;
5+ import WebSocket from 'ws' ;
6+ import Url from ' url' ;
7+ import { createPromise } from ' @secret-agent/commons/utils' ;
8+ import HttpRequestHandler from ' ../handlers/HttpRequestHandler' ;
9+ import RequestSession from ' ../handlers/RequestSession' ;
10+ import MitmServer from ' ../lib/MitmProxy' ;
11+ import HeadersHandler from ' ../handlers/HeadersHandler' ;
12+ import HttpUpgradeHandler from ' ../handlers/HttpUpgradeHandler' ;
13+ import { parseRawHeaders } from ' ../lib/Utils' ;
1414
1515const mocks = {
1616 httpRequestHandler : {
@@ -35,14 +35,16 @@ beforeEach(() => {
3535afterAll ( Helpers . afterAll ) ;
3636afterEach ( Helpers . afterEach ) ;
3737
38+ let sessionCounter = 1 ;
39+
3840describe ( 'basic MitM tests' , ( ) => {
3941 it ( 'should send request through proxy' , async ( ) => {
4042 const httpServer = await Helpers . runHttpServer ( ) ;
4143 const mitmServer = await MitmServer . start ( ) ;
4244 Helpers . needsClosing . push ( mitmServer ) ;
4345 const proxyHost = `http://localhost:${ mitmServer . port } ` ;
4446
45- const session = new RequestSession ( '1' , 'any agent' , null ) ;
47+ const session = new RequestSession ( ` ${ ( sessionCounter += 1 ) } ` , 'any agent' , null ) ;
4648
4749 const proxyCredentials = session . getProxyCredentials ( ) ;
4850 expect ( mocks . httpRequestHandler . onRequest ) . toBeCalledTimes ( 0 ) ;
@@ -69,7 +71,7 @@ describe('basic MitM tests', () => {
6971 const proxyCredentials = session . getProxyCredentials ( ) ;
7072 expect ( mocks . httpRequestHandler . onRequest ) . toBeCalledTimes ( 0 ) ;
7173
72- let rawHeaders : string [ ] ;
74+ let rawHeaders : string [ ] = null ;
7375 const res = await Helpers . httpRequest (
7476 httpServer . url ,
7577 'GET' ,
@@ -97,7 +99,7 @@ describe('basic MitM tests', () => {
9799 Helpers . needsClosing . push ( mitmServer ) ;
98100 const proxyHost = `http://localhost:${ mitmServer . port } ` ;
99101
100- const session = new RequestSession ( '1' , 'any agent' , null ) ;
102+ const session = new RequestSession ( ` ${ ( sessionCounter += 1 ) } ` , 'any agent' , null ) ;
101103
102104 const proxyCredentials = session . getProxyCredentials ( ) ;
103105 expect ( mocks . httpRequestHandler . onRequest ) . toBeCalledTimes ( 0 ) ;
@@ -123,7 +125,11 @@ describe('basic MitM tests', () => {
123125 socket . end ( ) ;
124126 } ) ;
125127
126- const session = new RequestSession ( '1' , 'any agent' , Promise . resolve ( upstreamProxyHost ) ) ;
128+ const session = new RequestSession (
129+ `${ ( sessionCounter += 1 ) } ` ,
130+ 'any agent' ,
131+ Promise . resolve ( upstreamProxyHost ) ,
132+ ) ;
127133
128134 const proxyCredentials = session . getProxyCredentials ( ) ;
129135
@@ -156,7 +162,7 @@ describe('basic MitM tests', () => {
156162 Helpers . needsClosing . push ( mitmServer ) ;
157163 const proxyHost = `http://localhost:${ mitmServer . port } ` ;
158164
159- const session = new RequestSession ( '1' , 'any agent' , null ) ;
165+ const session = new RequestSession ( ` ${ ( sessionCounter += 1 ) } ` , 'any agent' , null ) ;
160166
161167 const proxyCredentials = session . getProxyCredentials ( ) ;
162168 expect ( mocks . httpRequestHandler . onRequest ) . toBeCalledTimes ( 0 ) ;
@@ -185,7 +191,7 @@ describe('basic MitM tests', () => {
185191 Helpers . needsClosing . push ( mitmServer ) ;
186192 const proxyHost = `http://localhost:${ mitmServer . port } ` ;
187193
188- const session = new RequestSession ( '2' , 'any agent' , null ) ;
194+ const session = new RequestSession ( ` ${ ( sessionCounter += 1 ) } ` , 'any agent' , null ) ;
189195
190196 const proxyCredentials = session . getProxyCredentials ( ) ;
191197
@@ -204,7 +210,7 @@ describe('basic MitM tests', () => {
204210 Helpers . needsClosing . push ( mitmServer ) ;
205211 const proxyHost = `http://localhost:${ mitmServer . port } ` ;
206212
207- const session = new RequestSession ( '3' , 'any agent' , null ) ;
213+ const session = new RequestSession ( ` ${ ( sessionCounter += 1 ) } ` , 'any agent' , null ) ;
208214
209215 const proxyCredentials = session . getProxyCredentials ( ) ;
210216
@@ -278,7 +284,7 @@ describe('basic MitM tests', () => {
278284 const serverMessages = [ ] ;
279285 const serverMessagePromise = createPromise ( ) ;
280286 const wsServer = new WebSocket . Server ( { noServer : true } ) ;
281- const session = new RequestSession ( '4' , 'any agent' , null ) ;
287+ const session = new RequestSession ( ` ${ ( sessionCounter += 1 ) } ` , 'any agent' , null ) ;
282288
283289 httpServer . server . on ( 'upgrade' , ( request , socket , head ) => {
284290 // ensure header is stripped
@@ -337,11 +343,11 @@ describe('basic MitM tests', () => {
337343 Helpers . needsClosing . push ( mitmServer ) ;
338344 const proxyHost = `http://localhost:${ mitmServer . port } ` ;
339345
340- const session = new RequestSession ( '1' , 'any agent' , null ) ;
346+ const session = new RequestSession ( ` ${ ( sessionCounter += 1 ) } ` , 'any agent' , null ) ;
341347 session . delegate . modifyHeadersBeforeSend = jest . fn ( ) ;
342348 session . registerResource ( {
343349 tabId : '1' ,
344- browserRequestId : '1 ' ,
350+ browserRequestId : '25.123 ' ,
345351 url : `${ httpServer . url } page1` ,
346352 method : 'GET' ,
347353 resourceType : 'Document' ,
0 commit comments