2323import org .apache .commons .io .IOUtils ;
2424import org .asynchttpclient .filter .FilterContext ;
2525import org .asynchttpclient .filter .ResponseFilter ;
26+ import org .asynchttpclient .request .body .generator .ByteArrayBodyGenerator ;
2627import org .asynchttpclient .request .body .generator .InputStreamBodyGenerator ;
2728import org .asynchttpclient .request .body .multipart .InputStreamPart ;
2829import org .asynchttpclient .request .body .multipart .StringPart ;
4950import static io .netty .handler .codec .http .HttpHeaderNames .LOCATION ;
5051import static org .asynchttpclient .Dsl .asyncHttpClient ;
5152import static org .asynchttpclient .Dsl .config ;
53+ import static org .asynchttpclient .util .HttpConstants .Methods .GET ;
54+ import static org .asynchttpclient .util .HttpConstants .Methods .POST ;
55+ import static org .asynchttpclient .util .HttpConstants .Methods .QUERY ;
5256import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
5357import static org .junit .jupiter .api .Assertions .assertEquals ;
5458import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
@@ -64,13 +68,15 @@ public class RedirectBodyTest extends AbstractBasicTest {
6468 private static final List <String > receivedContentLengths = new CopyOnWriteArrayList <>();
6569 private static volatile boolean redirectAlreadyPerformed ;
6670 private static volatile String receivedContentType ;
71+ private static volatile String receivedMethod ;
6772 private static volatile Path fileToDeleteBeforeRedirect ;
6873
6974 @ BeforeEach
7075 public void setUp () {
7176 receivedContentLengths .clear ();
7277 redirectAlreadyPerformed = false ;
7378 receivedContentType = null ;
79+ receivedMethod = null ;
7480 fileToDeleteBeforeRedirect = null ;
7581 }
7682
@@ -94,6 +100,7 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
94100
95101 } else {
96102 receivedContentType = request .getContentType ();
103+ receivedMethod = request .getMethod ();
97104 httpResponse .setStatus (200 );
98105 httpResponse .setContentLength (body .length );
99106 if (body .length > 0 ) {
@@ -114,6 +121,7 @@ public void regular301LosesBody() throws Exception {
114121
115122 Response response = c .preparePost (getTargetUrl ()).setHeader (CONTENT_TYPE , contentType ).setBody (body ).setHeader ("X-REDIRECT" , "301" ).execute ().get (TIMEOUT , TimeUnit .SECONDS );
116123 assertEquals (response .getResponseBody (), "" );
124+ assertEquals (GET , receivedMethod );
117125 assertNull (receivedContentType );
118126 }
119127 }
@@ -126,6 +134,7 @@ public void regular302LosesBody() throws Exception {
126134
127135 Response response = c .preparePost (getTargetUrl ()).setHeader (CONTENT_TYPE , contentType ).setBody (body ).setHeader ("X-REDIRECT" , "302" ).execute ().get (TIMEOUT , TimeUnit .SECONDS );
128136 assertEquals (response .getResponseBody (), "" );
137+ assertEquals (GET , receivedMethod );
129138 assertNull (receivedContentType );
130139 }
131140 }
@@ -138,10 +147,24 @@ public void regular302StrictKeepsBody() throws Exception {
138147
139148 Response response = c .preparePost (getTargetUrl ()).setHeader (CONTENT_TYPE , contentType ).setBody (body ).setHeader ("X-REDIRECT" , "302" ).execute ().get (TIMEOUT , TimeUnit .SECONDS );
140149 assertEquals (response .getResponseBody (), body );
150+ assertEquals (POST , receivedMethod );
141151 assertEquals (receivedContentType , contentType );
142152 }
143153 }
144154
155+ @ RepeatedIfExceptionsTest (repeats = 5 )
156+ public void regular303SwitchesToGetAndLosesBody () throws Exception {
157+ try (AsyncHttpClient c = asyncHttpClient (config ().setFollowRedirect (true ))) {
158+ String body = "hello there" ;
159+ String contentType = "text/plain; charset=UTF-8" ;
160+
161+ Response response = c .preparePost (getTargetUrl ()).setHeader (CONTENT_TYPE , contentType ).setBody (body ).setHeader ("X-REDIRECT" , "303" ).execute ().get (TIMEOUT , TimeUnit .SECONDS );
162+ assertEquals ("" , response .getResponseBody ());
163+ assertEquals (GET , receivedMethod );
164+ assertNull (receivedContentType );
165+ }
166+ }
167+
145168 @ RepeatedIfExceptionsTest (repeats = 5 )
146169 public void regular307KeepsBody () throws Exception {
147170 try (AsyncHttpClient c = asyncHttpClient (config ().setFollowRedirect (true ))) {
@@ -150,10 +173,85 @@ public void regular307KeepsBody() throws Exception {
150173
151174 Response response = c .preparePost (getTargetUrl ()).setHeader (CONTENT_TYPE , contentType ).setBody (body ).setHeader ("X-REDIRECT" , "307" ).execute ().get (TIMEOUT , TimeUnit .SECONDS );
152175 assertEquals (response .getResponseBody (), body );
176+ assertEquals (POST , receivedMethod );
153177 assertEquals (receivedContentType , contentType );
154178 }
155179 }
156180
181+ @ RepeatedIfExceptionsTest (repeats = 5 )
182+ public void regular308KeepsBody () throws Exception {
183+ try (AsyncHttpClient c = asyncHttpClient (config ().setFollowRedirect (true ))) {
184+ String body = "hello there" ;
185+ String contentType = "text/plain; charset=UTF-8" ;
186+
187+ Response response = c .preparePost (getTargetUrl ()).setHeader (CONTENT_TYPE , contentType ).setBody (body ).setHeader ("X-REDIRECT" , "308" ).execute ().get (TIMEOUT , TimeUnit .SECONDS );
188+ assertEquals (body , response .getResponseBody ());
189+ assertEquals (POST , receivedMethod );
190+ assertEquals (contentType , receivedContentType );
191+ }
192+ }
193+
194+ @ RepeatedIfExceptionsTest (repeats = 5 )
195+ public void query301KeepsMethodAndBody () throws Exception {
196+ queryRedirectKeepsMethodAndBody (301 , false );
197+ }
198+
199+ @ RepeatedIfExceptionsTest (repeats = 5 )
200+ public void query302KeepsMethodAndBody () throws Exception {
201+ queryRedirectKeepsMethodAndBody (302 , false );
202+ }
203+
204+ @ RepeatedIfExceptionsTest (repeats = 5 )
205+ public void query302StrictKeepsMethodAndBody () throws Exception {
206+ queryRedirectKeepsMethodAndBody (302 , true );
207+ }
208+
209+ @ RepeatedIfExceptionsTest (repeats = 5 )
210+ public void query303SwitchesToGetAndDropsBody () throws Exception {
211+ try (AsyncHttpClient c = asyncHttpClient (config ().setFollowRedirect (true ))) {
212+ String body = "hello there" ;
213+ String contentType = "text/plain; charset=UTF-8" ;
214+
215+ Response response = c .prepare (QUERY , getTargetUrl ())
216+ .setHeader (CONTENT_TYPE , contentType )
217+ .setBody (body )
218+ .setHeader ("X-REDIRECT" , "303" )
219+ .execute ()
220+ .get (TIMEOUT , TimeUnit .SECONDS );
221+ assertEquals ("" , response .getResponseBody ());
222+ assertEquals (GET , receivedMethod );
223+ assertNull (receivedContentType );
224+ }
225+ }
226+
227+ @ RepeatedIfExceptionsTest (repeats = 5 )
228+ public void query307KeepsMethodAndBody () throws Exception {
229+ queryRedirectKeepsMethodAndBody (307 , false );
230+ }
231+
232+ @ RepeatedIfExceptionsTest (repeats = 5 )
233+ public void query308KeepsMethodAndBody () throws Exception {
234+ queryRedirectKeepsMethodAndBody (308 , false );
235+ }
236+
237+ @ RepeatedIfExceptionsTest (repeats = 5 )
238+ public void query301KeepsRepeatableBodyGenerator () throws Exception {
239+ try (AsyncHttpClient c = asyncHttpClient (config ().setFollowRedirect (true ))) {
240+ byte [] body = "hello there" .getBytes (UTF_8 );
241+ String contentType = "text/plain; charset=UTF-8" ;
242+
243+ Response response = c .prepare (QUERY , getTargetUrl ())
244+ .setHeader (CONTENT_TYPE , contentType )
245+ .setBody (new ByteArrayBodyGenerator (body ))
246+ .setHeader ("X-REDIRECT" , "301" )
247+ .execute ()
248+ .get (TIMEOUT , TimeUnit .SECONDS );
249+ assertEquals ("hello there" , response .getResponseBody ());
250+ assertEquals (QUERY , receivedMethod );
251+ assertEquals (contentType , receivedContentType );
252+ }
253+ }
254+
157255 @ RepeatedIfExceptionsTest (repeats = 5 )
158256 public void redirectPreservesPerRequestSettings () throws Exception {
159257 Duration readTimeout = Duration .ofSeconds (7 );
@@ -424,6 +522,25 @@ public void inputStreamMultipart307FailsPromptly() throws Exception {
424522 }
425523 }
426524
525+ private void queryRedirectKeepsMethodAndBody (int statusCode , boolean strict302Handling ) throws Exception {
526+ try (AsyncHttpClient c = asyncHttpClient (config ()
527+ .setFollowRedirect (true )
528+ .setStrict302Handling (strict302Handling ))) {
529+ String body = "hello there" ;
530+ String contentType = "text/plain; charset=UTF-8" ;
531+
532+ Response response = c .prepare (QUERY , getTargetUrl ())
533+ .setHeader (CONTENT_TYPE , contentType )
534+ .setBody (body )
535+ .setHeader ("X-REDIRECT" , Integer .toString (statusCode ))
536+ .execute ()
537+ .get (TIMEOUT , TimeUnit .SECONDS );
538+ assertEquals (body , response .getResponseBody ());
539+ assertEquals (QUERY , receivedMethod );
540+ assertEquals (contentType , receivedContentType );
541+ }
542+ }
543+
427544 private static Response execute307 (BoundRequestBuilder requestBuilder ) throws Exception {
428545 return requestBuilder
429546 .setHeader (CONTENT_TYPE , CONTENT_TYPE_VALUE )
0 commit comments