Skip to content

Commit 037a350

Browse files
authored
test(tracer): remove axios and use https directly (#2559)
1 parent 900c12e commit 037a350

7 files changed

+72
-18
lines changed

Diff for: package-lock.json

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/tracer/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"@aws-sdk/client-xray": "^3.574.0",
3434
"@types/promise-retry": "^1.1.6",
3535
"aws-sdk": "^2.1627.0",
36-
"axios": "^1.6.8",
3736
"promise-retry": "^2.0.1"
3837
},
3938
"peerDependencies": {

Diff for: packages/tracer/tests/e2e/allFeatures.decorator.test.functionCode.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Tracer } from '../../src/Tracer.js';
22
import type { Callback, Context } from 'aws-lambda';
33
import AWS from 'aws-sdk';
4-
import axios from 'axios';
4+
import { httpRequest } from '../helpers/httpRequest.js';
55

66
const serviceName =
77
process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler';
@@ -52,8 +52,9 @@ export class MyFunctionBase {
5252
Item: { id: `${serviceName}-${event.invocation}-sdkv2` },
5353
})
5454
.promise(),
55-
axios.get('https://docs.powertools.aws.dev/lambda/typescript/latest/', {
56-
timeout: 5000,
55+
httpRequest({
56+
hostname: 'docs.powertools.aws.dev',
57+
path: '/lambda/typescript/latest/',
5758
}),
5859
new Promise((resolve, reject) => {
5960
setTimeout(() => {
@@ -66,7 +67,7 @@ export class MyFunctionBase {
6667
}, 2000); // We need to wait for to make sure previous calls are finished
6768
}),
6869
])
69-
.then(([_dynamoDBRes, _axiosRes, promiseRes]) => promiseRes)
70+
.then(([_dynamoDBRes, _httpRes, promiseRes]) => promiseRes)
7071
.catch((err) => {
7172
throw err;
7273
});

Diff for: packages/tracer/tests/e2e/allFeatures.manual.test.functionCode.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Tracer } from '../../src/index.js';
22
import type { Context } from 'aws-lambda';
3-
import axios from 'axios';
43
import AWS from 'aws-sdk';
54
import type { Subsegment } from 'aws-xray-sdk-core';
5+
import { httpRequest } from '../helpers/httpRequest.js';
66

77
const serviceName =
88
process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler';
@@ -55,10 +55,10 @@ export const handler = async (
5555
Item: { id: `${serviceName}-${event.invocation}-sdkv2` },
5656
})
5757
.promise();
58-
await axios.get(
59-
'https://docs.powertools.aws.dev/lambda/typescript/latest/',
60-
{ timeout: 5000 }
61-
);
58+
await httpRequest({
59+
hostname: 'docs.powertools.aws.dev',
60+
path: '/lambda/typescript/latest/',
61+
});
6262

6363
const res = customResponseValue;
6464
if (event.throw) {

Diff for: packages/tracer/tests/e2e/allFeatures.middy.test.functionCode.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Tracer } from '../../src/index.js';
33
import { captureLambdaHandler } from '../../src/middleware/middy.js';
44
import type { Context } from 'aws-lambda';
55
import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb';
6-
import axios from 'axios';
6+
import { httpRequest } from '../helpers/httpRequest.js';
77

88
const serviceName =
99
process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler';
@@ -46,10 +46,10 @@ const testHandler = async (
4646
Item: { id: { S: `${serviceName}-${event.invocation}-sdkv3` } },
4747
})
4848
);
49-
await axios.get(
50-
'https://docs.powertools.aws.dev/lambda/typescript/latest/',
51-
{ timeout: 5000 }
52-
);
49+
await httpRequest({
50+
hostname: 'docs.powertools.aws.dev',
51+
path: '/lambda/typescript/latest/',
52+
});
5353

5454
const res = customResponseValue;
5555
if (event.throw) {

Diff for: packages/tracer/tests/e2e/asyncHandler.decorator.test.functionCode.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Tracer } from '../../src/index.js';
22
import type { Context } from 'aws-lambda';
33
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
44
import { DynamoDBDocumentClient, PutCommand } from '@aws-sdk/lib-dynamodb';
5-
import axios from 'axios';
5+
import { httpRequest } from '../helpers/httpRequest.js';
66

77
const serviceName =
88
process.env.EXPECTED_SERVICE_NAME ?? 'MyFunctionWithStandardHandler';
@@ -58,7 +58,10 @@ export class MyFunctionBase {
5858
const url = 'https://docs.powertools.aws.dev/lambda/typescript/latest/';
5959
// Add conditional behavior because fetch is not available in Node.js 16 - this can be removed once we drop support for Node.js 16
6060
if (process.version.startsWith('v16')) {
61-
await axios.get(url, { timeout: 5000 });
61+
await httpRequest({
62+
hostname: 'docs.powertools.aws.dev',
63+
path: '/lambda/typescript/latest/',
64+
});
6265
} else {
6366
await fetch(url);
6467
}

Diff for: packages/tracer/tests/helpers/httpRequest.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import https, { type RequestOptions } from 'node:https';
2+
3+
/**
4+
* Make an HTTP request using the built-in `https` module
5+
*
6+
* This helper function is used in Tracer's tests to make HTTP requests
7+
* and assert that the requests are being traced correctly.
8+
*
9+
* If the requests are traced correctly, then all 3rd party libraries
10+
* built on top of the `https` module should also be traced correctly.
11+
*
12+
* @param params - The request options
13+
*/
14+
const httpRequest = (params: RequestOptions): Promise<unknown> =>
15+
new Promise((resolve, reject) => {
16+
if (!params.protocol) {
17+
params.protocol = 'https:';
18+
}
19+
if (!params.timeout) {
20+
params.timeout = 5000;
21+
}
22+
23+
const req = https.request(params, (res) => {
24+
if (
25+
res.statusCode == null ||
26+
res.statusCode < 200 ||
27+
res.statusCode >= 300
28+
) {
29+
return reject(new Error(`statusCode=${res.statusCode || 'unknown'}`));
30+
}
31+
const incomingData: Uint8Array[] = [];
32+
let responseBody: string;
33+
res.on('data', (chunk) => {
34+
incomingData.push(chunk);
35+
});
36+
res.on('end', () => {
37+
try {
38+
responseBody = Buffer.concat(incomingData).toString();
39+
} catch (error) {
40+
reject(error instanceof Error ? error : new Error('Unknown error'));
41+
}
42+
resolve(responseBody);
43+
});
44+
});
45+
req.on('error', (error) => {
46+
reject(error instanceof Error ? error : new Error(error));
47+
});
48+
49+
req.end();
50+
});
51+
52+
export { httpRequest };

0 commit comments

Comments
 (0)