From 9a47e9c58c68042e70c62025ac75ec551bba9034 Mon Sep 17 00:00:00 2001 From: Barry Postma Date: Mon, 19 Jun 2023 23:25:31 +0100 Subject: [PATCH] Add library and version as HTTP User-Agent header --- .../PostcodeAPI.Net.Tests.csproj | 3 ++- PostcodeAPI.Net.Tests/PostcodeInfoTests.cs | 16 +++++++++++++++- PostcodeAPI.Net/Constants.cs | 7 ++++++- PostcodeAPI.Net/PostcodeApiClientBase.cs | 1 + 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/PostcodeAPI.Net.Tests/PostcodeAPI.Net.Tests.csproj b/PostcodeAPI.Net.Tests/PostcodeAPI.Net.Tests.csproj index 4966a44..3cfa2d6 100644 --- a/PostcodeAPI.Net.Tests/PostcodeAPI.Net.Tests.csproj +++ b/PostcodeAPI.Net.Tests/PostcodeAPI.Net.Tests.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 @@ -22,6 +22,7 @@ + diff --git a/PostcodeAPI.Net.Tests/PostcodeInfoTests.cs b/PostcodeAPI.Net.Tests/PostcodeInfoTests.cs index f9fc414..4f4502a 100644 --- a/PostcodeAPI.Net.Tests/PostcodeInfoTests.cs +++ b/PostcodeAPI.Net.Tests/PostcodeInfoTests.cs @@ -1,13 +1,15 @@ using System.Configuration; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; using PostcodeAPI; using PostcodeAPI.Model; using PostcodeAPI.Wrappers; +using RestSharp; namespace PostcodeAPI.Tests { [TestClass] - public class PostcodeInfoTests : TestBase + public class PostcodeApiClientTests : TestBase { [TestMethod] public void GetLargePostcodeSet() @@ -33,5 +35,17 @@ public void GetSinglePostcodeInformation() Assert.AreEqual("Pierre Lallementstraat", result.Streets[0]); Assert.AreEqual("Amsterdam", result.City.Label); } + + [TestMethod] + public void UserAgentIsSetToLibraryNameAndVersion() + { + var mock = new Mock(); + mock.Setup(c => c.Execute(It.IsAny())); + + PostcodeApiClient client = new PostcodeApiClient(ApiKey, mock.Object); + ApiHalResultWrapper result = client.GetPostcodes("1097"); + + mock.VerifySet(e => e.UserAgent = string.Format("", "")); + } } } diff --git a/PostcodeAPI.Net/Constants.cs b/PostcodeAPI.Net/Constants.cs index 47bfa16..42c34c2 100644 --- a/PostcodeAPI.Net/Constants.cs +++ b/PostcodeAPI.Net/Constants.cs @@ -1,7 +1,11 @@ -namespace PostcodeAPI +using System.Reflection; + +namespace PostcodeAPI { public static class Constants { + public static string AssemblyVersion => Assembly.GetAssembly(typeof(Constants)).GetName().Version.ToString(2); + public static class APIBaseUrls { public const string V1 = "http://api.postcodeapi.nu/"; @@ -12,6 +16,7 @@ public static class APIHeaderKeys { public const string AuthKeyV1 = "Api-Key"; public const string AuthKeyV2 = "X-Api-Key"; + public const string UserAgentFormatString = "PostcodeAPI.Net v{0}"; } public static class PostcodeFormatTypes diff --git a/PostcodeAPI.Net/PostcodeApiClientBase.cs b/PostcodeAPI.Net/PostcodeApiClientBase.cs index 59bb3d0..f0af6d2 100644 --- a/PostcodeAPI.Net/PostcodeApiClientBase.cs +++ b/PostcodeAPI.Net/PostcodeApiClientBase.cs @@ -23,6 +23,7 @@ protected void InitialiseClient(IRestClient client) { Client = client; Client.BaseUrl = new Uri(EndpointUrl); + client.UserAgent = string.Format(Constants.APIHeaderKeys.UserAgentFormatString, Constants.AssemblyVersion); Client.AddDefaultHeader(HeaderKey, APIKey); }