Skip to content

Commit e2b9470

Browse files
Nicholas Roscinonroscino
authored andcommitted
chore: Add tests
1 parent ededa6d commit e2b9470

File tree

2 files changed

+149
-2
lines changed

2 files changed

+149
-2
lines changed

tests/formatters/networkFormatter.test.ts

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
import assert from 'node:assert';
88
import {describe, it} from 'node:test';
99

10+
import {ProtocolError} from 'puppeteer-core';
11+
1012
import {
1113
getFormattedHeaderValue,
14+
getFormattedRequestBody,
15+
getFormattedResponseBody,
1216
getShortDescriptionForRequest,
1317
} from '../../src/formatters/networkFormatter.js';
1418
import {getMockRequest, getMockResponse} from '../utils.js';
@@ -34,7 +38,6 @@ describe('networkFormatter', () => {
3438

3539
assert.equal(result, 'http://example.com GET [success - 200]');
3640
});
37-
3841
it('shows correct status for request with response code in 100', async () => {
3942
const response = getMockResponse({
4043
status: 199,
@@ -53,7 +56,6 @@ describe('networkFormatter', () => {
5356

5457
assert.equal(result, 'http://example.com GET [failed - 300]');
5558
});
56-
5759
it('shows correct status for request that failed', async () => {
5860
const request = getMockRequest({
5961
failure() {
@@ -100,4 +102,137 @@ describe('networkFormatter', () => {
100102
assert.deepEqual(result, []);
101103
});
102104
});
105+
106+
describe('getFormattedRequestBody', () => {
107+
it('shows data from fetchPostData if postData is undefined', async () => {
108+
const request = getMockRequest({
109+
hasPostData: true,
110+
postData: undefined,
111+
fetchPostData: Promise.resolve('test'),
112+
});
113+
114+
const result = await getFormattedRequestBody('body', request, 200);
115+
116+
assert.strictEqual(result, 'body\ntest');
117+
});
118+
it('shows empty string when no postData available', async () => {
119+
const request = getMockRequest({
120+
hasPostData: false,
121+
});
122+
123+
const result = await getFormattedRequestBody('body', request, 200);
124+
125+
assert.strictEqual(result, '');
126+
});
127+
it('shows request body when postData is available', async () => {
128+
const request = getMockRequest({
129+
postData: JSON.stringify({
130+
request: 'body',
131+
}),
132+
hasPostData: true,
133+
});
134+
135+
const result = await getFormattedRequestBody('body', request, 200);
136+
137+
assert.strictEqual(
138+
result,
139+
`body\n${JSON.stringify({
140+
request: 'body',
141+
})}`,
142+
);
143+
});
144+
it('shows trunkated string correctly with postData', async () => {
145+
const response = getMockRequest({
146+
postData: 'some text that is longer than expected',
147+
hasPostData: true,
148+
});
149+
150+
const result = await getFormattedRequestBody('body', response, 20);
151+
152+
assert.strictEqual(result, 'body\nsome text that is lo... <truncated>');
153+
});
154+
it('shows trunkated string correctly with fetchPostData', async () => {
155+
const response = getMockRequest({
156+
fetchPostData: Promise.resolve(
157+
'some text that is longer than expected',
158+
),
159+
postData: undefined,
160+
hasPostData: true,
161+
});
162+
163+
const result = await getFormattedRequestBody('body', response, 20);
164+
165+
assert.strictEqual(result, 'body\nsome text that is lo... <truncated>');
166+
});
167+
it('shows nothing on exception', async () => {
168+
const request = getMockRequest({
169+
hasPostData: true,
170+
postData: undefined,
171+
fetchPostData: Promise.reject(new ProtocolError()),
172+
});
173+
174+
const result = await getFormattedRequestBody('body', request, 200);
175+
176+
assert.strictEqual(result, '');
177+
});
178+
});
179+
180+
describe('getFormattedResponseBody', () => {
181+
it('handles empty buffer correctly', async () => {
182+
const response = getMockResponse();
183+
response.buffer = () => {
184+
return Promise.resolve(Buffer.from(''));
185+
};
186+
187+
const result = await getFormattedResponseBody('body', response, 20);
188+
189+
assert.strictEqual(result, 'body\n<empty response>');
190+
});
191+
it('handles base64 text correctly', async () => {
192+
const binaryBuffer = Buffer.from([
193+
0xde, 0xad, 0xbe, 0xef, 0x00, 0x41, 0x42, 0x43,
194+
]);
195+
const response = getMockResponse();
196+
response.buffer = () => {
197+
return Promise.resolve(binaryBuffer);
198+
};
199+
200+
const result = await getFormattedResponseBody('body', response, 20);
201+
202+
assert.strictEqual(result, 'body\n<binary data>');
203+
});
204+
it('handles the text limit correctly', async () => {
205+
const response = getMockResponse();
206+
response.buffer = () => {
207+
return Promise.resolve(
208+
Buffer.from('some text that is longer than expected'),
209+
);
210+
};
211+
212+
const result = await getFormattedResponseBody('body', response, 20);
213+
214+
assert.strictEqual(result, 'body\nsome text that is lo... <truncated>');
215+
});
216+
it('handles the text format correctly', async () => {
217+
const response = getMockResponse();
218+
response.buffer = () => {
219+
return Promise.resolve(Buffer.from(JSON.stringify({response: 'body'})));
220+
};
221+
222+
const result = await getFormattedResponseBody('body', response, 200);
223+
224+
assert.strictEqual(result, `body\n${JSON.stringify({response: 'body'})}`);
225+
});
226+
it('handles error correctly', async () => {
227+
const response = getMockResponse();
228+
response.buffer = () => {
229+
// CDP Error simulation
230+
return Promise.reject(new ProtocolError());
231+
};
232+
233+
const result = await getFormattedResponseBody('body', response, 200);
234+
235+
assert.strictEqual(result, '');
236+
});
237+
});
103238
});

tests/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export function getMockRequest(
4646
response?: HTTPResponse;
4747
failure?: HTTPRequest['failure'];
4848
resourceType?: string;
49+
hasPostData?: boolean;
50+
postData?: string;
51+
fetchPostData?: Promise<string>;
4952
} = {},
5053
): HTTPRequest {
5154
return {
@@ -55,6 +58,15 @@ export function getMockRequest(
5558
method() {
5659
return options.method ?? 'GET';
5760
},
61+
fetchPostData() {
62+
return options.fetchPostData ?? Promise.reject();
63+
},
64+
hasPostData() {
65+
return options.hasPostData ?? false;
66+
},
67+
postData() {
68+
return options.postData;
69+
},
5870
response() {
5971
return options.response ?? null;
6072
},

0 commit comments

Comments
 (0)