Skip to content
Snippets Groups Projects
Commit e1f3ac79 authored by Klaus Fischer's avatar Klaus Fischer
Browse files

Added unit test to ensure default header contains User-Agent

parent 87d14487
No related branches found
No related tags found
1 merge request!29830 Version Header
using System.Net.Http;
using FitConnect.Services;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
namespace BasicUnitTest;
internal class DummyRestService : RestCallService {
public DummyRestService(string baseUrl) : base(baseUrl, null) {
public DummyRestService() : base("https://www.google.com", null) {
}
public HttpClient GetClient() => CreateClient();
}
public class RestCallServiceTest {
[Test]
public void RestCallShouldContainVersionHeader() {
var restCallService = new DummyRestService("");
var restCallService = new DummyRestService();
var client = restCallService.GetClient();
client.DefaultRequestHeaders.Contains("User-Agent").Should().BeTrue();
client.DefaultRequestHeaders.GetValues("User-Agent").Should()
.ContainMatch("FITConnectDotNetSDK*");
}
}
......@@ -31,6 +31,7 @@ internal class RestCallService : IRestCallService {
};
var client = new HttpClient(clientHandler);
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("User-Agent", GetVersionHeader());
if (AccessToken != null)
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {AccessToken}");
return client;
......@@ -54,11 +55,15 @@ internal class RestCallService : IRestCallService {
accept);
}
private static string? _versionHeader;
internal static string GetVersionHeader() {
if (_versionHeader != null) return _versionHeader;
var version = ProjectSpecification.PackageVersion;
var commit = ProjectSpecification.CommitId;
var osVersion = Environment.OSVersion.Platform.ToString() + Environment.OSVersion.Version;
return $"FITConnectDotNetSDK/{version} (commit:{commit}; os:{osVersion})";
_versionHeader = $"FITConnectDotNetSDK/{version} (commit:{commit}; os:{osVersion})";
return _versionHeader;
}
protected async Task<string> RestCallForString(Uri requestUri, HttpMethod method,
......@@ -83,7 +88,8 @@ internal class RestCallService : IRestCallService {
_logger?.LogTrace("Body: {Body}", body);
}
request.Headers.Add("User-Agent", GetVersionHeader());
#warning moved to CreateClient
// request.Headers.Add("User-Agent", GetVersionHeader());
var response = await client.SendAsync(request);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment