Skip to content

Commit

Permalink
Merge pull request #5 from garycarr/add_new_handler
Browse files Browse the repository at this point in the history
Add new handler constructor
  • Loading branch information
dankinder authored Oct 27, 2023
2 parents 9bc620e + 305dfef commit a21e0d3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ This example uses MockHandler, a Handler that is a [testify/mock](https://godoc.
object.

```go
downstream := &httpmock.MockHandler{}
downstream := httpmock.NewMockHandler(t)

// A simple GET that returns some pre-canned content
downstream.On("Handle", "GET", "/object/12345", mock.Anything).Return(httpmock.Response{
Expand All @@ -57,7 +57,7 @@ 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):

```go
downstream := &httpmock.MockHandlerWithHeaders{}
downstream := &httpmock.NewMockHandlerWithHeaders(t)

// A simple GET that returns some pre-canned content and expects a specific header.
// Use MultiHeaderMatcher for multiple headers.
Expand Down
13 changes: 11 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
module github.com/dankinder/httpmock

go 1.12
go 1.19

require (
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.8.4
gotest.tools/v3 v3.0.3
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.4.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 16 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -19,5 +26,10 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0=
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
35 changes: 24 additions & 11 deletions httpmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Examples
This example uses MockHandler, a Handler that is a github.com/stretchr/testify/mock object.
downstream := &httpmock.MockHandler{}
downstream := httpmock.NewMockHandler(t)
// A simple GET that returns some pre-canned content
downstream.On("Handle", "GET", "/object/12345", mock.Anything).Return(httpmock.Response{
Expand All @@ -33,15 +33,14 @@ This example uses MockHandler, a Handler that is a github.com/stretchr/testify/m
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{}
downstream := httpmock.NewMockHandlerWithHeaders(t)
// A simple GET that returns some pre-canned content
downstream.On("HandleWithHeaders", "GET", "/object/12345", MatchHeader("MOCK", "this"), mock.Anything).Return(httpmock.Response{
Body: []byte(`{"status": "ok"}`),
})
// ... same as above
// A simple GET that returns some pre-canned content
downstream.On("HandleWithHeaders", "GET", "/object/12345", MatchHeader("MOCK", "this"), mock.Anything).Return(httpmock.Response{
Body: []byte(`{"status": "ok"}`),
})
// ... same as above
Httpmock also provides helpers for checking calls using json objects, like so:
Expand All @@ -58,15 +57,15 @@ Httpmock also provides helpers for checking calls using json objects, like so:
downstream.On("Handle", "POST", "/echo", httpmock.JSONMatcher(o)).Return(httpmock.Response{
Body: httpmock.ToJSON(o),
})
*/
package httpmock

import (
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
"testing"
)

// Handler is the interface used by httpmock instead of http.Handler so that it can be mocked very easily.
Expand All @@ -81,6 +80,20 @@ type HandlerWithHeaders interface {
HandleWithHeaders(method, path string, headers http.Header, body []byte) Response
}

// NewMockHandler returns a pointer to a new mock handler with the test struct set
func NewMockHandler(t *testing.T) *MockHandler {
handler := &MockHandler{}
handler.Test(t)
return handler
}

// NewMockHandlerWithHeaders returns a pointer to a new mock handler with headers with the test struct set
func NewMockHandlerWithHeaders(t *testing.T) *MockHandlerWithHeaders {
handler := &MockHandlerWithHeaders{}
handler.Test(t)
return handler
}

// Response holds the response a handler wants to return to the client.
type Response struct {
// The HTTP status code to write (default: 200)
Expand Down Expand Up @@ -146,7 +159,7 @@ type httpToHTTPMockHandler struct {

// ServeHTTP makes this implement http.Handler
func (h *httpToHTTPMockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Failed to read HTTP body in httpmock: %v", err)
}
Expand Down
30 changes: 19 additions & 11 deletions httpmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package httpmock

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

Expand All @@ -13,7 +13,7 @@ import (

func TestBasicRequestResponse(t *testing.T) {

downstream := &MockHandler{}
downstream := NewMockHandler(t)

downstream.On("Handle", "GET", "/object/12345", mock.Anything).Return(Response{
Body: []byte(`{"status": "ok"}`),
Expand All @@ -22,11 +22,14 @@ func TestBasicRequestResponse(t *testing.T) {
s := NewServer(downstream)
defer s.Close()

req, _ := http.NewRequest("GET", fmt.Sprintf("%s/object/12345", s.URL()), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("%s/object/12345", s.URL()), nil)
require.NoError(t, err)

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)

body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
body, _ := ioutil.ReadAll(resp.Body)
assert.DeepEqual(t, []byte(`{"status": "ok"}`), body)

downstream.AssertExpectations(t)
Expand All @@ -35,7 +38,7 @@ func TestBasicRequestResponse(t *testing.T) {
func TestBasicRequestResponseWithHeaders(t *testing.T) {
headerKey := "HTTPMOCK-TEST"
headerVal := "its here"
downstream := &MockHandlerWithHeaders{}
downstream := NewMockHandlerWithHeaders(t)

downstream.On(
"HandleWithHeaders",
Expand All @@ -51,12 +54,16 @@ func TestBasicRequestResponseWithHeaders(t *testing.T) {
s := NewServer(downstream)
defer s.Close()

req, _ := http.NewRequest("GET", fmt.Sprintf("%s/object/12345", s.URL()), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("%s/object/12345", s.URL()), nil)
require.NoError(t, err)

req.Header.Set(headerKey, headerVal)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)

body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
body, _ := ioutil.ReadAll(resp.Body)

assert.DeepEqual(t, []byte(`{"status": "ok"}`), body)

downstream.AssertExpectations(t)
Expand All @@ -67,7 +74,7 @@ func TestMultiHeaderMatcher(t *testing.T) {
headerVal := "its here"
headerKey2 := "HTTPMOCK-TEST-2"
headerVal2 := "its here too!"
downstream := &MockHandlerWithHeaders{}
downstream := NewMockHandlerWithHeaders(t)

downstream.On(
"HandleWithHeaders",
Expand All @@ -86,11 +93,12 @@ func TestMultiHeaderMatcher(t *testing.T) {
s := NewServer(downstream)
defer s.Close()

req, _ := http.NewRequest("GET", fmt.Sprintf("%s/object/12345", s.URL()), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("%s/object/12345", s.URL()), nil)
require.NoError(t, err)

req.Header.Set(headerKey, headerVal)
req.Header.Set(headerKey2, headerVal2)
_, err := http.DefaultClient.Do(req)

_, err = http.DefaultClient.Do(req)
require.NoError(t, err)

downstream.AssertExpectations(t)
Expand Down

0 comments on commit a21e0d3

Please sign in to comment.