Skip to content

Commit

Permalink
Port POST request tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode committed Dec 24, 2024
1 parent af5bd86 commit 43db151
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -803,5 +803,187 @@ 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 rootSpan: api.Span;
let response: Response | undefined;

await startWorker(...handlers);

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);
};

const assertNoRequestContentLength = () => {
const span: tracing.ReadableSpan = exportedSpans[0][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][0];

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

describe('without `measureRequestSize`', () => {
it('should not measure request body size', async () => {
const { response } = await tracedFetch();
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();
debugger;
assert.strictEqual(
request.headers['content-type'],
'application/x-www-form-urlencoded'
);
assert.deepStrictEqual(request.body, 'hello=world');
assertHasRequestContentLength('hello=world');
});
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,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 @@ -665,7 +665,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 @@ -678,7 +678,7 @@ describe('fetch', () => {
});
});

describe('url and config object', () => {
xdescribe('url and config object', () => {
beforeEach(async () => {
await prepareData(
url,
Expand All @@ -702,7 +702,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 @@ -715,7 +715,7 @@ describe('fetch', () => {
});
});

describe('url and config object with stream', () => {
xdescribe('url and config object with stream', () => {
beforeEach(async () => {
await prepareData(
url,
Expand All @@ -739,7 +739,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 @@ -752,7 +752,7 @@ describe('fetch', () => {
});
});

describe('single request object', () => {
xdescribe('single request object', () => {
beforeEach(async () => {
await prepareData(
url,
Expand All @@ -778,7 +778,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 @@ -791,7 +791,7 @@ describe('fetch', () => {
});
});

describe('single request object with urlparams', () => {
xdescribe('single request object with urlparams', () => {
beforeEach(async () => {
await prepareData(
url,
Expand Down Expand Up @@ -819,7 +819,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

0 comments on commit 43db151

Please sign in to comment.