Unexpected HTTP Server Testing Plugin

9.0.0 · active · verified Wed Apr 22

unexpected-http is a plugin for the `unexpected` assertion library, specifically designed for testing HTTP servers and clients. It extends `unexpected`'s declarative syntax, allowing developers to write expressive assertions against HTTP requests and responses, mimicking the style of `unexpected-express`. As of version 9.0.0, it supports `unexpected` versions 10 through 13. The library enables fluent testing of server-side applications by asserting on status codes, headers, and body content of HTTP interactions. While its release cadence often aligns with its core `unexpected` dependency, it receives updates to maintain compatibility and introduce features relevant to HTTP testing. Its primary differentiator is its deep integration into the `unexpected` ecosystem, providing a consistent assertion experience across various testing scenarios, including frontend, backend, and API testing, by leveraging `unexpected`'s powerful diffing and introspection capabilities for failed assertions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `unexpected-http` with an Express application and `supertest` to perform declarative HTTP assertions on status, headers, and JSON body content for various endpoints.

import { expect } from 'unexpected';
import unexpectedHttp from 'unexpected-http';
import express from 'express';
import supertest from 'supertest';

// Activate the unexpected-http plugin
expect.use(unexpectedHttp);

const app = express();
app.use(express.json()); // For parsing application/json

app.get('/hello', (req, res) => {
  res.status(200).json({ message: 'Hello, World!' });
});

app.post('/echo', (req, res) => {
  if (!req.body || Object.keys(req.body).length === 0) {
    return res.status(400).json({ error: 'Request body is empty' });
  }
  res.status(200).json({ received: req.body });
});

describe('HTTP Server Tests with unexpected-http', () => {
  let requestAgent: supertest.SuperTest<supertest.Test>;

  beforeAll(() => {
    // Use supertest to create a test agent for the Express app
    requestAgent = supertest(app);
  });

  it('should respond with a greeting on GET /hello', async () => {
    await expect(
      requestAgent.get('/hello'),
      'to yield response',
      {
        status: 200,
        headers: { 'content-type': 'application/json; charset=utf-8' },
        body: { message: 'Hello, World!' }
      }
    );
  });

  it('should echo the JSON body on POST /echo', async () => {
    const payload = { data: 'test data', value: 123 };
    await expect(
      requestAgent.post('/echo').send(payload),
      'to yield response',
      {
        status: 200,
        headers: { 'content-type': 'application/json; charset=utf-8' },
        body: { received: payload }
      }
    );
  });

  it('should return 400 for an empty body on POST /echo', async () => {
    await expect(
      requestAgent.post('/echo').send({}),
      'to yield response',
      400
    );
  });
});

view raw JSON →