apimock

package module
v0.0.0-...-7523d1f Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

Build Status GoDoc Go Walker Coverage Status Build Status

apimock

Simple API mocker helper for tests

This library allows to mock easily any API to use in our test.

Normally when creating an API client we need to access many times, so instancing a local api mock will allow us to override problems like rate limiting, double asset creations or just submit any data incorrectly.

API mocks allows to pass any interface decodeable by JSON or XML unmarshallers, or RAW responses.

## Simple usage:

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/sirupsen/logrus"
	"github.com/fernandezvara/apimock"
)

func main() {
	a := apimock.NewAPIMock(true, logrus.New(), "json")

	route1 := apimock.URIMock{
		Method:     "GET",
		URI:        "/hello",
		StatusCode: http.StatusOK,
		Response:   "world",
	}

	// adds a mock using an struct
	a.AddMock(&route1)

	// adds a mock one liner (you can pass a raw message)
	a.Add("GET", "/hi", http.StatusOK, []byte("ho"))

	a.Start()
	defer a.Stop()

	b, r := httpCall("GET", fmt.Sprintf("%s/hello", a.URL()))
	fmt.Println("response:", string(b))
	fmt.Println("status  :", r.StatusCode)

	b, r = httpCall("GET", fmt.Sprintf("%s/hi", a.URL()))
	fmt.Println("response:", string(b))
	fmt.Println("status  :", r.StatusCode)
}

func httpCall(_type, uri string) ([]byte, *http.Response) {
	buf := new(bytes.Buffer)
	httpClient := new(http.Client)

	req, err := http.NewRequest(_type, uri, buf)
	isErr(err)
	res, err := httpClient.Do(req)
	isErr(err)
	defer res.Body.Close()
	objectByte, err := ioutil.ReadAll(res.Body)
	isErr(err)

	return objectByte, res
}

func isErr(err error) {
	if err != nil {
		fmt.Println(err)
	}
}

*Some more samples on the /examples folder.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIMock

type APIMock struct {
	CORSEnabled bool
	Log         logrus.FieldLogger
	Type        string
	URIMocks    []*URIMock
	// contains filtered or unexported fields
}

APIMock is the main struct that

func NewAPIMock

func NewAPIMock(cors bool, log logrus.FieldLogger, apiType string) *APIMock

NewAPIMock returns a new instance of the Mock API

func (*APIMock) Add

func (a *APIMock) Add(method, uri string, statusCode int, response interface{})

Add adds a mock avoiding the need of struct creation

func (*APIMock) AddMock

func (a *APIMock) AddMock(uriMock *URIMock)

AddMock adds a new mock route/Response

func (*APIMock) Port

func (a *APIMock) Port() int

Port returns the TCP port where the mock is started

func (*APIMock) Protocol

func (a *APIMock) Protocol() string

Protocol returns the protocol used by the APIMock

func (*APIMock) Start

func (a *APIMock) Start()

Start runs the APIMock server

func (*APIMock) Stop

func (a *APIMock) Stop()

Stop finishes listening

func (*APIMock) URL

func (a *APIMock) URL() string

URL returns the listener address

type ErrorMessage

type ErrorMessage struct {
	Code    int    `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
}

ErrorMessage is the struct to format error messages returned by API

type URIMock

type URIMock struct {
	Method     string
	URI        string
	StatusCode int
	Response   interface{}
}

URIMock represents a API call and its response

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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