Skip to content

Commit

Permalink
chore: Add User-Agent HTTP header to Docker.DotNet client (#970)
Browse files Browse the repository at this point in the history
Co-authored-by: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com>
  • Loading branch information
eddumelendez and HofmeisterAn authored Aug 14, 2023
1 parent 1a98ff4 commit 49c1fa4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/Testcontainers/Clients/DefaultLabels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace DotNet.Testcontainers.Clients
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using DotNet.Testcontainers.Containers;

internal sealed class DefaultLabels : ReadOnlyDictionary<string, string>
Expand All @@ -16,7 +15,7 @@ private DefaultLabels()
{
{ TestcontainersClient.TestcontainersLabel, bool.TrueString.ToLowerInvariant() },
{ TestcontainersClient.TestcontainersLangLabel, "dotnet" },
{ TestcontainersClient.TestcontainersVersionLabel, typeof(DefaultLabels).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion },
{ TestcontainersClient.TestcontainersVersionLabel, TestcontainersClient.Version },
{ TestcontainersClient.TestcontainersSessionIdLabel, ResourceReaper.DefaultSessionId.ToString("D") },
{ ResourceReaper.ResourceReaperSessionLabel, ResourceReaper.DefaultSessionId.ToString("D") },
})
Expand Down
3 changes: 3 additions & 0 deletions src/Testcontainers/Clients/TestcontainersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace DotNet.Testcontainers.Clients
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand All @@ -28,6 +29,8 @@ internal sealed class TestcontainersClient : ITestcontainersClient

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

public static readonly string Version = typeof(TestcontainersClient).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;

private static readonly string OSRootDirectory = Path.GetPathRoot(Directory.GetCurrentDirectory());

private static readonly Regex FromLinePattern = new Regex("FROM (?<arg>--[^\\s]+\\s)*(?<image>[^\\s]+).*", RegexOptions.None, TimeSpan.FromSeconds(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace DotNet.Testcontainers.Configurations
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Docker.DotNet;
using DotNet.Testcontainers.Clients;
using JetBrains.Annotations;

/// <inheritdoc cref="IDockerEndpointAuthenticationConfiguration" />
Expand All @@ -30,7 +31,12 @@ public DockerEndpointAuthenticationConfiguration(Uri endpoint, Credentials crede
/// <inheritdoc />
public DockerClientConfiguration GetDockerClientConfiguration(Guid sessionId = default)
{
var defaultHttpRequestHeaders = new ReadOnlyDictionary<string, string>(new Dictionary<string, string> { { "x-tc-sid", sessionId.ToString("D") } });
var defaultHttpRequestHeaders = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>
{
{ "User-Agent", "tc-dotnet/" + TestcontainersClient.Version },
{ "x-tc-sid", sessionId.ToString("D") },
});

return new DockerClientConfiguration(Endpoint, Credentials, defaultHttpRequestHeaders: defaultHttpRequestHeaders);
}
}
Expand Down

0 comments on commit 49c1fa4

Please sign in to comment.