Skip to content

httpmock: recording-disabled

Back to gallery

Exhibit path: docs/examples/codegen/httpmock/recording-disabled/

Recording mode turned off: all unmatched requests fail immediately with a clear error. Use this exhibit when your test must never let a real HTTP request escape.

Input (in.go)

go
//go:build glacier_codegen_fixture

// Package apiclient defines the HTTP transport for the strict API client.
package apiclient

import "net/http"

// StrictTransport is an HTTP transport that must not make unregistered requests.
//
// +glacier:mock
// +glacier:httpmock recording=disabled
type StrictTransport interface {
	// RoundTrip executes an HTTP request.
	RoundTrip(req *http.Request) (*http.Response, error)
}

Output (out.go)

go
//go:build glacier_codegen_fixture

// Code generated by glaciergen. DO NOT EDIT.
// Source: in.go
package apiclient

import (
	"net/http"

	"github.com/nathanbrophy/glacier/httpmock"
)

// MockStrictTransport is a generated httpmock-backed transport for StrictTransport.
// Recording is disabled: unregistered requests return an error immediately.
type MockStrictTransport struct {
	router *httpmock.Router
}

// NewMockStrictTransport returns a MockStrictTransport with recording disabled.
// Any request that does not match a registered route will return an error.
func NewMockStrictTransport() *MockStrictTransport {
	return &MockStrictTransport{
		router: httpmock.NewRouter(httpmock.WithRecordingDisabled()),
	}
}

// Route registers a handler for the given method and path pattern.
func (m *MockStrictTransport) Route(method, path string, handler httpmock.Handler) {
	m.router.Route(method, path, handler)
}

// RoundTrip implements StrictTransport (and http.RoundTripper).
func (m *MockStrictTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	return m.router.RoundTrip(req)
}

What the generator did

  • Generated the same MockTransport scaffold as the programmable-router exhibit.
  • The RecordingDisabled option is set in the generated initialization helper so any unregistered request returns an error immediately rather than being recorded for later inspection.
  • This is the safest posture for unit tests: the test fails loudly on any URL that was not explicitly registered, instead of silently recording the call.

programmable-router with-body-closure

Apache-2.0