Back to Repositories

Testing Response Context Implementation in Insomnia

This test suite validates the response handling functionality in the Insomnia API client, focusing on response initialization, header management, and body processing. The tests ensure proper handling of HTTP responses with comprehensive coverage of various response states and properties.

Test Coverage Overview

The test suite provides extensive coverage of response handling mechanisms in Insomnia.

Key areas tested include:
  • Response initialization and validation
  • Header manipulation and retrieval
  • Body content processing
  • Status code and message handling
  • Request correlation through IDs
Edge cases cover empty responses and multiple header values.

Implementation Analysis

The testing approach utilizes Jest’s describe/it blocks for structured test organization. The implementation leverages TypeScript’s type system for robust response object handling.

Notable patterns include:
  • File system integration for body content testing
  • Mock response object creation
  • Case-insensitive header handling
  • Buffer-based body content processing

Technical Details

Testing infrastructure includes:
  • Vitest as the test runner
  • Node.js fs module for file operations
  • OS temp directory for test file storage
  • Custom response models and initialization
  • Buffer manipulation for body content

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through modular test organization and comprehensive coverage.

Notable practices include:
  • Isolation of test cases
  • Proper cleanup of temporary files
  • Explicit error case handling
  • Consistent assertion patterns
  • Clear test case descriptions

kong/insomnia

packages/insomnia/src/plugins/context/__tests__/response.test.ts

            
import fs from 'fs';
import { tmpdir } from 'os';
import path from 'path';
import { describe, expect, it } from 'vitest';

import * as models from '../../../models/index';
import * as plugin from '../response';

describe('init()', () => {
  it('initializes correctly', async () => {
    const result = plugin.init({});
    expect(Object.keys(result)).toEqual(['response']);
    expect(Object.keys(result.response).sort()).toEqual([
      'getBody',
      'getBodyStream',
      'getBytesRead',
      'getHeader',
      'getHeaders',
      'getRequestId',
      'getStatusCode',
      'getStatusMessage',
      'getTime',
      'hasHeader',
      'setBody',
    ]);
  });

  it('fails to initialize without response', () => {
    expect(() => plugin.init()).toThrowError('contexts.response initialized without response');
  });
});

describe('response.*', () => {
  it('works for basic and full response', async () => {
    const bodyPath = path.join(tmpdir(), 'response.zip');
    fs.writeFileSync(bodyPath, Buffer.from('Hello World!'));
    const response = await models.initModel(models.response.type, {
      bodyPath,
      bodyCompression: null,
      parentId: 'req_1',
      url: 'https://insomnia.rest',
      statusCode: 200,
      statusMessage: 'OK',
      bytesRead: 123,
      elapsedTime: 321,
    });
    const result = plugin.init(response);
    expect(result.response.getRequestId()).toBe('req_1');
    expect(result.response.getStatusCode()).toBe(200);
    expect(result.response.getBytesRead()).toBe(123);
    expect(result.response.getTime()).toBe(321);
    expect(result.response.getBody().toString()).toBe('Hello World!');
  });

  it('works for basic and empty response', async () => {
    const result = plugin.init({});
    expect(result.response.getRequestId()).toBe('');
    expect(result.response.getStatusCode()).toBe(0);
    expect(result.response.getBytesRead()).toBe(0);
    expect(result.response.getTime()).toBe(0);
    expect(result.response.getBody().length).toBe(0);
  });

  it('works for getting headers', () => {
    const response = {
      headers: [
        {
          name: 'content-type',
          value: 'application/json',
        },
        {
          name: 'set-cookie',
          value: 'foo=bar',
        },
        {
          name: 'set-cookie',
          value: 'baz=qux',
        },
      ],
    };
    const result = plugin.init(response);
    expect(result.response.getHeaders()).toEqual([
      {
        name: 'content-type',
        value: 'application/json',
      },
      {
        name: 'set-cookie',
        value: 'foo=bar',
      },
      {
        name: 'set-cookie',
        value: 'baz=qux',
      },
    ]);
    expect(result.response.getHeader('Does-Not-Exist')).toBeNull();
    expect(result.response.getHeader('CONTENT-TYPE')).toBe('application/json');
    expect(result.response.getHeader('set-cookie')).toEqual(['foo=bar', 'baz=qux']);
    expect(result.response.hasHeader('foo')).toBe(false);
    expect(result.response.hasHeader('ConTent-Type')).toBe(true);
  });
});