Skip to content

Commit

Permalink
Merge pull request #31 from k1LoW/tb
Browse files Browse the repository at this point in the history
Fix to accept *testing.B
  • Loading branch information
k1LoW committed Dec 30, 2023
2 parents 370386d + f73e901 commit 8dc757e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 7 additions & 4 deletions httpstub.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (
"github.com/minio/pkg/wildcard"
)

var _ http.Handler = (*Router)(nil)
var (
_ http.Handler = (*Router)(nil)
_ TB = (testing.TB)(nil)
)

type TB interface {
Error(args ...any)
Expand Down Expand Up @@ -98,7 +101,7 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

// NewRouter returns a new router with methods for stubbing.
func NewRouter(t *testing.T, opts ...Option) *Router {
func NewRouter(t TB, opts ...Option) *Router {
t.Helper()
c := &config{}
for _, opt := range opts {
Expand All @@ -124,7 +127,7 @@ func NewRouter(t *testing.T, opts ...Option) *Router {
}

// NewServer returns a new router including *httptest.Server.
func NewServer(t *testing.T, opts ...Option) *Router {
func NewServer(t TB, opts ...Option) *Router {
t.Helper()
rt := NewRouter(t, opts...)
s := rt.Server()
Expand All @@ -133,7 +136,7 @@ func NewServer(t *testing.T, opts ...Option) *Router {
}

// NewTLSServer returns a new router including TLS *httptest.Server.
func NewTLSServer(t *testing.T, opts ...Option) *Router {
func NewTLSServer(t TB, opts ...Option) *Router {
t.Helper()
rt := NewRouter(t, opts...)
rt.useTLS = true
Expand Down
7 changes: 7 additions & 0 deletions httpstub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,10 @@ func TestURL(t *testing.T) {
}
}
}

func BenchmarkNewServer(b *testing.B) {
for i := 0; i < b.N; i++ {
ts := NewServer(b)
ts.Close()
}
}

0 comments on commit 8dc757e

Please sign in to comment.