From 6d55cc26e54087217813f63ef516751cc7133a51 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Mon, 9 Oct 2017 08:48:39 -0400 Subject: [PATCH] Fix panic when setting a client http client with no transport Fixes #3436 --- api/client.go | 3 +++ api/client_test.go | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/api/client.go b/api/client.go index 5cd96280b90a..28bfc017fdb8 100644 --- a/api/client.go +++ b/api/client.go @@ -267,6 +267,9 @@ func NewClient(c *Config) (*Client, error) { if c.HttpClient == nil { c.HttpClient = DefaultConfig().HttpClient } + if c.HttpClient.Transport == nil { + c.HttpClient.Transport = cleanhttp.DefaultTransport() + } tp := c.HttpClient.Transport.(*http.Transport) if err := http2.ConfigureTransport(tp); err != nil { diff --git a/api/client_test.go b/api/client_test.go index 84663eea8885..2dc89a86aa06 100644 --- a/api/client_test.go +++ b/api/client_test.go @@ -37,6 +37,15 @@ func TestDefaultConfig_envvar(t *testing.T) { } } +func TestClientDefaultHttpClient(t *testing.T) { + _, err := NewClient(&Config{ + HttpClient: http.DefaultClient, + }) + if err != nil { + t.Fatal(err) + } +} + func TestClientNilConfig(t *testing.T) { client, err := NewClient(nil) if err != nil {