-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathCosmosDbContainer.cs
61 lines (52 loc) · 2.36 KB
/
CosmosDbContainer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
namespace Testcontainers.CosmosDb;
/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class CosmosDbContainer : DockerContainer
{
private readonly CosmosDbConfiguration _configuration;
/// <summary>
/// Initializes a new instance of the <see cref="CosmosDbContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
/// <param name="logger">The logger.</param>
public CosmosDbContainer(CosmosDbConfiguration configuration, ILogger logger)
: base(configuration, logger)
{
_configuration = configuration;
}
/// <summary>
/// Gets the CosmosDb connection string.
/// </summary>
/// <returns>The CosmosDb connection string.</returns>
public string GetConnectionString()
{
var properties = new Dictionary<string, string>();
properties.Add("AccountEndpoint", new UriBuilder("https", Hostname, GetMappedPublicPort(CosmosDbBuilder.CosmosDbPort)).ToString());
properties.Add("AccountKey", CosmosDbBuilder.DefaultAccountKey);
return string.Join(";", properties.Select(property => string.Join("=", property.Key, property.Value)));
}
/// <summary>
/// Gets a configured HTTP message handler that automatically trusts the CosmosDb Emulator's certificate.
/// </summary>
public HttpMessageHandler HttpMessageHandler => new UriRewriter(Hostname, GetMappedPublicPort(CosmosDbBuilder.CosmosDbPort));
/// <summary>
/// Gets a configured HTTP client that automatically trusts the CosmosDb Emulator's certificate.
/// </summary>
public HttpClient HttpClient => new HttpClient(HttpMessageHandler);
private sealed class UriRewriter : DelegatingHandler
{
private readonly string _hostname;
private readonly ushort _port;
public UriRewriter(string hostname, ushort port)
: base(new HttpClientHandler { ServerCertificateCustomValidationCallback = (_, _, _, _) => true })
{
_hostname = hostname;
_port = port;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.RequestUri = new UriBuilder("https", _hostname, _port, request.RequestUri.PathAndQuery).Uri;
return base.SendAsync(request, cancellationToken);
}
}
}