https://github.com/michimani/http-client-mock
This is a package to generate a mock of http.Client in Go language. You can easily generate a http.
https://github.com/michimani/http-client-mock
golang http-client mock
Last synced: 3 months ago
JSON representation
This is a package to generate a mock of http.Client in Go language. You can easily generate a http.
- Host: GitHub
- URL: https://github.com/michimani/http-client-mock
- Owner: michimani
- License: mit
- Created: 2022-11-18T18:51:49.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-03T13:03:48.000Z (over 2 years ago)
- Last Synced: 2025-01-04T15:57:35.530Z (5 months ago)
- Topics: golang, http-client, mock
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
http-client-mock
===[](https://codecov.io/gh/michimani/http-client-mock)
This is a package to generate a mock of `http.Client` in Go language. This package will help you to more easily write test code that mocks http request/response.
# Usage
```bash
go get github.com/michimani/http-client-mock/hcmock
```## Sample
```go
import (
"bytes"
"io"
"net/http"
"testing""github.com/michimani/http-client-mock/hcmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)func Test_MyAPICall(t *testing.T) {
cases := []struct {
name string
method string
in *hcmock.MockInput
expect http.Response
}{
{
name: "200 OK",
method: http.MethodGet,
in: &hcmock.MockInput{
Status: "200 OK",
StatusCode: http.StatusOK,
Headers: []hcmock.Header{
{Key: "header-1", Value: "value-1-1"},
{Key: "header-1", Value: "value-1-2"},
{Key: "header-2", Value: "value-2"},
},
BodyBytes: []byte("ok response"),
},
expect: http.Response{
Status: "200 OK",
StatusCode: http.StatusOK,
Header: map[string][]string{
"header-1": {"value-1-1", "value-1-2"},
"header-2": {"value-2"},
},
Body: io.NopCloser(bytes.NewReader([]byte("ok response"))),
ContentLength: 11,
},
},
{
name: "404 Not Found",
method: http.MethodGet,
in: &hcmock.MockInput{
StatusCode: http.StatusNotFound,
BodyBytes: []byte("resource not found"),
},
expect: http.Response{
Status: "",
StatusCode: http.StatusNotFound,
Header: map[string][]string{},
Body: io.NopCloser(bytes.NewReader([]byte("resource not found"))),
ContentLength: 18,
},
},
{
name: "202 Accepted",
method: http.MethodPost,
in: &hcmock.MockInput{
Status: "202 Accepted",
StatusCode: http.StatusAccepted,
},
expect: http.Response{
Status: "202 Accepted",
StatusCode: http.StatusAccepted,
Header: map[string][]string{},
Body: io.NopCloser(bytes.NewReader([]byte(""))),
ContentLength: 0,
},
},
}for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
defer c.expect.Body.Close()asst := assert.New(tt)
rqir := require.New(tt)mc := hcmock.New(c.in)
req, err := http.NewRequest(c.method, "any-string-for-url", nil)
rqir.NoError(err)
rqir.NotNil(req)res, err := mc.Do(req)
rqir.NoError(err)
rqir.NotNil(res)
defer res.Body.Close()asst.Equal(c.expect.Status, res.Status)
asst.Equal(c.expect.StatusCode, res.StatusCode)
asst.Equal(c.expect.ContentLength, res.ContentLength)eb := new(bytes.Buffer)
_, err = io.Copy(eb, c.expect.Body)
rqir.NoError(err)rb := new(bytes.Buffer)
_, err = io.Copy(rb, res.Body)
rqir.NoError(err)asst.Equal(eb, rb)
})
}
}
```# License
[MIT](https://github.com/michimani/http-client-mock/blob/main/LICENSE)
# Author
[michimani210](https://twitter.com/michimani210)