33import static org .junit .Assert .assertTrue ;
44import static org .junit .Assert .fail ;
55
6+ import org .apache .http .Header ;
67import org .apache .http .HttpRequest ;
78import org .apache .http .HttpResponse ;
89import org .apache .http .HttpStatus ;
@@ -19,6 +20,7 @@ public class RedirectHandlerTest {
1920
2021 String testmeurl = "https://graph.microsoft.com/v1.0/me/" ;
2122 String testurl = "https://graph.microsoft.com/v1.0/" ;
23+ String differenthosturl = "https://graph.abc.com/v1.0/" ;
2224
2325 @ Test
2426 public void testIsRedirectedFailure () {
@@ -40,6 +42,7 @@ public void testIsRedirectedFailure1() {
4042 RedirectHandler redirectHandler = RedirectHandler .INSTANCE ;
4143 HttpGet httpget = new HttpGet (testmeurl );
4244 HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_BAD_REQUEST , "Bad Request" );
45+ response .setHeader ("location" , testmeurl );
4346 HttpClientContext localContext = HttpClientContext .create ();
4447 try {
4548 boolean isRedirected = redirectHandler .isRedirected (httpget , response , localContext );
@@ -66,6 +69,54 @@ public void testIsRedirectedSuccess() {
6669 }
6770 }
6871
72+ @ Test
73+ public void testIsRedirectedSuccess1 () {
74+ RedirectHandler redirectHandler = RedirectHandler .INSTANCE ;
75+ HttpGet httpget = new HttpGet (testmeurl );
76+ HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_MOVED_PERMANENTLY , "Moved Permanently" );
77+ response .setHeader ("location" , testmeurl );
78+ HttpClientContext localContext = HttpClientContext .create ();
79+ try {
80+ boolean isRedirected = redirectHandler .isRedirected (httpget , response , localContext );
81+ assertTrue (isRedirected );
82+ } catch (ProtocolException e ) {
83+ e .printStackTrace ();
84+ fail ("Redirect handler testIsRedirectedSuccess1 failure" );
85+ }
86+ }
87+
88+ @ Test
89+ public void testIsRedirectedSuccess2 () {
90+ RedirectHandler redirectHandler = RedirectHandler .INSTANCE ;
91+ HttpGet httpget = new HttpGet (testmeurl );
92+ HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_TEMPORARY_REDIRECT , "Temporary Redirect" );
93+ response .setHeader ("location" , testmeurl );
94+ HttpClientContext localContext = HttpClientContext .create ();
95+ try {
96+ boolean isRedirected = redirectHandler .isRedirected (httpget , response , localContext );
97+ assertTrue (isRedirected );
98+ } catch (ProtocolException e ) {
99+ e .printStackTrace ();
100+ fail ("Redirect handler testIsRedirectedSuccess2 failure" );
101+ }
102+ }
103+
104+ @ Test
105+ public void testIsRedirectedSuccess3 () {
106+ RedirectHandler redirectHandler = RedirectHandler .INSTANCE ;
107+ HttpGet httpget = new HttpGet (testmeurl );
108+ HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_SEE_OTHER , "See Other" );
109+ response .setHeader ("location" , testmeurl );
110+ HttpClientContext localContext = HttpClientContext .create ();
111+ try {
112+ boolean isRedirected = redirectHandler .isRedirected (httpget , response , localContext );
113+ assertTrue (isRedirected );
114+ } catch (ProtocolException e ) {
115+ e .printStackTrace ();
116+ fail ("Redirect handler testIsRedirectedSuccess3 failure" );
117+ }
118+ }
119+
69120 @ Test
70121 public void testGetRedirectForGetMethod () {
71122 RedirectHandler redirectHandler = RedirectHandler .INSTANCE ;
@@ -83,6 +134,27 @@ public void testGetRedirectForGetMethod() {
83134 fail ("Redirect handler testGetRedirectForGetMethod failure" );
84135 }
85136 }
137+
138+ @ Test
139+ public void testGetRedirectForGetMethodForAuthHeader () {
140+ RedirectHandler redirectHandler = RedirectHandler .INSTANCE ;
141+ HttpGet httpget = new HttpGet (testurl );
142+ httpget .addHeader ("Authorization" , "TOKEN" );
143+ HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_MOVED_TEMPORARILY , "Moved Temporarily" );
144+ response .setHeader ("location" , differenthosturl );
145+ HttpClientContext localContext = HttpClientContext .create ();
146+ try {
147+ HttpRequest request = redirectHandler .getRedirect (httpget , response , localContext );
148+ assertTrue (request != null );
149+ final String method = request .getRequestLine ().getMethod ();
150+ assertTrue (method .equalsIgnoreCase (HttpGet .METHOD_NAME ));
151+ Header header = request .getFirstHeader ("Authorization" );
152+ assertTrue (header == null );
153+ } catch (ProtocolException e ) {
154+ e .printStackTrace ();
155+ fail ("Redirect handler testGetRedirectForGetMethodForAuthHeader failure" );
156+ }
157+ }
86158
87159 @ Test
88160 public void testGetRedirectForHeadMethod () {
0 commit comments