Skip to content

Commit 9afbe3f

Browse files
authored
[core-rest-pipeline] Switch browser transport to fetch (Azure#20201)
Thanks to Azure#19530 we have a new HttpClient that uses Fetch. Currently, we can't make it the default because of recorded tests. However, we'd like folks to be able to try it out, which this PR makes possible. The solution here is for tests that are dependent on XHR to pass in a custom HttpClient to allow the previous recordings to be used until we can migrate those packages to the new recorder.
1 parent a6a534c commit 9afbe3f

File tree

36 files changed

+418
-135
lines changed

36 files changed

+418
-135
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dataplane.code-workspace

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@
281281
{
282282
"name": "mixed-reality-remote-rendering",
283283
"path": "sdk/remoterendering/mixed-reality-remote-rendering"
284+
},
285+
{
286+
"path": "sdk/test-utils/test-utils"
287+
},
288+
{
289+
"path": "sdk/purview/purview-administration-rest"
284290
}
285291
],
286292
"settings": {

sdk/confidentialledger/confidential-ledger-rest/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"autoPublish": false,
8484
"dependencies": {
8585
"@azure/core-auth": "^1.3.0",
86-
"@azure-rest/core-client": "1.0.0-beta.8",
86+
"@azure-rest/core-client": "1.0.0-beta.9",
8787
"@azure/core-rest-pipeline": "^1.1.0",
8888
"@azure/logger": "^1.0.0",
8989
"tslib": "^2.2.0"
@@ -92,6 +92,7 @@
9292
"@azure/dev-tool": "^1.0.0",
9393
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
9494
"@azure/identity": "^2.0.1",
95+
"@azure/test-utils": "^1.0.0",
9596
"@azure-tools/test-recorder": "^1.0.0",
9697
"@microsoft/api-extractor": "^7.18.11",
9798
"@types/chai": "^4.1.6",

sdk/confidentialledger/confidential-ledger-rest/test/public/utils/recordedClient.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55

66
import { Context } from "mocha";
77

8-
import { env, Recorder, record, RecorderEnvironmentSetup } from "@azure-tools/test-recorder";
8+
import {
9+
env,
10+
Recorder,
11+
record,
12+
RecorderEnvironmentSetup,
13+
isLiveMode,
14+
} from "@azure-tools/test-recorder";
915
import ConfidentialLedger, { ConfidentialLedgerRestClient } from "../../../src";
1016
import { ClientSecretCredential } from "@azure/identity";
17+
import { isNode, createXhrHttpClient } from "@azure/test-utils";
1118

1219
import "./env";
1320

@@ -38,12 +45,14 @@ export const environmentSetup: RecorderEnvironmentSetup = {
3845
};
3946

4047
export function createClient(): ConfidentialLedgerRestClient {
48+
const httpClient = isNode || isLiveMode() ? undefined : createXhrHttpClient();
4149
const credential = new ClientSecretCredential(
4250
env["AZURE_TENANT_ID"],
4351
env["AZURE_CLIENT_ID"],
44-
env["AZURE_CLIENT_SECRET"]
52+
env["AZURE_CLIENT_SECRET"],
53+
{ httpClient }
4554
);
46-
return ConfidentialLedger(env.ENDPOINT, env.LEDGER_IDENTITY, credential);
55+
return ConfidentialLedger(env.ENDPOINT, env.LEDGER_IDENTITY, credential, { httpClient });
4756
}
4857

4958
/**

sdk/containerregistry/container-registry/test/utils/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import {
66
env,
77
RecorderEnvironmentSetup,
88
pluginForClientSecretCredentialTests,
9+
isLiveMode,
910
} from "@azure-tools/test-recorder";
1011
import { ContainerRegistryClient, KnownContainerRegistryAudience } from "../../src";
12+
import { isNode } from "./isNode";
13+
import { createXhrHttpClient } from "@azure/test-utils";
1114

1215
// When the recorder observes the values of these environment variables in any
1316
// recorded HTTP request or response, it will replace them with the values they
@@ -92,9 +95,11 @@ export function createRegistryClient(
9295
const authorityHost = getAuthority(endpoint);
9396
const audience = getAudience(authorityHost);
9497
const tokenCredentialOptions = authorityHost ? { authorityHost } : undefined;
98+
const httpClient = isNode || isLiveMode() ? undefined : createXhrHttpClient();
9599
const clientOptions = {
96100
audience,
97101
serviceVersion: serviceVersion as ContainerRegistryServiceVersions,
102+
httpClient,
98103
};
99104

100105
if (options.anonymous) {
@@ -110,7 +115,7 @@ export function createRegistryClient(
110115
env.CONTAINERREGISTRY_TENANT_ID,
111116
env.CONTAINERREGISTRY_CLIENT_ID,
112117
env.CONTAINERREGISTRY_CLIENT_SECRET,
113-
tokenCredentialOptions
118+
{ ...tokenCredentialOptions, httpClient }
114119
);
115120

116121
return new ContainerRegistryClient(endpoint, credential, clientOptions);

sdk/core/core-client-rest/review/core-client.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
```ts
66

7+
import { HttpClient } from '@azure/core-rest-pipeline';
78
import { KeyCredential } from '@azure/core-auth';
89
import { Pipeline } from '@azure/core-rest-pipeline';
910
import { PipelineOptions } from '@azure/core-rest-pipeline';
@@ -43,6 +44,7 @@ export type ClientOptions = PipelineOptions & {
4344
apiVersion?: string;
4445
allowInsecureConnection?: boolean;
4546
additionalPolicies?: AdditionalPolicyConfig[];
47+
httpClient?: HttpClient;
4648
};
4749

4850
// @public

sdk/core/core-client-rest/src/common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
import {
5+
HttpClient,
56
Pipeline,
67
PipelineOptions,
78
PipelinePolicy,
@@ -181,6 +182,10 @@ export type ClientOptions = PipelineOptions & {
181182
* Additional policies to include in the HTTP pipeline.
182183
*/
183184
additionalPolicies?: AdditionalPolicyConfig[];
185+
/**
186+
* Specify a custom HttpClient when making requests.
187+
*/
188+
httpClient?: HttpClient;
184189
};
185190

186191
/**

sdk/core/core-client-rest/src/getClient.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { isTokenCredential, KeyCredential, TokenCredential } from "@azure/core-auth";
55
import { isCertificateCredential } from "./certificateCredential";
6-
import { HttpMethods, Pipeline, PipelineOptions } from "@azure/core-rest-pipeline";
6+
import { HttpClient, HttpMethods, Pipeline, PipelineOptions } from "@azure/core-rest-pipeline";
77
import { createDefaultPipeline } from "./clientHelpers";
88
import { Client, ClientOptions, HttpResponse, RequestParameters } from "./common";
99
import { sendRequest } from "./sendRequest";
@@ -52,7 +52,7 @@ export function getClient(
5252
}
5353
}
5454

55-
const { allowInsecureConnection } = clientOptions;
55+
const { allowInsecureConnection, httpClient } = clientOptions;
5656
const client = (path: string, ...args: Array<any>) => {
5757
return {
5858
get: (options: RequestParameters = {}): Promise<HttpResponse> => {
@@ -62,7 +62,8 @@ export function getClient(
6262
path,
6363
pipeline,
6464
{ allowInsecureConnection, ...options },
65-
args
65+
args,
66+
httpClient
6667
);
6768
},
6869
post: (options: RequestParameters = {}): Promise<HttpResponse> => {
@@ -72,7 +73,8 @@ export function getClient(
7273
path,
7374
pipeline,
7475
{ allowInsecureConnection, ...options },
75-
args
76+
args,
77+
httpClient
7678
);
7779
},
7880
put: (options: RequestParameters = {}): Promise<HttpResponse> => {
@@ -82,7 +84,8 @@ export function getClient(
8284
path,
8385
pipeline,
8486
{ allowInsecureConnection, ...options },
85-
args
87+
args,
88+
httpClient
8689
);
8790
},
8891
patch: (options: RequestParameters = {}): Promise<HttpResponse> => {
@@ -92,7 +95,8 @@ export function getClient(
9295
path,
9396
pipeline,
9497
{ allowInsecureConnection, ...options },
95-
args
98+
args,
99+
httpClient
96100
);
97101
},
98102
delete: (options: RequestParameters = {}): Promise<HttpResponse> => {
@@ -102,7 +106,8 @@ export function getClient(
102106
path,
103107
pipeline,
104108
{ allowInsecureConnection, ...options },
105-
args
109+
args,
110+
httpClient
106111
);
107112
},
108113
head: (options: RequestParameters = {}): Promise<HttpResponse> => {
@@ -112,7 +117,8 @@ export function getClient(
112117
path,
113118
pipeline,
114119
{ allowInsecureConnection, ...options },
115-
args
120+
args,
121+
httpClient
116122
);
117123
},
118124
options: (options: RequestParameters = {}): Promise<HttpResponse> => {
@@ -122,7 +128,8 @@ export function getClient(
122128
path,
123129
pipeline,
124130
{ allowInsecureConnection, ...options },
125-
args
131+
args,
132+
httpClient
126133
);
127134
},
128135
trace: (options: RequestParameters = {}): Promise<HttpResponse> => {
@@ -132,7 +139,8 @@ export function getClient(
132139
path,
133140
pipeline,
134141
{ allowInsecureConnection, ...options },
135-
args
142+
args,
143+
httpClient
136144
);
137145
},
138146
};
@@ -151,11 +159,12 @@ function buildSendRequest(
151159
path: string,
152160
pipeline: Pipeline,
153161
requestOptions: RequestParameters = {},
154-
args: string[] = []
162+
args: string[] = [],
163+
httpClient?: HttpClient
155164
): Promise<HttpResponse> {
156165
// If the client has an api-version and the request doesn't specify one, inject the one in the client options
157166
const url = buildRequestUrl(baseUrl, path, args, requestOptions);
158-
return sendRequest(method, url, pipeline, requestOptions);
167+
return sendRequest(method, url, pipeline, requestOptions, httpClient);
159168
}
160169

161170
function isCredential(

sdk/core/core-client-rest/src/sendRequest.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
createHttpHeaders,
66
createPipelineRequest,
77
FormDataMap,
8+
HttpClient,
89
HttpMethods,
910
Pipeline,
1011
PipelineRequest,
@@ -23,15 +24,17 @@ import { binaryArrayToString, stringToBinaryArray } from "./helpers/getBinaryBod
2324
* @param url - url to send the request to
2425
* @param pipeline - pipeline with the policies to run when sending the request
2526
* @param options - request options
27+
* @param customHttpClient - a custom HttpClient to use when making the request
2628
* @returns returns and HttpResponse
2729
*/
2830
export async function sendRequest(
2931
method: HttpMethods,
3032
url: string,
3133
pipeline: Pipeline,
32-
options: RequestParameters = {}
34+
options: RequestParameters = {},
35+
customHttpClient?: HttpClient
3336
): Promise<HttpResponse> {
34-
const httpClient = getCachedDefaultHttpsClient();
37+
const httpClient = customHttpClient ?? getCachedDefaultHttpsClient();
3538
const request = buildPipelineRequest(method, url, options);
3639
const response = await pipeline.sendRequest(httpClient, request);
3740
const rawHeaders: RawHttpHeaders = response.headers.toJSON();

sdk/core/core-rest-pipeline/src/defaultHttpClient.browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Licensed under the MIT license.
33

44
import { HttpClient } from "./interfaces";
5-
import { createXhrHttpClient } from "./xhrHttpClient";
5+
import { createFetchHttpClient } from "./fetchHttpClient";
66

77
/**
88
* Create the correct HttpClient for the current environment.
99
*/
1010
export function createDefaultHttpClient(): HttpClient {
11-
return createXhrHttpClient();
11+
return createFetchHttpClient();
1212
}

0 commit comments

Comments
 (0)