Skip to content

Commit

Permalink
Merge pull request #28 from qawatake/matcher-consuming-body
Browse files Browse the repository at this point in the history
Fix: Multiple Matchers Consuming Request Body Causing Stub Handler Failure
  • Loading branch information
k1LoW committed Dec 9, 2023
2 parents 3a8208b + dc888fe commit beac411
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
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

0 comments on commit beac411

Please sign in to comment.