33import static org .junit .Assert .assertEquals ;
44import static org .junit .Assert .assertNull ;
55
6+ import org .apache .http .HttpRequest ;
67import org .apache .http .HttpResponse ;
8+ import org .apache .http .ProtocolException ;
9+ import org .apache .http .client .RedirectStrategy ;
10+ import org .apache .http .client .fluent .Executor ;
711import org .apache .http .client .fluent .Request ;
12+ import org .apache .http .client .methods .HttpUriRequest ;
813import org .apache .http .client .utils .URIBuilder ;
14+ import org .apache .http .impl .client .HttpClientBuilder ;
15+ import org .apache .http .protocol .HttpContext ;
916import org .apache .http .util .EntityUtils ;
1017import org .junit .Test ;
1118
@@ -17,46 +24,55 @@ public interface HttpResponseValidator {
1724
1825 {
1926
20- use ((req , resp , chain ) -> {
21- chain .next (req , resp );
27+ use ((req , res , chain ) -> {
28+ chain .next (req , res );
2229 });
2330
24- get ("/no-next" , (req , resp , chain ) -> {
31+ get ("/no-next" , (req , res , chain ) -> {
2532 });
2633
27- get ("/no-next" , (req , resp , chain ) -> {
34+ get ("/no-next" , (req , res , chain ) -> {
2835 throw new IllegalStateException ("Should NOT execute ever" );
2936 });
3037
31- get ("/before" , (req , resp , chain ) -> {
32- resp .header ("before" , "before" );
33- chain .next (req , resp );
38+ get ("/before" , (req , res , chain ) -> {
39+ res .header ("before" , "before" );
40+ chain .next (req , res );
3441 });
3542
36- get ("/before" , (req , resp ) -> {
37- resp .send (resp .header ("before" ).stringValue ());
43+ get ("/before" , (req , res ) -> {
44+ res .send (res .header ("before" ).stringValue ());
3845 });
3946
40- get ("/after" , (req , resp , chain ) -> {
41- chain .next (req , resp );
42- resp .header ("after" , "after" );
47+ get ("/after" , (req , res , chain ) -> {
48+ chain .next (req , res );
49+ res .header ("after" , "after" );
4350 });
4451
45- get ("/after" , (req , resp ) -> {
46- resp .send (resp .header ("after" ).toOptional (String .class ).orElse ("after-missing" ));
52+ get ("/after" , (req , res ) -> {
53+ res .send (res .header ("after" ).toOptional (String .class ).orElse ("after-missing" ));
4754 });
4855
49- get ("/commit" , (req , resp , chain ) -> {
50- resp .send ("commit" );
56+ get ("/commit" , (req , res , chain ) -> {
57+ res .send ("commit" );
5158 });
5259
53- get ("/commitx2" , (req , resp , chain ) -> {
54- resp .send ("commit1" );
55- chain .next (req , resp );
60+ get ("/commitx2" , (req , res , chain ) -> {
61+ res .send ("commit1" );
62+ chain .next (req , res );
5663 });
5764
58- get ("/commitx2" , (req , resp ) -> {
59- resp .send ("ignored" );
65+ get ("/commitx2" , (req , res ) -> {
66+ res .send ("ignored" );
67+ });
68+
69+ get ("/redirect" , (req , res , chain ) -> {
70+ res .redirect ("/commit" );
71+ chain .next (req , res );
72+ });
73+
74+ get ("/redirect" , (req , res ) -> {
75+ res .send ("ignored" );
6076 });
6177
6278 }
@@ -98,6 +114,13 @@ public void commitIsPossibleFromFilter() throws Exception {
98114 }));
99115 }
100116
117+ @ Test
118+ public void redirectIsPossibleFromFilter () throws Exception {
119+ assertEquals ("" , execute (GET (uri ("/redirect" )), (response ) -> {
120+ assertEquals (302 , response .getStatusLine ().getStatusCode ());
121+ }));
122+ }
123+
101124 @ Test
102125 public void secondCommitIsIgnored () throws Exception {
103126 assertEquals ("commit1" , execute (GET (uri ("/commitx2" )), (response ) -> {
@@ -111,9 +134,25 @@ private static Request GET(final URIBuilder uri) throws Exception {
111134
112135 private static String execute (final Request request , final HttpResponseValidator validator )
113136 throws Exception {
114- HttpResponse resp = request .execute ().returnResponse ();
115- validator .validate (resp );
116- return EntityUtils .toString (resp .getEntity ());
137+ Executor executor = Executor .newInstance (HttpClientBuilder .create ()
138+ .setRedirectStrategy (new RedirectStrategy () {
139+
140+ @ Override
141+ public boolean isRedirected (final HttpRequest request , final HttpResponse response ,
142+ final HttpContext context ) throws ProtocolException {
143+ return false ;
144+ }
145+
146+ @ Override
147+ public HttpUriRequest getRedirect (final HttpRequest request , final HttpResponse response ,
148+ final HttpContext context ) throws ProtocolException {
149+ return null ;
150+ }
151+ }).build ());
152+
153+ HttpResponse response = executor .execute (request ).returnResponse ();
154+ validator .validate (response );
155+ return EntityUtils .toString (response .getEntity ());
117156 }
118157
119158}
0 commit comments