Skip to content

Commit 274632c

Browse files
authored
test(browser): add sendBeacon Content-Type test coverage (#3317)
Follow-up from #3297 review discussion. Adds: - blob.type assertions to existing JSON and Base64 sendBeacon tests (only gzip was previously checked) - Test verifying sendBeacon is NOT called when body is undefined - Parameterized regression test covering all compression modes, asserting the body is always a Blob with the correct Content-Type These tests would have caught the bug fixed in #3297, where sendBeacon received a raw ArrayBuffer (no Content-Type) instead of a Blob, breaking proxies/WAFs/CDNs that require Content-Type.
1 parent 68cd4e5 commit 274632c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

packages/browser/src/__tests__/request.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ describe('request', () => {
455455
expect.any(Blob)
456456
)
457457
const blob = mockedNavigator?.sendBeacon.mock.calls[0][1] as Blob
458+
expect(blob.type).toBe('application/json')
458459
459460
const reader = new FileReader()
460461
const result = await new Promise((resolve) => {
@@ -480,6 +481,7 @@ describe('request', () => {
480481
expect.any(Blob)
481482
)
482483
const blob = mockedNavigator?.sendBeacon.mock.calls[0][1] as Blob
484+
expect(blob.type).toBe('application/x-www-form-urlencoded')
483485

484486
const reader = new FileReader()
485487
const result = await new Promise((resolve) => {
@@ -517,6 +519,45 @@ describe('request', () => {
517519
"
518520
`)
519521
})
522+
523+
it('should not call sendBeacon when body is undefined', () => {
524+
request(
525+
createRequest({
526+
url: 'https://any.posthog-instance.com/',
527+
method: 'POST',
528+
data: undefined,
529+
})
530+
)
531+
532+
expect(mockedNavigator?.sendBeacon).not.toHaveBeenCalled()
533+
})
534+
535+
it.each([
536+
['no compression', undefined, 'application/json'],
537+
['base64 compression', Compression.Base64, 'application/x-www-form-urlencoded'],
538+
['gzip compression', Compression.GZipJS, 'text/plain'],
539+
])(
540+
'always sends a Blob with correct Content-Type for %s',
541+
(_name: string, compression: Compression | undefined, expectedContentType: string) => {
542+
request(
543+
createRequest({
544+
url: 'https://any.posthog-instance.com/',
545+
method: 'POST',
546+
compression,
547+
data: { event: 'test' },
548+
})
549+
)
550+
551+
expect(mockedNavigator?.sendBeacon).toHaveBeenCalledTimes(1)
552+
const body = mockedNavigator?.sendBeacon.mock.calls[0][1]
553+
554+
// The body must always be a Blob so the browser sets the Content-Type header.
555+
// Sending a raw ArrayBuffer (as happened before the fix in #3297) causes the
556+
// browser to omit Content-Type, which breaks proxies/WAFs/CDNs that require it.
557+
expect(body).toBeInstanceOf(Blob)
558+
expect((body as Blob).type).toBe(expectedContentType)
559+
}
560+
)
520561
})
521562
})
522563
})

0 commit comments

Comments
 (0)