Skip to content

Commit

Permalink
Test that userAgent is set appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjohnsonjr committed Jan 13, 2021
1 parent 0c938cd commit a28fe94
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docker/docker_client_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package docker

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"

"github.com/containers/image/v5/types"
"github.com/containers/image/v5/version"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -151,3 +156,36 @@ func assertBearerTokensEqual(t *testing.T, expected, subject *bearerToken) {
t.Fatalf("expected [%s] to equal [%s], it did not", subject.IssuedAt, expected.IssuedAt)
}
}

func TestUserAgent(t *testing.T) {
defaultUA := "containers-image/" + version.Version
const sentinelUA = "sentinel/1.0"

var expectedUA string
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got := r.Header.Get("User-Agent")
assert.Equal(t, expectedUA, got)
w.WriteHeader(http.StatusOK)
}))
defer s.Close()

for _, tc := range []struct {
sys *types.SystemContext
expected string
}{
// Can't both test nil and set DockerInsecureSkipTLSVerify :(
// {nil, defaultUA},
{&types.SystemContext{}, defaultUA},
{&types.SystemContext{DockerRegistryUserAgent: sentinelUA}, sentinelUA},
} {
// For this test against localhost, we don't care.
tc.sys.DockerInsecureSkipTLSVerify = types.OptionalBoolTrue

registry := strings.TrimPrefix(s.URL, "http://")

expectedUA = tc.expected
if err := CheckAuth(context.Background(), tc.sys, "", "", registry); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
}

0 comments on commit a28fe94

Please sign in to comment.