Skip to content

Commit

Permalink
Add google.WithUserAgent (#900)
Browse files Browse the repository at this point in the history
To match remote.WithUserAgent
  • Loading branch information
jonjohnsonjr committed Jan 13, 2021
1 parent b4c870f commit 0d81a61
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/v1/google/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type lister struct {
repo name.Repository
client *http.Client
ctx context.Context
userAgent string
}

func newLister(repo name.Repository, options ...ListerOption) (*lister, error) {
Expand All @@ -65,6 +66,11 @@ func newLister(repo name.Repository, options ...ListerOption) (*lister, error) {
// Wrap the transport in something that can retry network flakes.
l.transport = transport.NewRetry(l.transport)

// Wrap this last to prevent transport.New from double-wrapping.
if l.userAgent != "" {
l.transport = transport.NewUserAgent(l.transport, l.userAgent)
}

scopes := []string{repo.Scope(transport.PullScope)}
tr, err := transport.NewWithContext(l.ctx, repo.Registry, l.auth, l.transport, scopes)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion pkg/v1/google/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ package google

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -109,11 +111,16 @@ func TestList(t *testing.T) {
}}

repoName := "ubuntu"
// To test WithUserAgent
uaSentinel := "this-is-the-user-agent"

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tagsPath := fmt.Sprintf("/v2/%s/tags/list", repoName)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got, want := r.Header.Get("User-Agent"), uaSentinel; !strings.Contains(got, want) {
t.Errorf("request did not container useragent, got %q want Contains(%q)", got, want)
}
switch r.URL.Path {
case "/v2/":
w.WriteHeader(http.StatusOK)
Expand All @@ -138,7 +145,7 @@ func TestList(t *testing.T) {
t.Fatalf("name.NewRepository(%v) = %v", repoName, err)
}

tags, err := List(repo, WithAuthFromKeychain(authn.DefaultKeychain), WithTransport(http.DefaultTransport))
tags, err := List(repo, WithAuthFromKeychain(authn.DefaultKeychain), WithTransport(http.DefaultTransport), WithUserAgent(uaSentinel), WithContext(context.Background()))
if (err != nil) != tc.wantErr {
t.Errorf("List() wrong error: %v, want %v: %v\n", (err != nil), tc.wantErr, err)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/v1/google/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ func WithContext(ctx context.Context) ListerOption {
return nil
}
}

// WithUserAgent adds the given string to the User-Agent header for any HTTP
// requests. This header will also include "go-containerregistry/${version}".
//
// If you want to completely overwrite the User-Agent header, use WithTransport.
func WithUserAgent(ua string) ListerOption {
return func(l *lister) error {
l.userAgent = ua
return nil
}
}

0 comments on commit 0d81a61

Please sign in to comment.