mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-24 21:25:45 +00:00
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:
@@ -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) => {
|
||||
|
||||
@@ -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" } }');
|
||||
});
|
||||
|
||||
});
|
||||
@@ -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"
|
||||
}
|
||||
'''
|
||||
}
|
||||
18
tests/websockets/headers.spec.ts
Normal file
18
tests/websockets/headers.spec.ts
Normal 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\"/)
|
||||
});
|
||||
});
|
||||
23
tests/websockets/lib/locators.ts
Normal file
23
tests/websockets/lib/locators.ts
Normal 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' })
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user