Skip to content

Commit 9ef0e46

Browse files
authored
Add specialized support for avro/binary responses (#10674)
1 parent 06166e6 commit 9ef0e46

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/MockHttpClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.azure.core.test.http;
55

6+
import com.azure.core.http.ContentType;
67
import com.azure.core.http.HttpHeader;
78
import com.azure.core.http.HttpHeaders;
89
import com.azure.core.http.HttpRequest;
@@ -63,7 +64,7 @@ public Mono<HttpResponse> send(HttpRequest request) {
6364
final String byteCountString = requestPath.substring("/bytes/".length());
6465
final int byteCount = Integer.parseInt(byteCountString);
6566
HttpHeaders newHeaders = new HttpHeaders(RESPONSE_HEADERS)
66-
.put("Content-Type", "application/octet-stream")
67+
.put("Content-Type", ContentType.APPLICATION_OCTET_STREAM)
6768
.put("Content-Length", Integer.toString(byteCount));
6869
response = new MockHttpResponse(request, 200, newHeaders, byteCount == 0 ? null : new byte[byteCount]);
6970
} else if (requestPathLower.startsWith("/base64urlbytes/")) {

sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/PlaybackClient.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
// Licensed under the MIT License.
33
package com.azure.core.test.http;
44

5+
import com.azure.core.http.ContentType;
56
import com.azure.core.http.HttpClient;
67
import com.azure.core.http.HttpHeaders;
78
import com.azure.core.http.HttpRequest;
89
import com.azure.core.http.HttpResponse;
9-
import com.azure.core.util.UrlBuilder;
1010
import com.azure.core.test.models.NetworkCallRecord;
1111
import com.azure.core.test.models.RecordedData;
12+
import com.azure.core.util.UrlBuilder;
1213
import com.azure.core.util.logging.ClientLogger;
1314
import reactor.core.Exceptions;
1415
import reactor.core.publisher.Mono;
@@ -111,8 +112,13 @@ private Mono<HttpResponse> playbackHttpResponse(final HttpRequest request) {
111112

112113
String contentType = networkCallRecord.getResponse().get("Content-Type");
113114

114-
// octet-stream's are written to disk using Arrays.toString() which creates an output such as "[12, -1]".
115-
if (contentType != null && contentType.equalsIgnoreCase("application/octet-stream")) {
115+
/*
116+
* application/octet-stream and avro/binary are written to disk using Arrays.toString() which creates an
117+
* output such as "[12, -1]".
118+
*/
119+
if (contentType != null
120+
&& (contentType.equalsIgnoreCase(ContentType.APPLICATION_OCTET_STREAM)
121+
|| contentType.equalsIgnoreCase("avro/binary"))) {
116122
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
117123
for (String piece : rawBody.substring(1, rawBody.length() - 1).split(", ")) {
118124
outputStream.write(Byte.parseByte(piece));

sdk/core/azure-core-test/src/main/java/com/azure/core/test/implementation/RestProxyTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,13 @@ private interface Service9 {
440440
@Put("put")
441441
@ExpectedResponses({200})
442442
@UnexpectedResponseExceptionType(MyRestException.class)
443-
HttpBinJSON putBodyAndContentLength(@BodyParam("application/octet-stream") ByteBuffer body,
443+
HttpBinJSON putBodyAndContentLength(@BodyParam(ContentType.APPLICATION_OCTET_STREAM) ByteBuffer body,
444444
@HeaderParam("Content-Length") long contentLength);
445445

446446
@Put("put")
447447
@ExpectedResponses({200})
448448
@UnexpectedResponseExceptionType(MyRestException.class)
449-
Mono<HttpBinJSON> putAsyncBodyAndContentLength(@BodyParam("application/octet-stream") Flux<ByteBuffer> body,
449+
Mono<HttpBinJSON> putAsyncBodyAndContentLength(@BodyParam(ContentType.APPLICATION_OCTET_STREAM) Flux<ByteBuffer> body,
450450
@HeaderParam("Content-Length") long contentLength);
451451

452452
@Put("put")
@@ -535,7 +535,7 @@ public void syncPutRequestWithBodyAndEqualContentLength() {
535535
final HttpBinJSON json = createService(Service9.class).putBodyAndContentLength(body, 4L);
536536

537537
assertEquals("test", json.data());
538-
assertEquals("application/octet-stream", json.headers().get(("Content-Type")));
538+
assertEquals(ContentType.APPLICATION_OCTET_STREAM, json.headers().get(("Content-Type")));
539539
assertEquals("4", json.headers().get(("Content-Length")));
540540
}
541541

@@ -566,7 +566,7 @@ public void asyncPutRequestWithBodyAndEqualContentLength() {
566566
StepVerifier.create(createService(Service9.class).putAsyncBodyAndContentLength(body, 4L))
567567
.assertNext(json -> {
568568
assertEquals("test", json.data());
569-
assertEquals("application/octet-stream", json.headers().get(("Content-Type")));
569+
assertEquals(ContentType.APPLICATION_OCTET_STREAM, json.headers().get(("Content-Type")));
570570
assertEquals("4", json.headers().get(("Content-Length")));
571571
}).verifyComplete();
572572
}

sdk/core/azure-core-test/src/main/java/com/azure/core/test/policy/RecordNetworkCallPolicy.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.azure.core.test.policy;
55

6+
import com.azure.core.http.ContentType;
67
import com.azure.core.http.HttpHeader;
78
import com.azure.core.http.HttpHeaders;
89
import com.azure.core.http.HttpPipelineCallContext;
@@ -164,7 +165,8 @@ private Mono<Map<String, String>> extractResponseData(final HttpResponse respons
164165
responseData.put(BODY, content);
165166
return responseData;
166167
});
167-
} else if (contentType.equalsIgnoreCase("application/octet-stream")) {
168+
} else if (contentType.equalsIgnoreCase(ContentType.APPLICATION_OCTET_STREAM)
169+
|| contentType.equalsIgnoreCase("avro/binary")) {
168170
return response.getBodyAsByteArray().switchIfEmpty(Mono.just(new byte[0])).map(bytes -> {
169171
if (bytes.length == 0) {
170172
return responseData;

sdk/core/azure-core-test/src/test/java/com/azure/core/test/RestProxyTestsWireMockServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.azure.core.test;
55

6+
import com.azure.core.http.ContentType;
67
import com.azure.core.test.implementation.entities.HttpBinFormDataJSON;
78
import com.azure.core.test.implementation.entities.HttpBinJSON;
89
import com.azure.core.util.DateTimeRfc1123;
@@ -99,7 +100,7 @@ public ResponseDefinition transform(Request request, ResponseDefinition response
99100
private static ResponseDefinition createBytesResponse(String urlPath) {
100101
int bodySize = Integer.parseInt(urlPath.split("/", 3)[2]);
101102
Map<String, String> rawHeaders = getBaseHttpHeaders();
102-
rawHeaders.put("Content-Type", "application/octet-stream");
103+
rawHeaders.put("Content-Type", ContentType.APPLICATION_OCTET_STREAM);
103104
rawHeaders.put("Content-Length", String.valueOf(bodySize));
104105

105106
byte[] body = new byte[bodySize];

0 commit comments

Comments
 (0)