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:
|
// In accordance with RFC 7231 (5.5.3) is of the form:
|
||||||
//
|
//
|
||||||
// [docker client's UA] UpstreamClient([upstream client's UA])
|
// [docker client's UA] UpstreamClient([upstream client's UA])
|
||||||
func DockerUserAgent(ctx context.Context) string {
|
func DockerUserAgent(ctx context.Context, extraVersions ...useragent.VersionInfo) string {
|
||||||
ua := getDaemonUserAgent()
|
ua := useragent.AppendVersions(getDaemonUserAgent(), extraVersions...)
|
||||||
if upstreamUA := getUpstreamUserAgent(ctx); upstreamUA != "" {
|
if upstreamUA := getUpstreamUserAgent(ctx); upstreamUA != "" {
|
||||||
ua += " " + upstreamUA
|
ua += " " + upstreamUA
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/docker/docker/pkg/useragent"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
is "gotest.tools/v3/assert/cmp"
|
is "gotest.tools/v3/assert/cmp"
|
||||||
)
|
)
|
||||||
|
@ -15,10 +16,23 @@ func TestDockerUserAgent(t *testing.T) {
|
||||||
assert.Check(t, is.Equal(ua, expected))
|
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) {
|
t.Run("daemon user-agent with upstream", func(t *testing.T) {
|
||||||
ctx := context.WithValue(context.TODO(), UAStringKey{}, "Magic-Client/1.2.3 (linux)")
|
ctx := context.WithValue(context.TODO(), UAStringKey{}, "Magic-Client/1.2.3 (linux)")
|
||||||
ua := DockerUserAgent(ctx)
|
ua := DockerUserAgent(ctx)
|
||||||
expected := getDaemonUserAgent() + ` UpstreamClient(Magic-Client/1.2.3 \(linux\))`
|
expected := getDaemonUserAgent() + ` UpstreamClient(Magic-Client/1.2.3 \(linux\))`
|
||||||
assert.Check(t, is.Equal(ua, expected))
|
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