@@ -15,6 +15,7 @@ import net.lightbody.bmp.proxy.dns.AdvancedHostResolver
1515import net.lightbody.bmp.proxy.test.util.MockServerTest
1616import net.lightbody.bmp.proxy.test.util.ProxyServerTest
1717import net.lightbody.bmp.proxy.util.IOUtils
18+ import org.apache.http.client.config.RequestConfig
1819import org.apache.http.client.methods.CloseableHttpResponse
1920import org.apache.http.client.methods.HttpGet
2021import org.junit.After
@@ -857,5 +858,105 @@ class NewHarTest extends MockServerTest {
857858 assertEquals (" Expected receive time to not be populated" , 0L , harTimings. getReceive(TimeUnit . NANOSECONDS ))
858859 }
859860
861+ @Test
862+ void testRedirectUrlCapturedForRedirects () {
863+ mockServer. when(request()
864+ .withMethod(" GET" )
865+ .withPath(" /test300" ),
866+ Times . once())
867+ .respond(response()
868+ .withStatusCode(300 )
869+ .withHeader(" Location" , " /redirected-location" ))
870+
871+ mockServer. when(request()
872+ .withMethod(" GET" )
873+ .withPath(" /test301" ),
874+ Times . once())
875+ .respond(response()
876+ .withStatusCode(301 )
877+ .withHeader(" Location" , " /redirected-location" ))
878+
879+ mockServer. when(request()
880+ .withMethod(" GET" )
881+ .withPath(" /test302" ),
882+ Times . once())
883+ .respond(response()
884+ .withStatusCode(302 )
885+ .withHeader(" Location" , " /redirected-location" ))
886+
887+ mockServer. when(request()
888+ .withMethod(" GET" )
889+ .withPath(" /test303" ),
890+ Times . once())
891+ .respond(response()
892+ .withStatusCode(303 )
893+ .withHeader(" Location" , " /redirected-location" ))
894+
895+ mockServer. when(request()
896+ .withMethod(" GET" )
897+ .withPath(" /test307" ),
898+ Times . once())
899+ .respond(response()
900+ .withStatusCode(307 )
901+ .withHeader(" Location" , " /redirected-location" ))
902+
903+ mockServer. when(request()
904+ .withMethod(" GET" )
905+ .withPath(" /test301-no-location-header" ),
906+ Times . once())
907+ .respond(response()
908+ .withStatusCode(301 ))
909+
910+ proxy = new BrowserMobProxyServer ();
911+ proxy. start()
912+
913+ proxy. newHar()
914+
915+ def verifyRedirect = { String requestUrl , expectedStatusCode , expectedLocationValue ->
916+ ProxyServerTest . getNewHttpClient(proxy. port). withCloseable {
917+ // for some reason, even when the HTTP client is built with .disableRedirectHandling(), it still tries to follow
918+ // the 301. so explicitly disable following redirects at the request level.
919+ def request = new HttpGet (requestUrl)
920+ request. setConfig(RequestConfig . custom(). setRedirectsEnabled(false ). build())
921+
922+ CloseableHttpResponse response = it. execute(request)
923+ assertEquals (" HTTP response code did not match expected response code" , expectedStatusCode, response. getStatusLine(). getStatusCode())
924+ };
925+
926+ Thread . sleep(500 )
927+ Har har = proxy. getHar()
928+
929+ assertThat (" Expected to find entries in the HAR" , har. getLog(). getEntries(), not(empty()))
930+
931+ // make sure request data is still captured despite the failure
932+ String capturedUrl = har. log. entries[0 ]. request. url
933+ assertEquals (" URL captured in HAR did not match request URL" , requestUrl, capturedUrl)
934+
935+ HarResponse harResponse = har. log. entries[0 ]. response
936+ assertNotNull (" No HAR response found" , harResponse)
937+
938+ assertEquals (" Expected redirect location to be populated in redirectURL field" , expectedLocationValue, harResponse. redirectURL);
939+ }
940+
941+ verifyRedirect(" http://localhost:${ mockServerPort} /test300" , 300 , " /redirected-location" )
942+
943+ // clear the HAR between every request, to make the verification step easier
944+ proxy. newHar()
945+ verifyRedirect(" http://localhost:${ mockServerPort} /test301" , 301 , " /redirected-location" )
946+
947+ proxy. newHar()
948+ verifyRedirect(" http://localhost:${ mockServerPort} /test302" , 302 , " /redirected-location" )
949+
950+ proxy. newHar()
951+ verifyRedirect(" http://localhost:${ mockServerPort} /test303" , 303 , " /redirected-location" )
952+
953+ proxy. newHar()
954+ verifyRedirect(" http://localhost:${ mockServerPort} /test307" , 307 , " /redirected-location" )
955+
956+ proxy. newHar()
957+ // redirectURL should always be populated or an empty string, never null
958+ verifyRedirect(" http://localhost:${ mockServerPort} /test301-no-location-header" , 301 , " " )
959+ }
960+
860961 // TODO: Add Request Capture Type tests
861962}
0 commit comments