All Projects → dankinder → httpmock

dankinder / httpmock

Licence: MIT License
Lightweight HTTP mocking in Go (aka golang)

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to httpmock

htest
htest is a http-test package
Stars: ✭ 24 (-68%)
Mutual labels:  mock, httptest
msw-storybook-addon
Mock API requests in Storybook with Mock Service Worker.
Stars: ✭ 168 (+124%)
Mutual labels:  mock
mock-proxy-middleware
前后端分析项目中的本地mock和远程代理及接口文档
Stars: ✭ 26 (-65.33%)
Mutual labels:  mock
jest-launchdarkly-mock
Easily unit test LaunchDarkly feature flagged components with jest
Stars: ✭ 14 (-81.33%)
Mutual labels:  mock
animaris
Documentation and Mock for JSBridge base on ThinkJS & MongoDB & React & Antd.
Stars: ✭ 28 (-62.67%)
Mutual labels:  mock
kras
Efficient server proxying and mocking in Node.js. 💪
Stars: ✭ 18 (-76%)
Mutual labels:  mock
Stubmatic
Mock HTTP calls without coding. Designed specially for testing and testers.
Stars: ✭ 118 (+57.33%)
Mutual labels:  mock
jsxmock
使用 JSX 来定义 Mock Server
Stars: ✭ 31 (-58.67%)
Mutual labels:  mock
Retromock
Java library for mocking responses in a Retrofit service.
Stars: ✭ 48 (-36%)
Mutual labels:  mock
mockafka
A testing DSL for kafka-streams
Stars: ✭ 14 (-81.33%)
Mutual labels:  mock
emock
🐞 下一代C/C++跨平台mock库 (Next generation cross-platform mock library for C/C++)
Stars: ✭ 73 (-2.67%)
Mutual labels:  mock
doo
简单易用的接口管理解决方案,支持接口文档管理、Mock服务,接口测试等功能。接口文档采用 yaml 或 Excel 格式书写,简单快捷,Mock 基于该文档,无需数据库,一条命令秒变 Mock 服务。
Stars: ✭ 36 (-52%)
Mutual labels:  mock
mswjs.io
Official website and documentation for the Mock Service Worker library.
Stars: ✭ 77 (+2.67%)
Mutual labels:  mock
React-CleanArchitecture-Example
Sample frontend project with Clean Architecture using React.js and jest.
Stars: ✭ 73 (-2.67%)
Mutual labels:  mock
ng-apimock
Node plugin that provides the ability to use scenario based api mocking: for local development for protractor testing
Stars: ✭ 102 (+36%)
Mutual labels:  mock
deckard
DNS test harness
Stars: ✭ 28 (-62.67%)
Mutual labels:  mock
jest-how-do-i-mock-x
Runnable examples for common testing situations that often prove challenging
Stars: ✭ 63 (-16%)
Mutual labels:  mock
mountebank-api-php
Working with mountebank api it's easy!
Stars: ✭ 17 (-77.33%)
Mutual labels:  mock
Lol-Mock-API
Mock API Tool
Stars: ✭ 15 (-80%)
Mutual labels:  mock
stub-server
Stub server for REST APIs
Stars: ✭ 14 (-81.33%)
Mutual labels:  mock

httpmock

GoDoc Go Report Card Build Status

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 testify/mock. It does this by providing a Handler that receives HTTP components as separate arguments rather than a single *http.Request object.

Where the typical http.Handler interface is:

type Handler interface {
	ServeHTTP(ResponseWriter, *Request)
}

This library provides a server with the following interface, which works naturally with mocking libraries:

// Handler is the interface used by httpmock instead of http.Handler so that it can be mocked very easily.
type Handler interface {
	Handle(method, path string, body []byte) Response
}

Examples

The most primitive example, the OKHandler, just returns 200 OK to everything.

s := httpmock.NewServer(&httpmock.OKHandler{})
defer s.Close()

// Make any requests you want to s.URL(), using it as the mock downstream server

This example uses MockHandler, a Handler that is a testify/mock object.

downstream := &httpmock.MockHandler{}

// A simple GET that returns some pre-canned content
downstream.On("Handle", "GET", "/object/12345", mock.Anything).Return(httpmock.Response{
    Body: []byte(`{"status": "ok"}`),
})

s := httpmock.NewServer(downstream)
defer s.Close()

//
// Make any requests you want to s.URL(), using it as the mock downstream server
//

downstream.AssertExpectations(t)

If instead you wish to match against headers as well, a slightly different httpmock object can be used (please note the change in function name to be matched against):

downstream := &httpmock.MockHandlerWithHeaders{}

// A simple GET that returns some pre-canned content and a specific header
downstream.On("HandleWithHeaders", "GET", "/object/12345", MatchHeader("MOCK", "this"), mock.Anything).Return(httpmock.Response{
    Body: []byte(`{"status": "ok"}`),
})

// ... same as above

The httpmock package also provides helpers for checking calls using json objects, like so:

// This tests a hypothetical "echo" endpoint, which returns the body we pass to it.
type Obj struct {
    A string `json:"a"`
}

o := &Obj{A: "aye"}

// JSONMatcher ensures that this mock is triggered only when the HTTP body, when deserialized, matches the given
// object. Here, this mock response will get triggered only if `{"a":"aye"}` is sent.
downstream.On("Handle", "POST", "/echo", httpmock.JSONMatcher(o)).Return(httpmock.Response{
    Body: httpmock.ToJSON(o),
})
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].