Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e9c7253
Disable existing fetch tests
chancancode Jan 10, 2025
d605b5a
Install msw
chancancode Jan 10, 2025
ef0b56f
Port enabling/disabling tests
chancancode Jan 10, 2025
3a07a5f
Temporary fix for #5314
chancancode Jan 10, 2025
c00e7f8
Port the first set of actual fetch() test to new msw test infra
chancancode Jan 10, 2025
90de22d
Port trace propagation headers tests
chancancode Jan 13, 2025
c16b0b7
Add tests without global propagator
chancancode Jan 13, 2025
c1bec45
Port test for `clearTimingResources`
chancancode Jan 13, 2025
52d614c
Port CORS tests
chancancode Jan 13, 2025
ad093ba
Port POST requests (measureRequestSize) tests
chancancode Jan 13, 2025
3b20d38
Port secure origin tests
chancancode Jan 13, 2025
72089a5
Port `applyCustomAttributesOnSpan` tests
chancancode Jan 13, 2025
4214f6a
Port `ignoreUrls` tests
chancancode Jan 13, 2025
0344845
Port tests for HTTP errors
chancancode Jan 14, 2025
e97f9b6
Port `PerformanceObserver` available tests
chancancode Jan 14, 2025
8f3dd77
Port `PerformanceObserver` not available tests
chancancode Jan 14, 2025
5e51471
Port `performance.getEntriesByType` not available tests
chancancode Jan 14, 2025
04e844a
Port `ignoreNetworkEvents` tests
chancancode Jan 14, 2025
9f8149c
Cleanup: remove old tests
chancancode Jan 14, 2025
c34f0ee
Get rid of 500ms timeout
chancancode Jan 15, 2025
a155326
Merge branch 'main' into msw-fetch-test
pichlermarc Jan 27, 2025
c543bf6
Revert "Temporary fix for #5314"
chancancode Jan 29, 2025
f6a350d
Manually merge branch 'main' into msw-fetch-test-redux
chancancode Jan 29, 2025
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
Prev Previous commit
Next Next commit
Port POST requests (measureRequestSize) tests
  • Loading branch information
chancancode committed Jan 14, 2025
commit ad093ba8963c7f397a1e21fbfd7b8ac981ef7696
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@ describe('fetch', () => {
});
});

describe('post data', () => {
describe('url and config object when request body measurement is disabled', () => {
xdescribe('post data', () => {
xdescribe('url and config object when request body measurement is disabled', () => {
beforeEach(async () => {
await prepareData(
url,
Expand All @@ -666,7 +666,7 @@ describe('fetch', () => {
clearData();
});

it('should post data', async () => {
xit('should post data', async () => {
assert.strictEqual(requestBody, '{"hello":"world"}');

const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
Expand All @@ -679,7 +679,7 @@ describe('fetch', () => {
});
});

describe('url and config object', () => {
xdescribe('url and config object', () => {
beforeEach(async () => {
await prepareData(
url,
Expand All @@ -703,7 +703,7 @@ describe('fetch', () => {
clearData();
});

it('should post data', async () => {
xit('should post data', async () => {
assert.strictEqual(requestBody, '{"hello":"world"}');

const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
Expand All @@ -716,7 +716,7 @@ describe('fetch', () => {
});
});

describe('url and config object with stream', () => {
xdescribe('url and config object with stream', () => {
beforeEach(async () => {
await prepareData(
url,
Expand All @@ -740,7 +740,7 @@ describe('fetch', () => {
clearData();
});

it('should post data', async () => {
xit('should post data', async () => {
assert.strictEqual(requestBody, '{"hello":"world"}');

const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
Expand All @@ -753,7 +753,7 @@ describe('fetch', () => {
});
});

describe('single request object', () => {
xdescribe('single request object', () => {
beforeEach(async () => {
await prepareData(
url,
Expand All @@ -779,7 +779,7 @@ describe('fetch', () => {
clearData();
});

it('should post data', async () => {
xit('should post data', async () => {
assert.strictEqual(requestBody, '{"hello":"world"}');

const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
Expand All @@ -792,7 +792,7 @@ describe('fetch', () => {
});
});

describe('single request object with urlparams', () => {
xdescribe('single request object with urlparams', () => {
beforeEach(async () => {
await prepareData(
url,
Expand Down Expand Up @@ -820,7 +820,7 @@ describe('fetch', () => {
clearData();
});

it('should post data', async () => {
xit('should post data', async () => {
assert.strictEqual(requestBody, 'hello=world');

const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -866,5 +866,197 @@ describe('fetch', () => {
});
});
});

describe('POST requests', () => {
const DEFAULT_BODY = Object.freeze({ hello: 'world' });

async function tracedFetch({
handlers = [
msw.http.post('/api/echo-body.json', async ({ request }) => {
if (request.headers.get('Content-Type') === 'application/json') {
return msw.HttpResponse.json({
request: {
headers: Object.fromEntries(request.headers),
body: await request.json(),
},
});
} else {
return msw.HttpResponse.json({
request: {
headers: Object.fromEntries(request.headers),
body: await request.text(),
},
});
}
}),
],
body = DEFAULT_BODY,
callback = () =>
fetch('/api/echo-body.json', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
}),
config = {},
}: {
handlers?: msw.RequestHandler[];
body?: unknown;
callback?: () => Promise<Response>;
config?: FetchInstrumentationConfig;
} = {}): Promise<{ rootSpan: api.Span; response: Response }> {
let response: Response | undefined;

await startWorker(...handlers);

const rootSpan = await trace(async () => {
response = await callback();
}, config);

assert.ok(response instanceof Response);
assert.strictEqual(exportedSpans.length, 1);

return { rootSpan, response };
}

const assertJSONBody = async (
response: Response,
body: unknown = DEFAULT_BODY
) => {
const { request } = await response.json();
assert.strictEqual(request.headers['content-type'], 'application/json');
assert.deepStrictEqual(request.body, body);
};

describe('measureRequestSize', () => {
const assertNoRequestContentLength = () => {
const span: tracing.ReadableSpan = exportedSpans[0];

assert.strictEqual(
span.attributes[SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED],
undefined
);
};

const assertHasRequestContentLength = (
body = JSON.stringify(DEFAULT_BODY)
) => {
const span: tracing.ReadableSpan = exportedSpans[0];

assert.strictEqual(
span.attributes[SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED],
body.length
);
};

describe('when `measureRequestSize` is not set', () => {
it('should not measure request body size', async () => {
const { response } = await tracedFetch();
assertJSONBody(response);
assertNoRequestContentLength();
});
});

describe('when `measureRequestSize` is `false`', () => {
it('should not measure request body size', async () => {
const { response } = await tracedFetch({
config: { measureRequestSize: false },
});
assertJSONBody(response);
assertNoRequestContentLength();
});
});

describe('with `measureRequestSize: `true`', () => {
describe('with url and init object', () => {
it('should measure request body size', async () => {
const { response } = await tracedFetch({
config: { measureRequestSize: true },
});
assertJSONBody(response);
assertHasRequestContentLength();
});
});

describe('with url and init object with a body stream', () => {
it('should measure request body size', async () => {
const body = JSON.stringify(DEFAULT_BODY);
const encoder = new TextEncoder();
const stream = new ReadableStream({
start: controller => {
controller.enqueue(encoder.encode(body));
controller.close();
},
cancel: controller => {
controller.close();
},
});
const { response } = await tracedFetch({
callback: () =>
fetch('/api/echo-body.json', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: stream,
// @ts-expect-error this is required IRL but missing on the current TS definition
// https://developer.chrome.com/docs/capabilities/web-apis/fetch-streaming-requests#half_duplex
duplex: 'half',
}),
config: { measureRequestSize: true },
});
assertJSONBody(response);
assertHasRequestContentLength();
});
});

describe('with a Request object', () => {
it('should measure request body size', async () => {
const { response } = await tracedFetch({
callback: () =>
fetch(
new Request('/api/echo-body.json', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(DEFAULT_BODY),
})
),
config: { measureRequestSize: true },
});
assertJSONBody(response);
assertHasRequestContentLength();
});
});

describe('with a Request object and a URLSearchParams body', () => {
it('should measure request body size', async () => {
const { response } = await tracedFetch({
callback: () =>
fetch(
new Request('/api/echo-body.json', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams(DEFAULT_BODY),
})
),
config: { measureRequestSize: true },
});
const { request } = await response.json();
assert.strictEqual(
request.headers['content-type'],
'application/x-www-form-urlencoded'
);
assert.strictEqual(request.body, 'hello=world');
assertHasRequestContentLength('hello=world');
});
});
});
});
});
});
});