1717import static org .assertj .core .api .Assertions .assertThat ;
1818import static org .hamcrest .core .IsEqual .equalTo ;
1919import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .assertNotNull ;
21+ import static org .junit .Assert .assertFalse ;
2022import static org .junit .Assert .assertThat ;
2123import static org .junit .Assert .assertTrue ;
2224import static org .junit .Assert .fail ;
2325
2426import java .io .IOException ;
2527import java .net .URI ;
2628import java .net .URL ;
29+ import java .util .Collection ;
2730
2831import org .junit .After ;
2932import org .junit .AfterClass ;
4245import feign .Feign ;
4346import feign .Param ;
4447import feign .Request ;
48+ import feign .Response ;
4549import feign .RequestLine ;
4650import feign .RetryableException ;
4751import feign .Retryer ;
@@ -277,6 +281,62 @@ public void ribbonRetryOnStatusCodes() throws IOException, InterruptedException
277281 assertEquals (1 , server1 .getRequestCount ());
278282 assertEquals (1 , server2 .getRequestCount ());
279283 }
284+
285+
286+ @ Test
287+ public void testFeignOptionsFollowRedirect () {
288+ String expectedLocation = server2 .url ("" ).url ().toString ();
289+ server1 .enqueue (new MockResponse ().setResponseCode (302 ).setHeader ("Location" , expectedLocation ));
290+
291+ getConfigInstance ().setProperty (serverListKey (), hostAndPort (server1 .url ("" ).url ()));
292+
293+ Request .Options options = new Request .Options (1000 , 1000 , false );
294+ TestInterface api = Feign .builder ()
295+ .options (options )
296+ .client (RibbonClient .create ())
297+ .retryer (Retryer .NEVER_RETRY )
298+ .target (TestInterface .class , "http://" + client ());
299+
300+ try {
301+ Response response = api .get ();
302+ assertEquals (302 , response .status ());
303+ Collection <String > location = response .headers ().get ("Location" );
304+ assertNotNull (location );
305+ assertFalse (location .isEmpty ());
306+ assertEquals (expectedLocation , location .iterator ().next ());
307+ } catch (Exception ignored ) {
308+ ignored .printStackTrace ();
309+ fail ("Shouldn't throw " );
310+ }
311+
312+ }
313+
314+ @ Test
315+ public void testFeignOptionsNoFollowRedirect () {
316+ // 302 will say go to server 2
317+ server1 .enqueue (new MockResponse ().setResponseCode (302 ).setHeader ("Location" , server2 .url ("" ).url ().toString ()));
318+ // server 2 will send back 200 with "Hello" as body
319+ server2 .enqueue (new MockResponse ().setResponseCode (200 ).setBody ("Hello" ));
320+
321+ getConfigInstance ().setProperty (serverListKey (), hostAndPort (server1 .url ("" ).url ()) + "," + hostAndPort (server2 .url ("" ).url ()));
322+
323+ Request .Options options = new Request .Options (1000 , 1000 , true );
324+ TestInterface api = Feign .builder ()
325+ .options (options )
326+ .client (RibbonClient .create ())
327+ .retryer (Retryer .NEVER_RETRY )
328+ .target (TestInterface .class , "http://" + client ());
329+
330+ try {
331+ Response response = api .get ();
332+ assertEquals (200 , response .status ());
333+ assertEquals ("Hello" , response .body ().toString ());
334+ } catch (Exception ignored ) {
335+ ignored .printStackTrace ();
336+ fail ("Shouldn't throw " );
337+ }
338+
339+ }
280340
281341 @ Test
282342 public void testFeignOptionsClientConfig () {
@@ -285,7 +345,8 @@ public void testFeignOptionsClientConfig() {
285345 assertThat (config .get (CommonClientConfigKey .ConnectTimeout ),
286346 equalTo (options .connectTimeoutMillis ()));
287347 assertThat (config .get (CommonClientConfigKey .ReadTimeout ), equalTo (options .readTimeoutMillis ()));
288- assertEquals (2 , config .getProperties ().size ());
348+ assertThat (config .get (CommonClientConfigKey .FollowRedirects ), equalTo (options .isFollowRedirects ()));
349+ assertEquals (3 , config .getProperties ().size ());
289350 }
290351
291352 @ Test
@@ -320,5 +381,8 @@ interface TestInterface {
320381
321382 @ RequestLine ("GET /?a={a}" )
322383 void getWithQueryParameters (@ Param ("a" ) String a );
384+
385+ @ RequestLine ("GET /" )
386+ Response get ();
323387 }
324388}
0 commit comments