oapitesthandler

module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2026 License: MIT

README

oapitesthandler

A code generator that creates test handlers for testing HTTP clients generated by oapi-codegen. It generates mock HTTP servers that implement the oapi-codegen strict server interface, allowing you to set expectations on API calls in your tests.

The generated code has type-safe methods for setting expected requests and their corresponding responses. It validates that all expectations are met during test execution and that no unexpected requests are made.

petstore_test.go has several examples of how to use the generated handler.

Set go:generate directives near where you are already generating your oapi-codegen client/server. For example:

//go:generate go tool oapi-codegen -config ./oapi-codegen.yaml ./openapi.yaml
//go:generate go tool oapitesthandler --config=./oapi-codegen.yaml --out=internal/testhandler ./openapi.yaml

Installation

With bindown
bindown template-source add oapitesthandler https://github.com/WillAbides/oapitesthandler/releases/latest/download/bindown.yaml
bindown dependency add oapitesthandler --source oapitesthandler -y
With go install
go install github.com/willabides/oapitesthandler/cmd/oapitesthandler@latest
With go get -tool
go get -tool github.com/willabides/oapitesthandler/cmd/oapitesthandler

CLI Usage

Usage: oapitesthandler --config=STRING --out=STRING <spec> [flags]

Arguments:
  <spec>    Path to OpenAPI spec YAML file

Flags:
  -h, --help             Show context-sensitive help.
  -c, --config=STRING    Path to oapi-codegen config YAML file
  -o, --out=STRING       Directory to write the generated test handler to
      --version          Output the oapitesthandler version and exit.

Directories

Path Synopsis
cmd
oapitesthandler command
example
petstore/internal/common/oapi
Package oapi provides primitives to interact with the openapi HTTP API.
Package oapi provides primitives to interact with the openapi HTTP API.
petstore/internal/oapi
Package oapi provides primitives to interact with the openapi HTTP API.
Package oapi provides primitives to interact with the openapi HTTP API.
petstore/internal/petstoretest
Package petstoretest provides primitives to interact with the openapi HTTP API.
Package petstoretest provides primitives to interact with the openapi HTTP API.
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL