From 2a674786e20cbe94e00240509632b57600876616 Mon Sep 17 00:00:00 2001 From: Siddharth Gelera Date: Thu, 18 Sep 2025 17:21:53 +0530 Subject: [PATCH] test(websocket): add headers handling in tests - Implemented logic to send headers in websocket messages. - Added tests for websocket connections and message handling. - Created locators for common elements in websocket tests. --- packages/bruno-tests/src/ws/index.js | 23 +++++---- .../{basic.spec.ts => connection.spec.ts} | 47 ++++--------------- .../ws-test-request-with-headers.bru | 24 ++++++++++ tests/websockets/headers.spec.ts | 18 +++++++ tests/websockets/lib/locators.ts | 23 +++++++++ 5 files changed, 88 insertions(+), 47 deletions(-) rename tests/websockets/{basic.spec.ts => connection.spec.ts} (65%) create mode 100644 tests/websockets/fixtures/collection/ws-test-request-with-headers.bru create mode 100644 tests/websockets/headers.spec.ts create mode 100644 tests/websockets/lib/locators.ts diff --git a/packages/bruno-tests/src/ws/index.js b/packages/bruno-tests/src/ws/index.js index 01092a1b3..777cdecf8 100644 --- a/packages/bruno-tests/src/ws/index.js +++ b/packages/bruno-tests/src/ws/index.js @@ -9,16 +9,23 @@ const wss = new ws.Server({ }); wss.on('connection', function connection(ws, request) { - ws.on('message', function message(data) { - ws.send( - JSON.stringify({ - data: JSON.parse(Buffer.from(data).toString()), - }) - ); + const msg = Buffer.from(data).toString().trim(); + const obj = JSON.parse(msg); + if ('func' in obj && obj.func === 'headers') { + ws.send( + JSON.stringify({ + headers: request.headers + }) + ); + } else { + ws.send( + JSON.stringify({ + data: JSON.parse(Buffer.from(data).toString()) + }) + ); + } }); - - }); const wsRouter = (request, socket, head) => { diff --git a/tests/websockets/basic.spec.ts b/tests/websockets/connection.spec.ts similarity index 65% rename from tests/websockets/basic.spec.ts rename to tests/websockets/connection.spec.ts index e6cb5104b..ee47a07a0 100644 --- a/tests/websockets/basic.spec.ts +++ b/tests/websockets/connection.spec.ts @@ -1,29 +1,8 @@ -import { test, expect, Page } from '../../playwright'; +import { expect, test } from '../../playwright'; +import { buildCommonLocators } from './lib/locators'; const MAX_CONNECTION_TIME = 3000; -const buildCommonLocators = (page: Page) => ({ - runner: () => page.getByTestId('run-button'), - connectionControls: { - connect: () => - page - .locator('div.connection-controls') - .locator('.infotip') - .filter({ hasText: /^Connect$/ }), - disconnect: () => - page - .locator('div.connection-controls') - .locator('.infotip') - .filter({ hasText: /^Close Connection$/ }) - }, - messages: () => page.locator('.ws-message').all(), - toolbar: { - latestFirst:() => page.getByRole('button', { name: 'Latest First' }), - latestLast:() => page.getByRole('button', { name: 'Latest Last' }), - clearResponse: () => page.getByRole('button', { name: 'Clear Response' }) - } -}); - test.describe.serial('websockets', () => { test.setTimeout(2 * 10 * 1000); test('websocket requests are visible', async ({ pageWithUserData: page, restartApp }) => { @@ -76,24 +55,14 @@ test.describe.serial('websockets', () => { test('websocket request can send messages', async ({ pageWithUserData: page, restartApp }) => { const locators = buildCommonLocators(page); - - await locators.toolbar.clearResponse().click() - await locators.runner().click() - + + await locators.toolbar.clearResponse().click(); + await locators.runner().click(); + const messages = await locators.messages(); - expect( - await messages[1] - .locator('.text-ellipsis') - .innerText() - ).toMatch('{ "foo": "bar" }') + expect(await messages[1].locator('.text-ellipsis').innerText()).toMatch('{ "foo": "bar" }'); - - expect( - await messages[2] - .locator('.text-ellipsis') - .innerText() - ).toMatch('{ "data": { "foo": "bar" } }') + expect(await messages[2].locator('.text-ellipsis').innerText()).toMatch('{ "data": { "foo": "bar" } }'); }); - }); diff --git a/tests/websockets/fixtures/collection/ws-test-request-with-headers.bru b/tests/websockets/fixtures/collection/ws-test-request-with-headers.bru new file mode 100644 index 000000000..2185d0f99 --- /dev/null +++ b/tests/websockets/fixtures/collection/ws-test-request-with-headers.bru @@ -0,0 +1,24 @@ +meta { + name: ws-test-request-with-headers + type: ws + seq: 2 +} + +ws { + url: ws://localhost:8081/ws + body: ws + auth: inherit +} + +headers { + Authorization: Dummy +} + +body:ws { + name: message 1 + content: ''' + { + "func":"headers" + } + ''' +} diff --git a/tests/websockets/headers.spec.ts b/tests/websockets/headers.spec.ts new file mode 100644 index 000000000..7c3287495 --- /dev/null +++ b/tests/websockets/headers.spec.ts @@ -0,0 +1,18 @@ +import { test, expect, Page } from '../../playwright'; +import { buildCommonLocators } from './lib/locators'; + +const BRU_FILE_NAME = 'ws-test-request-with-headers'; + +test.describe.serial('headers', () => { + test.setTimeout(2 * 10 * 1000); + test('headers are returned if passed', async ({ pageWithUserData: page, restartApp }) => { + const locators = buildCommonLocators(page); + + await page.locator('#sidebar-collection-name').click(); + await page.getByTitle(BRU_FILE_NAME).click(); + await locators.runner().click(); + + const messages = await locators.messages(); + expect(await messages[2].locator('.text-ellipsis').innerText()).toMatch(/\"(authorization)\"\:\s+\"Dummy\"/) + }); +}); diff --git a/tests/websockets/lib/locators.ts b/tests/websockets/lib/locators.ts new file mode 100644 index 000000000..01509b3ca --- /dev/null +++ b/tests/websockets/lib/locators.ts @@ -0,0 +1,23 @@ +import { Page } from '../../../playwright'; + +export const buildCommonLocators = (page: Page) => ({ + runner: () => page.getByTestId('run-button'), + connectionControls: { + connect: () => + page + .locator('div.connection-controls') + .locator('.infotip') + .filter({ hasText: /^Connect$/ }), + disconnect: () => + page + .locator('div.connection-controls') + .locator('.infotip') + .filter({ hasText: /^Close Connection$/ }) + }, + messages: () => page.locator('.ws-message').all(), + toolbar: { + latestFirst:() => page.getByRole('button', { name: 'Latest First' }), + latestLast:() => page.getByRole('button', { name: 'Latest Last' }), + clearResponse: () => page.getByRole('button', { name: 'Clear Response' }) + } +}); \ No newline at end of file