Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRAC-18-Upgrade-axios-latest-version-in-node-tracer #515

Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,5 @@ Secrets scrubbing are subject to the following limitations:

* Only JSON data secrets scrubbing is supported



1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = () => {
// fetch is not supported in Node.js versions lower than 18,
// so no need to check coverage for that version
collectCoverageFrom.push('!./src/hooks/fetch.ts');
collectCoverageFrom.push('!./src/hooks/baseFetch.ts');
}

if (NODE_MAJOR_VERSION > 14) {
Expand Down
229 changes: 229 additions & 0 deletions src/hooks/baseFetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import { NODE_MAJOR_VERSION } from '../../testUtils/nodeVersion';
import { BaseFetch } from './baseFetch';

// Conditionally skip the test suite
// if (NODE_MAJOR_VERSION < 18) {
// describe.skip('fetchUtils.skip', () => {
// it('should not run on Node.js versions less than 18', () => {
// // test cases here
// });
// });
// } else {
describe('fetchUtils', () => {
test('Test convertHeadersToKeyValuePairs - Headers input', () => {
// @ts-ignore
const headers = new Headers({
'content-type': 'application/json',
'content-length': '12345',
});
// @ts-ignore
const parsedHeaders = BaseFetch._convertHeadersToKeyValuePairs(headers);
expect(parsedHeaders).toEqual({
'content-type': 'application/json',
'content-length': '12345',
});
});

test('Test convertHeadersToKeyValuePairs - Record<string, string> input', () => {
const headers = {
'content-type': 'application/json',
'content-length': '12345',
};
// @ts-ignore
const parsedHeaders = BaseFetch._convertHeadersToKeyValuePairs(headers);
expect(parsedHeaders).toEqual({
'content-type': 'application/json',
'content-length': '12345',
});
});

test('Test convertHeadersToKeyValuePairs - string[][] input', () => {
const headers = [
['content-type', 'application/json'],
['content-length', '12345'],
];
// @ts-ignore
const parsedHeaders = BaseFetch._convertHeadersToKeyValuePairs(headers);
expect(parsedHeaders).toEqual({
'content-type': 'application/json',
'content-length': '12345',
});
});

// TODO: Test the parseRequestArguments func
test.each([
[
{
input: 'https://example.com',
init: undefined,
expectedUrl: 'https://example.com',
expectedOptions: {
method: 'GET',
headers: {},
},
},
],
[
{
input: new URL('https://example.com'),
init: undefined,
expectedUrl: 'https://example.com/',
expectedOptions: {
method: 'GET',
headers: {},
},
},
],
[
{
input: new URL('https://example.com/'),
init: undefined,
expectedUrl: 'https://example.com/',
expectedOptions: {
method: 'GET',
headers: {},
},
},
],
[
{
// @ts-ignore
input: new Request('https://example.com'),
init: undefined,
expectedUrl: 'https://example.com/',
expectedOptions: {
method: 'GET',
headers: {},
},
},
],
[
{
// @ts-ignore
input: new Request(new URL('https://example.com')),
init: undefined,
expectedUrl: 'https://example.com/',
expectedOptions: {
method: 'GET',
headers: {},
},
},
],
[
{
// @ts-ignore
input: new Request('https://example.com', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ data: '12345' }),
}),
init: undefined,
expectedUrl: 'https://example.com/',
expectedOptions: {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ data: '12345' }),
},
},
],
// Here we will add the init object, making sure it overrides the input values
[
{
// @ts-ignore
input: new Request('https://example.com', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ data: '12345' }),
}),
init: {
method: 'PUT',
headers: { 'content-type': 'application/xml' },
body: JSON.stringify({ data: '54321' }),
},
expectedUrl: 'https://example.com/',
expectedOptions: {
method: 'PUT',
headers: { 'content-type': 'application/xml' },
body: JSON.stringify({ data: '54321' }),
},
},
],
[
{
input: 'https://example.com',
init: {
method: 'PUT',
headers: { 'content-type': 'application/xml' },
body: JSON.stringify({ data: '54321' }),
},
expectedUrl: 'https://example.com',
expectedOptions: {
method: 'PUT',
headers: { 'content-type': 'application/xml' },
body: JSON.stringify({ data: '54321' }),
},
},
],
[
{
input: new URL('https://example.com'),
init: {
method: 'PUT',
headers: { 'content-type': 'application/xml' },
body: JSON.stringify({ data: '54321' }),
},
expectedUrl: 'https://example.com/',
expectedOptions: {
method: 'PUT',
headers: { 'content-type': 'application/xml' },
body: JSON.stringify({ data: '54321' }),
},
},
],
// Test different formats for body
[
{
input: 'https://example.com',
init: {
method: 'PUT',
headers: { 'content-type': 'application/xml' },
// @ts-ignore
body: new Blob(['Blob contents']),
},
expectedUrl: 'https://example.com',
expectedOptions: {
method: 'PUT',
headers: { 'content-type': 'application/xml' },
body: 'Blob contents',
},
},
],
[
{
input: 'https://example.com',
init: {
method: 'PUT',
headers: { 'content-type': 'application/xml' },
// Unsupported body type
body: 123,
},
expectedUrl: 'https://example.com',
expectedOptions: {
method: 'PUT',
headers: { 'content-type': 'application/xml' },
},
},
],
// TODO: Test FormData body
// TODO: Test ReadableStream body
])(
'Test parsing fetch command arguments: %p',
async ({ input, init, expectedUrl, expectedOptions }) => {
// @ts-ignore
const { url, options } = await BaseFetch._parseRequestArguments({ input, init });
expect(url).toEqual(expectedUrl);
expect(options).toEqual(expectedOptions);
}
);
});
// }
Loading
Loading