|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForError, waitForRequest, waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | +import { SDK_VERSION } from '@sentry/cloudflare'; |
| 4 | +import { WebSocket } from 'ws'; |
| 5 | + |
| 6 | +test('Index page', async ({ baseURL }) => { |
| 7 | + const result = await fetch(baseURL!); |
| 8 | + expect(result.status).toBe(200); |
| 9 | + await expect(result.text()).resolves.toBe('Hello World!'); |
| 10 | +}); |
| 11 | + |
| 12 | +test("worker's withSentry", async ({ baseURL }) => { |
| 13 | + const eventWaiter = waitForError('cloudflare-workersentrypoint', event => { |
| 14 | + return event.exception?.values?.[0]?.mechanism?.type === 'auto.http.cloudflare'; |
| 15 | + }); |
| 16 | + const response = await fetch(`${baseURL}/throwException`); |
| 17 | + expect(response.status).toBe(500); |
| 18 | + const event = await eventWaiter; |
| 19 | + expect(event.exception?.values?.[0]?.value).toBe('To be recorded in Sentry.'); |
| 20 | +}); |
| 21 | + |
| 22 | +test('RPC method which throws an exception to be logged to sentry', async ({ baseURL }) => { |
| 23 | + const eventWaiter = waitForError('cloudflare-workersentrypoint', event => { |
| 24 | + return event.exception?.values?.[0]?.mechanism?.type === 'auto.faas.cloudflare.durable_object'; |
| 25 | + }); |
| 26 | + const response = await fetch(`${baseURL}/rpc/throwException`); |
| 27 | + expect(response.status).toBe(500); |
| 28 | + const event = await eventWaiter; |
| 29 | + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry.'); |
| 30 | +}); |
| 31 | + |
| 32 | +test("Request processed by DurableObject's fetch is recorded", async ({ baseURL }) => { |
| 33 | + const eventWaiter = waitForError('cloudflare-workersentrypoint', event => { |
| 34 | + return event.exception?.values?.[0]?.mechanism?.type === 'auto.faas.cloudflare.durable_object'; |
| 35 | + }); |
| 36 | + const response = await fetch(`${baseURL}/pass-to-object/throwException`); |
| 37 | + expect(response.status).toBe(500); |
| 38 | + const event = await eventWaiter; |
| 39 | + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry.'); |
| 40 | +}); |
| 41 | + |
| 42 | +test('Websocket.webSocketMessage', async ({ baseURL }) => { |
| 43 | + const eventWaiter = waitForError('cloudflare-workersentrypoint', event => { |
| 44 | + return !!event.exception?.values?.[0]; |
| 45 | + }); |
| 46 | + const url = new URL('/pass-to-object/ws', baseURL); |
| 47 | + url.protocol = url.protocol.replace('http', 'ws'); |
| 48 | + const socket = new WebSocket(url.toString()); |
| 49 | + socket.addEventListener('open', () => { |
| 50 | + socket.send('throwException'); |
| 51 | + }); |
| 52 | + const event = await eventWaiter; |
| 53 | + socket.close(); |
| 54 | + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry: webSocketMessage'); |
| 55 | + expect(event.exception?.values?.[0]?.mechanism?.type).toBe('auto.faas.cloudflare.durable_object'); |
| 56 | +}); |
| 57 | + |
| 58 | +test('Websocket.webSocketClose', async ({ baseURL }) => { |
| 59 | + const eventWaiter = waitForError('cloudflare-workersentrypoint', event => { |
| 60 | + return !!event.exception?.values?.[0]; |
| 61 | + }); |
| 62 | + const url = new URL('/pass-to-object/ws', baseURL); |
| 63 | + url.protocol = url.protocol.replace('http', 'ws'); |
| 64 | + const socket = new WebSocket(url.toString()); |
| 65 | + socket.addEventListener('open', () => { |
| 66 | + socket.send('throwOnExit'); |
| 67 | + socket.close(); |
| 68 | + }); |
| 69 | + const event = await eventWaiter; |
| 70 | + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry: webSocketClose'); |
| 71 | + expect(event.exception?.values?.[0]?.mechanism?.type).toBe('auto.faas.cloudflare.durable_object'); |
| 72 | +}); |
| 73 | + |
| 74 | +test('sends user-agent header with SDK name and version in envelope requests', async ({ baseURL }) => { |
| 75 | + const requestPromise = waitForRequest('cloudflare-workersentrypoint', () => true); |
| 76 | + |
| 77 | + await fetch(`${baseURL}/throwException`); |
| 78 | + |
| 79 | + const request = await requestPromise; |
| 80 | + |
| 81 | + expect(request.rawProxyRequestHeaders).toMatchObject({ |
| 82 | + 'user-agent': `sentry.javascript.cloudflare/${SDK_VERSION}`, |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +test('Storage operations create spans in Durable Object transactions', async ({ baseURL }) => { |
| 87 | + const transactionWaiter = waitForTransaction('cloudflare-workersentrypoint', event => { |
| 88 | + return event.spans?.some(span => span.op === 'db' && span.description === 'durable_object_storage_put') ?? false; |
| 89 | + }); |
| 90 | + |
| 91 | + const response = await fetch(`${baseURL}/pass-to-object/storage/put`); |
| 92 | + expect(response.status).toBe(200); |
| 93 | + |
| 94 | + const transaction = await transactionWaiter; |
| 95 | + const putSpan = transaction.spans?.find(span => span.description === 'durable_object_storage_put'); |
| 96 | + |
| 97 | + expect(putSpan).toBeDefined(); |
| 98 | + expect(putSpan?.op).toBe('db'); |
| 99 | + expect(putSpan?.data?.['db.system.name']).toBe('cloudflare.durable_object.storage'); |
| 100 | + expect(putSpan?.data?.['db.operation.name']).toBe('put'); |
| 101 | +}); |
0 commit comments