Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CSGI-2365] Add client User Agent overwrite capability #503

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ type Client struct {
// PagerDuty API. You can use either *http.Client here, or your own
// implementation.
HTTPClient HTTPClient

userAgent string
}

// NewClient creates an API client using an account/user API token
Expand Down Expand Up @@ -322,6 +324,14 @@ func WithAPIEndpoint(endpoint string) ClientOptions {
}
}

// WithTerraformProvider configures the client to be used as the PagerDuty
// Terraform provider
func WithTerraformProvider(version string) ClientOptions {
return func(c *Client) {
c.userAgent = fmt.Sprintf("(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, version)
}
}

// WithV2EventsAPIEndpoint allows for a custom V2 Events API endpoint to be passed into the client
func WithV2EventsAPIEndpoint(endpoint string) ClientOptions {
return func(c *Client) {
Expand Down Expand Up @@ -498,7 +508,13 @@ func (c *Client) prepRequest(req *http.Request, authRequired bool, headers map[s
}
}

req.Header.Set("User-Agent", userAgentHeader)
var userAgent string
if c.userAgent != "" {
userAgent = c.userAgent
} else {
userAgent = userAgentHeader
}
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Content-Type", contentTypeHeader)
}

Expand Down
49 changes: 49 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"runtime"
"strings"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -687,6 +688,54 @@ func TestClient_Do(t *testing.T) {
}
}

func TestClient_UserAgentDefault(t *testing.T) {
setup()
defer teardown()

defaultUserAgent := userAgentHeader
mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
userAgentHeader := r.Header.Get("User-Agent")
if userAgentHeader != defaultUserAgent {
t.Fatalf("want %q, but got %q", defaultUserAgent, userAgentHeader)
}
w.WriteHeader(http.StatusOK)
})

client := defaultTestClient(server.URL, "foo")

_, err := client.do(context.Background(), "GET", "/foo", nil, nil)
if err != nil {
t.Fatal(err)
}
}

func TestClient_UserAgentOverwrite(t *testing.T) {
setup()
defer teardown()

terraformVersion := "terraform-version-for-testing"
newUserAgent := fmt.Sprintf("(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, terraformVersion)
mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
userAgentHeader := r.Header.Get("User-Agent")
if userAgentHeader != newUserAgent {
t.Fatalf("want %q, but got %q", newUserAgent, userAgentHeader)
}
w.WriteHeader(http.StatusOK)
})

client := NewClient("foo",
WithAPIEndpoint(server.URL),
WithTerraformProvider(terraformVersion),
)

_, err := client.do(context.Background(), "GET", "/foo", nil, nil)
if err != nil {
t.Fatal(err)
}
}

func TestNullAPIErrorObject_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
Expand Down