Skip to content

Commit 49c1fa4

Browse files
chore: Add User-Agent HTTP header to Docker.DotNet client (#970)
Co-authored-by: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com>
1 parent 1a98ff4 commit 49c1fa4

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

src/Testcontainers/Clients/DefaultLabels.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace DotNet.Testcontainers.Clients
22
{
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
5-
using System.Reflection;
65
using DotNet.Testcontainers.Containers;
76

87
internal sealed class DefaultLabels : ReadOnlyDictionary<string, string>
@@ -16,7 +15,7 @@ private DefaultLabels()
1615
{
1716
{ TestcontainersClient.TestcontainersLabel, bool.TrueString.ToLowerInvariant() },
1817
{ TestcontainersClient.TestcontainersLangLabel, "dotnet" },
19-
{ TestcontainersClient.TestcontainersVersionLabel, typeof(DefaultLabels).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion },
18+
{ TestcontainersClient.TestcontainersVersionLabel, TestcontainersClient.Version },
2019
{ TestcontainersClient.TestcontainersSessionIdLabel, ResourceReaper.DefaultSessionId.ToString("D") },
2120
{ ResourceReaper.ResourceReaperSessionLabel, ResourceReaper.DefaultSessionId.ToString("D") },
2221
})

src/Testcontainers/Clients/TestcontainersClient.cs

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace DotNet.Testcontainers.Clients
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Reflection;
78
using System.Text;
89
using System.Text.RegularExpressions;
910
using System.Threading;
@@ -28,6 +29,8 @@ internal sealed class TestcontainersClient : ITestcontainersClient
2829

2930
public const string TestcontainersSessionIdLabel = TestcontainersLabel + ".session-id";
3031

32+
public static readonly string Version = typeof(TestcontainersClient).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
33+
3134
private static readonly string OSRootDirectory = Path.GetPathRoot(Directory.GetCurrentDirectory());
3235

3336
private static readonly Regex FromLinePattern = new Regex("FROM (?<arg>--[^\\s]+\\s)*(?<image>[^\\s]+).*", RegexOptions.None, TimeSpan.FromSeconds(1));

src/Testcontainers/Configurations/AuthConfigs/DockerEndpointAuthenticationConfiguration.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace DotNet.Testcontainers.Configurations
44
using System.Collections.Generic;
55
using System.Collections.ObjectModel;
66
using Docker.DotNet;
7+
using DotNet.Testcontainers.Clients;
78
using JetBrains.Annotations;
89

910
/// <inheritdoc cref="IDockerEndpointAuthenticationConfiguration" />
@@ -30,7 +31,12 @@ public DockerEndpointAuthenticationConfiguration(Uri endpoint, Credentials crede
3031
/// <inheritdoc />
3132
public DockerClientConfiguration GetDockerClientConfiguration(Guid sessionId = default)
3233
{
33-
var defaultHttpRequestHeaders = new ReadOnlyDictionary<string, string>(new Dictionary<string, string> { { "x-tc-sid", sessionId.ToString("D") } });
34+
var defaultHttpRequestHeaders = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>
35+
{
36+
{ "User-Agent", "tc-dotnet/" + TestcontainersClient.Version },
37+
{ "x-tc-sid", sessionId.ToString("D") },
38+
});
39+
3440
return new DockerClientConfiguration(Endpoint, Credentials, defaultHttpRequestHeaders: defaultHttpRequestHeaders);
3541
}
3642
}

0 commit comments

Comments
 (0)