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: 2 additions & 2 deletions packages/tracer/tests/e2e/decorator.test.functionCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions packages/tracer/tests/e2e/decorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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,
});
Expand All @@ -100,11 +100,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));
Expand Down
2 changes: 1 addition & 1 deletion packages/tracer/tests/e2e/manual.test.functionCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/tracer/tests/e2e/middy.test.functionCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions packages/tracer/tests/e2e/middy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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));
Expand Down
27 changes: 27 additions & 0 deletions packages/tracer/tests/helpers/httpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { OutgoingHttpHeaders } from 'node:http';
import https, { type RequestOptions } from 'node:https';

/**
Expand All @@ -20,6 +21,32 @@ const httpRequest = (params: RequestOptions): Promise<unknown> =>
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<string, string>
>((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<string, string>)['User-Agent'] =
'node-fetch/1.0 (+https://github.com/node-fetch/node-fetch)';
}
if (!lowerMap.accept) {
(originalHeaders as Record<string, string>).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 ||
Expand Down
Loading