A simple and expressive HTTP server mocking library for end-to-end tests in Go.

mockhttp

Go Reference Go Report Card

A simple and expressive HTTP server mocking library for end-to-end tests in Go.

Installation

go get -d github.com/americanas-go/mockhttp

Example

The following example shows a Book API as the application under test, which calls an external ISBN database exposed with an REST interface.

Since mockhttp only starts the server, you must have a way to inject the address of the mocked server into your application. For example, the Book API may use an environment variable BOOK_API_ISBN_URL that is set to http://localhost:12000/ during the end-to-end test execution.

package e2e

import (
	"net/url"
	"strings"
	"testing"
	"github.com/americanas-go/mockhttp"
	"net/http"
)

func TestCreateBook(t *testing.T) {
	mockServer := mockhttp.NewMockServer(mockhttp.WithPort(12000))
	mockServer.
		Get("/isbn", mockhttp.MatchQueryParams(url.Values{"title": []string{"Foundation"}})).
		Return(mockhttp.JSONBody(`{"isbn": "9780345317988"}`))

	mockServer.Start(t)
	defer mockServer.Teardown()

	bookBody := strings.NewReader(`{"title": "Foundation"}`)
	response, err := http.Post("http://localhost:9000/book", "application/json", bookBody)
	if err != nil {
		t.Error(err.Error())
		return
	}

	if response.StatusCode != http.StatusOK {
		t.Errorf("unexpected status code: %d", response.StatusCode)
	}
	
	mockServer.AssertExpectations()
}

Contributing

Every help is always welcome. Feel free do throw us a pull request, we'll do our best to check it out as soon as possible. But before that, let us establish some guidelines:

  1. This is an open source project so please do not add any proprietary code or infringe any copyright of any sort.
  2. Avoid unnecessary dependencies or messing up go.mod file.
  3. Be aware of golang coding style. Use a lint to help you out.
  4. Add tests to cover your contribution.
  5. Use meaningful messages to your commits.
  6. Use pull requests.
  7. At last, but also important, be kind and polite with the community.

Any submitted issue which disrespect one or more guidelines above, will be discarded and closed.


Released under the MIT License.

Owner
Americanas Go
Americanas open source software for Go development
Americanas Go
Similar Resources

Go Interface Mocking Tool

Charlatan Percolate's Go Interface Mocking Tool. Please read our introductory blog post. Installation go get github.com/percolate/charlatan Usage c

Nov 3, 2022

GoMock is a mocking framework for the Go programming language.

gomock GoMock is a mocking framework for the Go programming language. It integrates well with Go's built-in testing package, but can be used in other

Dec 28, 2022

A clipboard-based mocking framework for Go that gets out of your way.

A clipboard-based mocking framework for Go that gets out of your way.

A clipboard-based mocking framework for Go that gets out of your way. This tool has been built with inspiration lovingly taken from Moq, and fuelled b

Nov 18, 2022

Interface mocking tool for go generate

Interface mocking tool for go generate

Interface mocking tool for go generate. What is Moq? Moq is a tool that generates a struct from any interface. The struct can be used in test code as

Jan 5, 2023

Record and replay your HTTP interactions for fast, deterministic and accurate tests

go-vcr go-vcr simplifies testing by recording your HTTP interactions and replaying them in future runs in order to provide fast, deterministic and acc

Dec 25, 2022

Extremely flexible golang deep comparison, extends the go testing package and tests HTTP APIs

Extremely flexible golang deep comparison, extends the go testing package and tests HTTP APIs

go-testdeep Extremely flexible golang deep comparison, extends the go testing package. Latest news Synopsis Description Installation Functions Availab

Dec 22, 2022

Terratest is a Go library that makes it easier to write automated tests for your infrastructure code.

Terratest is a Go library that makes it easier to write automated tests for your infrastructure code. It provides a variety of helper functions and patterns for common infrastructure testing tasks,

Dec 30, 2022

gostub is a library to make stubbing in unit tests easy

gostub gostub is a library to make stubbing in unit tests easy. Getting started Import the following package: github.com/prashantv/gostub Click here t

Dec 28, 2022

gomonkey is a library to make monkey patching in unit tests easy

gomonkey is a library to make monkey patching in unit tests easy, and the core idea of monkey patching comes from Bouke, you can read this blogpost for an explanation on how it works.

Jan 4, 2023
Rr-e2e-tests - Roadrunner end-to-end tests repository
Rr-e2e-tests - Roadrunner end-to-end tests repository

RoadRunner end-to-end plugins tests License: The MIT License (MIT). Please see L

Dec 15, 2022
End-to-end HTTP and REST API testing for Go.

httpexpect Concise, declarative, and easy to use end-to-end HTTP and REST API testing for Go (golang). Basically, httpexpect is a set of chainable bui

Jan 5, 2023
πŸš€πŸŒ Orbital is a simple end-to-end testing framework for Go

Orbital is a test framework which enables a developer to write end to end tests just like one would writing unit tests. We do this by effectively copying the testing.T API and registering tests to be run periodically on a configured schedule.

Nov 18, 2022
HTTP traffic mocking and testing made easy in Go ΰΌΌΚ˜ΜšΩ„ΝœΚ˜ΜšΰΌ½

gock Versatile HTTP mocking made easy in Go that works with any net/http based stdlib implementation. Heavily inspired by nock. There is also its Pyth

Jan 4, 2023
HTTP mocking for Golang

httpmock Easy mocking of http responses from external resources. Install Currently supports Go 1.7 - 1.15. v1 branch has to be used instead of master.

Jan 3, 2023
HTTP mocking to test API services for chaos scenarios
HTTP mocking to test API services for chaos scenarios

GAOS HTTP mocking to test API services for chaos scenarios Gaos, can create and provide custom mock restful services via using your fully-customizable

Nov 5, 2022
Lightweight HTTP mocking in Go (aka golang)

httpmock This library builds on Go's built-in httptest library, adding a more mockable interface that can be used easily with other mocking tools like

Dec 16, 2022
End to end functional test and automation framework
End to end functional test and automation framework

Declarative end to end functional testing (endly) This library is compatible with Go 1.12+ Please refer to CHANGELOG.md if you encounter breaking chan

Jan 6, 2023
An always-on framework that performs end-to-end functional network testing for reachability, latency, and packet loss

Arachne Arachne is a packet loss detection system and an underperforming path detection system. It provides fast and easy active end-to-end functional

Dec 31, 2022
Full-featured test framework for Go! Assertions, mocking, input testing, output capturing, and much more! πŸ•
Full-featured test framework for Go! Assertions, mocking, input testing, output capturing, and much more! πŸ•

testza ?? Testza is like pizza for Go - you could life without it, but why should you? Get The Module | Documentation | Contributing | Code of Conduct

Dec 10, 2022