Skip to content

Commit 1d53c73

Browse files
author
Nakul Sabharwal
committed
added Relative reference in location url
1 parent de336ea commit 1d53c73

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,15 @@ Request getRedirect(
6666
final Request request,
6767
final Response userResponse) throws ProtocolException {
6868
String location = userResponse.header("Location");
69-
if (location == null) return null;
69+
if (location == null || location.length() == 0) return null;
7070

71-
// TODO: If Location header is relative reference then final URL should be relative to original target URL.
71+
// For relative URL in location header, the new url to redirect is relative to original request
72+
if(location.startsWith("/")) {
73+
if(request.url().toString().endsWith("/")) {
74+
location = location.substring(1);
75+
}
76+
location = request.url() + location;
77+
}
7278

7379
HttpUrl requestUrl = userResponse.request().url();
7480

src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class RedirectHandlerTest {
2222

2323
String testmeurl = "https://graph.microsoft.com/v1.0/me/";
24-
String testurl = "https://graph.microsoft.com/v1.0/";
24+
String testurl = "https://graph.microsoft.com/v1.0";
2525
String differenthosturl = "https://graph.abc.com/v1.0/";
2626

2727
@Test
@@ -228,5 +228,39 @@ public void testGetRedirectForPostMethodWithStatusCodeSeeOther() {
228228
fail("Redirect handler testGetRedirectForPostMethod1 failure");
229229
}
230230
}
231+
232+
@Test
233+
public void testGetRedirectForRelativeURL() throws ProtocolException {
234+
RedirectHandler redirectHandler = new RedirectHandler();
235+
Request httppost = new Request.Builder().url(testurl).build();
236+
237+
Response response = new Response.Builder()
238+
.protocol(Protocol.HTTP_1_1)
239+
.code(HttpURLConnection.HTTP_SEE_OTHER)
240+
.message("See Other")
241+
.addHeader("location", "/testrelativeurl")
242+
.request(httppost)
243+
.build();
244+
245+
Request request = redirectHandler.getRedirect(httppost, response);
246+
assertTrue(request.url().toString().compareTo(testurl+"/testrelativeurl") == 0);
247+
}
248+
249+
@Test
250+
public void testGetRedirectRelativeLocationRequestURLwithSlash() throws ProtocolException {
251+
RedirectHandler redirectHandler = new RedirectHandler();
252+
Request httppost = new Request.Builder().url(testmeurl).build();
253+
254+
Response response = new Response.Builder()
255+
.protocol(Protocol.HTTP_1_1)
256+
.code(HttpURLConnection.HTTP_SEE_OTHER)
257+
.message("See Other")
258+
.addHeader("location", "/testrelativeurl")
259+
.request(httppost)
260+
.build();
261+
Request request = redirectHandler.getRedirect(httppost, response);
262+
String expected = "https://graph.microsoft.com/v1.0/me/testrelativeurl";
263+
assertTrue(request.url().toString().compareTo(expected) == 0);
264+
}
231265

232266
}

0 commit comments

Comments
 (0)