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

[Recorder] Add support for skipping the request body #22433

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
44 changes: 44 additions & 0 deletions sdk/test-utils/recorder/src/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ import { isNode } from "@azure/core-util";
import { env } from "./utils/env";
import { decodeBase64 } from "./utils/encoding";

/**
* Skip mode to be passed to Recorder#setSkipMode.
*
* There are 3 possible modes:
* - "skip-recording": Skips recording all requests and responses until the skip mode is changed.
* - "skip-request-body": Skips recording the request body of requests until the skip mode is changed.
* - "default": Record all requests and responses as normal.
*/
export type SkipMode = "skip-recording" | "skip-request-body" | "default";

const skipModeToHeaderValue: { [k in SkipMode]?: string } = {
"skip-recording": "request-response",
"skip-request-body": "request-body",
};

/**
* This client manages the recorder life cycle and interacts with the proxy-tool to do the recording,
* eventually save them in record mode and playing them back in playback mode.
Expand All @@ -61,6 +76,7 @@ export class Recorder {
private httpClient?: HttpClient;
private sessionFile?: string;
private variables: Record<string, string>;
private skipMode: SkipMode = "default";

constructor(private testContext?: Test | undefined) {
logger.info(`[Recorder#constructor] Creating a recorder instance in ${getTestMode()} mode`);
Expand Down Expand Up @@ -122,6 +138,22 @@ export class Recorder {
request.headers.set("x-recording-id", this.recordingId);
request.headers.set("x-recording-mode", getTestMode());

const skipHeaderValue = skipModeToHeaderValue[this.skipMode];
if (skipHeaderValue) {
logger.verbose(
`[Recorder#redirectRequest] Setting x-recording-skip header to ${skipHeaderValue}`
);
request.headers.set("x-recording-skip", skipHeaderValue);
}

// Drop request body in playback mode if we skipped recording it
if (isPlaybackMode() && this.skipMode === "skip-request-body") {
logger.verbose(
"[Recorder#redirectRequest] Setting request body to null for playback of recording with skipped body"
);
request.body = null;
}

redirectedUrl.host = testProxyUrl.host;
redirectedUrl.port = testProxyUrl.port;
redirectedUrl.protocol = testProxyUrl.protocol;
Expand Down Expand Up @@ -488,4 +520,16 @@ export class Recorder {

return this.variables[name];
}

/**
* Set the current skip mode that the recorder should use. All requests made through the recorder
* following a call to setSkipMode will be recorded according to the provided skip mode. To
* return to recording everything as usual, call setSkipMode("default").
*
* @param skipMode - the new skip mode to use
*/
setSkipMode(skipMode: SkipMode) {
logger.info(`[Recorder#setSkipMode] setting skip mode to ${skipMode}`);
this.skipMode = skipMode;
}
}