Skip to content

Commit

Permalink
Merge pull request #27 from k1LoW/url
Browse files Browse the repository at this point in the history
Add Router.URL for get *httptest.Server.URL
  • Loading branch information
k1LoW committed Jun 1, 2023
2 parents e8f068d + ce124ca commit 662b9e3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 7 additions & 2 deletions httpstub.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type TB interface {
}

type Router struct {
// Set *httptest.Server.URL
URL string
matchers []*matcher
server *httptest.Server
middlewares middlewareFuncs
Expand Down Expand Up @@ -125,7 +127,8 @@ func NewRouter(t *testing.T, opts ...Option) *Router {
func NewServer(t *testing.T, opts ...Option) *Router {
t.Helper()
rt := NewRouter(t, opts...)
_ = rt.Server()
s := rt.Server()
rt.URL = s.URL
return rt
}

Expand All @@ -134,7 +137,8 @@ func NewTLSServer(t *testing.T, opts ...Option) *Router {
t.Helper()
rt := NewRouter(t, opts...)
rt.useTLS = true
_ = rt.TLSServer()
s := rt.TLSServer()
rt.URL = s.URL
return rt
}

Expand Down Expand Up @@ -221,6 +225,7 @@ func (rt *Router) Server() *httptest.Server {
client := rt.server.Client()
tp := client.Transport.(*http.Transport)
client.Transport = newTransport(rt.server.URL, tp)
rt.URL = rt.server.URL
return rt.server
}

Expand Down
20 changes: 20 additions & 0 deletions httpstub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,23 @@ func TestRouterResponseExample(t *testing.T) {
})
}
}

func TestURL(t *testing.T) {
rt := NewRouter(t)
{
got := rt.URL
if got != "" {
t.Errorf("got %v want %v", got, "")
}
}
ts := rt.Server()
t.Cleanup(func() {
ts.Close()
})
{
got := rt.URL
if got == "" {
t.Error("want url")
}
}
}

0 comments on commit 662b9e3

Please sign in to comment.