-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathCosmosDbTestcontainer.cs
63 lines (53 loc) · 2.03 KB
/
CosmosDbTestcontainer.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
62
63
namespace DotNet.Testcontainers.Containers
{
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using DotNet.Testcontainers.Configurations;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
/// <inheritdoc cref="TestcontainerDatabase" />
[PublicAPI]
public sealed class CosmosDbTestcontainer : TestcontainerDatabase
{
/// <summary>
/// Initializes a new instance of the <see cref="CosmosDbTestcontainer" /> class.
/// </summary>
/// <param name="configuration">The Testcontainers configuration.</param>
/// <param name="logger">The logger.</param>
internal CosmosDbTestcontainer(ITestcontainersConfiguration configuration, ILogger logger)
: base(configuration, logger)
{
}
/// <inheritdoc />
public override string ConnectionString =>
$"AccountEndpoint=https://{this.Hostname}:{this.Port};AccountKey={this.Password}";
/// <summary>
/// Gets a configured HTTP message handler.
/// </summary>
public HttpMessageHandler HttpMessageHandler => new UriRewriter(this.Hostname, this.Port);
/// <summary>
/// Gets a configured HTTP client.
/// </summary>
public HttpClient HttpClient => new HttpClient(this.HttpMessageHandler);
private sealed class UriRewriter : DelegatingHandler
{
private readonly string hostname;
private readonly int port;
#pragma warning disable S4830
public UriRewriter(string hostname, int port)
: base(new HttpClientHandler { ServerCertificateCustomValidationCallback = (sender, certificate, chain, errors) => true })
{
this.hostname = hostname;
this.port = port;
}
#pragma warning restore S4830
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.RequestUri = new UriBuilder("https", this.hostname, this.port, request.RequestUri.PathAndQuery).Uri;
return base.SendAsync(request, cancellationToken);
}
}
}
}