From 752b4a5029c94de39dac98dec654d0eaaac90497 Mon Sep 17 00:00:00 2001 From: thdinizm <61257022+thdinizm@users.noreply.github.com> Date: Mon, 19 Apr 2021 14:15:05 -0700 Subject: [PATCH] Add ms-cv header logging to failed requests during tests (#14926) --- .../test/utils/recordedClient.ts | 32 ++++++++++++- .../test/public/utils/recordedClient.ts | 40 +++++++++++++++-- .../test/utils/recordedClient.ts | 40 +++++++++++++++-- .../test/internal/smsClient.internal.spec.ts | 18 ++++---- .../test/public/smsClient.spec.ts | 17 +++---- .../test/public/utils/recordedClient.ts | 45 ++++++++++++++++++- 6 files changed, 164 insertions(+), 28 deletions(-) diff --git a/sdk/communication/communication-chat/test/utils/recordedClient.ts b/sdk/communication/communication-chat/test/utils/recordedClient.ts index 022aec7fbc4e..a2a2f5f322d9 100644 --- a/sdk/communication/communication-chat/test/utils/recordedClient.ts +++ b/sdk/communication/communication-chat/test/utils/recordedClient.ts @@ -5,7 +5,13 @@ import { Context } from "mocha"; import * as dotenv from "dotenv"; import { env, Recorder, record, RecorderEnvironmentSetup } from "@azure/test-utils-recorder"; -import { isNode } from "@azure/core-http"; +import { + DefaultHttpClient, + HttpClient, + HttpOperationResponse, + isNode, + WebResourceLike +} from "@azure/core-http"; import { ChatClient } from "../../src"; import { CommunicationUserIdentifier, @@ -60,5 +66,27 @@ export function createChatClient(userToken: string): ChatClient { userToken = generateToken(); } const { url } = parseClientArguments(env.COMMUNICATION_CONNECTION_STRING); - return new ChatClient(url, new AzureCommunicationTokenCredential(userToken)); + + return new ChatClient(url, new AzureCommunicationTokenCredential(userToken), { + httpClient: createTestHttpClient() + }); +} + +function createTestHttpClient(): HttpClient { + const customHttpClient = new DefaultHttpClient(); + + const originalSendRequest = customHttpClient.sendRequest; + customHttpClient.sendRequest = async function( + httpRequest: WebResourceLike + ): Promise { + const requestResponse = await originalSendRequest.apply(this, [httpRequest]); + + if (requestResponse.status < 200 || requestResponse.status > 299) { + console.log(`MS-CV header for failed request: ${requestResponse.headers.get("ms-cv")}`); + } + + return requestResponse; + }; + + return customHttpClient; } diff --git a/sdk/communication/communication-identity/test/public/utils/recordedClient.ts b/sdk/communication/communication-identity/test/public/utils/recordedClient.ts index 7118fb8f7f5e..2b7ea4e6e665 100644 --- a/sdk/communication/communication-identity/test/public/utils/recordedClient.ts +++ b/sdk/communication/communication-identity/test/public/utils/recordedClient.ts @@ -11,7 +11,14 @@ import { RecorderEnvironmentSetup, isPlaybackMode } from "@azure/test-utils-recorder"; -import { isNode, TokenCredential } from "@azure/core-http"; +import { + DefaultHttpClient, + HttpClient, + HttpOperationResponse, + isNode, + TokenCredential, + WebResourceLike +} from "@azure/core-http"; import { CommunicationIdentityClient } from "../../../src"; import { DefaultAzureCredential } from "@azure/identity"; import { parseConnectionString } from "@azure/communication-common"; @@ -64,7 +71,9 @@ export function createRecordedCommunicationIdentityClient( const recorder = record(context, environmentSetup); return { - client: new CommunicationIdentityClient(env.COMMUNICATION_CONNECTION_STRING), + client: new CommunicationIdentityClient(env.COMMUNICATION_CONNECTION_STRING, { + httpClient: createTestHttpClient() + }), recorder }; } @@ -83,7 +92,9 @@ export function createRecordedCommunicationIdentityClientWithToken( }; return { - client: new CommunicationIdentityClient(endpoint, credential), + client: new CommunicationIdentityClient(endpoint, credential, { + httpClient: createTestHttpClient() + }), recorder }; } @@ -95,7 +106,28 @@ export function createRecordedCommunicationIdentityClientWithToken( } return { - client: new CommunicationIdentityClient(endpoint, credential), + client: new CommunicationIdentityClient(endpoint, credential, { + httpClient: createTestHttpClient() + }), recorder }; } + +function createTestHttpClient(): HttpClient { + const customHttpClient = new DefaultHttpClient(); + + const originalSendRequest = customHttpClient.sendRequest; + customHttpClient.sendRequest = async function( + httpRequest: WebResourceLike + ): Promise { + const requestResponse = await originalSendRequest.apply(this, [httpRequest]); + + if (requestResponse.status < 200 || requestResponse.status > 299) { + console.log(`MS-CV header for failed request: ${requestResponse.headers.get("ms-cv")}`); + } + + return requestResponse; + }; + + return customHttpClient; +} diff --git a/sdk/communication/communication-phone-numbers/test/utils/recordedClient.ts b/sdk/communication/communication-phone-numbers/test/utils/recordedClient.ts index 8e6e9b4e5646..788b5c30b87f 100644 --- a/sdk/communication/communication-phone-numbers/test/utils/recordedClient.ts +++ b/sdk/communication/communication-phone-numbers/test/utils/recordedClient.ts @@ -11,7 +11,14 @@ import { RecorderEnvironmentSetup, isPlaybackMode } from "@azure/test-utils-recorder"; -import { isNode, TokenCredential } from "@azure/core-http"; +import { + DefaultHttpClient, + HttpClient, + HttpOperationResponse, + isNode, + TokenCredential, + WebResourceLike +} from "@azure/core-http"; import { PhoneNumbersClient } from "../../src"; import { parseConnectionString } from "@azure/communication-common"; import { DefaultAzureCredential } from "@azure/identity"; @@ -50,7 +57,9 @@ export function createRecordedClient(context: Context): RecordedClient { + const requestResponse = await originalSendRequest.apply(this, [httpRequest]); + + if (requestResponse.status < 200 || requestResponse.status > 299) { + console.log(`MS-CV header for failed request: ${requestResponse.headers.get("ms-cv")}`); + } + + return requestResponse; + }; + + return customHttpClient; +} diff --git a/sdk/communication/communication-sms/test/internal/smsClient.internal.spec.ts b/sdk/communication/communication-sms/test/internal/smsClient.internal.spec.ts index 54744209e4f0..763742668b13 100644 --- a/sdk/communication/communication-sms/test/internal/smsClient.internal.spec.ts +++ b/sdk/communication/communication-sms/test/internal/smsClient.internal.spec.ts @@ -8,17 +8,20 @@ * These tests will be skipped in Live Mode since the public tests run in live mode only. */ -import { SmsClient } from "../../src/smsClient"; -import { env, isLiveMode, isPlaybackMode, record, Recorder } from "@azure/test-utils-recorder"; +import { isLiveMode, isPlaybackMode, record, Recorder } from "@azure/test-utils-recorder"; import { isNode } from "@azure/core-http"; import * as dotenv from "dotenv"; import * as sinon from "sinon"; import { Uuid } from "../../src/utils/uuid"; -import { createCredential, recorderConfiguration } from "../public/utils/recordedClient"; +import { + createCredential, + createSmsClient, + createSmsClientWithToken, + recorderConfiguration +} from "../public/utils/recordedClient"; import { Context } from "mocha"; import sendSmsSuites from "../public/suites/smsClient.send"; import { matrix } from "../public/utils/matrix"; -import { parseConnectionString } from "@azure/communication-common"; if (isNode) { dotenv.config(); @@ -37,13 +40,12 @@ matrix([[true, false]], async function(useAad) { sinon.stub(Uuid, "generateUuid").returns("sanitized"); sinon.stub(Date, "now").returns(0); } - const connectionString = env.AZURE_COMMUNICATION_LIVETEST_CONNECTION_STRING as string; + if (useAad) { const token = createCredential() || this.skip(); - const { endpoint } = parseConnectionString(connectionString); - this.smsClient = new SmsClient(endpoint, token); + this.smsClient = createSmsClientWithToken(token); } else { - this.smsClient = new SmsClient(connectionString); + this.smsClient = createSmsClient(); } }); diff --git a/sdk/communication/communication-sms/test/public/smsClient.spec.ts b/sdk/communication/communication-sms/test/public/smsClient.spec.ts index 89a75f24997e..c033d4695062 100644 --- a/sdk/communication/communication-sms/test/public/smsClient.spec.ts +++ b/sdk/communication/communication-sms/test/public/smsClient.spec.ts @@ -1,15 +1,18 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { SmsClient } from "../../src"; -import { env, record, Recorder } from "@azure/test-utils-recorder"; +import { record, Recorder } from "@azure/test-utils-recorder"; import { isNode } from "@azure/core-http"; import * as dotenv from "dotenv"; -import { createCredential, recorderConfiguration } from "./utils/recordedClient"; +import { + createCredential, + createSmsClient, + createSmsClientWithToken, + recorderConfiguration +} from "./utils/recordedClient"; import { Context } from "mocha"; import sendSmsSuites from "./suites/smsClient.send"; import { matrix } from "./utils/matrix"; -import { parseConnectionString } from "@azure/communication-common"; if (isNode) { dotenv.config(); @@ -26,13 +29,11 @@ matrix([[true, false]], async function(useAad) { "A UUID is randomly generated within the SDK and used in the HTTP request and cannot be preserved." ); - const connectionString = env.AZURE_COMMUNICATION_LIVETEST_CONNECTION_STRING as string; if (useAad) { const token = createCredential() || this.skip(); - const { endpoint } = parseConnectionString(connectionString); - this.smsClient = new SmsClient(endpoint, token); + this.smsClient = createSmsClientWithToken(token); } else { - this.smsClient = new SmsClient(connectionString); + this.smsClient = createSmsClient(); } }); diff --git a/sdk/communication/communication-sms/test/public/utils/recordedClient.ts b/sdk/communication/communication-sms/test/public/utils/recordedClient.ts index 57404ed115c8..259cfd236786 100644 --- a/sdk/communication/communication-sms/test/public/utils/recordedClient.ts +++ b/sdk/communication/communication-sms/test/public/utils/recordedClient.ts @@ -1,9 +1,17 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { isNode } from "@azure/core-http"; +import { parseConnectionString } from "@azure/communication-common"; +import { + DefaultHttpClient, + HttpClient, + HttpOperationResponse, + isNode, + WebResourceLike +} from "@azure/core-http"; import { DefaultAzureCredential, TokenCredential } from "@azure/identity"; -import { isPlaybackMode, RecorderEnvironmentSetup } from "@azure/test-utils-recorder"; +import { env, isPlaybackMode, RecorderEnvironmentSetup } from "@azure/test-utils-recorder"; +import { SmsClient } from "../../../src"; export const recorderConfiguration: RecorderEnvironmentSetup = { replaceableVariables: { @@ -46,3 +54,36 @@ export function createCredential(): TokenCredential | undefined { } } } + +export function createSmsClient(): SmsClient { + return new SmsClient(env.AZURE_COMMUNICATION_LIVETEST_CONNECTION_STRING, { + httpClient: createTestHttpClient() + }); +} + +export function createSmsClientWithToken(credential: TokenCredential): SmsClient { + const { endpoint } = parseConnectionString(env.AZURE_COMMUNICATION_LIVETEST_CONNECTION_STRING); + + return new SmsClient(endpoint, credential, { + httpClient: createTestHttpClient() + }); +} + +function createTestHttpClient(): HttpClient { + const customHttpClient = new DefaultHttpClient(); + + const originalSendRequest = customHttpClient.sendRequest; + customHttpClient.sendRequest = async function( + httpRequest: WebResourceLike + ): Promise { + const requestResponse = await originalSendRequest.apply(this, [httpRequest]); + + if (requestResponse.status < 200 || requestResponse.status > 299) { + console.log(`MS-CV header for failed request: ${requestResponse.headers.get("ms-cv")}`); + } + + return requestResponse; + }; + + return customHttpClient; +}