Help us shape the future of Go! Take the 2026 Go Developer Survey

mockhttp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2023 License: MIT Imports: 9 Imported by: 0

README

gomockhttp Go Report Card

  • Mock the external http APIs your code is calling.
  • Verify/assert the calls your code performed

Getting started

Create the API mock

func TestApiCall(t *testing.T) {
  api := httpmock.Api(t)
  //...
}

Stub endpoints

api.
  Stub(http.MethodGet, "/endpoint").
  WithOkJson(http.StatusOK, jsonStub).

  Stub(http.MethodPost, "/endpoint").
  WithStatusCode(http.StatusCreated)

Call the mocked API

resultBasedOnMockedResponses, err := codeCallingTheApi(api.GetUrl())

Verify the API invocations

calls := api.
  Verify(http.MethodPost, "/endpoint").
  HasBeenCalled(2)

expectCall1 := calls[0]
expectCall1.WithPayload(expectedPayload1)

expectCall2 := calls[1]
expectCall2.WithPayload(expectedPayload2)

Examples

package main

import (
	"github.com/le-yams/gomockhttp"
	"net/http"
	"testing"
)

type Foo struct {
	Bar string `json:"foo"`
}

func TestApiCall(t *testing.T) {
	api := httpmock.Api(t)
	foo := NewFoo(api.GetUrl())

	api.Stub(http.MethodGet, "/foo").
		WithJson(http.StatusOK, &Foo{Bar: "bar"})

	bar := foo.GetBar() // the code to be tested calling the api endpoint to rerieve the value

	if bar != "bar" {
		t.Errorf("unexpected bar value: %s\n", bar)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiMock

type ApiMock struct {
	// contains filtered or unexported fields
}

func Api

func Api(testState TestingT) *ApiMock

func (*ApiMock) Close

func (mockedApi *ApiMock) Close()

func (*ApiMock) GetHost

func (mockedApi *ApiMock) GetHost() string

func (*ApiMock) GetUrl

func (mockedApi *ApiMock) GetUrl() *url.URL

func (*ApiMock) Stub

func (mockedApi *ApiMock) Stub(method string, path string) *StubBuilder

func (*ApiMock) Verify added in v0.1.0

func (mockedApi *ApiMock) Verify(method string, path string) *CallVerifier

type CallVerifier added in v0.1.0

type CallVerifier struct {
	// contains filtered or unexported fields
}

func (*CallVerifier) HasBeenCalled added in v0.1.0

func (verifier *CallVerifier) HasBeenCalled(expectedCallsCount int) []*Invocation

func (*CallVerifier) HasBeenCalledOnce added in v0.1.0

func (verifier *CallVerifier) HasBeenCalledOnce() *Invocation

type HttpCall

type HttpCall struct {
	Method string
	Path   string
}

type Invocation

type Invocation struct {
	// contains filtered or unexported fields
}

func (*Invocation) GetPayload added in v0.1.0

func (call *Invocation) GetPayload() []byte

func (*Invocation) GetRequest added in v0.1.0

func (call *Invocation) GetRequest() *http.Request

func (*Invocation) ReadJsonPayload added in v0.1.0

func (call *Invocation) ReadJsonPayload(obj any)

func (*Invocation) WithHeader added in v0.1.0

func (call *Invocation) WithHeader(name string, expectedValues ...string) *Invocation

func (*Invocation) WithPayload added in v0.1.0

func (call *Invocation) WithPayload(expected []byte) *Invocation

func (*Invocation) WithStringPayload added in v0.1.0

func (call *Invocation) WithStringPayload(expected string) *Invocation

func (*Invocation) WithoutHeader added in v0.1.0

func (call *Invocation) WithoutHeader(name string) *Invocation

type StubBuilder

type StubBuilder struct {
	// contains filtered or unexported fields
}

func (*StubBuilder) With

func (stub *StubBuilder) With(handler http.HandlerFunc) *ApiMock

func (*StubBuilder) WithJson

func (stub *StubBuilder) WithJson(statusCode int, content interface{}) *ApiMock

func (*StubBuilder) WithStatusCode added in v0.1.0

func (stub *StubBuilder) WithStatusCode(statusCode int) *ApiMock

type TestingT added in v0.1.0

type TestingT interface {
	Error(args ...any)
	Errorf(format string, args ...any)
	Fatal(args ...any)
	Fatalf(format string, args ...any)
}

TestingT is an interface wrapper around *testing.T

Jump to

Keyboard shortcuts

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