Back to Repositories

Validating Client Certificate Filtering in Insomnia

This test suite validates the client certificate filtering functionality in Insomnia, focusing on matching certificates based on host and port combinations. The tests ensure proper certificate selection for HTTPS requests with varying URL patterns.

Test Coverage Overview

The test suite provides comprehensive coverage of the filterClientCertificates function.

Key areas tested include:
  • Certificate matching with both host and port specifications
  • Certificate matching with host-only patterns
  • Handling of HTTPS protocol validation
  • Array filtering of multiple certificate options

Implementation Analysis

The testing approach uses Jest/Vitest framework with TypeScript for type-safe test implementations.

Notable patterns include:
  • Mock certificate data structures
  • Explicit test cases for different URL matching scenarios
  • Array-based assertions for filtered results
  • Type-safe certificate interface implementation

Technical Details

Testing infrastructure includes:
  • Vitest as the test runner
  • TypeScript for type definitions
  • ClientCertificate model integration
  • HTTPS protocol handling
  • URL parsing and matching logic

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test case isolation
  • Descriptive test naming
  • Comprehensive mock data setup
  • Explicit assertions
  • Type-safe implementation
  • Focused test scenarios

kong/insomnia

packages/insomnia/src/network/__tests__/certificate.test.ts

            
import { describe, expect, it } from 'vitest';

import { ClientCertificate } from '../../models/client-certificate';
import { filterClientCertificates } from '../certificate';

describe('filterClientCertificates', () => {

  const requestUrl = 'https://www.example.com:1234';
  const clientCertificatesWithMatchPort: ClientCertificate[] = [
    {
      host: 'https://www.example.com:1234',
      _id: '',
      parentId: '',
      passphrase: '',
      cert: '',
      key: '',
      pfx: '',
      disabled: false,
      isPrivate: true,
      modified: 0,
      type: '',
      created: 0,
      name: '',
    },
    {
      host: 'https://www.example.com',
      _id: '',
      parentId: '',
      passphrase: '',
      cert: '',
      key: '',
      pfx: '',
      disabled: false,
      isPrivate: true,
      modified: 0,
      type: '',
      created: 0,
      name: '',
    },
  ];

  const clientCertificatesOnlyMatchHost = [
    {
      host: 'https://www.example.com',
      _id: '',
      parentId: '',
      passphrase: '',
      cert: '',
      key: '',
      pfx: '',
      disabled: false,
      isPrivate: true,
      modified: 0,
      type: '',
      created: 0,
      name: '',
    },
  ];
  it('should return certificate which can match both host and port', () => {
    const res = filterClientCertificates(clientCertificatesWithMatchPort, requestUrl, 'https:');
    expect(res.length).toEqual(1);
    expect(res[0].host).toEqual('https://www.example.com:1234');
  });

  it('should return certificate which can match host', () => {
    const res = filterClientCertificates(clientCertificatesOnlyMatchHost, requestUrl, 'https:');
    expect(res.length).toEqual(1);
    expect(res[0].host).toEqual('https://www.example.com');
  });
});