Skip to content

Commit

Permalink
chore: add a test for content-length header
Browse files Browse the repository at this point in the history
  • Loading branch information
daveleek committed Dec 7, 2023
1 parent 8121ba2 commit 8f319a3
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using Unleash.Communication;
using Unleash.Internal;
using Unleash.Metrics;
using Unleash.Serialization;

namespace Unleash.Tests.Communication
{
Expand All @@ -30,5 +34,50 @@ public async Task RegisterClient_Success()
var result = await api.RegisterClient(clientRegistration, CancellationToken.None);
result.Should().Be(true);
}

[Test]
public async Task Headers()
{
var jsonSerializer = new DynamicNewtonsoftJsonSerializer();
jsonSerializer.TryLoad();
var messageHandler = new InvokingMessageHandler((message) => {
Assert.AreEqual(true, message.Content.Headers.Contains("Content-Length"), "Content-Length header is not set");
Assert.AreEqual("183", message.Content.Headers.GetValues("Content-Length").First(), "Content-Length header value mismatch");
});
var client = new HttpClient(messageHandler);
client.BaseAddress = new Uri("http://localhost:8080/api/");
var apiClient = new UnleashApiClient(client, jsonSerializer, new UnleashApiClientRequestHeaders(), new EventCallbackConfig());
var clientRegistration = new ClientRegistration()
{
AppName = GetType().Name,
InstanceId = "instance1",
Interval = 1000,
SdkVersion = "sdk101",
Started = DateTimeOffset.UtcNow,
Strategies = new List<string>
{
"abc"
}
};

var result = await apiClient.RegisterClient(clientRegistration, CancellationToken.None);
result.Should().Be(true);
}
}

internal class InvokingMessageHandler : HttpMessageHandler
{
private readonly Action<HttpRequestMessage> action;

internal InvokingMessageHandler(Action<HttpRequestMessage> action)
{
this.action = action;
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
action(request);
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK));
}
}
}

0 comments on commit 8f319a3

Please sign in to comment.