|
| 1 | +package com.microsoft.graph.httpcore; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | +import static org.junit.Assert.fail; |
| 5 | + |
| 6 | +import org.apache.http.HttpRequest; |
| 7 | +import org.apache.http.HttpResponse; |
| 8 | +import org.apache.http.HttpStatus; |
| 9 | +import org.apache.http.HttpVersion; |
| 10 | +import org.apache.http.ProtocolException; |
| 11 | +import org.apache.http.client.methods.HttpGet; |
| 12 | +import org.apache.http.client.methods.HttpHead; |
| 13 | +import org.apache.http.client.methods.HttpPost; |
| 14 | +import org.apache.http.client.protocol.HttpClientContext; |
| 15 | +import org.apache.http.message.BasicHttpResponse; |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.AfterClass; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +public class RedirectHandlerTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testIsRedirectedFailure() { |
| 26 | + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; |
| 27 | + HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/me/"); |
| 28 | + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); |
| 29 | + HttpClientContext localContext = HttpClientContext.create(); |
| 30 | + try { |
| 31 | + boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); |
| 32 | + assertTrue(!isRedirected); |
| 33 | + } catch (ProtocolException e) { |
| 34 | + // TODO Auto-generated catch block |
| 35 | + e.printStackTrace(); |
| 36 | + fail("Redirect handler isRedirect failure"); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testIsRedirectedFailure1() { |
| 42 | + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; |
| 43 | + HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/me/"); |
| 44 | + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad Request"); |
| 45 | + HttpClientContext localContext = HttpClientContext.create(); |
| 46 | + try { |
| 47 | + boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); |
| 48 | + assertTrue(!isRedirected); |
| 49 | + } catch (ProtocolException e) { |
| 50 | + // TODO Auto-generated catch block |
| 51 | + e.printStackTrace(); |
| 52 | + fail("Redirect handler isRedirect failure"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testIsRedirectedSuccess() { |
| 58 | + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; |
| 59 | + HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/me/"); |
| 60 | + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); |
| 61 | + response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); |
| 62 | + HttpClientContext localContext = HttpClientContext.create(); |
| 63 | + try { |
| 64 | + boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); |
| 65 | + assertTrue(isRedirected); |
| 66 | + } catch (ProtocolException e) { |
| 67 | + // TODO Auto-generated catch block |
| 68 | + e.printStackTrace(); |
| 69 | + fail("Redirect handler isRedirect failure"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testGetRedirectForGetMethod() { |
| 75 | + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; |
| 76 | + HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/"); |
| 77 | + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); |
| 78 | + response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); |
| 79 | + HttpClientContext localContext = HttpClientContext.create(); |
| 80 | + try { |
| 81 | + HttpRequest request = redirectHandler.getRedirect(httpget, response, localContext); |
| 82 | + assertTrue(request != null); |
| 83 | + final String method = request.getRequestLine().getMethod(); |
| 84 | + assertTrue(method.equalsIgnoreCase(HttpGet.METHOD_NAME)); |
| 85 | + } catch (ProtocolException e) { |
| 86 | + // TODO Auto-generated catch block |
| 87 | + e.printStackTrace(); |
| 88 | + fail("Redirect handler isRedirect failure"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testGetRedirectForHeadMethod() { |
| 94 | + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; |
| 95 | + HttpHead httphead = new HttpHead("https://graph.microsoft.com/v1.0/"); |
| 96 | + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); |
| 97 | + response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); |
| 98 | + HttpClientContext localContext = HttpClientContext.create(); |
| 99 | + try { |
| 100 | + HttpRequest request = redirectHandler.getRedirect(httphead, response, localContext); |
| 101 | + assertTrue(request != null); |
| 102 | + final String method = request.getRequestLine().getMethod(); |
| 103 | + assertTrue(method.equalsIgnoreCase(HttpHead.METHOD_NAME)); |
| 104 | + } catch (ProtocolException e) { |
| 105 | + // TODO Auto-generated catch block |
| 106 | + e.printStackTrace(); |
| 107 | + fail("Redirect handler isRedirect failure"); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testGetRedirectForPostMethod() { |
| 113 | + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; |
| 114 | + HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); |
| 115 | + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); |
| 116 | + response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); |
| 117 | + HttpClientContext localContext = HttpClientContext.create(); |
| 118 | + try { |
| 119 | + HttpRequest request = redirectHandler.getRedirect(httppost, response, localContext); |
| 120 | + assertTrue(request != null); |
| 121 | + final String method = request.getRequestLine().getMethod(); |
| 122 | + assertTrue(method.equalsIgnoreCase(HttpPost.METHOD_NAME)); |
| 123 | + } catch (ProtocolException e) { |
| 124 | + // TODO Auto-generated catch block |
| 125 | + e.printStackTrace(); |
| 126 | + fail("Redirect handler isRedirect failure"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testGetRedirectForPostMethod1() { |
| 132 | + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; |
| 133 | + HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); |
| 134 | + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_SEE_OTHER, "See Other"); |
| 135 | + response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); |
| 136 | + HttpClientContext localContext = HttpClientContext.create(); |
| 137 | + try { |
| 138 | + HttpRequest request = redirectHandler.getRedirect(httppost, response, localContext); |
| 139 | + assertTrue(request != null); |
| 140 | + final String method = request.getRequestLine().getMethod(); |
| 141 | + assertTrue(method.equalsIgnoreCase(HttpGet.METHOD_NAME)); |
| 142 | + } catch (ProtocolException e) { |
| 143 | + // TODO Auto-generated catch block |
| 144 | + e.printStackTrace(); |
| 145 | + fail("Redirect handler isRedirect failure"); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments