Skip to content

Commit

Permalink
Add ms-cv header logging to failed requests during tests (Azure#14926)
Browse files Browse the repository at this point in the history
  • Loading branch information
thdinizm authored Apr 19, 2021
1 parent 07c14e4 commit 752b4a5
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 28 deletions.
32 changes: 30 additions & 2 deletions sdk/communication/communication-chat/test/utils/recordedClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<HttpOperationResponse> {
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
};
}
Expand All @@ -83,7 +92,9 @@ export function createRecordedCommunicationIdentityClientWithToken(
};

return {
client: new CommunicationIdentityClient(endpoint, credential),
client: new CommunicationIdentityClient(endpoint, credential, {
httpClient: createTestHttpClient()
}),
recorder
};
}
Expand All @@ -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<HttpOperationResponse> {
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -50,7 +57,9 @@ export function createRecordedClient(context: Context): RecordedClient<PhoneNumb
const recorder = record(context, environmentSetup);

return {
client: new PhoneNumbersClient(env.AZURE_COMMUNICATION_LIVETEST_CONNECTION_STRING),
client: new PhoneNumbersClient(env.AZURE_COMMUNICATION_LIVETEST_CONNECTION_STRING, {
httpClient: createTestHttpClient()
}),
recorder
};
}
Expand Down Expand Up @@ -79,7 +88,9 @@ export function createRecordedClientWithToken(
};

return {
client: new PhoneNumbersClient(endpoint, credential),
client: new PhoneNumbersClient(endpoint, credential, {
httpClient: createTestHttpClient()
}),
recorder
};
}
Expand All @@ -91,11 +102,32 @@ export function createRecordedClientWithToken(
}

return {
client: new PhoneNumbersClient(endpoint, credential),
client: new PhoneNumbersClient(endpoint, credential, {
httpClient: createTestHttpClient()
}),
recorder
};
}

export const testPollerOptions = {
pollInterval: isPlaybackMode() ? 0 : undefined
};

function createTestHttpClient(): HttpClient {
const customHttpClient = new DefaultHttpClient();

const originalSendRequest = customHttpClient.sendRequest;
customHttpClient.sendRequest = async function(
httpRequest: WebResourceLike
): Promise<HttpOperationResponse> {
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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();
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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<HttpOperationResponse> {
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;
}

0 comments on commit 752b4a5

Please sign in to comment.