Back to Repositories

Testing Basic Auth Header Generation in Insomnia

This test suite validates the getBasicAuthHeader function’s ability to generate proper Authorization headers for basic authentication in the Insomnia API client. It covers various authentication scenarios including standard credentials, special character encoding, and edge cases with missing or null values.

Test Coverage Overview

The test suite provides comprehensive coverage of the basic authentication header generation functionality.

  • Standard username/password authentication
  • Special character handling with ISO-8859-1 encoding
  • Edge cases with null username
  • Empty and null password scenarios
  • Base64 encoding verification

Implementation Analysis

The tests utilize Jest’s describe/it pattern for structured test organization. Each test case validates the header generation with specific input combinations, ensuring the correct format and encoding of the Authorization header value. The implementation leverages vitest for test execution and assertion handling.

Technical Details

  • Testing Framework: Vitest/Jest
  • Language: TypeScript
  • Key Functions: getBasicAuthHeader
  • Encoding: Base64, ISO-8859-1 (Latin1)
  • Test Structure: Unit tests with describe/it blocks

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolated test cases, clear naming conventions, and comprehensive edge case coverage. Each test focuses on a specific scenario with explicit expectations and proper assertion patterns. The code organization follows the AAA (Arrange-Act-Assert) pattern for clarity and maintainability.

kong/insomnia

packages/insomnia/src/network/basic-auth/__tests__/get-header.test.ts

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

import { getBasicAuthHeader } from '../get-header';

describe('getBasicAuthHeader()', () => {
  it('succeed with username and password', () => {
    const header = getBasicAuthHeader('user', 'password');
    expect(header).toEqual({
      name: 'Authorization',
      value: 'Basic dXNlcjpwYXNzd29yZA==',
    });
  });

  it('succeed with username and password using iso-8859-1 encoding', () => {
    const header = getBasicAuthHeader('user', 'password-é', 'latin1');
    expect(header).toEqual({
      name: 'Authorization',
      value: 'Basic dXNlcjpwYXNzd29yZC3p',
    });
  });

  it('succeed with no username', () => {
    const header = getBasicAuthHeader(null, 'password');
    expect(header).toEqual({
      name: 'Authorization',
      value: 'Basic OnBhc3N3b3Jk',
    });
  });

  it('succeed with username and empty password', () => {
    const header = getBasicAuthHeader('user', '');
    expect(header).toEqual({
      name: 'Authorization',
      value: 'Basic dXNlcjo=',
    });
  });

  it('succeed with username and null password', () => {
    const header = getBasicAuthHeader('user', null);
    expect(header).toEqual({
      name: 'Authorization',
      value: 'Basic dXNlcjo=',
    });
  });
});