1313import com .azure .core .http .MockHttpResponse ;
1414import com .azure .core .http .clients .NoOpHttpClient ;
1515import org .junit .jupiter .api .Test ;
16+ import org .junit .jupiter .params .ParameterizedTest ;
17+ import org .junit .jupiter .params .provider .ValueSource ;
1618import reactor .core .publisher .Mono ;
1719
1820import java .net .MalformedURLException ;
2527import java .util .function .Function ;
2628
2729import static org .junit .jupiter .api .Assertions .assertEquals ;
30+ import static org .junit .jupiter .api .Assertions .assertNull ;
2831
2932public class RedirectPolicyTest {
3033
@@ -53,14 +56,16 @@ public Mono<HttpResponse> send(HttpRequest request) {
5356 assertEquals (308 , response .getStatusCode ());
5457 }
5558
56- @ Test
57- public void defaultRedirectWhen308 () throws Exception {
59+ @ ParameterizedTest
60+ @ ValueSource (ints = {308 , 307 , 301 , 302 })
61+ public void defaultRedirectExpectedStatusCodes (int statusCode ) throws Exception {
5862 RecordingHttpClient httpClient = new RecordingHttpClient (request -> {
5963 if (request .getUrl ().toString ().equals ("http://localhost/" )) {
6064 Map <String , String > headers = new HashMap <>();
6165 headers .put ("Location" , "http://redirecthost/" );
66+ headers .put ("Authorization" , "12345" );
6267 HttpHeaders httpHeader = new HttpHeaders (headers );
63- return Mono .just (new MockHttpResponse (request , 308 , httpHeader ));
68+ return Mono .just (new MockHttpResponse (request , statusCode , httpHeader ));
6469 } else {
6570 return Mono .just (new MockHttpResponse (request , 200 ));
6671 }
@@ -74,8 +79,8 @@ public void defaultRedirectWhen308() throws Exception {
7479 HttpResponse response = pipeline .send (new HttpRequest (HttpMethod .GET ,
7580 new URL ("http://localhost/" ))).block ();
7681
77- // assertEquals(2, httpClient.getCount());
7882 assertEquals (200 , response .getStatusCode ());
83+ assertNull (response .getHeaders ().getValue ("Authorization" ));
7984 }
8085
8186 @ Test
@@ -326,6 +331,32 @@ public Mono<HttpResponse> send(HttpRequest request) {
326331 assertEquals (401 , response .getStatusCode ());
327332 }
328333
334+ @ Test
335+ public void defaultRedirectAuthorizationHeaderCleared () throws Exception {
336+ RecordingHttpClient httpClient = new RecordingHttpClient (request -> {
337+ if (request .getUrl ().toString ().equals ("http://localhost/" )) {
338+ Map <String , String > headers = new HashMap <>();
339+ headers .put ("Location" , "http://redirecthost/" );
340+ headers .put ("Authorization" , "12345" );
341+ HttpHeaders httpHeader = new HttpHeaders (headers );
342+ return Mono .just (new MockHttpResponse (request , 308 , httpHeader ));
343+ } else {
344+ return Mono .just (new MockHttpResponse (request , 200 ));
345+ }
346+ });
347+
348+ HttpPipeline pipeline = new HttpPipelineBuilder ()
349+ .httpClient (httpClient )
350+ .policies (new RedirectPolicy ())
351+ .build ();
352+
353+ HttpResponse response = pipeline .send (new HttpRequest (HttpMethod .GET ,
354+ new URL ("http://localhost/" ))).block ();
355+
356+ assertEquals (200 , response .getStatusCode ());
357+ assertNull (response .getHeaders ().getValue ("Authorization" ));
358+ }
359+
329360 static class RecordingHttpClient implements HttpClient {
330361
331362 private final AtomicInteger count = new AtomicInteger ();
0 commit comments