Commit 89ebe5bb authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http/httptest: don't panic on Close of user-constructed Server value

If the user created an httptest.Server directly without using a
constructor it won't have the new unexported 'client' field. So don't
assume it's non-nil.

Fixes #19729

Change-Id: Ie92e5da66cf4e7fb8d95f3ad0f4e3987d3ae8b77
Reviewed-on: https://go-review.googlesource.com/38710
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarKevin Burke <kev@inburke.com>
parent 4234d1de
......@@ -209,8 +209,10 @@ func (s *Server) Close() {
}
// Also close the client idle connections.
if t, ok := s.client.Transport.(closeIdleTransport); ok {
t.CloseIdleConnections()
if s.client != nil {
if t, ok := s.client.Transport.(closeIdleTransport); ok {
t.CloseIdleConnections()
}
}
s.wg.Wait()
......
......@@ -145,3 +145,20 @@ func TestTLSServerClientTransportType(t *testing.T) {
t.Errorf("got %T, want *http.Transport", client.Transport)
}
}
type onlyCloseListener struct {
net.Listener
}
func (onlyCloseListener) Close() error { return nil }
// Issue 19729: panic in Server.Close for values created directly
// without a constructor (so the unexported client field is nil).
func TestServerZeroValueClose(t *testing.T) {
ts := &Server{
Listener: onlyCloseListener{},
Config: &http.Server{},
}
ts.Close() // tests that it doesn't panic
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment