Skip to content

Commit bca88f5

Browse files
Microsoft Graph DevX ToolingMicrosoft Graph DevX Tooling
authored andcommitted
Updated tests
1 parent dd45dab commit bca88f5

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,12 @@ private Response throwIfFailedResponse(
644644
final int statusCode = response.code();
645645
final ResponseHeaders responseHeaders =
646646
HeadersCompatibility.getResponseHeaders(response.headers());
647-
if (statusCode >= 300 && statusCode < 400 && responseHeaders != null && responseHeaders.get("Location") == null) {
647+
if (statusCode >= 300 && statusCode < 400) {
648648

649+
if (responseHeaders.get("Location") != null) {
650+
spanForAttributes.setAttribute("Location", true);
651+
return response;
652+
}
649653
spanForAttributes.setAttribute(errorMappingFoundAttributeName, false);
650654
return null;
651655
}

components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,44 @@ void testHandle3XXResponseWithoutLocationHeader() throws Exception {
571571
assertNull(nativeResponse);
572572
}
573573

574+
@Test
575+
void handle3XXResponseWithLocationHeader() throws Exception {
576+
final var authenticationProviderMock = mock(AuthenticationProvider.class);
577+
authenticationProviderMock.authenticateRequest(
578+
any(RequestInformation.class), any(Map.class));
579+
final var client =
580+
getMockClient(
581+
new Response.Builder()
582+
.code(301)
583+
.message("Moved Permanently")
584+
.protocol(Protocol.HTTP_1_1)
585+
.request(new Request.Builder().url("http://localhost").build())
586+
.header("Location", "https://newlocation")
587+
.body(
588+
ResponseBody.create(
589+
"test".getBytes("UTF-8"),
590+
MediaType.parse("application/json")))
591+
.build());
592+
final var requestInformation =
593+
new RequestInformation() {
594+
{
595+
setUri(new URI("https://localhost"));
596+
httpMethod = HttpMethod.GET;
597+
}
598+
};
599+
final var mockEntity = creatMockEntity();
600+
final var mockParseNode = creatMockParseNode(mockEntity);
601+
final var mockFactory = creatMockParseNodeFactory(mockParseNode, "application/json");
602+
603+
final var requestAdapter =
604+
new OkHttpRequestAdapter(authenticationProviderMock, mockFactory, null, client);
605+
var nativeResponseHandler = new NativeResponseHandler();
606+
requestAdapter.send(requestInformation, null, node -> mockEntity);
607+
// Should throw an exception
608+
var nativeResponse = (Response) nativeResponseHandler.getValue();
609+
assertNull(nativeResponse);
610+
}
611+
574612
public static OkHttpClient getMockClient(final Response response) throws IOException {
575613
final OkHttpClient mockClient = mock(OkHttpClient.class);
576614
final Call remoteCall = mock(Call.class);

0 commit comments

Comments
 (0)