Back to Repositories

Validating gRPC URL Parsing Implementation in Insomnia

This test suite validates the parsing functionality of gRPC URLs in the Insomnia application, focusing on TLS enablement and URL formatting. The tests verify correct handling of different URL protocols and edge cases to ensure robust gRPC connection handling.

Test Coverage Overview

The test suite provides comprehensive coverage of gRPC URL parsing scenarios:

  • Protocol handling (grpc://, grpcs://)
  • Case sensitivity validation
  • TLS enablement logic
  • Falsey input handling
  • Custom domain support

Implementation Analysis

The testing approach utilizes Jest’s parameterized testing with it.each() to efficiently test multiple input variations. The implementation follows a data-driven testing pattern, systematically validating URL parsing behavior across different protocols and formats.

Tests verify both the URL transformation and TLS flag setting, ensuring consistent behavior across various input formats.

Technical Details

Testing tools and configuration:

  • Testing Framework: Vitest
  • Test Style: Unit tests with parameterized test cases
  • Assertion Method: toStrictEqual for exact object matching
  • Test Organization: Grouped by protocol type and edge cases

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Comprehensive edge case coverage
  • Consistent test case organization
  • Clear test descriptions using template literals
  • Efficient test case multiplication using it.each()
  • Proper handling of invalid inputs

kong/insomnia

packages/insomnia/src/network/grpc/__tests__/parse-grpc-url.test.ts

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

import { parseGrpcUrl } from '../parse-grpc-url';

describe('parseGrpcUrl', () => {
  it.each([
    ['grpcb.in:9000', 'grpcb.in:9000'],
    ['GRPCB.IN:9000', 'grpcb.in:9000'],
    ['custom.co', 'custom.co'],
  ])('should not enable tls with no protocol: %s', (input, expected) => {
    expect(parseGrpcUrl(input)).toStrictEqual({
      url: expected,
      enableTls: false,
    });
  });

  it.each([
    ['grpcs://grpcb.in:9000', 'grpcb.in:9000'],
    ['GRPCS://GRPCB.IN:9000', 'grpcb.in:9000'],
    ['grpcs://custom.co', 'custom.co'],
  ])('should enable tls with grpcs:// protocol: %s', (input, expected) => {
    expect(parseGrpcUrl(input)).toStrictEqual({
      url: expected,
      enableTls: true,
    });
  });

  it.each([
    ['grpc://grpcb.in:9000', 'grpcb.in:9000'],
    ['GRPC://GRPCB.IN:9000', 'grpcb.in:9000'],
    ['grpc://custom.co', 'custom.co'],
  ])('should not enable tls with no grpc:// protocol: %s', (input, expected) => {
    expect(parseGrpcUrl(input)).toStrictEqual({
      url: expected,
      enableTls: false,
    });
  });

  it.each([null, undefined, ''])('can handle falsey urls', input => {
    expect(parseGrpcUrl(input)).toStrictEqual({
      url: '',
      enableTls: false,
    });
  });
});