or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

assert.mdhttp.mdindex.mdmock.mdrequire.mdsuite.md
tile.json

http.mddocs/

HTTP Package

Deprecated: This package is deprecated. Use net/http/httptest instead.

Package Information

  • Package Name: http
  • Package Type: go
  • Language: Go
  • Module: github.com/stretchr/testify/http
  • Status: Deprecated

Import

import "github.com/stretchr/testify/http"

Overview

The http package provided HTTP testing utilities but has been deprecated in favor of the standard library's net/http/httptest package, which provides more comprehensive and better-maintained HTTP testing capabilities.

Types

TestResponseWriter

Deprecated: Use net/http/httptest.ResponseRecorder instead.

type TestResponseWriter struct {
    // StatusCode is the last int written by the call to WriteHeader(int)
    StatusCode int

    // Output is a string containing the written bytes using the Write([]byte) func
    Output string

    // Has unexported fields
}

A mock HTTP response writer for testing HTTP handlers.

Methods:

func (rw *TestResponseWriter) Header() http.Header
func (rw *TestResponseWriter) Write(bytes []byte) (int, error)
func (rw *TestResponseWriter) WriteHeader(i int)
  • Header() - Returns the HTTP header map
  • Write(bytes) - Writes response body bytes
  • WriteHeader(i) - Sets the HTTP status code

TestRoundTripper

Deprecated: Use net/http/httptest instead.

type TestRoundTripper struct {
    mock.Mock
}

A mock HTTP round tripper for testing HTTP clients. Embeds mock.Mock for setting expectations.

Methods:

func (t *TestRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
  • RoundTrip(req) - Executes the HTTP round trip request

Migration Guide

Replacing TestResponseWriter

Old (deprecated):

import "github.com/stretchr/testify/http"

func TestHandler(t *testing.T) {
    rw := new(http.TestResponseWriter)
    handler(rw, req)

    assert.Equal(t, 200, rw.StatusCode)
    assert.Contains(t, rw.Output, "expected content")
}

New (recommended):

import "net/http/httptest"

func TestHandler(t *testing.T) {
    rw := httptest.NewRecorder()
    handler(rw, req)

    assert.Equal(t, 200, rw.Code)
    assert.Contains(t, rw.Body.String(), "expected content")
}

Replacing TestRoundTripper

Old (deprecated):

import "github.com/stretchr/testify/http"

func TestHTTPClient(t *testing.T) {
    rt := new(http.TestRoundTripper)
    rt.On("RoundTrip", mock.Anything).Return(response, nil)

    client := &http.Client{Transport: rt}
    // use client...
}

New (recommended):

import (
    "net/http/httptest"
    "github.com/stretchr/testify/mock"
)

// Create custom mock implementing http.RoundTripper
type MockRoundTripper struct {
    mock.Mock
}

func (m *MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    args := m.Called(req)
    return args.Get(0).(*http.Response), args.Error(1)
}

func TestHTTPClient(t *testing.T) {
    rt := new(MockRoundTripper)
    rt.On("RoundTrip", mock.Anything).Return(response, nil)

    client := &http.Client{Transport: rt}
    // use client...

    rt.AssertExpectations(t)
}

Alternatively, use httptest.Server for integration testing:

import "net/http/httptest"

func TestHTTPClient(t *testing.T) {
    // Create test server
    server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(200)
        w.Write([]byte("test response"))
    }))
    defer server.Close()

    // Use test server URL
    resp, err := http.Get(server.URL)
    require.NoError(t, err)
    defer resp.Body.Close()

    assert.Equal(t, 200, resp.StatusCode)
}

Recommendation

Do not use this package in new code. It exists only for backward compatibility. Use the standard library's net/http/httptest package instead, which provides:

  • httptest.ResponseRecorder - Records HTTP responses
  • httptest.Server - Creates test HTTP servers
  • httptest.NewRequest - Creates test HTTP requests
  • Better maintained and more feature-complete

For mocking HTTP clients, use testify/mock directly to create custom http.RoundTripper implementations as shown in the migration guide above.