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

mockhttp

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: MIT Imports: 10 Imported by: 0

README

gomockhttp

Build Status Coverage Status Go Report Card GoDoc Version

A fluent testing library for mocking http apis.

  • 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 := mockhttp.Api(t)
  defer func() { api.Close() }()
  //...
}

Stub endpoints

api.
  Stub(http.MethodGet, "/endpoint").
  WithJson(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)

Example

package main

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

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

func TestApiCall(t *testing.T) {
  // Arrange
  api := mockhttp.Api(t)
  defer func() { api.Close() }()

  api.
    Stub(http.MethodGet, "/foo").
    WithJson(http.StatusOK, &FooDto{Foo: "bar"})
  token := "testToken"

  //Act
  fooService := NewFooService(api.GetUrl(), token)
  foo := fooService.GetFoo() // the code actually making the http call to the api endpoint

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

  api.
    Verify(http.MethodGet, "/foo").
    HasBeenCalledOnce().
    WithHeader("Authorization", fmt.Sprintf("Bearer %s", token))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIMock added in v0.1.1

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

func API added in v0.1.1

func API(testState TestingT) *APIMock

func (*APIMock) Close added in v0.1.1

func (mockedAPI *APIMock) Close()

func (*APIMock) GetHost added in v0.1.1

func (mockedAPI *APIMock) GetHost() string

func (*APIMock) GetURL added in v0.1.1

func (mockedAPI *APIMock) GetURL() *url.URL

func (*APIMock) Stub added in v0.1.1

func (mockedAPI *APIMock) Stub(method string, path string) *StubBuilder

func (*APIMock) Verify added in v0.1.1

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 added in v0.1.1

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.1

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) WithUrlEncodedFormPayload added in v0.3.0

func (call *Invocation) WithUrlEncodedFormPayload() *InvocationRequestForm

func (*Invocation) WithoutHeader added in v0.1.0

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

type InvocationRequestForm added in v0.3.0

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

func (InvocationRequestForm) WithValues added in v0.3.0

func (form InvocationRequestForm) WithValues(expectedValues map[string]string)

func (InvocationRequestForm) WithValuesExactly added in v0.3.0

func (form InvocationRequestForm) WithValuesExactly(expectedValues map[string]string)

type StubBuilder

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

func (*StubBuilder) With

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

func (*StubBuilder) WithBody added in v0.2.0

func (stub *StubBuilder) WithBody(statusCode int, body []byte, contentType string) *APIMock

func (*StubBuilder) WithJSON added in v0.1.1

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