From 4f36561d49bdc6d57f9af555cde741d965f1e132 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Thu, 2 Oct 2025 22:26:56 +0200 Subject: [PATCH 1/2] test: update domain in e2e tests --- .../tests/e2e/decorator.test.functionCode.ts | 4 +-- packages/tracer/tests/e2e/decorator.test.ts | 14 ++++++---- .../tests/e2e/manual.test.functionCode.ts | 2 +- .../tests/e2e/middy.test.functionCode.ts | 2 +- packages/tracer/tests/e2e/middy.test.ts | 8 +++--- packages/tracer/tests/helpers/httpRequest.ts | 27 +++++++++++++++++++ 6 files changed, 44 insertions(+), 13 deletions(-) diff --git a/packages/tracer/tests/e2e/decorator.test.functionCode.ts b/packages/tracer/tests/e2e/decorator.test.functionCode.ts index 678d256058..780b58d1e4 100644 --- a/packages/tracer/tests/e2e/decorator.test.functionCode.ts +++ b/packages/tracer/tests/e2e/decorator.test.functionCode.ts @@ -44,8 +44,8 @@ export class LambdaFunction { await this.methodNoResponse(event.invocation); await httpRequest({ - hostname: 'docs.powertools.aws.dev', - path: '/lambda/typescript/latest/', + hostname: 'docs.aws.amazon.com', + path: '/powertools/typescript/latest/', }); const res = this.myMethod(); diff --git a/packages/tracer/tests/e2e/decorator.test.ts b/packages/tracer/tests/e2e/decorator.test.ts index 64ee1cbf5c..9a3638b208 100644 --- a/packages/tracer/tests/e2e/decorator.test.ts +++ b/packages/tracer/tests/e2e/decorator.test.ts @@ -4,7 +4,7 @@ import { TestDynamodbTable } from '@aws-lambda-powertools/testing-utils/resource import { TestNodejsFunction } from '@aws-lambda-powertools/testing-utils/resources/lambda'; import type { EnrichedXRayTraceDocumentParsed } from '@aws-lambda-powertools/testing-utils/types'; import { getTraces } from '@aws-lambda-powertools/testing-utils/utils/xray-traces'; -import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; import { invokeAllTestCases } from '../helpers/invokeAllTests.js'; import { EXPECTED_ANNOTATION_KEY as expectedCustomAnnotationKey, @@ -17,6 +17,10 @@ import { RESOURCE_NAME_PREFIX, } from './constants.js'; +vi.hoisted(() => { + process.env.AWS_PROFILE = 'aamorosi-Admin'; +}); + describe('Tracer E2E tests, decorator instrumentation', () => { const testStack = new TestStack({ stackNameProps: { @@ -76,7 +80,7 @@ describe('Tracer E2E tests, decorator instrumentation', () => { * 1. Lambda Context (AWS::Lambda) * 2. Lambda Function (AWS::Lambda::Function) * 4. DynamoDB (AWS::DynamoDB) - * 4. Remote call (docs.powertools.aws.dev) + * 4. Remote call (docs.aws.amazon.com) */ expectedSegmentsCount: 4, }); @@ -100,11 +104,11 @@ describe('Tracer E2E tests, decorator instrumentation', () => { expect(subsegments.size).toBe(3); // Check remote call subsegment - expect(subsegments.has('docs.powertools.aws.dev')).toBe(true); - const httpSubsegment = subsegments.get('docs.powertools.aws.dev'); + expect(subsegments.has('docs.aws.amazon.com')).toBe(true); + const httpSubsegment = subsegments.get('docs.aws.amazon.com'); expect(httpSubsegment?.namespace).toBe('remote'); expect(httpSubsegment?.http?.request?.url).toEqual( - 'https://docs.powertools.aws.dev/lambda/typescript/latest/' + 'https://docs.aws.amazon.com/powertools/typescript/latest/' ); expect(httpSubsegment?.http?.request?.method).toBe('GET'); expect(httpSubsegment?.http?.response?.status).toEqual(expect.any(Number)); diff --git a/packages/tracer/tests/e2e/manual.test.functionCode.ts b/packages/tracer/tests/e2e/manual.test.functionCode.ts index 5ec65c6946..9c13aece91 100644 --- a/packages/tracer/tests/e2e/manual.test.functionCode.ts +++ b/packages/tracer/tests/e2e/manual.test.functionCode.ts @@ -33,7 +33,7 @@ export const handler = async ( tracer.putMetadata(customMetadataKey, customMetadataValue); try { - await fetch('https://docs.powertools.aws.dev/lambda/typescript/latest/'); + await fetch('https://docs.aws.amazon.com/powertools/typescript/latest/'); const res = customResponseValue; if (event.throw) { diff --git a/packages/tracer/tests/e2e/middy.test.functionCode.ts b/packages/tracer/tests/e2e/middy.test.functionCode.ts index cff6e205f4..e78dd75f7f 100644 --- a/packages/tracer/tests/e2e/middy.test.functionCode.ts +++ b/packages/tracer/tests/e2e/middy.test.functionCode.ts @@ -34,7 +34,7 @@ export const handler = middy( }, }) ); - await fetch('https://docs.powertools.aws.dev/lambda/typescript/latest/'); + await fetch('https://docs.aws.amazon.com/powertools/typescript/latest/'); if (event.throw) { throw new Error(customErrorMessage); diff --git a/packages/tracer/tests/e2e/middy.test.ts b/packages/tracer/tests/e2e/middy.test.ts index 49f152d1eb..26cc09f4fa 100644 --- a/packages/tracer/tests/e2e/middy.test.ts +++ b/packages/tracer/tests/e2e/middy.test.ts @@ -75,7 +75,7 @@ describe('Tracer E2E tests, middy instrumentation', () => { * 1. Lambda Context (AWS::Lambda) * 2. Lambda Function (AWS::Lambda::Function) * 4. DynamoDB (AWS::DynamoDB) - * 4. Remote call (docs.powertools.aws.dev) + * 4. Remote call (docs.aws.amazon.com) */ expectedSegmentsCount: 4, }); @@ -100,11 +100,11 @@ describe('Tracer E2E tests, middy instrumentation', () => { expect(subsegments.has('DynamoDB')).toBe(true); // Check remote call subsegment - expect(subsegments.has('docs.powertools.aws.dev')).toBe(true); - const httpSubsegment = subsegments.get('docs.powertools.aws.dev'); + expect(subsegments.has('docs.aws.amazon.com')).toBe(true); + const httpSubsegment = subsegments.get('docs.aws.amazon.com'); expect(httpSubsegment?.namespace).toBe('remote'); expect(httpSubsegment?.http?.request?.url).toEqual( - 'https://docs.powertools.aws.dev/lambda/typescript/latest/' + 'https://docs.aws.amazon.com/powertools/typescript/latest/' ); expect(httpSubsegment?.http?.request?.method).toBe('GET'); expect(httpSubsegment?.http?.response?.status).toEqual(expect.any(Number)); diff --git a/packages/tracer/tests/helpers/httpRequest.ts b/packages/tracer/tests/helpers/httpRequest.ts index 579f56ac3f..7918a38fd2 100644 --- a/packages/tracer/tests/helpers/httpRequest.ts +++ b/packages/tracer/tests/helpers/httpRequest.ts @@ -1,3 +1,4 @@ +import type { OutgoingHttpHeaders } from 'node:http'; import https, { type RequestOptions } from 'node:https'; /** @@ -20,6 +21,32 @@ const httpRequest = (params: RequestOptions): Promise => params.timeout = 5000; } + // Ensure some common headers are present. Many sites (including AWS docs) + // block requests that don't include a User-Agent or typical Accept header, + // which is why the `fetch` call (which sets defaults) succeeds while a + // bare https.request may receive a 403. + const originalHeaders = (params.headers ?? {}) as OutgoingHttpHeaders; + // Create a quick lowercase map of header names to detect presence case-insensitively + const lowerMap = Object.keys(originalHeaders).reduce< + Record + >((acc, k) => { + acc[k.toLowerCase()] = k; + return acc; + }, {}); + + // Only add defaults when not present (case-insensitive) + if (!lowerMap['user-agent']) { + // prefer a Node/undici-like UA to match what fetch/undici would send + (originalHeaders as Record)['User-Agent'] = + 'node-fetch/1.0 (+https://github.com/node-fetch/node-fetch)'; + } + if (!lowerMap.accept) { + (originalHeaders as Record).Accept = + 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; + } + + params.headers = originalHeaders; + const req = https.request(params, (res) => { if ( res.statusCode == null || From 1241ab037a4baec38a384421a599b068f4c02f9c Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Thu, 2 Oct 2025 23:23:43 +0200 Subject: [PATCH 2/2] chore: remove unused hoist --- packages/tracer/tests/e2e/decorator.test.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/tracer/tests/e2e/decorator.test.ts b/packages/tracer/tests/e2e/decorator.test.ts index 9a3638b208..70425b88d8 100644 --- a/packages/tracer/tests/e2e/decorator.test.ts +++ b/packages/tracer/tests/e2e/decorator.test.ts @@ -4,7 +4,7 @@ import { TestDynamodbTable } from '@aws-lambda-powertools/testing-utils/resource import { TestNodejsFunction } from '@aws-lambda-powertools/testing-utils/resources/lambda'; import type { EnrichedXRayTraceDocumentParsed } from '@aws-lambda-powertools/testing-utils/types'; import { getTraces } from '@aws-lambda-powertools/testing-utils/utils/xray-traces'; -import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import { invokeAllTestCases } from '../helpers/invokeAllTests.js'; import { EXPECTED_ANNOTATION_KEY as expectedCustomAnnotationKey, @@ -17,10 +17,6 @@ import { RESOURCE_NAME_PREFIX, } from './constants.js'; -vi.hoisted(() => { - process.env.AWS_PROFILE = 'aamorosi-Admin'; -}); - describe('Tracer E2E tests, decorator instrumentation', () => { const testStack = new TestStack({ stackNameProps: {