Skip to content

Commit

Permalink
Merge pull request Azure#10 from samvaity/test-proxy-2
Browse files Browse the repository at this point in the history
checkstyle + updates
  • Loading branch information
billwert authored Feb 27, 2023
2 parents 3cb205a + 304d1f8 commit ce4cfc1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ public RecordedData getRecordedData() {
public Supplier<String> getProxyVariableSupplier() {
return () -> {
Objects.requireNonNull(this.testProxyPlaybackClient, "Playback must be started to retrieve values");
return proxyVariableQueue.remove();
if (!CoreUtils.isNullOrEmpty(proxyVariableQueue)) {
return proxyVariableQueue.remove();
} else {
throw LOGGER.logExceptionAsError(new RuntimeException("'proxyVariableQueue' cannot be null or empty."));
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import static com.azure.core.test.utils.TestProxyUtils.checkForTestProxyErrors;
import static com.azure.core.test.utils.TestProxyUtils.getMatcherRequests;
import static com.azure.core.test.utils.TestProxyUtils.getSanitizerRequests;
import static com.azure.core.test.utils.TestProxyUtils.loadMatchers;
import static com.azure.core.test.utils.TestProxyUtils.loadSanitizers;

/**
Expand All @@ -44,16 +43,13 @@ public class TestProxyPlaybackClient implements HttpClient {
private static final List<TestProxySanitizer> DEFAULT_SANITIZERS = loadSanitizers();
private final List<TestProxySanitizer> sanitizers = new ArrayList<>();

private static final List<TestProxyRequestMatcher> DEFAULT_MATCHERS = loadMatchers();

private final List<TestProxyRequestMatcher> matchers = new ArrayList<>();

/**
* Create an instance of {@link TestProxyPlaybackClient} with a list of custom sanitizers.
*/
public TestProxyPlaybackClient() {
this.sanitizers.addAll(DEFAULT_SANITIZERS);
this.matchers.addAll(DEFAULT_MATCHERS);
}

/**
Expand Down Expand Up @@ -118,6 +114,22 @@ public Mono<HttpResponse> send(HttpRequest request) {
});
}

/**
* Redirects the request to the test-proxy to retrieve the playback response synchronously.
* @param request The HTTP request to send.
* @return The HTTP response.
*/
@Override
public HttpResponse sendSync(HttpRequest request, Context context) {
if (xRecordingId == null) {
throw new RuntimeException("Playback was not started before a request was sent.");
}
TestProxyUtils.changeHeaders(request, xRecordingId, "playback");
HttpResponse response = client.sendSync(request, context);
TestProxyUtils.checkForTestProxyErrors(response);
return response;
}

/**
* Add a list of {@link TestProxySanitizer} to the current playback session.
* @param sanitizers The sanitizers to add.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public boolean isQueryOrderingIgnored() {
/**
* Sets query ordering to a boolean value to sort query params alphabetically before comparing URIs when matching.
*
* @param queryOrderingIgnored the ignore query ordering boolean value
* @param queryOrderingIgnored to ignore query ordering boolean value
* @return The updated {@link CustomMatcher} object.
*/
public CustomMatcher setQueryOrderingIgnored(boolean queryOrderingIgnored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -78,7 +77,6 @@ private HttpResponse createHttpResponse(HttpURLConnection connection, HttpReques
return null;
}


return new HttpURLResponse(connection, request);
}

Expand Down Expand Up @@ -150,8 +148,6 @@ protected HttpURLResponse(HttpRequest request) {
this.body = null;
}
} catch (IOException e) {


throw new UncheckedIOException(e);
}

Expand Down Expand Up @@ -201,5 +197,5 @@ public Mono<String> getBodyAsString() {
public Mono<String> getBodyAsString(Charset charset) {
return Mono.just(new String(body.array(), charset));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public class TestProxyUtils {
"(?:User ID=)(?<secret>.*)(?:;)", "(?:<PrimaryKey>)(?<secret>.*)(?:</PrimaryKey>)",
"(?:<SecondaryKey>)(?<secret>.*)(?:</SecondaryKey>)"));

// Should be removed once playback client is fixed
private static final List<String> EXCLUDED_HEADERS =
new ArrayList<>(Arrays.asList("Connection", "Content-Length"));

private static final String URL_REGEX = "(?<=http://|https://)([^/?]+)";
private static final List<String> HEADERS_TO_REDACT = new ArrayList<>(Arrays.asList("Ocp-Apim-Subscription-Key"));
private static final String REDACTED_VALUE = "REDACTED";
Expand Down Expand Up @@ -144,20 +140,6 @@ public static List<TestProxySanitizer> loadSanitizers() {
return sanitizers;
}

/**
* Registers the default set of matchers for matching requests on playback.
* @return The list of default matchers to be added.
*/
public static List<TestProxyRequestMatcher> loadMatchers() {
List<TestProxyRequestMatcher> matchers = new ArrayList<>();
matchers.add(addDefaultCustomDefaultMatcher());
return matchers;
}

private static TestProxyRequestMatcher addDefaultCustomDefaultMatcher() {
return new CustomMatcher().setExcludedHeaders(EXCLUDED_HEADERS);
}

private static String createCustomMatcherRequestBody(CustomMatcher customMatcher) {
return String.format("{\"ignoredHeaders\":\"%s\",\"excludedHeaders\":\"%s\",\"compareBodies\":%s,\"ignoredQueryParameters\":\"%s\", \"ignoreQueryOrdering\":%s}",
getCommaSeperatedString(customMatcher.getHeadersKeyOnlyMatch()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package com.azure.core.test.utils;

import com.azure.core.test.InterceptorManager;
import com.azure.core.util.logging.ClientLogger;
import org.junit.jupiter.api.Assertions;

Expand Down

0 comments on commit ce4cfc1

Please sign in to comment.