HTTPtestify

1.0.3 · active · verified Wed Apr 22

HTTPtestify is a Node.js library designed for comprehensive integration testing of HTTP APIs. It provides a promise-based API, drawing inspiration from Axios, to facilitate making HTTP requests, asserting response properties, and conducting end-to-end tests. The current stable version is 1.0.3, with releases driven by feature additions and minor bug fixes rather than a strict schedule. Key differentiators include its extensive support for various HTTP methods (GET, POST, PUT, DELETE), advanced request customization (headers, body, query parameters), and sophisticated parallel request handling capabilities like `all`, `race`, and `allSettled`. The library also offers features such as request cancellation, proxy support, robust cookie and session management, and automatic parsing of JSON and optional XML responses, making it a versatile tool for complex API testing scenarios.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up `http-testify` with a simple Express server, make GET and POST requests, and execute parallel requests, logging their responses.

import HTTPtestify from 'http-testify';
import express from 'express'; // Common framework for HTTP APIs

// 1. Create a minimal Express app instance for testing
const app = express();
app.use(express.json()); // Enable JSON body parsing for POST requests

// Define a simple GET endpoint
app.get('/api/status', (req, res) => {
  res.status(200).json({ status: 'ok', version: '1.0' });
});

// Define a simple POST endpoint
app.post('/api/submit', (req, res) => {
  const data = req.body;
  res.status(201).json({ message: 'Data received', received: data });
});

// 2. Initialize HTTPtestify with the app instance
const server = HTTPtestify.request(app);

// 3. Define an async test function
async function runExampleTests() {
  try {
    // Perform a GET request
    const getResponse = await server.get('/api/status');
    console.log('GET /api/status Status:', getResponse.status); // Expected: 200
    console.log('GET /api/status Data:', getResponse.data);    // Expected: { status: 'ok', version: '1.0' }

    // Perform a POST request with a JSON body
    const postResponse = await server.post('/api/submit', { item: 'widget', quantity: 5 });
    console.log('POST /api/submit Status:', postResponse.status); // Expected: 201
    console.log('POST /api/submit Data:', postResponse.data);    // Expected: { message: 'Data received', received: { item: 'widget', quantity: 5 } }

    // Demonstrate parallel GET requests
    const [res1, res2] = await server.all((instance) => [
      instance.get('/api/status'),
      instance.get('/api/status')
    ]);
    console.log('Parallel Requests Results:', res1.status, res2.status);

    console.log('\nAll example tests completed successfully!');
  } catch (error) {
    console.error('An error occurred during tests:', error);
  }
}

runExampleTests();

view raw JSON →