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.
This commit is contained in:
Siddharth Gelera
2025-09-18 17:21:53 +05:30
parent c0b2cc914d
commit 2a674786e2
5 changed files with 88 additions and 47 deletions

View File

@@ -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) => {

View File

@@ -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" } }');
});
});

View File

@@ -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"
}
'''
}

View File

@@ -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\"/)
});
});

View File

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