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] Unified recorder prototyping with storage-queue/data-tables SDKs #15826

Merged
merged 117 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 92 commits
Commits
Show all changes
117 commits
Select commit Hold shift + click to select a range
d13f614
update rush.json
HarshaNalluru Jun 3, 2021
c1f1145
lock file
HarshaNalluru Jun 3, 2021
e71e26f
package.json, rollup and tsconfig
HarshaNalluru Jun 3, 2021
9b3c32e
src file
HarshaNalluru Jun 3, 2021
77bc49b
test
HarshaNalluru Jun 3, 2021
2b83ec6
add recorder.stop call
HarshaNalluru Jun 3, 2021
e5d5269
set http
HarshaNalluru Jun 3, 2021
8c3828b
set protocol to http
HarshaNalluru Jun 3, 2021
f630cda
core-rest-pipeline draft
HarshaNalluru Jun 8, 2021
470b842
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-js in…
HarshaNalluru Jun 8, 2021
224bbee
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-js in…
HarshaNalluru Jun 9, 2021
2368ff7
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-js in…
HarshaNalluru Jun 15, 2021
8d76e96
storage test
HarshaNalluru Jun 16, 2021
7a11116
keep only the storage test
HarshaNalluru Jun 17, 2021
29fca04
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Jul 8, 2021
07267b2
lock file
HarshaNalluru Jul 8, 2021
e9efe0a
update package.json
HarshaNalluru Jul 8, 2021
593f583
recorder-new
HarshaNalluru Jul 8, 2021
10c96ac
lock file
HarshaNalluru Jul 8, 2021
2ac2a7b
undefined to null
HarshaNalluru Jul 8, 2021
5e633c3
remove target es5
HarshaNalluru Jul 9, 2021
62e7e54
add a guide with starter steps
HarshaNalluru Jul 9, 2021
af29558
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Jul 9, 2021
edf3644
testing-recorder-new
HarshaNalluru Jul 9, 2021
b7ca3af
lock file
HarshaNalluru Jul 9, 2021
2c702f9
types - recorder-new
HarshaNalluru Jul 9, 2021
4b0bfda
import urlBuilder from core-http
HarshaNalluru Jul 9, 2021
f74f134
disclaimer in the guide
HarshaNalluru Jul 9, 2021
ab42bd1
comment out the test in recorder-new
HarshaNalluru Jul 9, 2021
500016c
testing recorder-new setup and test
HarshaNalluru Jul 9, 2021
4208082
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Jul 9, 2021
5b36133
rest-pipeline 1.1.0
HarshaNalluru Jul 9, 2021
8febc81
lock file
HarshaNalluru Jul 9, 2021
28fc698
"versionPolicyName": "test" and sdkType
HarshaNalluru Jul 9, 2021
710ceb4
karma.conf
HarshaNalluru Jul 9, 2021
c56bdb2
fix package.json
HarshaNalluru Jul 9, 2021
7d71697
update test to take sas url
HarshaNalluru Jul 9, 2021
bb5f0c3
omit readme checks for testing-recorder-new, recorder-new
HarshaNalluru Jul 12, 2021
12e8999
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Jul 12, 2021
aff2b98
lock file from master
HarshaNalluru Jul 12, 2021
0727862
lock file and readmes
HarshaNalluru Jul 12, 2021
bdb22e8
remove TEST_MODE variable
HarshaNalluru Jul 12, 2021
bdf1b46
update readme with temp-location
HarshaNalluru Jul 12, 2021
b5523e5
resources update
HarshaNalluru Jul 12, 2021
e7f3560
skip runnign in ci
HarshaNalluru Jul 12, 2021
4dfbef7
update package.json
HarshaNalluru Jul 12, 2021
8b084ad
remove console.logs and fix queue name
HarshaNalluru Jul 12, 2021
879cae3
fix browser mappings
HarshaNalluru Jul 12, 2021
249f915
index.browser.ts and console.logs
HarshaNalluru Jul 12, 2021
dc3cbae
remove .olg from clean command
HarshaNalluru Jul 12, 2021
40c1074
test file with logs
HarshaNalluru Jul 12, 2021
bb9126a
update readme
HarshaNalluru Jul 13, 2021
90249b0
update readme to reflect additional environment variables that must b…
scbedd Jul 13, 2021
ebd33d9
login steps
HarshaNalluru Jul 21, 2021
5ec3d61
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Aug 20, 2021
84d204e
lock file
HarshaNalluru Aug 20, 2021
1a85cd1
test file
HarshaNalluru Aug 20, 2021
dfa3838
Copying the recordings saved in the container
HarshaNalluru Aug 20, 2021
f181829
remove console logs
HarshaNalluru Aug 20, 2021
9b1f2ab
"@azure/data-tables": "^12.1.2"
HarshaNalluru Aug 20, 2021
a7db71f
dependencies
HarshaNalluru Aug 20, 2021
99fec55
rename test file
HarshaNalluru Aug 20, 2021
64c1964
core-v2 recorder first draft
HarshaNalluru Aug 20, 2021
727f294
core-v2 node test works
HarshaNalluru Aug 20, 2021
335f48c
karma-conf fix
HarshaNalluru Aug 20, 2021
968e17b
uri -> url
HarshaNalluru Aug 20, 2021
eee1249
update tests
HarshaNalluru Aug 21, 2021
bbe4571
refactor core-v1 and core-v2 recorder clients
HarshaNalluru Aug 21, 2021
2de33b5
refactor common code between core-v1 core-v2 and node and browser
HarshaNalluru Aug 21, 2021
a27e3a2
renames and underscore removals
HarshaNalluru Aug 21, 2021
079bec9
typings -> types
HarshaNalluru Aug 21, 2021
675fe90
address feedback
HarshaNalluru Aug 21, 2021
1ff021a
RecordingStateManager
HarshaNalluru Aug 21, 2021
6eac674
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Aug 21, 2021
cfb162e
lock file
HarshaNalluru Aug 21, 2021
43356fd
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Aug 23, 2021
5395f69
lock file
HarshaNalluru Aug 23, 2021
5cec898
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Aug 24, 2021
a4ce6d8
lock file from master
HarshaNalluru Aug 24, 2021
b5f384a
lock file
HarshaNalluru Aug 24, 2021
370019b
recorder-new package test skipped
HarshaNalluru Aug 24, 2021
a589bf1
delete commented test file
HarshaNalluru Aug 24, 2021
6f066c0
Update sdk/test-utils/testing-recorder-new/README.md
HarshaNalluru Aug 24, 2021
090d2e5
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Aug 24, 2021
bf97266
empty test file
HarshaNalluru Aug 24, 2021
2e0eec2
lock file
HarshaNalluru Aug 24, 2021
df0a4f3
Merge branch 'harshan/recorder/unified' of https://github.com/HarshaN…
HarshaNalluru Aug 24, 2021
c04e92b
add link descriptions
HarshaNalluru Aug 24, 2021
d8629f1
Daniel's feedback
HarshaNalluru Aug 24, 2021
e030f45
Update sdk/test-utils/recorder-new/README.md
HarshaNalluru Aug 24, 2021
eb26b85
Scott's feedback
HarshaNalluru Aug 24, 2021
dec81b5
Merge branch 'harshan/recorder/unified' of https://github.com/HarshaN…
HarshaNalluru Aug 24, 2021
5508864
Addressing Will's feedback
HarshaNalluru Aug 24, 2021
9284c12
Add tslib
HarshaNalluru Aug 24, 2021
416509b
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Aug 25, 2021
93123c7
RecordingState
HarshaNalluru Aug 25, 2021
09dc6bb
lock file
HarshaNalluru Aug 25, 2021
07838e8
currentState
HarshaNalluru Aug 26, 2021
9e0dfb5
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Aug 26, 2021
a3b95c2
Merge branch 'harshan/recorder/unified' of https://github.com/HarshaN…
HarshaNalluru Aug 26, 2021
bee2997
lock file
HarshaNalluru Aug 26, 2021
c200a0e
docker cp
HarshaNalluru Aug 26, 2021
af43aa5
remove lib from tsconfig
HarshaNalluru Aug 26, 2021
d8a1459
more feedback
HarshaNalluru Aug 26, 2021
0103399
utils file and base tests
HarshaNalluru Aug 26, 2021
ccc8f3c
update error message
HarshaNalluru Aug 26, 2021
626724c
initial set of tests
HarshaNalluru Aug 26, 2021
293f78a
Add copyright headers
HarshaNalluru Aug 26, 2021
63bdbcd
No need of the if checks
HarshaNalluru Aug 26, 2021
e72e3a2
both Test_Modes
HarshaNalluru Aug 26, 2021
03dff7d
Append ${testMode} mode:
HarshaNalluru Aug 26, 2021
0d0694c
karma conf and tests
HarshaNalluru Aug 26, 2021
45e5743
Daniel's new found love - npm run clean move from prebuild to build
HarshaNalluru Aug 26, 2021
c2dd7b9
Added many many comments for Daniel 🐱‍👤
HarshaNalluru Aug 26, 2021
b9ef37d
lock file
HarshaNalluru Aug 26, 2021
0b05af6
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-js into…
HarshaNalluru Aug 26, 2021
93b7d0f
package renames
HarshaNalluru Aug 26, 2021
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
1,540 changes: 565 additions & 975 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions eng/.docsettings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ omitted_paths:
- sdk/storage/storage-internal-avro/*
- sdk/test-utils/perfstress/README.md
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
- sdk/test-utils/multi-version/README.md
- /sdk/test-utils/testing-recorder-new/README.md
- /sdk/test-utils/recorder-new/README.md

language: js
root_check_enabled: True
required_readme_sections:
Expand Down
10 changes: 10 additions & 0 deletions rush.json
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,11 @@
"projectFolder": "sdk/template/template",
"versionPolicyName": "client"
},
{
"packageName": "@azure/testing-recorder-new",
"projectFolder": "sdk/test-utils/testing-recorder-new",
"versionPolicyName": "test"
},
{
"packageName": "@azure/eslint-plugin-azure-sdk",
"projectFolder": "common/tools/eslint-plugin-azure-sdk",
Expand All @@ -696,6 +701,11 @@
"projectFolder": "sdk/test-utils/recorder",
"versionPolicyName": "utility"
},
{
"packageName": "@azure/test-utils-recorder-new",
"projectFolder": "sdk/test-utils/recorder-new",
"versionPolicyName": "utility"
},
{
"packageName": "@azure/test-utils-perfstress",
"projectFolder": "sdk/test-utils/perfstress",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"dependencies": {
"@azure/abort-controller": "^1.0.0",
"@azure/event-hubs": "^5.6.0",
"@azure/data-tables": "^12.1.1",
"@azure/data-tables": "^12.1.2",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion sdk/tables/perf-tests/data-tables/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@azure/data-tables": "^12.1.1",
"@azure/data-tables": "^12.1.2",
"@azure/test-utils-perfstress": "^1.0.0",
"dotenv": "^8.2.0",
"uuid": "^8.3.0"
Expand Down
34 changes: 34 additions & 0 deletions sdk/test-utils/recorder-new/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## Azure test-utils-recorder-new library for JavaScript

This is an experimental tool to record and playback the tests in the JS repo by leveraging the unified out-of-proc test proxy server. This library is still under construction.
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
Feature work is being tracked at [#15829](https://github.com/Azure/azure-sdk-for-js/issues/15829)

## Resources

- [Azure SDK Tools Test Proxy](https://github.com/Azure/azure-sdk-tools/tree/main/tools/test-proxy/Azure.Sdk.Tools.TestProxy)
- [Using Test Proxy with docker container](https://github.com/Azure/azure-sdk-tools/tree/main/tools/test-proxy/docker#build-and-run)

## Running the proxy server

Run this command

> `docker run -v temp-location:/etc/testproxy -p 5001:5001 -p 5000:5000 azsdkengsys.azurecr.io/engsys/testproxy-lin:latest`
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved

(Eventually, recorder will trigger this for you!)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!!!


[Note: Update `temp-location` in the command to your desired location.]

If the above command doesn't work directly, try [Troubleshooting Access to Public Container Registry](https://github.com/Azure/azure-sdk-tools/tree/main/tools/test-proxy/docker#troubleshooting-access-to-public-container-registry).

Reference: [Using Test Proxy with docker container](https://github.com/Azure/azure-sdk-tools/tree/main/tools/test-proxy/docker#build-and-run)

## Running the test at test-utils\testing-recorder-new
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved

- Navigate to the test-utils\testing-recorder-new folder
- Run `rush update && rush build -t .`
- Run `rushx test:node`
- Run `rushx test:browser`

## Copying the recordings saved in the container

- `docker cp <container_id>:/etc/testproxy/ temp-location`
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
112 changes: 112 additions & 0 deletions sdk/test-utils/recorder-new/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"name": "@azure/test-utils-recorder-new",
"version": "1.0.0",
"sdk-type": "utility",
"description": "This library provides interfaces and helper methods to provide recording and playback capabilities for the tests in Azure JS/TS SDKs",
"main": "dist/index.js",
"module": "dist-esm/src/index.js",
"types": "./types/src/index.d.ts",
"scripts": {
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
"build:browser": "echo skipped",
"build:node": "tsc -p . && cross-env ONLY_NODE=true rollup -c 2>&1",
"build:samples": "echo Skipped.",
"build:test": "tsc -p .",
"build": "tsc -p . && rollup -c 2>&1",
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"clean": "rimraf dist dist-esm test-dist typings *.tgz *.log",
"extract-api": "echo skipped",
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"integration-test:browser": "echo skipped",
"integration-test:node": "nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 5000000 --full-trace dist-esm/test/*.spec.js dist-esm/test/node/*.spec.js",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
"lint:fix": "eslint package.json src test --ext .ts --fix --fix-type [problem,suggestion]",
"lint": "eslint package.json src test --ext .ts -f html -o recorder-lintReport.html || exit 0",
"pack": "npm pack 2>&1",
"prebuild": "npm run clean",
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
"unit-test:browser": "echo skipped",
"unit-test:node": "mocha -r esm --require ts-node/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace \"test/*.spec.ts\"",
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"test:browser": "npm run clean && npm run build:test && npm run unit-test:browser",
"test:node": "npm run clean && npm run build:test && npm run unit-test:node",
"test": "npm run clean && npm run build:test && npm run unit-test",
"docs": "echo Skipped."
},
"files": [
"dist/",
"dist-esm/src/",
"types/src",
"README.md",
"LICENSE"
],
"repository": "github:Azure/azure-sdk-for-js",
"keywords": [
"azure",
"recording",
"cloud",
"playback"
],
"author": "Microsoft Corporation",
"license": "MIT",
"bugs": {
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
},
"engines": {
"node": ">=8.0.0"
},
"homepage": "https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/test-utils/recorder-new/",
"sideEffects": false,
"private": true,
"dependencies": {
"@azure/test-utils-recorder": "^1.0.0",
"@azure/core-http": "^2.0.0",
"@azure/core-rest-pipeline": "^1.1.0"
},
"devDependencies": {
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@rollup/plugin-commonjs": "11.0.2",
"@rollup/plugin-multi-entry": "^3.0.0",
"@rollup/plugin-node-resolve": "^8.0.0",
"@rollup/plugin-replace": "^2.2.0",
"@types/fs-extra": "^8.0.0",
"@types/chai": "^4.1.6",
"@types/md5": "^2.2.0",
"@types/mocha": "^7.0.2",
"@types/nise": "^1.4.0",
"@types/node": "^12.0.0",
"@types/mock-require": "~2.0.0",
"@types/mock-fs": "~4.10.0",
"chai": "^4.2.0",
"dotenv": "^8.2.0",
"eslint": "^7.15.0",
"karma": "^6.2.0",
"karma-chrome-launcher": "^3.0.0",
"karma-coverage": "^2.0.0",
"karma-edge-launcher": "^0.4.2",
"karma-env-preprocessor": "^0.1.1",
"karma-firefox-launcher": "^1.1.0",
"karma-ie-launcher": "^1.0.0",
"karma-json-preprocessor": "^0.3.3",
"karma-json-to-file-reporter": "^1.0.1",
"karma-junit-reporter": "^2.0.1",
"karma-mocha": "^2.0.1",
"karma-mocha-reporter": "^2.2.5",
"karma-sourcemap-loader": "^0.3.8",
"mocha": "^7.1.1",
"mocha-junit-reporter": "^1.18.0",
"mock-fs": "^4.10.4",
"mock-require": "^3.0.3",
"npm-run-all": "^4.1.5",
"nyc": "^14.0.0",
"prettier": "^1.16.4",
"rimraf": "^3.0.0",
"rollup": "^1.16.3",
"rollup-plugin-shim": "^1.0.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe in a follow-up PR, rollup plugins can be removed from dep list when shared config is used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding it to the other list

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-terser": "^5.1.1",
"rollup-plugin-visualizer": "^4.0.4",
"typescript": "~4.2.0",
"xhr-mock": "^2.4.1"
}
}
3 changes: 3 additions & 0 deletions sdk/test-utils/recorder-new/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { makeConfig } from "@azure/dev-tool/shared-config/rollup";

export default makeConfig(require("./package.json"));
21 changes: 21 additions & 0 deletions sdk/test-utils/recorder-new/src/core-v1-recorder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { HttpClient, HttpOperationResponse } from "@azure/core-http";
import { DefaultHttpClient, WebResourceLike } from "@azure/core-http";
import { TestProxyHttpClient } from "./core-v2-recorder";

export class TestProxyHttpClientCoreV1 extends TestProxyHttpClient {
sadasant marked this conversation as resolved.
Show resolved Hide resolved
public httpClientCoreV1: HttpClient;
constructor(sessionFile: string, playback: boolean) {
super(sessionFile, playback);
this.httpClientCoreV1 = new DefaultHttpClient();
}

async sendRequest(request: WebResourceLike): Promise<HttpOperationResponse> {
if (this.recordingId && (this.mode === "record" || this.mode === "playback")) {
request = this.redirectRequest(request);
}
return await this.httpClientCoreV1.sendRequest(request);
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
}
}
171 changes: 171 additions & 0 deletions sdk/test-utils/recorder-new/src/core-v2-recorder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { WebResourceLike } from "@azure/core-http";
import {
createDefaultHttpClient,
createPipelineRequest,
HttpClient,
PipelinePolicy,
PipelineRequest,
PipelineResponse,
SendRequest
} from "@azure/core-rest-pipeline";

const paths = {
playback: "/playback",
record: "/record",
start: "/start",
stop: "/stop"
};

/**
* Helper class to manage the recording state to make sure the proxy-tool is not flooded with unintended requests.
*
* => then start
* => run the runAsync
* => stop record
* => start playback
* => stop playback
*/
export class RecordingStateManager {
public state: "started" | "stopped" | undefined;

/**
* validateState
*/
public validateState(currentFlow: "starting" | "stopping") {
if (currentFlow === "starting") {
if (this.state === "started") {
throw new Error("Recorder Error: Already started, should not have called start again.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, would a no-op here work, or throwing during runtime is better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why no-op? The point is we think the dev is doing something wrong if they have called recorder.start twice, right?

}
}
if (currentFlow === "stopping") {
if (this.state === "stopped") {
throw new Error("Recorder Error: Already stopped, should not have called stop again.");
}
if (this.state !== "started") {
throw new Error("Recorder Error: Please start before calling stop.");
}
}
if (currentFlow === "starting") {
if (this.state !== "stopped" && this.state !== undefined) {
throw new Error("Recorder Error: Please stop before calling start.");
}
}
if (currentFlow === "stopping") {
if (this.state !== "started") {
throw new Error("Recorder Error: Please start before calling stop.");
}
}
}

/**
* setState
*/
public setState(state: "started" | "stopped") {
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
this.state = state;
}
}

export class TestProxyHttpClient {
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
private url: string;
public recordingId?: string;
public mode: string;
public httpClient: HttpClient;
private sessionFile: string;
private playback: boolean;
private stateManager = new RecordingStateManager();

constructor(sessionFile: string, playback: boolean) {
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
this.sessionFile = sessionFile;
this.playback = playback;
this.url = "http://localhost:5000";
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
this.mode = playback ? "playback" : "record";
this.httpClient = createDefaultHttpClient();
}
// For core-v1
redirectRequest(request: WebResourceLike): WebResourceLike;
// For core-v2
redirectRequest(request: PipelineRequest): PipelineRequest;
redirectRequest(request: WebResourceLike | PipelineRequest) {
if (!request.headers.get("x-recording-id")) {
request.headers.set("x-recording-id", this.recordingId!);
request.headers.set("x-recording-mode", this.mode);

const upstreamUrl = new URL(request.url);
const redirectedUrl = new URL(request.url);
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
const providedUrl = new URL(this.url);

redirectedUrl.host = providedUrl.host;
redirectedUrl.port = providedUrl.port;
redirectedUrl.protocol = providedUrl.protocol;
request.headers.set("x-recording-upstream-base-uri", upstreamUrl.toString());
request.url = redirectedUrl.toString();
}
return request;
}

async modifyRequest(request: PipelineRequest): Promise<PipelineRequest> {
if (this.recordingId && (this.mode === "record" || this.mode === "playback")) {
request = this.redirectRequest(request);
request.allowInsecureConnection = true;
}

return request;
}

async start(): Promise<void> {
this.stateManager.validateState("starting");
if (this.recordingId === undefined) {
const startUri = `${this.url}${this.playback ? paths.playback : paths.record}${paths.start}`;
const req = this._createRecordingRequest(startUri);
const rsp = await this.httpClient.sendRequest({
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
...req,
allowInsecureConnection: true
});
if (rsp.status !== 200) {
throw new Error("Start request failed.");
}
const id = rsp.headers.get("x-recording-id");
if (!id) {
throw new Error("No recording ID returned.");
}
this.recordingId = id;
}
this.stateManager.setState("started");
}

async stop(): Promise<void> {
this.stateManager.validateState("stopping");
if (this.recordingId !== undefined) {
const stopUri = `${this.url}${this.playback ? paths.playback : paths.record}${paths.stop}`;
const req = this._createRecordingRequest(stopUri);
req.headers.set("x-recording-save", "true");

await this.httpClient.sendRequest({ ...req, allowInsecureConnection: true });
} else {
throw new Error("Recorder Error: Bad state, recordingId is not defined when called stop.");
}
this.stateManager.setState("stopped");
}

HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
private _createRecordingRequest(url: string) {
const req = createPipelineRequest({ url: url, method: "POST" });
req.headers.set("x-recording-file", this.sessionFile);
if (this.recordingId !== undefined) {
req.headers.set("x-recording-id", this.recordingId);
}
return req;
}
}

export function recorderHttpPolicy(testProxyHttpClient: TestProxyHttpClient): PipelinePolicy {
return {
name: "recording policy",
async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {
const modifiedRequest = await testProxyHttpClient.modifyRequest(request);
HarshaNalluru marked this conversation as resolved.
Show resolved Hide resolved
return next(modifiedRequest);
}
};
}
Loading