Back to Repositories

Testing URL Protocol Handling Implementation in Insomnia

This test suite evaluates the setDefaultProtocol utility function in the Insomnia application, focusing on URL protocol handling. The tests verify the function’s ability to properly handle various URL formats and ensure correct protocol assignment for web addresses.

Test Coverage Overview

The test suite provides comprehensive coverage of URL protocol handling scenarios.

Key areas tested include:
  • Empty URL handling
  • URLs without protocols
  • URLs with whitespace
  • Valid HTTP/HTTPS URLs
  • Invalid protocol URLs
Edge cases are well-covered, including empty strings and malformed URLs with incorrect protocols.

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for clear test organization. Each test case isolates a specific URL scenario, using expect assertions to verify the setDefaultProtocol function’s output. The implementation leverages Vitest’s testing framework features for TypeScript compatibility.

Technical patterns include input normalization testing and protocol validation checks.

Technical Details

Testing tools and configuration:
  • Vitest as the testing framework
  • TypeScript for type safety
  • Jest-style assertion syntax
  • Isolated unit tests for pure function testing
  • ES6 module import system

Best Practices Demonstrated

The test suite exemplifies several testing best practices including atomic test cases, clear test descriptions, and comprehensive edge case coverage. Each test focuses on a single responsibility with descriptive naming conventions. The code organization follows the AAA (Arrange-Act-Assert) pattern and maintains high readability through consistent formatting and structure.

kong/insomnia

packages/insomnia/src/utils/url/protocol.test.ts

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

import { setDefaultProtocol } from './protocol';

describe('setDefaultProtocol()', () => {
  it('no-ops on empty url', () => {
    const url = setDefaultProtocol('');
    expect(url).toBe('');
  });

  it('correctly sets protocol for empty', () => {
    const url = setDefaultProtocol('google.com');
    expect(url).toBe('http://google.com');
  });

  it('correctly sets protocol for padded domain', () => {
    const url = setDefaultProtocol('   google.com   ');
    expect(url).toBe('http://google.com');
  });

  it('does not set for valid url', () => {
    const url = setDefaultProtocol('https://google.com');
    expect(url).toBe('https://google.com');
  });

  it('does not set for valid url', () => {
    const url = setDefaultProtocol('http://google.com');
    expect(url).toBe('http://google.com');
  });

  it('does not set for invalid url', () => {
    const url = setDefaultProtocol('httbad://google.com');
    expect(url).toBe('httbad://google.com');
  });
});