Anjola A
Anjola A

Reputation: 51

How can I use testcafe to intercept and mock websocket requests

I am trying to override some websockets requests for functional testing purposes.

I have tried using intercepts and mocking as advised here https://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/ however this only seems straightforward for HTTP and can only update status code in WSS. The messages in the WSS is in JSON which I need to override.

import { RequestMock } from 'testcafe';
import { debug } from 'util';


const mockStream = new RegExp('test\/socket.io\/v2\/.?');

const ipUrl = 'https://example.com';
const mockedResponse = Buffer.from([0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01]);

const mock = RequestMock()
    .onRequestTo(mockStream)
    .respond(mockedResponse, 101, {
        'access-control-allow-credentials': true,
    });

fixture `Fixture`
    .page(ipUrl)
    .requestHooks(mock);
test('Mocking', async t => {
    await t
    .debug()
        .click('#button')
        .wait(5000)
        .debug();
});

Upvotes: 3

Views: 1243

Answers (1)

mlosev
mlosev

Reputation: 5227

At present, it's not possible to intercept WebSocket requests. I've created an issue for this case in the TestCafe repository. Track it to be informed about our progress with implementing this feature.

Upvotes: 4

Related Questions