Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.microsoft.kiota.http.middleware;

import static okhttp3.internal.http.StatusLine.HTTP_PERM_REDIRECT;
import static okhttp3.internal.http.StatusLine.HTTP_TEMP_REDIRECT;

import static java.net.HttpURLConnection.HTTP_MOVED_PERM;
import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
import static java.net.HttpURLConnection.HTTP_SEE_OTHER;
Expand All @@ -28,6 +25,10 @@
* Middleware that determines whether a redirect information should be followed or not, and follows it if necessary.
*/
public class RedirectHandler implements Interceptor {
// 308 Permanent Redirect and 307 Temporary Redirect, previously sourced from
// okhttp3.internal.http.StatusLine which is no longer exposed in OkHttp 5.
private static final int HTTP_PERM_REDIRECT = 308;
private static final int HTTP_TEMP_REDIRECT = 307;
@Nonnull private final RedirectHandlerOption mRedirectOption;
@Nullable private final java.net.ProxySelector mProxySelector;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void sendStreamReturnsUsableStream(int statusCode) throws Exception {
}

@ParameterizedTest
@ValueSource(ints = {200, 201, 202, 203, 204, 304})
@ValueSource(ints = {204, 304})
void sendStreamReturnsNullOnNoContent(int statusCode) throws Exception {
final var authenticationProviderMock = mock(AuthenticationProvider.class);
authenticationProviderMock.authenticateRequest(
Expand All @@ -136,7 +136,6 @@ void sendStreamReturnsNullOnNoContent(int statusCode) throws Exception {
.message("OK")
.protocol(Protocol.HTTP_1_1)
.request(new Request.Builder().url("http://localhost").build())
.body(null)
.build());
final var requestAdapter =
new OkHttpRequestAdapter(authenticationProviderMock, null, null, client);
Expand All @@ -152,6 +151,37 @@ void sendStreamReturnsNullOnNoContent(int statusCode) throws Exception {
assertNull(response);
}

@ParameterizedTest
@ValueSource(ints = {200, 201, 202, 203})
void sendStreamReturnsEmptyStreamOnEmptyBody(int statusCode) throws Exception {
// OkHttp 5 responses always carry a non-null (possibly empty) body, so a success
// response with an empty body yields an empty, non-null stream rather than null.
final var authenticationProviderMock = mock(AuthenticationProvider.class);
authenticationProviderMock.authenticateRequest(
any(RequestInformation.class), any(Map.class));
final var client =
getMockClient(
new Response.Builder()
.code(statusCode)
.message("OK")
.protocol(Protocol.HTTP_1_1)
.request(new Request.Builder().url("http://localhost").build())
.build());
final var requestAdapter =
new OkHttpRequestAdapter(authenticationProviderMock, null, null, client);
final var requestInformation =
new RequestInformation() {
{
setUri(new URI("https://localhost"));
httpMethod = HttpMethod.GET;
}
};
final var response =
requestAdapter.sendPrimitive(requestInformation, null, InputStream.class);
assertNotNull(response);
assertEquals(0, response.readAllBytes().length);
}

@ParameterizedTest
@ValueSource(ints = {200, 201, 202, 203, 204, 205, 304})
void sendReturnsNullOnNoContent(int statusCode) throws Exception {
Expand All @@ -165,7 +195,6 @@ void sendReturnsNullOnNoContent(int statusCode) throws Exception {
.message("OK")
.protocol(Protocol.HTTP_1_1)
.request(new Request.Builder().url("http://localhost").build())
.body(null)
.build());
final var requestAdapter =
new OkHttpRequestAdapter(authenticationProviderMock, null, null, client);
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
okhttp = "4.12.0"
okhttp = "5.4.0"
opentelemetry = "1.64.0"

[libraries]
Expand Down
Loading