diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/policy/RecordNetworkCallPolicy.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/policy/RecordNetworkCallPolicy.java index 9e8eb0f1d6d7a..0f2dbcd1ad25a 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/policy/RecordNetworkCallPolicy.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/policy/RecordNetworkCallPolicy.java @@ -17,6 +17,7 @@ import com.azure.core.test.models.RecordedData; import com.azure.core.test.models.RecordingRedactor; import com.azure.core.util.CoreUtils; +import com.azure.core.util.FluxUtil; import com.azure.core.util.UrlBuilder; import com.azure.core.util.logging.ClientLogger; import reactor.core.Exceptions; @@ -180,9 +181,9 @@ private static Mono>> extractResponseDa } final HttpResponse bufferedResponse = response.buffer(); + final Mono responseBody = FluxUtil.collectBytesInByteBufferStream(bufferedResponse.getBody()); if (contentType == null) { - return bufferedResponse.getBodyAsByteArray() - .switchIfEmpty(Mono.defer(() -> Mono.just(new byte[0]))) + return responseBody.switchIfEmpty(Mono.defer(() -> Mono.just(new byte[0]))) .map(bytes -> { if (bytes.length == 0) { return Tuples.of(bufferedResponse, responseData); @@ -195,8 +196,7 @@ private static Mono>> extractResponseDa }); } else if (contentType.equalsIgnoreCase(ContentType.APPLICATION_OCTET_STREAM) || contentType.equalsIgnoreCase("avro/binary")) { - return bufferedResponse.getBodyAsByteArray() - .switchIfEmpty(Mono.defer(() -> Mono.just(new byte[0]))) + return responseBody.switchIfEmpty(Mono.defer(() -> Mono.just(new byte[0]))) .map(bytes -> { if (bytes.length == 0) { return Tuples.of(bufferedResponse, responseData); @@ -206,15 +206,14 @@ private static Mono>> extractResponseDa return Tuples.of(bufferedResponse, responseData); }); } else if (contentType.contains("json") || response.getHeaderValue(CONTENT_ENCODING) == null) { - return bufferedResponse.getBodyAsString(StandardCharsets.UTF_8) + return responseBody.map(bytes -> CoreUtils.bomAwareToString(bytes, response.getHeaderValue(CONTENT_TYPE))) .switchIfEmpty(Mono.defer(() -> Mono.just(""))) .map(content -> { responseData.put(BODY, redactor.redact(content)); return Tuples.of(bufferedResponse, responseData); }); } else { - return bufferedResponse.getBodyAsByteArray() - .switchIfEmpty(Mono.defer(() -> Mono.just(new byte[0]))) + return responseBody.switchIfEmpty(Mono.defer(() -> Mono.just(new byte[0]))) .map(bytes -> { if (bytes.length == 0) { return Tuples.of(bufferedResponse, responseData); diff --git a/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableAsyncClientTest.java b/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableAsyncClientTest.java index 482abce5f52b7..004b103a2fb9a 100644 --- a/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableAsyncClientTest.java +++ b/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableAsyncClientTest.java @@ -55,8 +55,7 @@ public class TableAsyncClientTest extends TestBase { private static final Duration TIMEOUT = Duration.ofSeconds(100); private static final HttpClient DEFAULT_HTTP_CLIENT = HttpClient.createDefault(); - private static final boolean IS_COSMOS_TEST = System.getenv("AZURE_TABLES_CONNECTION_STRING") != null - && System.getenv("AZURE_TABLES_CONNECTION_STRING").contains("cosmos.azure.com"); + private static final boolean IS_COSMOS_TEST = TestUtils.isCosmosTest(); private TableAsyncClient tableClient; private HttpPipelinePolicy recordPolicy; diff --git a/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableClientTest.java b/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableClientTest.java index 4f382bc14bd8e..5550796dbf972 100644 --- a/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableClientTest.java +++ b/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableClientTest.java @@ -54,8 +54,8 @@ */ public class TableClientTest extends TestBase { private static final HttpClient DEFAULT_HTTP_CLIENT = HttpClient.createDefault(); - private static final boolean IS_COSMOS_TEST = System.getenv("AZURE_TABLES_CONNECTION_STRING") != null - && System.getenv("AZURE_TABLES_CONNECTION_STRING").contains("cosmos.azure.com"); + private static final boolean IS_COSMOS_TEST = TestUtils.isCosmosTest(); + private TableClient tableClient; private HttpPipelinePolicy recordPolicy; diff --git a/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableServiceAsyncClientTest.java b/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableServiceAsyncClientTest.java index b2dc436297ae4..e6bec1ea549f7 100644 --- a/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableServiceAsyncClientTest.java +++ b/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableServiceAsyncClientTest.java @@ -52,8 +52,7 @@ public class TableServiceAsyncClientTest extends TestBase { private static final Duration TIMEOUT = Duration.ofSeconds(100); private static final HttpClient DEFAULT_HTTP_CLIENT = HttpClient.createDefault(); - private static final boolean IS_COSMOS_TEST = System.getenv("AZURE_TABLES_CONNECTION_STRING") != null - && System.getenv("AZURE_TABLES_CONNECTION_STRING").contains("cosmos.azure.com"); + private static final boolean IS_COSMOS_TEST = TestUtils.isCosmosTest(); private TableServiceAsyncClient serviceClient; private HttpPipelinePolicy recordPolicy; diff --git a/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableServiceClientTest.java b/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableServiceClientTest.java index 960b96e0f020f..617f761b55f5a 100644 --- a/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableServiceClientTest.java +++ b/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TableServiceClientTest.java @@ -55,8 +55,7 @@ */ public class TableServiceClientTest extends TestBase { private static final HttpClient DEFAULT_HTTP_CLIENT = HttpClient.createDefault(); - private static final boolean IS_COSMOS_TEST = System.getenv("AZURE_TABLES_CONNECTION_STRING") != null - && System.getenv("AZURE_TABLES_CONNECTION_STRING").contains("cosmos.azure.com"); + private static final boolean IS_COSMOS_TEST = TestUtils.isCosmosTest(); private TableServiceClient serviceClient; private HttpPipelinePolicy recordPolicy; diff --git a/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TestUtils.java b/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TestUtils.java index 642036527d02b..3dfc02487bdce 100644 --- a/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TestUtils.java +++ b/sdk/tables/azure-data-tables/src/test/java/com/azure/data/tables/TestUtils.java @@ -13,6 +13,7 @@ import com.azure.core.http.HttpResponse; import com.azure.core.http.policy.HttpPipelinePolicy; import com.azure.core.test.http.MockHttpResponse; +import com.azure.core.util.Configuration; import com.azure.core.util.CoreUtils; import com.azure.core.util.DateTimeRfc1123; import com.azure.data.tables.models.TableServiceProperties; @@ -46,7 +47,7 @@ private TestUtils() { public static String getConnectionString(boolean isPlaybackMode) { return isPlaybackMode ? "DefaultEndpointsProtocol=https;AccountName=dummyAccount;AccountKey=xyzDummy;EndpointSuffix=core.windows.net" - : System.getenv("AZURE_TABLES_CONNECTION_STRING"); + : Configuration.getGlobalConfiguration().get("AZURE_TABLES_CONNECTION_STRING"); } public static HttpRequest request(String url) throws MalformedURLException { @@ -185,4 +186,10 @@ static void assertPropertiesEquals(TableServiceProperties expected, assertNull(actual.getMinuteMetrics()); } } + + static boolean isCosmosTest() { + Configuration globalConfiguration = Configuration.getGlobalConfiguration(); + return globalConfiguration.get("AZURE_TABLES_CONNECTION_STRING") != null + && globalConfiguration.get("AZURE_TABLES_CONNECTION_STRING").contains("cosmos.azure.com"); + } }