|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Net.Http.Headers; |
| 4 | +using FluentAssertions; |
| 5 | +using Qdrant.Client.Grpc; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Qdrant.Client; |
| 9 | + |
| 10 | +public class UserAgentHandlerTests |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public async Task AddsQdrantUserAgentToken() |
| 14 | + { |
| 15 | + var (handler, capture) = CreateInvoker(); |
| 16 | + |
| 17 | + using var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost"); |
| 18 | + await handler.SendAsync(request, CancellationToken.None); |
| 19 | + |
| 20 | + capture.Request.Should().NotBeNull(); |
| 21 | + capture.Request!.Headers.UserAgent |
| 22 | + .Should().ContainSingle(p => |
| 23 | + p.Product != null |
| 24 | + && p.Product.Name == UserAgentHandler.ProductName |
| 25 | + && p.Product.Version == UserAgentHandler.ProductVersion); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public async Task PreservesExistingUserAgentTokens() |
| 30 | + { |
| 31 | + var (handler, capture) = CreateInvoker(); |
| 32 | + |
| 33 | + using var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost"); |
| 34 | + // Mimic the token that Grpc.Net.Client injects. |
| 35 | + request.Headers.UserAgent.Add(new ProductInfoHeaderValue("grpc-dotnet", "2.71.0")); |
| 36 | + |
| 37 | + await handler.SendAsync(request, CancellationToken.None); |
| 38 | + |
| 39 | + var userAgents = capture.Request!.Headers.UserAgent; |
| 40 | + userAgents.Should().Contain(p => p.Product != null && p.Product.Name == "grpc-dotnet"); |
| 41 | + userAgents.Should().Contain(p => p.Product != null && p.Product.Name == UserAgentHandler.ProductName); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public async Task DoesNotDuplicateTokenOnRetry() |
| 46 | + { |
| 47 | + var (handler, capture) = CreateInvoker(); |
| 48 | + |
| 49 | + using var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost"); |
| 50 | + await handler.SendAsync(request, CancellationToken.None); |
| 51 | + // Re-send the same request instance (as can happen on a retry). |
| 52 | + await handler.SendAsync(request, CancellationToken.None); |
| 53 | + |
| 54 | + capture.Request!.Headers.UserAgent |
| 55 | + .Count(p => p.Product != null && p.Product.Name == UserAgentHandler.ProductName) |
| 56 | + .Should().Be(1); |
| 57 | + } |
| 58 | + |
| 59 | + private static (HttpMessageInvoker invoker, RequestCapturingHandler capture) CreateInvoker() |
| 60 | + { |
| 61 | + var capture = new RequestCapturingHandler(); |
| 62 | + var handler = new UserAgentHandler { InnerHandler = capture }; |
| 63 | + return (new HttpMessageInvoker(handler), capture); |
| 64 | + } |
| 65 | + |
| 66 | + private sealed class RequestCapturingHandler : HttpMessageHandler |
| 67 | + { |
| 68 | + public HttpRequestMessage? Request { get; private set; } |
| 69 | + |
| 70 | + protected override Task<HttpResponseMessage> SendAsync( |
| 71 | + HttpRequestMessage request, |
| 72 | + CancellationToken cancellationToken) |
| 73 | + { |
| 74 | + Request = request; |
| 75 | + return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments