Package com.azure.core.test
Class InterceptorManager
java.lang.Object
com.azure.core.test.InterceptorManager
- All Implemented Interfaces:
AutoCloseable
A class that keeps track of network calls by either reading the data from an existing test session record or
recording the network calls in memory. Test session records are saved or read from:
"session-records/
testName.json"
- If the
testModeisTestMode.PLAYBACK, the manager tries to find an existing test session record to read network calls from. - If the
testModeisTestMode.RECORD, the manager creates a new test session record and saves all the network calls to it. - If the
testModeisTestMode.LIVE, the manager won't attempt to read or create a test session record.
InterceptorManager is disposed, if the testMode is TestMode.RECORD, the network
calls that were recorded are persisted to: "session-records/testName.json"-
Constructor Summary
ConstructorsConstructorDescriptionInterceptorManager(TestContextManager testContextManager) Creates a new InterceptorManager that either replays test-session records or saves them.InterceptorManager(String testName, TestMode testMode) Deprecated.InterceptorManager(String testName, Map<String, String> textReplacementRules) Deprecated.UseInterceptorManager(String, Map, boolean)instead.InterceptorManager(String testName, Map<String, String> textReplacementRules, boolean doNotRecord) Deprecated.UseInterceptorManager(String, Map, boolean, String)instead.InterceptorManager(String testName, Map<String, String> textReplacementRules, boolean doNotRecord, String playbackRecordName) Creates a new InterceptorManager that replays test session records. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMatchers(com.azure.core.test.models.TestProxyRequestMatcher... testProxyRequestMatchers) Add matcher rules to match recorded data in playback.voidaddMatchers(List<com.azure.core.test.models.TestProxyRequestMatcher> testProxyMatchers) Add matcher rules to match recorded data in playback.voidaddSanitizers(com.azure.core.test.models.TestProxySanitizer... testProxySanitizers) Add sanitizer rule for sanitization during record or playback.voidaddSanitizers(List<com.azure.core.test.models.TestProxySanitizer> testProxySanitizers) Add sanitizer rule for sanitization during record or playback.voidaddTextReplacementRule(String regex, String replacement) Add text replacement rule (regex as key, the replacement text as value) intotextReplacementRulesvoidclose()Disposes of resources used by this InterceptorManager.Gets a new HTTP client that plays back test session records managed byInterceptorManager.Gets the name of the playback record.Get aConsumerfor adding variables used in test proxy tests.ASupplierfor retrieving a variable from a test proxy recording.com.azure.core.test.models.RecordedDataGets the recorded data InterceptorManager is keeping track of.Get the recording file location in assets repo.Gets a new HTTP pipeline policy that records network calls and its data is managed byInterceptorManager.getRecordPolicy(List<Function<String, String>> recordingRedactors) Gets a new HTTP pipeline policy that records network calls.Gets the name of the running test.booleanGets whether this InterceptorManager is in live mode.booleanGets whether this InterceptorManager is in playback mode.booleanGets whether this InterceptorManager is in record mode.voidsetProxyRecordingOptions(com.azure.core.test.models.TestProxyRecordingOptions testProxyRecordingOptions) Sets the recording options for the proxy.
-
Constructor Details
-
InterceptorManager
Deprecated.UseInterceptorManager(TestContextManager)instead.Creates a new InterceptorManager that either replays test-session records or saves them.- If
testModeisTestMode.PLAYBACK, the manager tries to find an existing test session record to read network calls from. - If
testModeisTestMode.RECORD, the manager creates a new test session record and saves all the network calls to it.
testName.json"- Parameters:
testName- Name of the test session record.testMode- TheTestModefor this interceptor.- Throws:
UncheckedIOException- IftestModeisTestMode.PLAYBACKand an existing test session record could not be located or the data could not be deserialized into an instance ofRecordedData.NullPointerException- IftestNameisnull.
- If
-
InterceptorManager
Creates a new InterceptorManager that either replays test-session records or saves them.- If
testModeisTestMode.PLAYBACK, the manager tries to find an existing test session record to read network calls from. - If
testModeisTestMode.RECORD, the manager creates a new test session record and saves all the network calls to it. - If
testModeisTestMode.LIVE, the manager won't attempt to read or create a test session record.
testName.json"- Parameters:
testContextManager- Contextual information about the test being ran, such as test name,TestMode, and others.- Throws:
UncheckedIOException- IftestModeisTestMode.PLAYBACKand an existing test session record could not be located or the data could not be deserialized into an instance ofRecordedData.NullPointerException- IftestNameisnull.
- If
-
InterceptorManager
Deprecated.UseInterceptorManager(String, Map, boolean)instead.Creates a new InterceptorManager that replays test session records. It takes a set oftextReplacementRules, that can be used byPlaybackClientto replace values in aNetworkCallRecord.getResponse(). The test session records are read from: "session-records/testName.json"- Parameters:
testName- Name of the test session record.textReplacementRules- A set of rules to replace text inNetworkCallRecord.getResponse()when playing back network calls.- Throws:
UncheckedIOException- An existing test session record could not be located or the data could not be deserialized into an instance ofRecordedData.NullPointerException- IftestNameortextReplacementRulesisnull.
-
InterceptorManager
@Deprecated public InterceptorManager(String testName, Map<String, String> textReplacementRules, boolean doNotRecord) Deprecated.UseInterceptorManager(String, Map, boolean, String)instead.Creates a new InterceptorManager that replays test session records. It takes a set oftextReplacementRules, that can be used byPlaybackClientto replace values in aNetworkCallRecord.getResponse(). The test session records are read from: "session-records/testName.json"- Parameters:
testName- Name of the test session record.textReplacementRules- A set of rules to replace text inNetworkCallRecord.getResponse()when playing back network calls.doNotRecord- Flag indicating whether network calls should be record or played back.- Throws:
UncheckedIOException- An existing test session record could not be located or the data could not be deserialized into an instance ofRecordedData.NullPointerException- IftestNameortextReplacementRulesisnull.
-
InterceptorManager
public InterceptorManager(String testName, Map<String, String> textReplacementRules, boolean doNotRecord, String playbackRecordName) Creates a new InterceptorManager that replays test session records. It takes a set oftextReplacementRules, that can be used byPlaybackClientto replace values in aNetworkCallRecord.getResponse(). The test session records are read from: "session-records/testName.json"- Parameters:
testName- Name of the test.textReplacementRules- A set of rules to replace text inNetworkCallRecord.getResponse()when playing back network calls.doNotRecord- Flag indicating whether network calls should be record or played back.playbackRecordName- Full name of the test including its iteration, used as the playback record name.- Throws:
UncheckedIOException- An existing test session record could not be located or the data could not be deserialized into an instance ofRecordedData.NullPointerException- IftestNameortextReplacementRulesisnull.
-
-
Method Details
-
isPlaybackMode
public boolean isPlaybackMode()Gets whether this InterceptorManager is in playback mode.- Returns:
- true if the InterceptorManager is in playback mode and false otherwise.
-
isLiveMode
public boolean isLiveMode()Gets whether this InterceptorManager is in live mode.- Returns:
- true if the InterceptorManager is in live mode and false otherwise.
-
isRecordMode
public boolean isRecordMode()Gets whether this InterceptorManager is in record mode.- Returns:
- true if the InterceptorManager is in record mode and false otherwise.
-
getRecordedData
public com.azure.core.test.models.RecordedData getRecordedData()Gets the recorded data InterceptorManager is keeping track of.- Returns:
- The recorded data managed by InterceptorManager.
-
getProxyVariableSupplier
ASupplierfor retrieving a variable from a test proxy recording.- Returns:
- The supplier for retrieving a variable.
-
getProxyVariableConsumer
Get aConsumerfor adding variables used in test proxy tests.- Returns:
- The consumer for adding a variable.
-
getRecordPolicy
Gets a new HTTP pipeline policy that records network calls and its data is managed byInterceptorManager.- Returns:
- HttpPipelinePolicy to record network calls.
- Throws:
IllegalStateException- A recording policy was requested when the test proxy is enabled and test mode is not RECORD.
-
getRecordPolicy
Gets a new HTTP pipeline policy that records network calls. The recorded content is redacted by the given list of redactor functions to hide sensitive information.- Parameters:
recordingRedactors- The custom redactor functions that are applied in addition to the default redactor functions defined inRecordingRedactor.- Returns:
HttpPipelinePolicyto record network calls.- Throws:
IllegalStateException- A recording policy was requested when the test proxy is enabled and test mode is not RECORD.
-
getPlaybackClient
Gets a new HTTP client that plays back test session records managed byInterceptorManager.- Returns:
- An HTTP client that plays back network calls from its recorded data.
- Throws:
IllegalStateException- A playback client was requested when the test proxy is enabled and test mode is LIVE.
-
close
public void close()Disposes of resources used by this InterceptorManager. IftestModeisTestMode.RECORD, all the network calls are persisted to: "session-records/testName.json"- Specified by:
closein interfaceAutoCloseable
-
addTextReplacementRule
Add text replacement rule (regex as key, the replacement text as value) intotextReplacementRules- Parameters:
regex- the pattern to locate the position of replacementreplacement- the replacement text
-
addSanitizers
Add sanitizer rule for sanitization during record or playback.- Parameters:
testProxySanitizers- the list of replacement regex and rules.- Throws:
RuntimeException- Neither playback or record has started.
-
addSanitizers
public void addSanitizers(com.azure.core.test.models.TestProxySanitizer... testProxySanitizers) Add sanitizer rule for sanitization during record or playback.- Parameters:
testProxySanitizers- the list of replacement regex and rules.
-
addMatchers
Add matcher rules to match recorded data in playback. Matchers are only applied for playback session and so this will be a noop when invoked in RECORD/LIVE mode.- Parameters:
testProxyMatchers- the list of matcher rules when playing back recorded data.- Throws:
RuntimeException- Playback has not started.
-
addMatchers
public void addMatchers(com.azure.core.test.models.TestProxyRequestMatcher... testProxyRequestMatchers) Add matcher rules to match recorded data in playback. Matchers are only applied for playback session and so this will be a noop when invoked in RECORD/LIVE mode.- Parameters:
testProxyRequestMatchers- the list of matcher rules when playing back recorded data.
-
getRecordingFileLocation
Get the recording file location in assets repo.- Returns:
- the assets repo location of the recording file.
-
setProxyRecordingOptions
public void setProxyRecordingOptions(com.azure.core.test.models.TestProxyRecordingOptions testProxyRecordingOptions) Sets the recording options for the proxy.- Parameters:
testProxyRecordingOptions- TheTestProxyRecordingOptionsto use.- Throws:
RuntimeException- if test mode is not record.
-
getTestName
Gets the name of the running test.- Returns:
- Name of the running test.
-
getPlaybackRecordName
Gets the name of the playback record.The playback record name is equivalent to
<testClass>.<testMethod>[<testIteration>].- Returns:
- Name of the playback record.
-
InterceptorManager(TestContextManager)instead.