@@ -503,6 +503,82 @@ public void filterResponse(HttpResponse response, HttpMessageContents contents,
503
503
}
504
504
}
505
505
506
+ @ Test
507
+ public void testMitmDisabledHttpsRequestFilterNotAvailable () throws IOException {
508
+ mockServer .when (request ()
509
+ .withMethod ("GET" )
510
+ .withPath ("/mitmdisabled" ),
511
+ Times .exactly (1 ))
512
+ .respond (response ()
513
+ .withStatusCode (200 )
514
+ .withBody ("success" ));
515
+
516
+ proxy = new BrowserMobProxyServer ();
517
+ proxy .setMitmDisabled (true );
518
+
519
+ proxy .start ();
520
+
521
+ final AtomicBoolean connectRequestFilterFired = new AtomicBoolean (false );
522
+ final AtomicBoolean getRequestFilterFired = new AtomicBoolean (false );
523
+
524
+ proxy .addRequestFilter (new RequestFilter () {
525
+ @ Override
526
+ public HttpResponse filterRequest (HttpRequest request , HttpMessageContents contents , HttpRequest originalRequest ) {
527
+ if (request .getMethod ().equals (HttpMethod .CONNECT )) {
528
+ connectRequestFilterFired .set (true );
529
+ } else if (request .getMethod ().equals (HttpMethod .GET )) {
530
+ getRequestFilterFired .set (true );
531
+ }
532
+ return null ;
533
+ }
534
+ });
535
+
536
+ try (CloseableHttpClient httpClient = ProxyServerTest .getNewHttpClient (proxy .getPort ())) {
537
+ CloseableHttpResponse response = httpClient .execute (new HttpGet ("https://localhost:" + mockServerPort + "/mitmdisabled" ));
538
+
539
+ assertEquals ("Expected server to return a 200" , 200 , response .getStatusLine ().getStatusCode ());
540
+
541
+ assertTrue ("Expected request filter to fire on CONNECT" , connectRequestFilterFired .get ());
542
+ assertFalse ("Expected request filter to fail to fire on GET because MITM is disabled" , getRequestFilterFired .get ());
543
+ }
544
+ }
545
+
546
+ @ Test
547
+ public void testMitmDisabledHttpsResponseFilterNotAvailable () throws IOException {
548
+ mockServer .when (request ()
549
+ .withMethod ("GET" )
550
+ .withPath ("/mitmdisabled" ),
551
+ Times .exactly (1 ))
552
+ .respond (response ()
553
+ .withStatusCode (200 )
554
+ .withBody ("success" ));
555
+
556
+ proxy = new BrowserMobProxyServer ();
557
+ proxy .setMitmDisabled (true );
558
+
559
+ proxy .start ();
560
+
561
+ // unlike the request filter, the response filter doesn't fire when the 200 response to the CONNECT is sent to the client.
562
+ // this is because the response filter is triggered when the serverToProxyResponse() filtering method is called, and
563
+ // the "200 Connection established" is generated by the proxy itself.
564
+
565
+ final AtomicBoolean responseFilterFired = new AtomicBoolean (false );
566
+
567
+ proxy .addResponseFilter (new ResponseFilter () {
568
+ @ Override
569
+ public void filterResponse (HttpResponse response , HttpMessageContents contents , HttpRequest originalRequest ) {
570
+ responseFilterFired .set (true );
571
+ }
572
+ });
573
+
574
+ try (CloseableHttpClient httpClient = ProxyServerTest .getNewHttpClient (proxy .getPort ())) {
575
+ CloseableHttpResponse response = httpClient .execute (new HttpGet ("https://localhost:" + mockServerPort + "/mitmdisabled" ));
576
+
577
+ assertEquals ("Expected server to return a 200" , 200 , response .getStatusLine ().getStatusCode ());
578
+ assertFalse ("Expected response filter to fail to fire because MITM is disabled" , responseFilterFired .get ());
579
+ }
580
+ }
581
+
506
582
/**
507
583
* Helper method for executing response modification tests.
508
584
*/
0 commit comments