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

Fix Exception in Test Recording #24675

Merged
merged 2 commits into from
Oct 8, 2021
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 @@ -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;
Expand Down Expand Up @@ -180,9 +181,9 @@ private static Mono<Tuple2<HttpResponse, Map<String, String>>> extractResponseDa
}

final HttpResponse bufferedResponse = response.buffer();
final Mono<byte[]> 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);
Expand All @@ -195,8 +196,7 @@ private static Mono<Tuple2<HttpResponse, Map<String, String>>> 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);
Expand All @@ -206,15 +206,14 @@ private static Mono<Tuple2<HttpResponse, Map<String, String>>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
}
}