Skip to content

Commit

Permalink
Add specialized support for avro/binary responses (#10674)
Browse files Browse the repository at this point in the history
  • Loading branch information
alzimmermsft authored May 1, 2020
1 parent 06166e6 commit 9ef0e46
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.core.test.http;

import com.azure.core.http.ContentType;
import com.azure.core.http.HttpHeader;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpRequest;
Expand Down Expand Up @@ -63,7 +64,7 @@ public Mono<HttpResponse> send(HttpRequest request) {
final String byteCountString = requestPath.substring("/bytes/".length());
final int byteCount = Integer.parseInt(byteCountString);
HttpHeaders newHeaders = new HttpHeaders(RESPONSE_HEADERS)
.put("Content-Type", "application/octet-stream")
.put("Content-Type", ContentType.APPLICATION_OCTET_STREAM)
.put("Content-Length", Integer.toString(byteCount));
response = new MockHttpResponse(request, 200, newHeaders, byteCount == 0 ? null : new byte[byteCount]);
} else if (requestPathLower.startsWith("/base64urlbytes/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Licensed under the MIT License.
package com.azure.core.test.http;

import com.azure.core.http.ContentType;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpResponse;
import com.azure.core.util.UrlBuilder;
import com.azure.core.test.models.NetworkCallRecord;
import com.azure.core.test.models.RecordedData;
import com.azure.core.util.UrlBuilder;
import com.azure.core.util.logging.ClientLogger;
import reactor.core.Exceptions;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -111,8 +112,13 @@ private Mono<HttpResponse> playbackHttpResponse(final HttpRequest request) {

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

// octet-stream's are written to disk using Arrays.toString() which creates an output such as "[12, -1]".
if (contentType != null && contentType.equalsIgnoreCase("application/octet-stream")) {
/*
* application/octet-stream and avro/binary are written to disk using Arrays.toString() which creates an
* output such as "[12, -1]".
*/
if (contentType != null
&& (contentType.equalsIgnoreCase(ContentType.APPLICATION_OCTET_STREAM)
|| contentType.equalsIgnoreCase("avro/binary"))) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (String piece : rawBody.substring(1, rawBody.length() - 1).split(", ")) {
outputStream.write(Byte.parseByte(piece));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,13 @@ private interface Service9 {
@Put("put")
@ExpectedResponses({200})
@UnexpectedResponseExceptionType(MyRestException.class)
HttpBinJSON putBodyAndContentLength(@BodyParam("application/octet-stream") ByteBuffer body,
HttpBinJSON putBodyAndContentLength(@BodyParam(ContentType.APPLICATION_OCTET_STREAM) ByteBuffer body,
@HeaderParam("Content-Length") long contentLength);

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

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

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

Expand Down Expand Up @@ -566,7 +566,7 @@ public void asyncPutRequestWithBodyAndEqualContentLength() {
StepVerifier.create(createService(Service9.class).putAsyncBodyAndContentLength(body, 4L))
.assertNext(json -> {
assertEquals("test", json.data());
assertEquals("application/octet-stream", json.headers().get(("Content-Type")));
assertEquals(ContentType.APPLICATION_OCTET_STREAM, json.headers().get(("Content-Type")));
assertEquals("4", json.headers().get(("Content-Length")));
}).verifyComplete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.core.test.policy;

import com.azure.core.http.ContentType;
import com.azure.core.http.HttpHeader;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpPipelineCallContext;
Expand Down Expand Up @@ -164,7 +165,8 @@ private Mono<Map<String, String>> extractResponseData(final HttpResponse respons
responseData.put(BODY, content);
return responseData;
});
} else if (contentType.equalsIgnoreCase("application/octet-stream")) {
} else if (contentType.equalsIgnoreCase(ContentType.APPLICATION_OCTET_STREAM)
|| contentType.equalsIgnoreCase("avro/binary")) {
return response.getBodyAsByteArray().switchIfEmpty(Mono.just(new byte[0])).map(bytes -> {
if (bytes.length == 0) {
return responseData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.core.test;

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

byte[] body = new byte[bodySize];
Expand Down

0 comments on commit 9ef0e46

Please sign in to comment.