Skip to content

Commit

Permalink
Merge pull request #8 from samvaity/review-feedback-test-proxy
Browse files Browse the repository at this point in the history
Update matcher feedback + banned dep update
  • Loading branch information
billwert authored Feb 22, 2023
2 parents 6653199 + d027723 commit 0711aee
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 40 deletions.
1 change: 1 addition & 0 deletions sdk/core/azure-core-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
<includes>
<include>io.projectreactor:reactor-test:[3.4.26]</include> <!-- {x-include-update;io.projectreactor:reactor-test;external_dependency} -->
<include>com.fasterxml.jackson.dataformat:jackson-dataformat-xml:[2.13.5]</include> <!-- {x-include-update;com.fasterxml.jackson.dataformat:jackson-dataformat-xml;external_dependency} -->
<include>io.projectreactor.netty:reactor-netty-http:[1.0.27]</include> <!-- {x-include-update;io.projectreactor.netty:reactor-netty-http;external_dependency} -->
<!-- special allowance for azure-core-test as it is not a shipping library: -->
<include>org.junit.jupiter:junit-jupiter-api:[5.9.1]</include> <!-- {x-include-update;org.junit.jupiter:junit-jupiter-api;external_dependency} -->
<include>org.junit.jupiter:junit-jupiter-params:[5.9.1]</include> <!-- {x-include-update;org.junit.jupiter:junit-jupiter-params;external_dependency} -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@

package com.azure.core.test.models;

import java.util.List;

/**
* This matcher exposes the default matcher in a customizable way.
* Currently, this includes ignoring/excluding headers, comparing request bodies and ignoring query params or query params ordering.
*/
public class CustomMatcher extends TestProxyRequestMatcher {
private String excludedHeaders;
private List<String> excludedHeaders;

private String ignoredHeaders;
private boolean ignoreQueryOrdering;
private String ignoredQueryParameters;
private List<String> headersKeyOnlyMatch;
private boolean queryOrderingIgnored;
private List<String> ignoredQueryParameters;

private boolean compareBodies;
private boolean comparingBodies;

/**
* Creates an instance of CustomMatcher
Expand All @@ -24,110 +26,110 @@ public CustomMatcher() {
}

/**
* Gets the A comma separated list of headers that should be excluded during matching. For example,
* "Authorization, Content-Length",.
* Gets the list of headers that should be excluded during matching.
* The presence of these headers will not be taken into account while matching.
*
* @return the excluded headers
* @return the excluded headers list
*/
public String getExcludedHeaders() {
public List<String> getExcludedHeaders() {
return excludedHeaders;
}

/**
* Sets the comma separated list of headers that should be excluded during matching. For example,
* "Authorization, Content-Length",.
* Sets the list of headers that should be excluded during matching.
* The presence of these headers will not be taken into account while matching.
*
* @param excludedHeaders the excluded headers
* @param excludedHeaders the list of excluded headers
* @return The updated {@link CustomMatcher} object.
*/
public CustomMatcher setExcludedHeaders(String excludedHeaders) {
public CustomMatcher setExcludedHeaders(List<String> excludedHeaders) {
this.excludedHeaders = excludedHeaders;
return this;
}


/**
* Gets the comma separated list of headers that should be ignored during matching.
* Gets the list of headers that should be ignored during matching.
* The header values won't be matched, but the presence of these headers will be taken into account while matching.
*
* @return the ignored headers
* @return the ignored headers list
*/
public String getIgnoredHeaders() {
return ignoredHeaders;
public List<String> getHeadersKeyOnlyMatch() {
return headersKeyOnlyMatch;
}


/**
* Sets ignored headers. A comma separated list of headers that should be ignored during matching.
* Sets the list of headers that should be ignored during matching.
* The header values won't be matched, but the presence of these headers will be taken into account while matching.
*
* @param ignoredHeaders the ignored headers
* @param headersKeyOnlyMatch the ignored headers list
* @return The updated {@link CustomMatcher} object.
*/
public CustomMatcher setIgnoredHeaders(String ignoredHeaders) {
this.ignoredHeaders = ignoredHeaders;
public CustomMatcher setHeadersKeyOnlyMatch(List<String> headersKeyOnlyMatch) {
this.headersKeyOnlyMatch = headersKeyOnlyMatch;
return this;
}

/**
* Get the boolean value to sort query params alphabetically before comparing URIs when matching
* @return the boolean
*/
public boolean isIgnoreQueryOrdering() {
return ignoreQueryOrdering;
public boolean isQueryOrderingIgnored() {
return queryOrderingIgnored;
}

/**
* Sets query ordering to a boolean value to sort query params alphabetically before comparing URIs when matching.
*
* @param ignoreQueryOrdering the ignore query ordering boolean value
* @param queryOrderingIgnored the ignore query ordering boolean value
* @return The updated {@link CustomMatcher} object.
*/
public CustomMatcher setIgnoreQueryOrdering(boolean ignoreQueryOrdering) {
this.ignoreQueryOrdering = ignoreQueryOrdering;
public CustomMatcher setQueryOrderingIgnored(boolean queryOrderingIgnored) {
this.queryOrderingIgnored = queryOrderingIgnored;
return this;
}

/**
* Gets the comma separated list of query parameters that should be ignored during matching.
* Gets the list of query parameters that should be ignored during matching.
* The parameter values won't be matched, but the presence of these parameters will be taken into account.
*
* @return the ignored query parameters
*/
public String getIgnoredQueryParameters() {
public List<String> getIgnoredQueryParameters() {
return ignoredQueryParameters;
}

/**
* Sets the comma separated list of query parameters that should be ignored during matching.
* Sets the list of query parameters that should be ignored during matching.
* The parameter values won't be matched, but the presence of these parameters will be taken into account.
*
* @param ignoredQueryParameters the ignored query parameters
* @return The updated {@link CustomMatcher} object.
*/
public CustomMatcher setIgnoredQueryParameters(String ignoredQueryParameters) {
public CustomMatcher setIgnoredQueryParameters(List<String> ignoredQueryParameters) {
this.ignoredQueryParameters = ignoredQueryParameters;
return this;
}

/**
* Get the compare bodies boolean.
* Get the comparing bodies boolean.
* True to enable body matching (default behavior), or false to disable body matching.
*
* @return the boolean
*/
public boolean isCompareBodies() {
return compareBodies;
public boolean isComparingBodies() {
return comparingBodies;
}

/**
* Sets true to enable body matching (default behavior), or false to disable body matching.
*
* @param compareBodies the compare bodies
* @param comparingBodies the compare bodies
* @return The updated {@link CustomMatcher} object.
*/
public CustomMatcher setCompareBodies(boolean compareBodies) {
this.compareBodies = compareBodies;
public CustomMatcher setComparingBodies(boolean comparingBodies) {
this.comparingBodies = comparingBodies;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.azure.core.test.models.TestProxySanitizer;
import com.azure.core.test.models.TestProxySanitizerType;
import com.azure.core.util.UrlBuilder;
import com.azure.core.util.logging.ClientLogger;

import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -24,6 +25,7 @@
* Utility functions for interaction with the test proxy.
*/
public class TestProxyUtils {
private static final ClientLogger LOGGER = new ClientLogger(TestProxyUtils.class);
private static final String PROXY_URL_SCHEME = "http";
private static final String PROXY_URL_HOST = "localhost";
private static final int PROXY_URL_PORT = 5000;
Expand Down Expand Up @@ -133,11 +135,25 @@ public static List<TestProxyRequestMatcher> loadMatchers() {
}

private static TestProxyRequestMatcher addDefaultCustomDefaultMatcher() {
return new CustomMatcher().setExcludedHeaders(String.join(",", EXCLUDED_HEADERS));
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}", customMatcher.getIgnoredHeaders(), customMatcher.getExcludedHeaders(), customMatcher.isCompareBodies(), customMatcher.getIgnoredQueryParameters(), customMatcher.isIgnoreQueryOrdering());
return String.format("{\"ignoredHeaders\":\"%s\",\"excludedHeaders\":\"%s\",\"compareBodies\":%s,\"ignoredQueryParameters\":\"%s\", \"ignoreQueryOrdering\":%s}",
getCommaSeperatedString(customMatcher.getHeadersKeyOnlyMatch()),
getCommaSeperatedString(customMatcher.getExcludedHeaders()),
customMatcher.isComparingBodies(),
getCommaSeperatedString(customMatcher.getIgnoredQueryParameters()),
customMatcher.isQueryOrderingIgnored());
}

private static String getCommaSeperatedString(List<String> stringList) {
if (stringList == null) {
return null;
}
return stringList.stream()
.filter(s -> s != null && !s.isEmpty())
.collect(Collectors.joining(","));
}

private static String createUrlRegexRequestBody(String regexValue, String redactedValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SuppressWarnings("deprecation")
/**
* Test class for testing Test proxy functionality of record, playback and redaction.
*/
public class TestProxyTests extends TestProxyTestBase {
static TestProxyTestServer server;
private static final ObjectMapper RECORD_MAPPER = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
Expand Down Expand Up @@ -196,7 +198,7 @@ public void testRecordWithRedaction() {
@Tag("Playback")
public void testPlaybackWithRedaction() {
interceptorManager.addSanitizers(customSanitizer);
interceptorManager.addMatchers(new ArrayList<>(Arrays.asList(new CustomMatcher().setExcludedHeaders("Ocp-Apim-Subscription-Key"))));
interceptorManager.addMatchers(new ArrayList<>(Arrays.asList(new CustomMatcher().setExcludedHeaders(Arrays.asList("Ocp-Apim-Subscription-Key")))));
HttpClient client = interceptorManager.getPlaybackClient();
URL url;

Expand Down

0 comments on commit 0711aee

Please sign in to comment.