Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework low-level HTTP methods and Response structure #44555

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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 @@ -5,16 +5,15 @@

import io.clientcore.core.http.models.HttpHeaders;
import io.clientcore.core.http.models.HttpRequest;
import io.clientcore.core.implementation.http.HttpResponse;
import io.clientcore.core.http.models.Response;
import io.clientcore.core.serialization.json.JsonSerializer;
import io.clientcore.core.models.binarydata.BinaryData;
import io.clientcore.core.serialization.ObjectSerializer;

/**
* A mock implementation of {@link HttpResponse} that can be used for testing.
* A mock implementation of {@link Response} that can be used for testing.
*/
public class MockHttpResponse extends HttpResponse<BinaryData> {
private static final ObjectSerializer SERIALIZER = new JsonSerializer();
public class MockHttpResponse extends Response<BinaryData> {

/**
* Creates an HTTP response associated with a {@code request}, returns the {@code statusCode}, and has an empty
Expand All @@ -39,17 +38,6 @@ public MockHttpResponse(HttpRequest request, int statusCode, BinaryData body) {
this(request, statusCode, new HttpHeaders(), body);
}

/**
* Creates an HTTP response associated with a {@code request}, returns the {@code statusCode}, and http headers.
*
* @param request HttpRequest associated with the response.
* @param statusCode Status code of the response.
* @param headers Headers of the response.
*/
public MockHttpResponse(HttpRequest request, int statusCode, HttpHeaders headers) {
this(request, statusCode, headers, BinaryData.empty());
}

/**
* Creates an HTTP response associated with a {@code request}, returns the {@code statusCode}, contains the
* {@code headers}, and response body of {@code bodyBytes}.
Expand All @@ -63,23 +51,4 @@ public MockHttpResponse(HttpRequest request, int statusCode, HttpHeaders headers
super(request, statusCode, headers, body);
}

/**
* Creates an HTTP response associated with a {@code request}, returns the {@code statusCode}, and response body
* that is JSON serialized from {@code serializable}.
*
* @param request HttpRequest associated with the response.
* @param statusCode Status code of the response.
* @param serializable Contents to be serialized into JSON for the response.
*/
public MockHttpResponse(HttpRequest request, int statusCode, Object serializable) {
this(request, statusCode, new HttpHeaders(), serialize(serializable));
}

private static BinaryData serialize(Object serializable) {
if (serializable == null) {
return null;
}

return BinaryData.fromObject(serializable, SERIALIZER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

import io.clientcore.core.http.client.HttpClient;
import io.clientcore.core.http.models.HttpHeaderName;
import io.clientcore.core.http.models.HttpHeaders;
import io.clientcore.core.http.models.HttpMethod;
import io.clientcore.core.http.models.HttpRequest;
import io.clientcore.core.http.models.Response;
import io.clientcore.core.models.binarydata.BinaryData;

import java.io.IOException;

Expand All @@ -19,7 +21,7 @@ public final class LocalHttpClient implements HttpClient {
volatile boolean closeCalledOnResponse;

@Override
public Response<?> send(HttpRequest request) {
public Response<BinaryData> send(HttpRequest request) {
lastHttpRequest = request;
boolean success = request.getUri().getPath().equals("/my/uri/path");

Expand All @@ -30,7 +32,7 @@ public Response<?> send(HttpRequest request) {
|| request.getHttpMethod().equals(HttpMethod.HEAD);
}

return new MockHttpResponse(request, success ? 200 : 400) {
return new Response<BinaryData>(request, success ? 200 : 400, new HttpHeaders(), BinaryData.empty()) {
@Override
public void close() throws IOException {
closeCalledOnResponse = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public void testListFoo() {
String requestUri = request.getUri().toString();
request.setRequestOptions(requestOptions);
if (firstPageUri.equals(requestUri)) {
return createMockResponse(request, 200, FIRST_PAGE_RESPONSE, nextLinkUri);
return createMockResponse(request, FIRST_PAGE_RESPONSE, nextLinkUri);
} else if (nextLinkUri.equals(requestUri)) {
return createMockResponse(request, 200, NEXTLINK_RESPONSE, null);
return createMockResponse(request, NEXTLINK_RESPONSE, null);
}

return new MockHttpResponse(request, 404);
Expand Down Expand Up @@ -93,13 +93,13 @@ public void testListFooListResult() {
String requestUri = request.getUri().toString();
request.setRequestOptions(requestOptions);
if (firstPageUri.equals(requestUri)) {
return createMockResponse(request, 200, BinaryData.fromString(
return createMockResponse(request, BinaryData.fromString(
"{\"items\":[{\"bar\":\"hello.world\",\"baz\":[\"hello\",\"hello.world\"],\"qux\":{\"a" +
".b\":\"c.d\"," +
"\"bar.a\":\"ttyy\",\"bar.b\":\"uuzz\",\"hello\":\"world\"}}], \"nextLink\":\"" + nextLinkUri + "\"}"),
nextLinkUri);
} else if (nextLinkUri.equals(requestUri)) {
return createMockResponse(request, 200, BinaryData.fromString(
return createMockResponse(request, BinaryData.fromString(
"{\"items\":[{\"bar\":\"hello.world2\",\"additionalProperties\":{\"bar\":\"baz\",\"a" +
".b\":\"c.d\",\"properties.bar\":\"barbar\"}}]"),
null);
Expand All @@ -124,13 +124,13 @@ public void testListFooListResult() {
/**
* Creates a mock HTTP response with JSON body and optional nextLink header.
*/
private MockHttpResponse createMockResponse(HttpRequest request, int statusCode, BinaryData jsonBody, String nextLink) {
private MockHttpResponse createMockResponse(HttpRequest request, BinaryData jsonBody, String nextLink) {
HttpHeaders headers = new HttpHeaders();
if (nextLink != null) {
headers.set(HttpHeaderName.fromString("nextLink"), nextLink);
}

return new MockHttpResponse(request, statusCode, headers, jsonBody);
return new MockHttpResponse(request, 200, headers, jsonBody);
}

/**
Expand All @@ -144,7 +144,6 @@ private <T> PagedResponse<Foo> toPagedResponse(Response<T> response, String next
response != null ? response.getRequest() : new HttpRequest().setMethod(HttpMethod.GET).setUri("https://example.com"),
200,
response != null ? response.getHeaders() : new HttpHeaders(),
response != null ? response.getBody() : null,
Collections.emptyList() // Return an empty list when null
);
}
Expand All @@ -159,17 +158,7 @@ private <T> PagedResponse<Foo> toPagedResponse(Response<T> response, String next
throw new IllegalArgumentException("Unsupported response type: " + response.getValue().getClass().getName());
}

return new PagedResponse<>(
response.getRequest(),
response.getStatusCode(),
response.getHeaders(),
response.getBody(),
items,
nextLink,
null,
null,
null,
null
);
return new PagedResponse<>(response.getRequest(), response.getStatusCode(), response.getHeaders(), items,
nextLink, null, null, null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,22 @@
import io.clientcore.core.http.pipeline.HttpPipeline;
import io.clientcore.core.http.pipeline.HttpPipelineBuilder;
import io.clientcore.core.models.binarydata.BinaryData;
import io.clientcore.core.models.binarydata.ByteArrayBinaryData;
import io.clientcore.core.models.binarydata.InputStreamBinaryData;
import io.clientcore.core.shared.HttpClientTestsServer;
import io.clientcore.core.shared.LocalTestServer;
import io.clientcore.http.okhttp3.OkHttpHttpClientProvider;
import java.io.IOException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static io.clientcore.core.http.models.ResponseBodyMode.BUFFER;
import java.io.IOException;

import static io.clientcore.core.http.models.ResponseBodyMode.DESERIALIZE;
import static io.clientcore.core.http.models.ResponseBodyMode.IGNORE;
import static io.clientcore.core.http.models.ResponseBodyMode.STREAM;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestInterfaceGenerationTests {
private static LocalTestServer server;
Expand Down Expand Up @@ -80,7 +75,6 @@ public void testGetFoo() {
Response<Foo> response = testInterface.getFoo("key", "label", "sync-token-value");
assertNotNull(response);
assertEquals(200, response.getStatusCode());
assertEquals(wireValue, response.getBody().toString());

Foo foo = response.getValue();
assertNotNull(foo);
Expand All @@ -104,8 +98,6 @@ public void bodyIsEmptyWhenIgnoreBodyIsSet() throws IOException {
assertNull(httpBinJSON);

try (Response<HttpBinJSON> response = testInterface.putResponse(getServerUri(false), 42, requestOptions)) {
assertNotNull(response.getBody());
assertEquals(0, response.getBody().getLength());
assertNull(response.getValue());
}
}
Expand All @@ -122,44 +114,41 @@ public void bodyIsEmptyWhenIgnoreBodyIsSetForStreamResponse() throws IOException

try (
Response<HttpBinJSON> response = testInterface.postStreamResponse(getServerUri(false), 42, requestOptions)) {
assertNotNull(response.getBody());
assertEquals(0, response.getBody().getLength());
assertNull(response.getValue());
}
}

@Test
public void bodyIsStreamedWhenResponseBodyModeIndicatesIt() throws IOException {
HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(getHttpClient()).build();
TestInterfaceClientService testInterface = TestInterfaceClientService.getNewInstance(pipeline, null);
assertNotNull(testInterface);
RequestOptions requestOptions = new RequestOptions().setResponseBodyMode(STREAM);

try (
Response<HttpBinJSON> response = testInterface.postStreamResponse(getServerUri(false), 42, requestOptions)) {
assertNotNull(response.getBody());
assertNotEquals(0, response.getBody().getLength());
assertTrue(response.getBody() instanceof InputStreamBinaryData);
}
}

@Test
public void bodyIsBufferedWhenResponseBodyModeIndicatesIt() throws IOException {
HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(getHttpClient()).build();
TestInterfaceClientService testInterface = TestInterfaceClientService.getNewInstance(pipeline, null);
assertNotNull(testInterface);
RequestOptions requestOptions = new RequestOptions().setResponseBodyMode(BUFFER);
HttpBinJSON httpBinJSON = testInterface.postStreamConvenience(getServerUri(false), 42, requestOptions);

assertNotNull(httpBinJSON);

try (
Response<HttpBinJSON> response = testInterface.postStreamResponse(getServerUri(false), 42, requestOptions)) {
assertNotNull(response.getBody());
assertNotEquals(0, response.getBody().getLength());
assertTrue(response.getBody() instanceof ByteArrayBinaryData);
}
}
// TODO (alzimmer): How do we handle streaming?
// @Test
// public void bodyIsStreamedWhenResponseBodyModeIndicatesIt() throws IOException {
// HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(getHttpClient()).build();
// TestInterfaceClientService testInterface = TestInterfaceClientService.getNewInstance(pipeline, null);
// assertNotNull(testInterface);
// RequestOptions requestOptions = new RequestOptions().setResponseBodyMode(STREAM);
//
// try (Response<HttpBinJSON> response = testInterface.postStreamResponse(getServerUri(false), 42, requestOptions)) {
// assertNotNull(response.getBody());
// assertNotEquals(0, response.getBody().getLength());
// assertTrue(response.getBody() instanceof InputStreamBinaryData);
// }
// }
//
// @Test
// public void bodyIsBufferedWhenResponseBodyModeIndicatesIt() throws IOException {
// HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(getHttpClient()).build();
// TestInterfaceClientService testInterface = TestInterfaceClientService.getNewInstance(pipeline, null);
// assertNotNull(testInterface);
// RequestOptions requestOptions = new RequestOptions().setResponseBodyMode(BUFFER);
// HttpBinJSON httpBinJSON = testInterface.postStreamConvenience(getServerUri(false), 42, requestOptions);
//
// assertNotNull(httpBinJSON);
//
// try (Response<HttpBinJSON> response = testInterface.postStreamResponse(getServerUri(false), 42, requestOptions)) {
// assertNotNull(response.getBody());
// assertNotEquals(0, response.getBody().getLength());
// assertTrue(response.getBody() instanceof ByteArrayBinaryData);
// }
// }

@Test
public void bodyIsDeserializedWhenResponseBodyModeIndicatesIt() throws IOException {
Expand All @@ -171,10 +160,7 @@ public void bodyIsDeserializedWhenResponseBodyModeIndicatesIt() throws IOExcepti

assertNotNull(httpBinJSON);

try (
Response<HttpBinJSON> response = testInterface.postStreamResponse(getServerUri(false), 42, requestOptions)) {
assertNotNull(response.getBody());
assertNotEquals(0, response.getBody().getLength());
try (Response<HttpBinJSON> response = testInterface.postStreamResponse(getServerUri(false), 42, requestOptions)) {
assertNotNull(response.getValue());
}
}
Expand Down
Loading