Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion packages/commons/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { PT_VERSION } from './version.js';

if (!process.env.AWS_SDK_UA_APP_ID) {
process.env.AWS_SDK_UA_APP_ID = `PT/NO-OP/${PT_VERSION}`;
process.env.AWS_SDK_UA_APP_ID = `PT/TEST/${PT_VERSION}`;
} else {
process.env.AWS_SDK_UA_APP_ID = `${process.env.AWS_SDK_UA_APP_ID}/PT/TEST/${PT_VERSION}`;
}

export { addUserAgentMiddleware, isSdkClient } from './awsSdkUtils.js';
Expand Down
17 changes: 8 additions & 9 deletions packages/commons/tests/unit/awsSdkUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeAll, describe, expect, it, vi } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { customUserAgentMiddleware } from '../../src/awsSdkUtils.js';
import {
addUserAgentMiddleware,
Expand All @@ -8,14 +8,11 @@ import {

vi.hoisted(() => {
process.env.AWS_EXECUTION_ENV = '';
process.env.AWS_SDK_UA_APP_ID = 'test';
});

describe('Helpers: awsSdk', () => {
describe('Function: userAgentMiddleware', () => {
beforeAll(() => {
vi.spyOn(console, 'warn').mockImplementation(() => ({}));
});

it('handles gracefully failures in adding a middleware and only log a warning', () => {
// Prepare
const client = {
Expand All @@ -25,13 +22,10 @@ describe('Helpers: awsSdk', () => {
},
},
};
const warningSpy = vi
.spyOn(console, 'warn')
.mockImplementation(() => ({}));

// Act & Assess
expect(() => addUserAgentMiddleware(client, 'my-feature')).not.toThrow();
expect(warningSpy).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledTimes(1);
});

it('should return and do nothing if the client already has a Powertools UA middleware', async () => {
Expand Down Expand Up @@ -88,6 +82,11 @@ describe('Helpers: awsSdk', () => {
});
});

it('concatenates the PT AWS_SDK_UA_APP_ID when one is already set', () => {
// Assess
expect(process.env.AWS_SDK_UA_APP_ID).toEqual(`test/PT/TEST/${version}`);
});

describe('Function: customUserAgentMiddleware', () => {
it('returns a middleware function', () => {
// Prepare
Expand Down