Back to Repositories

Testing WebSocket Connection Management Implementation in Insomnia

This test suite validates WebSocket functionality in the Insomnia API client, covering connection establishment, authentication methods, and connection management. It ensures reliable WebSocket communication with various authentication scenarios and connection handling features.

Test Coverage Overview

The test suite provides comprehensive coverage of WebSocket functionality in Insomnia.

Key areas tested include:
  • Basic WebSocket connection establishment and termination
  • Authentication methods (Basic Auth and Bearer Token)
  • Redirect handling
  • Multiple concurrent connections
  • Bulk connection termination

Implementation Analysis

The test implementation uses Playwright’s testing framework with async/await patterns for WebSocket testing. It leverages fixture loading and page object interactions to validate WebSocket behaviors systematically.

Technical patterns include:
  • Status verification through UI elements
  • Response body content validation
  • Connection state management
  • Authentication header verification

Technical Details

Testing tools and configuration:
  • Playwright test runner
  • Custom fixtures for app initialization
  • YAML configuration loading
  • TestID-based element selection
  • Platform-specific test speed adjustments
  • CodeMirror integration for response validation

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through structured validation approaches and comprehensive coverage.

Notable practices include:
  • Systematic test organization
  • Clear test case separation
  • Robust element selection strategies
  • Platform-specific considerations
  • Thorough state verification
  • Effective async operation handling

kong/insomnia

packages/insomnia-smoke-test/tests/smoke/websocket.test.ts

            
import { expect } from '@playwright/test';

import { loadFixture } from '../../playwright/paths';
import { test } from '../../playwright/test';

test('can make websocket connection', async ({ app, page }) => {
  test.slow(process.platform === 'darwin' || process.platform === 'win32', 'Slow app start on these platforms');
  const statusTag = page.locator('[data-testid="response-status-tag"]:visible');
  const responseBody = page.locator('[data-testid="response-pane"] >> [data-testid="CodeEditor"]:visible', {
    has: page.locator('.CodeMirror-activeline'),
  });

  const text = await loadFixture('websockets.yaml');
  await app.evaluate(async ({ clipboard }, text) => clipboard.writeText(text), text);

  await page.getByLabel('Import').click();
  await page.locator('[data-test-id="import-from-clipboard"]').click();
  await page.getByRole('button', { name: 'Scan' }).click();
  await page.getByRole('dialog').getByRole('button', { name: 'Import' }).click();
  await page.getByLabel('WebSockets').click();

  await page.getByLabel('Request Collection').getByTestId('localhost:4010').press('Enter');
  await expect(page.locator('.app')).toContainText('ws://localhost:4010');
  await page.click('text=Connect');
  await expect(statusTag).toContainText('101 Switching Protocols');
  await page.getByRole('tab', { name: 'Console' }).click();
  await expect(responseBody).toContainText('WebSocket connection established');
  await page.click('text=Disconnect');
  await expect(responseBody).toContainText('Closing connection with code 1005');

  // Can connect with Basic Auth
  await page.getByLabel('Request Collection').getByTestId('basic-auth').press('Enter');
  await expect(page.locator('.app')).toContainText('ws://localhost:4010/basic-auth');
  await page.click('text=Connect');
  await expect(statusTag).toContainText('101 Switching Protocols');
  await page.getByRole('tab', { name: 'Console' }).click();
  await expect(responseBody).toContainText('> authorization: Basic dXNlcjpwYXNzd29yZA==');

  // Can connect with Bearer Auth
  await page.getByLabel('Request Collection').getByTestId('bearer').press('Enter');
  await expect(page.locator('.app')).toContainText('ws://localhost:4010/bearer');
  await page.click('text=Connect');
  await expect(statusTag).toContainText('101 Switching Protocols');
  await page.getByRole('tab', { name: 'Console' }).click();
  await expect(responseBody).toContainText('> authorization: Bearer insomnia-cool-token-!!!1112113243111');

  // Can handle redirects
  await page.getByLabel('Request Collection').getByTestId('redirect').press('Enter');
  await expect(page.locator('.app')).toContainText('ws://localhost:4010/redirect');
  await page.click('text=Connect');
  await expect(statusTag).toContainText('101 Switching Protocols');
  await page.getByRole('tab', { name: 'Console' }).click();
  await expect(responseBody).toContainText('WebSocket connection established');

  const webSocketActiveConnections = page.locator('[data-testid="WebSocketSpinner__Connected"]');

  // Basic auth, Bearer auth, and Redirect connections are displayed as open
  await expect(webSocketActiveConnections).toHaveCount(3);

  // Can disconnect from all connections
  await page.locator('button[name="DisconnectDropdown__DropdownButton"]').click();
  await page.getByRole('menuitem', { name: 'Disconnect all requests' }).click();
  await expect(webSocketActiveConnections).toHaveCount(0);
});