Deprecated: This package is deprecated. Use net/http/httptest instead.
github.com/stretchr/testify/httpimport "github.com/stretchr/testify/http"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.
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 mapWrite(bytes) - Writes response body bytesWriteHeader(i) - Sets the HTTP status codeDeprecated: 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 requestOld (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")
}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)
}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 responseshttptest.Server - Creates test HTTP servershttptest.NewRequest - Creates test HTTP requestsFor mocking HTTP clients, use testify/mock directly to create custom http.RoundTripper implementations as shown in the migration guide above.