Back to Repositories

Validating Proxy Configuration Management in Insomnia

This test suite validates the ProxyConfig object functionality in the Insomnia SDK, focusing on proxy configuration handling and URL transformation. It ensures proper proxy settings management and URL pattern matching for HTTP/HTTPS protocols.

Test Coverage Overview

The test suite provides comprehensive coverage of proxy configuration functionality.

Key areas tested include:
  • Protocol handling for HTTP/HTTPS
  • Proxy URL generation with authentication
  • URL pattern matching
  • ProxyConfigList management
  • Various proxy URL format transformations

Implementation Analysis

The testing approach utilizes Jest’s describe/it blocks for structured test organization. The implementation follows a behavior-driven development pattern, testing both basic operations and edge cases through discrete test cases.

Key patterns include:
  • Object instantiation testing
  • Method return validation
  • URL matching verification
  • Dynamic test generation using forEach

Technical Details

Testing tools and setup:
  • Vitest as the test runner
  • Jest-style assertions (expect)
  • TypeScript for type safety
  • ProxyConfig and ProxyConfigList class testing
  • Url object integration

Best Practices Demonstrated

The test suite exemplifies several testing best practices for proxy configuration validation.

Notable practices include:
  • Isolated test cases for specific functionality
  • Comprehensive edge case coverage
  • Parametrized testing for multiple URL formats
  • Clear test case descriptions
  • Proper type definitions and imports

kong/insomnia

packages/insomnia-sdk/src/objects/__tests__/proxy-configs.test.ts

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

import { ProxyConfig, ProxyConfigList, transformToSdkProxyOptions } from '../proxy-configs';
import { Url } from '../urls';

describe('test ProxyConfig object', () => {
    it('test basic operations', () => {

        const proxyConfig = new ProxyConfig({
            match: 'http+https://*.example.com:80/*',
            host: 'proxy.com',
            port: 8080,
            tunnel: true,
            disabled: false,
            authenticate: true,
            username: 'proxy_username',
            password: 'proxy_password',
            protocol: 'https:',
        });

        expect(
            proxyConfig.getProtocols()
        ).toEqual(
            ['http', 'https']
        );

        expect(proxyConfig.getProxyUrl()).toEqual(
            'https://proxy_username:[email protected]:8080'
        );

        expect(
            proxyConfig.test('http://a.example.com:80/a')
        ).toBeTruthy();

        const configList = new ProxyConfigList<ProxyConfig>(undefined, []);
        configList.add(proxyConfig);
        configList.add(new ProxyConfig({
            match: 'https://*.example.com:80/*',
            host: 'proxy.com',
            port: 8080,
            tunnel: true,
            disabled: false,
            authenticate: true,
            username: 'proxy_username',
            password: 'proxy_password',
            protocol: 'https:',
        }));

        const matchedProxyConfigDef = configList.resolve(new Url('http://sub.example.com:80/path'));
        expect(matchedProxyConfigDef?.host).toEqual('proxy.com');
    });

    const proxyUrls = [
        'http://wormhole',
        'http://wormhole:0',
        'https://localhost',
        'http://user:pass@localhost:666',
        'http://user:pass@localhost:0',
        'http://user:pass@localhost',
    ];

    proxyUrls.forEach(url => {
        it(`test proxy transforming: ${url}`, () => {
            const proxy = new ProxyConfig(transformToSdkProxyOptions(url, '', true, ''));
            expect(proxy.getProxyUrl()).toEqual(url);
        });
    });
});