Sdílet prostřednictvím


InterceptorManager Class

  • java.lang.Object
    • com.azure.core.test.InterceptorManager

Implements

public class InterceptorManager
implements 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 testMode is PLAYBACK, the manager tries to find an existing test session record to read network calls from.
  • If the testMode is RECORD, the manager creates a new test session record and saves all the network calls to it.
  • If the testMode is LIVE, the manager won't attempt to read or create a test session record.

When the InterceptorManager is disposed, if the testMode is RECORD, the network calls that were recorded are persisted to: "session-records/testName.json"

Constructor Summary

Constructor Description
InterceptorManager(TestContextManager testContextManager)

Creates a new InterceptorManager that either replays test-session records or saves them.

InterceptorManager(String testName, TestMode testMode)

Deprecated

Creates a new InterceptorManager that either replays test-session records or saves them.

InterceptorManager(String testName, Map<String,String> textReplacementRules)

Deprecated

Creates a new InterceptorManager that replays test session records.

InterceptorManager(String testName, Map<String,String> textReplacementRules, boolean doNotRecord)

Deprecated

Creates a new InterceptorManager that replays test session records.

InterceptorManager(String testName, Map<String,String> textReplacementRules, boolean doNotRecord, String playbackRecordName)

Creates a new InterceptorManager that replays test session records.

Method Summary

Modifier and Type Method and Description
void addMatchers(TestProxyRequestMatcher[] testProxyRequestMatchers)

Add matcher rules to match recorded data in playback.

void addMatchers(List<TestProxyRequestMatcher> testProxyMatchers)

Add matcher rules to match recorded data in playback.

void addSanitizers(TestProxySanitizer[] testProxySanitizers)

Add sanitizer rule for sanitization during record or playback.

void addSanitizers(List<TestProxySanitizer> testProxySanitizers)

Add sanitizer rule for sanitization during record or playback.

void addTextReplacementRule(String regex, String replacement)

Add text replacement rule (regex as key, the replacement text as value) into InterceptorManager#textReplacementRules

void close()

Disposes of resources used by this InterceptorManager.

HttpClient getPlaybackClient()

Gets a new HTTP client that plays back test session records managed by InterceptorManager.

String getPlaybackRecordName()

Gets the name of the playback record.

Consumer<String> getProxyVariableConsumer()

Get a Consumer for adding variables used in test proxy tests.

Supplier<String> getProxyVariableSupplier()

A Supplier for retrieving a variable from a test proxy recording.

HttpPipelinePolicy getRecordPolicy()

Gets a new HTTP pipeline policy that records network calls and its data is managed by InterceptorManager.

HttpPipelinePolicy getRecordPolicy(List<Function<String,String>> recordingRedactors)

Gets a new HTTP pipeline policy that records network calls.

RecordedData getRecordedData()

Gets the recorded data InterceptorManager is keeping track of.

String getRecordingFileLocation()

Get the recording file location in assets repo.

String getTestName()

Gets the name of the running test.

boolean isLiveMode()

Gets whether this InterceptorManager is in live mode.

boolean isPlaybackMode()

Gets whether this InterceptorManager is in playback mode.

boolean isRecordMode()

Gets whether this InterceptorManager is in record mode.

void removeSanitizers(String[] testProxySanitizersId)

Disable common sanitizer rule for sanitization during record or playback.

void setProxyRecordingOptions(TestProxyRecordingOptions testProxyRecordingOptions)

Sets the recording options for the proxy.

Methods inherited from java.lang.Object

Constructor Details

InterceptorManager

public InterceptorManager(TestContextManager testContextManager)

Creates a new InterceptorManager that either replays test-session records or saves them.

  • If testMode is PLAYBACK, the manager tries to find an existing test session record to read network calls from.
  • If testMode is RECORD, the manager creates a new test session record and saves all the network calls to it.
  • If testMode is LIVE, the manager won't attempt to read or create a test session record.

The test session records are persisted in the path: "session-records/testName.json"

Parameters:

testContextManager - Contextual information about the test being ran, such as test name, TestMode, and others.

InterceptorManager

@Deprecated
public InterceptorManager(String testName, TestMode testMode)

Deprecated

Creates a new InterceptorManager that either replays test-session records or saves them.

  • If testMode is PLAYBACK, the manager tries to find an existing test session record to read network calls from.
  • If testMode is RECORD, the manager creates a new test session record and saves all the network calls to it.

The test session records are persisted in the path: "session-records/testName.json"

Parameters:

testName - Name of the test session record.
testMode - The TestMode for this interceptor.

InterceptorManager

@Deprecated
public InterceptorManager(String testName, Map textReplacementRules)

Deprecated

Creates a new InterceptorManager that replays test session records. It takes a set of textReplacementRules, that can be used by PlaybackClient to replace values in a 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 in getResponse() when playing back network calls.

InterceptorManager

@Deprecated
public InterceptorManager(String testName, Map textReplacementRules, boolean doNotRecord)

Deprecated

Creates a new InterceptorManager that replays test session records. It takes a set of textReplacementRules, that can be used by PlaybackClient to replace values in a 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 in getResponse() when playing back network calls.
doNotRecord - Flag indicating whether network calls should be record or played back.

InterceptorManager

public InterceptorManager(String testName, Map textReplacementRules, boolean doNotRecord, String playbackRecordName)

Creates a new InterceptorManager that replays test session records. It takes a set of textReplacementRules, that can be used by PlaybackClient to replace values in a 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 in 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.

Method Details

addMatchers

public void addMatchers(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.

addMatchers

public void addMatchers(List testProxyMatchers)

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.

addSanitizers

public void addSanitizers(TestProxySanitizer[] testProxySanitizers)

Add sanitizer rule for sanitization during record or playback.

Parameters:

testProxySanitizers - the list of replacement regex and rules.

addSanitizers

public void addSanitizers(List testProxySanitizers)

Add sanitizer rule for sanitization during record or playback.

Parameters:

testProxySanitizers - the list of replacement regex and rules.

addTextReplacementRule

public void addTextReplacementRule(String regex, String replacement)

Add text replacement rule (regex as key, the replacement text as value) into InterceptorManager#textReplacementRules

Parameters:

regex - the pattern to locate the position of replacement
replacement - the replacement text

close

public void close()

Disposes of resources used by this InterceptorManager. If testMode is RECORD, all the network calls are persisted to: "session-records/testName.json"

getPlaybackClient

public HttpClient getPlaybackClient()

Gets a new HTTP client that plays back test session records managed by InterceptorManager.

Returns:

An HTTP client that plays back network calls from its recorded data.

getPlaybackRecordName

public String getPlaybackRecordName()

Gets the name of the playback record.

The playback record name is equivalent to .[].

Returns:

Name of the playback record.

getProxyVariableConsumer

public Consumer getProxyVariableConsumer()

Get a Consumer for adding variables used in test proxy tests.

Returns:

The consumer for adding a variable.

getProxyVariableSupplier

public Supplier getProxyVariableSupplier()

A Supplier for retrieving a variable from a test proxy recording.

Returns:

The supplier for retrieving a variable.

getRecordPolicy

public HttpPipelinePolicy getRecordPolicy()

Gets a new HTTP pipeline policy that records network calls and its data is managed by InterceptorManager.

Returns:

HttpPipelinePolicy to record network calls.

getRecordPolicy

public HttpPipelinePolicy getRecordPolicy(List> recordingRedactors)

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 in RecordingRedactor.

Returns:

HttpPipelinePolicy to record network calls.

getRecordedData

public RecordedData getRecordedData()

Gets the recorded data InterceptorManager is keeping track of.

Returns:

The recorded data managed by InterceptorManager.

getRecordingFileLocation

public String getRecordingFileLocation()

Get the recording file location in assets repo.

Returns:

the assets repo location of the recording file.

getTestName

public String getTestName()

Gets the name of the running test.

Returns:

Name of the running test.

isLiveMode

public boolean isLiveMode()

Gets whether this InterceptorManager is in live mode.

Returns:

true if the InterceptorManager is in live mode and false otherwise.

isPlaybackMode

public boolean isPlaybackMode()

Gets whether this InterceptorManager is in playback mode.

Returns:

true if the InterceptorManager is in playback 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.

removeSanitizers

public void removeSanitizers(String[] testProxySanitizersId)

Disable common sanitizer rule for sanitization during record or playback.

Parameters:

testProxySanitizersId - the list of sanitizer rule Id to disable.

setProxyRecordingOptions

public void setProxyRecordingOptions(TestProxyRecordingOptions testProxyRecordingOptions)

Sets the recording options for the proxy.

Parameters:

testProxyRecordingOptions - The TestProxyRecordingOptions to use.

Applies to