dockerversion: DockerUserAgent(): allow custom versions to be passed
Allow additional metadata to be passed as part of the generated User-Agent. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
9a1f2e6d7c
commit
a6da1480b5
2 changed files with 16 additions and 2 deletions
|
@ -17,8 +17,8 @@ type UAStringKey struct{}
|
|||
// In accordance with RFC 7231 (5.5.3) is of the form:
|
||||
//
|
||||
// [docker client's UA] UpstreamClient([upstream client's UA])
|
||||
func DockerUserAgent(ctx context.Context) string {
|
||||
ua := getDaemonUserAgent()
|
||||
func DockerUserAgent(ctx context.Context, extraVersions ...useragent.VersionInfo) string {
|
||||
ua := useragent.AppendVersions(getDaemonUserAgent(), extraVersions...)
|
||||
if upstreamUA := getUpstreamUserAgent(ctx); upstreamUA != "" {
|
||||
ua += " " + upstreamUA
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/pkg/useragent"
|
||||
"gotest.tools/v3/assert"
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
)
|
||||
|
@ -15,10 +16,23 @@ func TestDockerUserAgent(t *testing.T) {
|
|||
assert.Check(t, is.Equal(ua, expected))
|
||||
})
|
||||
|
||||
t.Run("daemon user-agent custom metadata", func(t *testing.T) {
|
||||
ua := DockerUserAgent(context.TODO(), useragent.VersionInfo{Name: "hello", Version: "world"}, useragent.VersionInfo{Name: "foo", Version: "bar"})
|
||||
expected := getDaemonUserAgent() + ` hello/world foo/bar`
|
||||
assert.Check(t, is.Equal(ua, expected))
|
||||
})
|
||||
|
||||
t.Run("daemon user-agent with upstream", func(t *testing.T) {
|
||||
ctx := context.WithValue(context.TODO(), UAStringKey{}, "Magic-Client/1.2.3 (linux)")
|
||||
ua := DockerUserAgent(ctx)
|
||||
expected := getDaemonUserAgent() + ` UpstreamClient(Magic-Client/1.2.3 \(linux\))`
|
||||
assert.Check(t, is.Equal(ua, expected))
|
||||
})
|
||||
|
||||
t.Run("daemon user-agent with upstream and custom metadata", func(t *testing.T) {
|
||||
ctx := context.WithValue(context.TODO(), UAStringKey{}, "Magic-Client/1.2.3 (linux)")
|
||||
ua := DockerUserAgent(ctx, useragent.VersionInfo{Name: "hello", Version: "world"}, useragent.VersionInfo{Name: "foo", Version: "bar"})
|
||||
expected := getDaemonUserAgent() + ` hello/world foo/bar UpstreamClient(Magic-Client/1.2.3 \(linux\))`
|
||||
assert.Check(t, is.Equal(ua, expected))
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue