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

Add http clients to test proxy #33939

Merged
6 commits merged into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -71,6 +71,7 @@ public class InterceptorManager implements AutoCloseable {
private TestProxyRecordPolicy testProxyRecordPolicy;
private TestProxyPlaybackClient testProxyPlaybackClient;
private final Queue<String> proxyVariableQueue = new LinkedList<>();
private HttpClient httpClient;

/**
* Creates a new InterceptorManager that either replays test-session records or saves them.
Expand Down Expand Up @@ -270,6 +271,8 @@ public Consumer<String> getProxyVariableConsumer() {
* {@link InterceptorManager}.
*
* @return HttpPipelinePolicy to record network calls.
*
* @throws RuntimeException A playback client was requested when the test proxy is enabled and test mode is LIVE.
*/
public HttpPipelinePolicy getRecordPolicy() {
if (testProxyEnabled) {
Expand Down Expand Up @@ -297,11 +300,16 @@ public HttpPipelinePolicy getRecordPolicy(List<Function<String, String>> recordi
* Gets a new HTTP client that plays back test session records managed by {@link InterceptorManager}.
*
* @return An HTTP client that plays back network calls from its recorded data.
*
* @throws RuntimeException A playback client was requested when the test proxy is enabled and test mode is LIVE.
*/
public HttpClient getPlaybackClient() {
if (testProxyEnabled) {
if (isLiveMode()) {
billwert marked this conversation as resolved.
Show resolved Hide resolved
throw new RuntimeException("A playback client cannot be requested in LIVE mode.");
billwert marked this conversation as resolved.
Show resolved Hide resolved
}
if (testProxyPlaybackClient == null) {
testProxyPlaybackClient = new TestProxyPlaybackClient();
testProxyPlaybackClient = new TestProxyPlaybackClient(httpClient);
proxyVariableQueue.addAll(testProxyPlaybackClient.startPlayback(playbackRecordName));
}
return testProxyPlaybackClient;
Expand Down Expand Up @@ -346,7 +354,10 @@ private RecordedData readDataFromFile() {

private HttpPipelinePolicy getProxyRecordingPolicy() {
if (testProxyRecordPolicy == null) {
testProxyRecordPolicy = new TestProxyRecordPolicy();
if (isLiveMode()) {
throw new RuntimeException("A recording policy cannot be requested in LIVE mode.");
}
testProxyRecordPolicy = new TestProxyRecordPolicy(httpClient);
testProxyRecordPolicy.startRecording(playbackRecordName);
}
return testProxyRecordPolicy;
Expand Down Expand Up @@ -433,4 +444,13 @@ public void addMatchers(List<TestProxyRequestMatcher> testProxyMatchers) {
throw new RuntimeException("Playback must have been started before adding matchers.");
}
}

/**
* Sets the httpClient to be used for this test.
* @param httpClient The {@link HttpClient} implementation to use.
*/
void setHttpClient(HttpClient httpClient) {

this.httpClient = httpClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public void setupTest(TestInfo testInfo) {
localTestMode = TestMode.RECORD;
} else if (testInfo.getTags().contains("Playback")) {
localTestMode = TestMode.PLAYBACK;
} else if (testInfo.getTags().contains("Live")) {
localTestMode = TestMode.LIVE;
}
this.testContextManager = new TestContextManager(testInfo.getTestMethod().get(), localTestMode, isTestProxyEnabled());
testContextManager.setTestIteration(testIterationContext.getTestIteration());
Expand All @@ -155,6 +157,7 @@ public void setupTest(TestInfo testInfo) {
}

if (isTestProxyEnabled()) {
interceptorManager.setHttpClient(getHttpClients().findFirst().orElse(null));
// The supplier/consumer are used to retrieve/store variables over the wire.
testResourceNamer = new TestResourceNamer(testContextManager,
interceptorManager.getProxyVariableConsumer(),
Expand Down Expand Up @@ -238,7 +241,7 @@ public static Stream<HttpClient> getHttpClients() {
* In LIVE or RECORD mode load all HttpClient instances and let the test run determine which HttpClient
* implementation it will use.
*/
if (testMode == TestMode.PLAYBACK) {
if (testMode == TestMode.PLAYBACK && !enableTestProxy) {
return Stream.of(new HttpClient[] { null });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
public class TestProxyPlaybackClient implements HttpClient {

private final HttpURLConnectionHttpClient client = new HttpURLConnectionHttpClient();
private final HttpClient client;
private String xRecordingId;
private static final SerializerAdapter SERIALIZER = new JacksonAdapter();

Expand All @@ -47,8 +47,11 @@ public class TestProxyPlaybackClient implements HttpClient {

/**
* Create an instance of {@link TestProxyPlaybackClient} with a list of custom sanitizers.
*
* @param httpClient The {@link HttpClient} to use. If none is passed {@link HttpURLConnectionHttpClient} is the default.
*/
public TestProxyPlaybackClient() {
public TestProxyPlaybackClient(HttpClient httpClient) {
this.client = (httpClient == null ? new HttpURLConnectionHttpClient() : httpClient);
this.sanitizers.addAll(DEFAULT_SANITIZERS);
}

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.HttpClient;
import com.azure.core.http.HttpMethod;
import com.azure.core.http.HttpPipelineCallContext;
import com.azure.core.http.HttpPipelineNextPolicy;
Expand Down Expand Up @@ -36,15 +37,18 @@
*/
public class TestProxyRecordPolicy implements HttpPipelinePolicy {
private static final SerializerAdapter SERIALIZER = new JacksonAdapter();
private final HttpURLConnectionHttpClient client = new HttpURLConnectionHttpClient();
private final HttpClient client;
private String xRecordingId;
private final List<TestProxySanitizer> sanitizers = new ArrayList<>();
private static final List<TestProxySanitizer> DEFAULT_SANITIZERS = loadSanitizers();

/**
* Create an instance of {@link TestProxyRecordPolicy} with a list of custom sanitizers.
*
* @param httpClient The {@link HttpClient} to use. If none is passed {@link HttpURLConnectionHttpClient} is the default.
*/
public TestProxyRecordPolicy() {
public TestProxyRecordPolicy(HttpClient httpClient) {
this.client = (httpClient == null ? new HttpURLConnectionHttpClient() : httpClient);
this.sanitizers.addAll(DEFAULT_SANITIZERS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ public void testPlayback() {
assertEquals(200, response.getStatusCode());
}

@Test
@Tag("Live")
public void testCannotGetPlaybackClient() {
RuntimeException thrown = assertThrows(RuntimeException.class, () -> interceptorManager.getPlaybackClient());
assertEquals("A playback client cannot be requested in LIVE mode.", thrown.getMessage());
}

@Test
@Tag("Live")
public void testCannotGetRecordPolicy() {
RuntimeException thrown = assertThrows(RuntimeException.class, () -> interceptorManager.getRecordPolicy());
assertEquals("A recording policy cannot be requested in LIVE mode.", thrown.getMessage());
}

@Test
@Tag("Record")
public void testRecordWithRedaction() {
Expand Down