Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Multiple Matchers Consuming Request Body Causing Stub Handler Failure #28

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions httpstub.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (rt *Router) Close() {
// Match create request matcher with matchFunc (func(r *http.Request) bool).
func (rt *Router) Match(fn func(r *http.Request) bool) *matcher {
m := &matcher{
matchFuncs: []matchFunc{fn},
matchFuncs: []matchFunc{withCloneReq(fn)},
router: rt,
}
rt.mu.Lock()
Expand All @@ -260,7 +260,7 @@ func (rt *Router) Match(fn func(r *http.Request) bool) *matcher {
func (m *matcher) Match(fn func(r *http.Request) bool) *matcher {
m.mu.Lock()
defer m.mu.Unlock()
m.matchFuncs = append(m.matchFuncs, fn)
m.matchFuncs = append(m.matchFuncs, withCloneReq(fn))
return m
}

Expand Down Expand Up @@ -650,3 +650,10 @@ func matchOne[T any](m map[string]*T, pattern string) (string, *T) {
}
return "", nil
}

func withCloneReq(fn matchFunc) matchFunc {
return func(r *http.Request) bool {
r2 := cloneReq(r)
return fn(r2)
}
}
90 changes: 90 additions & 0 deletions httpstub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"os"
"strings"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -107,6 +108,95 @@ func TestMatcherMatch(t *testing.T) {
}
}

func TestMatcherMatchConsumingBody(t *testing.T) {
t.Run("only Match", func(t *testing.T) {
rt := NewRouter(t)
rt.Match(func(r *http.Request) bool {
b, err := io.ReadAll(r.Body)
if err != nil {
return false
}
return strings.Contains(string(b), "add")
}).Response(http.StatusAccepted, nil)
rt.Match(func(r *http.Request) bool {
b, err := io.ReadAll(r.Body)
if err != nil {
return false
}
return strings.Contains(string(b), "subtract")
}).Response(http.StatusAccepted, nil)
ts := rt.Server()
t.Cleanup(func() {
ts.Close()
})
tc := ts.Client()
res, err := tc.Post("https://example.com/jrpc", "application/json", strings.NewReader(`{"jsonrpc":"2.0", "method": "subtract", "params": {"a": 10, "b": 3}, "id": 123}`))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
res.Body.Close()
})

requests := rt.Requests()
if len(requests) != 1 {
t.Errorf("got %v\nwant %v", len(requests), 1)
}
b, err := io.ReadAll(requests[0].Body)
if err != nil {
t.Fatal(err)
}
got := string(b)
want := `{"jsonrpc":"2.0", "method": "subtract", "params": {"a": 10, "b": 3}, "id": 123}`
if got != want {
t.Errorf("got %v\nwant %v", got, want)
}
})
t.Run("Path and Match", func(t *testing.T) {
rt := NewRouter(t)
rt.Path("/jrpc").Match(func(r *http.Request) bool {
b, err := io.ReadAll(r.Body)
if err != nil {
return false
}
return strings.Contains(string(b), "add")
}).Response(http.StatusAccepted, nil)
rt.Path("/jrpc").Match(func(r *http.Request) bool {
b, err := io.ReadAll(r.Body)
if err != nil {
return false
}
return strings.Contains(string(b), "subtract")
}).Response(http.StatusAccepted, nil)
ts := rt.Server()
t.Cleanup(func() {
ts.Close()
})
tc := ts.Client()
res, err := tc.Post("https://example.com/jrpc", "application/json", strings.NewReader(`{"jsonrpc":"2.0", "method": "subtract", "params": {"a": 10, "b": 3}, "id": 123}`))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
res.Body.Close()
})

requests := rt.Requests()
if len(requests) != 1 {
t.Errorf("got %v\nwant %v", len(requests), 1)
}
b, err := io.ReadAll(requests[0].Body)
if err != nil {
t.Fatal(err)
}
got := string(b)
want := `{"jsonrpc":"2.0", "method": "subtract", "params": {"a": 10, "b": 3}, "id": 123}`
if got != want {
t.Errorf("got %v\nwant %v", got, want)
}
})
}

func TestMatcherMethod(t *testing.T) {
rt := NewRouter(t)
rt.Path("/api/v1/users/1").Method(http.MethodGet).ResponseString(http.StatusAccepted, `{"message":"accepted"}`)
Expand Down