forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standardize http.Client creation with User-Agent
- Loading branch information
Showing
9 changed files
with
146 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package httpclient | ||
|
||
import ( | ||
"net/http" | ||
|
||
cleanhttp "github.com/hashicorp/go-cleanhttp" | ||
) | ||
|
||
// New returns the DefaultPooledClient from the cleanhttp | ||
// package that will also send a Terraform User-Agent string. | ||
func New() *http.Client { | ||
cli := cleanhttp.DefaultPooledClient() | ||
cli.Transport = &userAgentRoundTripper{ | ||
userAgent: UserAgentString(), | ||
inner: cli.Transport, | ||
} | ||
return cli | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package httpclient | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/version" | ||
) | ||
|
||
func TestUserAgent(t *testing.T) { | ||
var actualUserAgent string | ||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
actualUserAgent = req.UserAgent() | ||
})) | ||
defer ts.Close() | ||
|
||
tsURL, err := url.Parse(ts.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
for i, c := range []struct { | ||
expected string | ||
request func(c *http.Client) error | ||
}{ | ||
{ | ||
fmt.Sprintf("Terraform/%s", version.Version), | ||
func(c *http.Client) error { | ||
_, err := c.Get(ts.URL) | ||
return err | ||
}, | ||
}, | ||
{ | ||
"foo/1", | ||
func(c *http.Client) error { | ||
req := &http.Request{ | ||
Method: "GET", | ||
URL: tsURL, | ||
Header: http.Header{ | ||
"User-Agent": []string{"foo/1"}, | ||
}, | ||
} | ||
_, err := c.Do(req) | ||
return err | ||
}, | ||
}, | ||
{ | ||
"", | ||
func(c *http.Client) error { | ||
req := &http.Request{ | ||
Method: "GET", | ||
URL: tsURL, | ||
Header: http.Header{ | ||
"User-Agent": []string{""}, | ||
}, | ||
} | ||
_, err := c.Do(req) | ||
return err | ||
}, | ||
}, | ||
} { | ||
t.Run(fmt.Sprintf("%d %s", i, c.expected), func(t *testing.T) { | ||
actualUserAgent = "" | ||
cli := New() | ||
err := c.request(cli) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if actualUserAgent != c.expected { | ||
t.Fatalf("actual User-Agent '%s' is not '%s'", actualUserAgent, c.expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package httpclient | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hashicorp/terraform/version" | ||
) | ||
|
||
const userAgentFormat = "Terraform/%s" | ||
|
||
func UserAgentString() string { | ||
return fmt.Sprintf(userAgentFormat, version.Version) | ||
} | ||
|
||
type userAgentRoundTripper struct { | ||
inner http.RoundTripper | ||
userAgent string | ||
} | ||
|
||
func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
if _, ok := req.Header["User-Agent"]; !ok { | ||
req.Header.Set("User-Agent", rt.userAgent) | ||
} | ||
return rt.inner.RoundTrip(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
package terraform | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/hashicorp/terraform/version" | ||
"github.com/hashicorp/terraform/httpclient" | ||
) | ||
|
||
// The standard Terraform User-Agent format | ||
const UserAgent = "Terraform %s (%s)" | ||
|
||
// Generate a UserAgent string | ||
// | ||
// Deprecated: Use httpclient.UserAgentString if you are setting your | ||
// own User-Agent header. | ||
func UserAgentString() string { | ||
return fmt.Sprintf(UserAgent, version.String(), runtime.Version()) | ||
return httpclient.UserAgentString() | ||
} |