Back to Repositories

Validating Header String Parsing and AWS Authentication in Insomnia

This test suite validates the header string parsing functionality in the Insomnia API client, focusing on request header manipulation and AWS IAM authentication integration. The tests ensure proper header formatting and handling across different request scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of header string parsing functionality.

Key areas tested include:
  • Default header handling with empty inputs
  • Header modifications for requests with body content
  • Multipart form data boundary handling
  • AWS IAM authentication header generation
Edge cases covered include empty request scenarios and specific content-type handling.

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for structured test organization. Tests implement expect assertions to verify header string generation and formatting.

Key patterns include:
  • Isolated test cases for each header scenario
  • Mock request object construction
  • AWS IAM credential verification
  • Content-type specific header validation

Technical Details

Testing tools and configuration:
  • Framework: Vitest/Jest
  • Test Type: Unit tests
  • Key Dependencies: AWS IAM constants, content-type constants
  • Test Structure: Modular describe blocks with individual test cases

Best Practices Demonstrated

The test suite exemplifies strong testing practices through clear organization and comprehensive validation.

Notable practices include:
  • Isolated test cases for specific functionality
  • Consistent assertion patterns
  • Clear test descriptions
  • Thorough validation of generated headers
  • Proper handling of authentication scenarios

kong/insomnia

packages/insomnia/src/network/__tests__/parse-header-strings.test.ts

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

import { AUTH_AWS_IAM, CONTENT_TYPE_FORM_DATA } from '../../common/constants';
import { parseHeaderStrings } from '../../main/network/parse-header-strings';

describe('parseHeaderStrings', () => {
  it('should default with empty inputs', () => {
    const req = { authentication: {}, body: {}, headers: [] };
    expect(parseHeaderStrings({ req })).toEqual(['Accept: */*', 'Accept-Encoding:', 'content-type:']);
  });

  it('should disable expect and transfer-encoding with body', () => {
    const req = { authentication: {}, body: {}, headers: [] };
    expect(parseHeaderStrings({ req, requestBody: 'test' })).toEqual(['Expect:', 'Transfer-Encoding:', 'Accept: */*', 'Accept-Encoding:', 'content-type:']);
  });

  it('should add boundary with multipart body path', () => {
    const req = { authentication: {}, body: { mimeType: CONTENT_TYPE_FORM_DATA }, headers: [] };
    expect(parseHeaderStrings({ req, requestBodyPath: '/tmp/x.z' })).toEqual(['Expect:', 'Transfer-Encoding:', 'Content-Type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY', 'Accept: */*', 'Accept-Encoding:']);
  });

  it('should sign with aws iam', () => {
    const req = { authentication: {
      sessionToken: 'someTokenSomethingSomething',
      type: AUTH_AWS_IAM,
    }, body: {}, headers: [] };
    const [host, token, date, authorization] = parseHeaderStrings({ req, finalUrl: 'http://x.y' });
    expect(host).toBe('Host: x.y');
    expect(token).toContain('X-Amz-Security-Token: someTokenSomethingSomething');
    expect(date).toContain('X-Amz-Date: ');
    expect(authorization).toContain('Authorization: AWS4-HMAC-SHA256 ');
    expect(authorization).toContain('Credential=');
    expect(authorization).toContain('SignedHeaders=host;x-amz-date;x-amz-security-token');
    expect(authorization).toContain('Signature=');
  });
});