Back to Repositories

Testing Response Object Validation and Assertions in Insomnia SDK

This test suite validates the Response object functionality in the Insomnia SDK, focusing on HTTP response handling and assertions. It verifies response parsing, content type handling, and provides comprehensive assertion chains for API testing.

Test Coverage Overview

The test suite provides comprehensive coverage of Response object functionality in Insomnia SDK.

Key areas tested include:
  • Response parsing and property access
  • Content-type and header handling
  • JSON body validation
  • Status code verification
  • Schema validation
Edge cases covered include content disposition parsing, charset handling, and negative assertion testing.

Implementation Analysis

The testing approach utilizes Vitest framework with a focus on behavior-driven development patterns. The implementation leverages chainable assertions for improved readability and maintainability.

Technical patterns include:
  • Request-Response object relationship testing
  • Fluent assertion chains using .to.have syntax
  • JSON schema validation
  • Content type parsing and validation

Technical Details

Testing tools and configuration:
  • Vitest as the primary testing framework
  • Custom Response class implementation
  • JSON Schema validation utilities
  • Content-type parser integration
  • Chainable assertion helpers
  • Mock request/response data structures

Best Practices Demonstrated

The test suite exemplifies several testing best practices for API response validation.

Notable practices include:
  • Comprehensive positive and negative assertions
  • Structured test data setup
  • Isolated test cases
  • Clear assertion messaging
  • Modular test organization
  • Type-safe testing approaches

kong/insomnia

packages/insomnia-sdk/src/objects/__tests__/response.test.ts

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

import { Request } from '../request';
import { Response } from '../response';

describe('test request and response objects', () => {
    it('test Response methods', () => {
        const req = new Request({
            url: 'https://hostname.com/path',
            method: 'GET',
            header: [
                { key: 'header1', value: 'val1' },
                { key: 'header2', value: 'val2' },
            ],
            body: {
                mode: 'raw',
                raw: '{"key": 888}',
            },
            auth: {
                type: 'basic',
                basic: [
                    { key: 'username', value: 'myname' },
                    { key: 'password', value: 'mypwd' },
                ],
            },
            proxy: undefined,
            certificate: undefined,
        });

        const resp = new Response({
            code: 200,
            reason: 'OK',
            header: [
                { key: 'header1', value: 'val1' },
                { key: 'header2', value: 'val2' },
                { key: 'Content-Length', value: '100' },
                { key: 'Content-Disposition', value: 'attachment; filename="filename.txt"' },
                { key: 'Content-Type', value: 'text/plain; charset=utf-8' },
            ],
            cookie: [
                { key: 'header1', value: 'val1' },
                { key: 'header2', value: 'val2' },
            ],
            body: '{"key": 888}',
            stream: undefined,
            responseTime: 100,
            originalRequest: req,
        });

        // TODO: this will work after PropertyList.one is improved
        // expect(resp.size()).toBe(100);

        expect(resp.json()).toEqual({
            key: 888,
        });
        expect(resp.contentInfo()).toEqual({
            charset: 'utf-8',
            contentType: 'text/plain; charset=utf-8',
            fileExtension: 'txt',
            fileName: 'filename',
            mimeFormat: '',
            mimeType: 'text/plain',
        });

        // extended assertion chains
        resp.to.have.status(200);
        resp.to.have.status('OK');
        resp.to.have.header('header1');
        resp.to.have.jsonBody({ 'key': 888 });
        resp.to.have.body('{"key": 888}');
        resp.to.have.jsonSchema({
            type: 'object',
            properties: {
                key: { type: 'integer' },
            },
            required: ['key'],
            additionalProperties: false,
        });

        resp.to.not.have.status(201);
        resp.to.not.have.status('NOT FOUND');
        resp.to.not.have.header('header_nonexist');
        resp.to.not.have.jsonBody({ 'key': 777 });
        resp.to.not.have.body('{"key": 777}');
        resp.to.not.have.jsonSchema({
            type: 'object',
            properties: {
                keyNoExist: { type: 'integer' },
            },
            required: ['keyNoExist'],
            additionalProperties: false,
        });
    });
});