Skip to content
Merged
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
Expand Up @@ -70,17 +70,24 @@ public void setBaseUrl(@Nonnull final String baseUrl) {
}

/**
* Instantiates a new OkHttp request adapter with the provided authentication provider.
* @param authenticationProvider the authentication provider to use for authenticating requests.
* Instantiates a new OkHttp request adapter with the provided authentication
* provider.
*
* @param authenticationProvider the authentication provider to use for
* authenticating requests.
*/
public OkHttpRequestAdapter(@Nonnull final AuthenticationProvider authenticationProvider) {
this(authenticationProvider, null, null, null, null);
}

/**
* Instantiates a new OkHttp request adapter with the provided authentication provider, and the parse node factory.
* @param authenticationProvider the authentication provider to use for authenticating requests.
* @param parseNodeFactory the parse node factory to use for parsing responses.
* Instantiates a new OkHttp request adapter with the provided authentication
* provider, and the parse node factory.
*
* @param authenticationProvider the authentication provider to use for
* authenticating requests.
* @param parseNodeFactory the parse node factory to use for parsing
* responses.
*/
@SuppressWarnings("LambdaLast")
public OkHttpRequestAdapter(
Expand All @@ -90,10 +97,15 @@ public OkHttpRequestAdapter(
}

/**
* Instantiates a new OkHttp request adapter with the provided authentication provider, parse node factory, and the serialization writer factory.
* @param authenticationProvider the authentication provider to use for authenticating requests.
* @param parseNodeFactory the parse node factory to use for parsing responses.
* @param serializationWriterFactory the serialization writer factory to use for serializing requests.
* Instantiates a new OkHttp request adapter with the provided authentication
* provider, parse node factory, and the serialization writer factory.
*
* @param authenticationProvider the authentication provider to use for
* authenticating requests.
* @param parseNodeFactory the parse node factory to use for parsing
* responses.
* @param serializationWriterFactory the serialization writer factory to use for
* serializing requests.
*/
@SuppressWarnings("LambdaLast")
public OkHttpRequestAdapter(
Expand All @@ -104,11 +116,18 @@ public OkHttpRequestAdapter(
}

/**
* Instantiates a new OkHttp request adapter with the provided authentication provider, parse node factory, serialization writer factory, and the http client.
* @param authenticationProvider the authentication provider to use for authenticating requests.
* @param parseNodeFactory the parse node factory to use for parsing responses.
* @param serializationWriterFactory the serialization writer factory to use for serializing requests.
* @param client the http client to use for sending requests.
* Instantiates a new OkHttp request adapter with the provided authentication
* provider, parse node factory, serialization writer factory, and the http
* client.
*
* @param authenticationProvider the authentication provider to use for
* authenticating requests.
* @param parseNodeFactory the parse node factory to use for parsing
* responses.
* @param serializationWriterFactory the serialization writer factory to use for
* serializing requests.
* @param client the http client to use for sending
* requests.
*/
@SuppressWarnings("LambdaLast")
public OkHttpRequestAdapter(
Expand All @@ -120,12 +139,20 @@ public OkHttpRequestAdapter(
}

/**
* Instantiates a new OkHttp request adapter with the provided authentication provider, parse node factory, serialization writer factory, http client and observability options.
* @param authenticationProvider the authentication provider to use for authenticating requests.
* @param parseNodeFactory the parse node factory to use for parsing responses.
* @param serializationWriterFactory the serialization writer factory to use for serializing requests.
* @param client the http client to use for sending requests.
* @param observabilityOptions the observability options to use for sending requests.
* Instantiates a new OkHttp request adapter with the provided authentication
* provider, parse node factory, serialization writer factory, http client and
* observability options.
*
* @param authenticationProvider the authentication provider to use for
* authenticating requests.
* @param parseNodeFactory the parse node factory to use for parsing
* responses.
* @param serializationWriterFactory the serialization writer factory to use for
* serializing requests.
* @param client the http client to use for sending
* requests.
* @param observabilityOptions the observability options to use for
* sending requests.
*/
@SuppressWarnings("LambdaLast")
public OkHttpRequestAdapter(
Expand Down Expand Up @@ -590,7 +617,10 @@ private boolean shouldReturnNull(final Response response) {
return statusCode == 204;
}

/** key used for the attribute when the error response has models mappings provided */
/**
* key used for the attribute when the error response has models mappings
* provided
*/
@Nonnull public static final String errorMappingFoundAttributeName =
"com.microsoft.kiota.error_mapping_found";

Expand All @@ -614,6 +644,7 @@ private Response throwIfFailedResponse(
final int statusCode = response.code();
final ResponseHeaders responseHeaders =
HeadersCompatibility.getResponseHeaders(response.headers());
if (statusCode >= 300 && statusCode < 400) return response;
if (errorMappings == null
|| !errorMappings.containsKey(statusCodeAsString)
&& !(statusCode >= 400
Expand Down Expand Up @@ -751,7 +782,9 @@ private String getHeaderValue(final Response response, String key) {
return null;
}

/** Key used for events when an authentication challenge is returned by the API */
/**
* Key used for events when an authentication challenge is returned by the API
*/
@Nonnull public static final String authenticateChallengedEventKey =
"com.microsoft.kiota.authenticate_challenge_received";

Expand Down Expand Up @@ -831,7 +864,7 @@ private void setBaseUrlForRequestInformation(@Nonnull final RequestInformation r
* @param spanForAttributes the span for the attributes.
* @return the created request instance.
* @throws URISyntaxException if the URI is invalid.
* @throws IOException if the URL is invalid.
* @throws IOException if the URL is invalid.
*/
protected @Nonnull Request getRequestFromRequestInformation(
@Nonnull final RequestInformation requestInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,80 @@ void loggingInterceptorDoesNotDrainRequestBodyForNonMarkableStreams() throws Exc
}
}

@Test
void testHandle3XXResponseWithoutLocationHeader() throws Exception {
final var authenticationProviderMock = mock(AuthenticationProvider.class);
authenticationProviderMock.authenticateRequest(
any(RequestInformation.class), any(Map.class));
final var client =
getMockClient(
new Response.Builder()
.code(301)
.message("Moved Permanently")
.protocol(Protocol.HTTP_1_1)
.request(new Request.Builder().url("http://localhost").build())
.body(
ResponseBody.create(
"test".getBytes("UTF-8"),
MediaType.parse("application/json")))
.build());
final var requestInformation =
new RequestInformation() {
{
setUri(new URI("https://localhost"));
httpMethod = HttpMethod.GET;
}
};
final var mockEntity = creatMockEntity();
final var mockParseNode = creatMockParseNode(mockEntity);
final var mockFactory = creatMockParseNodeFactory(mockParseNode, "application/json");

final var requestAdapter =
new OkHttpRequestAdapter(authenticationProviderMock, mockFactory, null, client);
var nativeResponseHandler = new NativeResponseHandler();
requestAdapter.send(requestInformation, null, node -> mockEntity);
var nativeResponse = (Response) nativeResponseHandler.getValue();
assertNull(nativeResponse);
}

@Test
void handle3XXResponseWithLocationHeader() throws Exception {
final var authenticationProviderMock = mock(AuthenticationProvider.class);
authenticationProviderMock.authenticateRequest(
any(RequestInformation.class), any(Map.class));
final var client =
getMockClient(
new Response.Builder()
.code(301)
.message("Moved Permanently")
.protocol(Protocol.HTTP_1_1)
.request(new Request.Builder().url("http://localhost").build())
.header("Location", "https://newlocation")
.body(
ResponseBody.create(
"test".getBytes("UTF-8"),
MediaType.parse("application/json")))
.build());
final var requestInformation =
new RequestInformation() {
{
setUri(new URI("https://localhost"));
httpMethod = HttpMethod.GET;
}
};
final var mockEntity = creatMockEntity();
final var mockParseNode = creatMockParseNode(mockEntity);
final var mockFactory = creatMockParseNodeFactory(mockParseNode, "application/json");

final var requestAdapter =
new OkHttpRequestAdapter(authenticationProviderMock, mockFactory, null, client);
var nativeResponseHandler = new NativeResponseHandler();
requestAdapter.send(requestInformation, null, node -> mockEntity);
// Should throw an exception
var nativeResponse = (Response) nativeResponseHandler.getValue();
assertNull(nativeResponse);
}

public static OkHttpClient getMockClient(final Response response) throws IOException {
final OkHttpClient mockClient = mock(OkHttpClient.class);
final Call remoteCall = mock(Call.class);
Expand Down