Skip to content

Commit 060ff3d

Browse files
Closes #79
1 parent de485e3 commit 060ff3d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/mock/server.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ func (efrt *EnforceHostRoundTripper) RoundTrip(r *http.Request) (*http.Response,
164164
//
165165
// c := github.NewClient(mockedHTTPClient)
166166
func NewMockedHTTPClient(options ...MockBackendOption) *http.Client {
167+
_, c := NewMockedHTTPClientAndServer(options...)
168+
169+
return c
170+
}
171+
172+
// Same as the original `NewMockedHTTPClient` but returns a pointer to the internal
173+
// `*httptest.Server`, which let's you close it, avoiding GoRoutine leaks on your tests.
174+
//
175+
// This avoids creating a breaking change, for now.
176+
//
177+
// To close the server, use: `mockServer.Close()`.
178+
func NewMockedHTTPClientAndServer(options ...MockBackendOption) (*httptest.Server, *http.Client) {
167179
router := mux.NewRouter()
168180

169181
router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -187,5 +199,5 @@ func NewMockedHTTPClient(options ...MockBackendOption) *http.Client {
187199
UpstreamRoundTripper: mockServer.Client().Transport,
188200
}
189201

190-
return c
202+
return mockServer, c
191203
}

0 commit comments

Comments
 (0)