Skip to content

hrishin/httpmock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTTP Mock

GitHub Actions CI status

The httpmock is a simple mocking library to mock the golang http client response.

Its intent is to reduce the boiler plate code required to write the unit test where a unit of code to test is wrapping the http.Client or passing http.Client as a dependency.

Usage

  1. Execute the go get github.com/hrishin/httpmock

  2. Let's mock the response for GET /foo request which return the HTTP 200 OK response with some response body content

    import (
        "testing"
        "github.com/hrishin/httpmock"
    )
    
    
    func Test_some_test(t *testing.T) { // I prefer using _ as convention(readability)
    
        mockResponse := httpmock.Response{
            URI:        "/foo",
            StatusCode: 200,
            Body:       "bar response",
        }
    
        api := &someAPI{
            client: httpmock.Client(&mockResponse),
            API: "https://some.com/api/",
        }
    
        got := api.do()
    
        // assert got response from API
    }
    

In this example, someAPI is using http.Client as a dependency in order to execute the HTTP request for a given API URL.

About

A mocking library for the go http client

Resources

License

Stars

Watchers

Forks

Packages

No packages published