From 3d24821f3247bc0d3f0b3c4ff6f6fc3a9877668a Mon Sep 17 00:00:00 2001 From: Christian LaCourt Date: Mon, 23 Jul 2018 11:18:56 -0400 Subject: [PATCH] Updated tests to reflect the use of RequestManagerRestSharp over RequestManager. --- SnipeSharp.Tests/RequestManagerTests.cs | 29 ++++++++++++------------ SnipeSharp.Tests/SnipeSharp.Tests.csproj | 3 +++ SnipeSharp/Properties/AssemblyInfo.cs | 2 +- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/SnipeSharp.Tests/RequestManagerTests.cs b/SnipeSharp.Tests/RequestManagerTests.cs index ff76eff..1b61887 100644 --- a/SnipeSharp.Tests/RequestManagerTests.cs +++ b/SnipeSharp.Tests/RequestManagerTests.cs @@ -4,6 +4,8 @@ using SnipeSharp.Common; using System.Reflection; using System.Net.Http; +using RestSharp; +using RestSharp.Authenticators; namespace SnipeSharp.Tests { @@ -31,37 +33,36 @@ public void CheckApiTokenAndUrl_NoUrlInApiSettings_ThrowException() [TestMethod] public void CheckApiTokenAndUrl_SetHttpClientBaseAddress_SetCorrectly() { - SnipeItApi snipe = new SnipeItApi(); - Uri url = new Uri("http://google.com"); + var snipe = new SnipeItApi(); + var url = new Uri("http://google.com"); snipe.ApiSettings.ApiToken = "xxxxx"; snipe.ApiSettings.BaseUrl = url; snipe.ReqManager.CheckApiTokenAndUrl(); // Get the Static property value - Type type = typeof(RequestManager); - FieldInfo prop = type.GetField("Client", BindingFlags.NonPublic | BindingFlags.Static); - HttpClient value = prop.GetValue(snipe.ReqManager) as HttpClient; + var prop = typeof(RequestManagerRestSharp).GetField("Client", BindingFlags.NonPublic | BindingFlags.Static); + var client = prop.GetValue(snipe.ReqManager) as RestClient; - Assert.AreEqual(url, value.BaseAddress); + Assert.AreEqual(url, client.BaseUrl); } [TestMethod] public void CheckApiTokenAndUrl_SetAuthorizationHeader_SetCorrectly() { - SnipeItApi snipe = new SnipeItApi(); - Uri url = new Uri("http://google.com"); + var snipe = new SnipeItApi(); + var url = new Uri("http://google.com"); snipe.ApiSettings.ApiToken = "xxxxx"; snipe.ApiSettings.BaseUrl = url; snipe.ReqManager.CheckApiTokenAndUrl(); // Get the Static property value - Type type = typeof(RequestManager); - FieldInfo prop = type.GetField("Client", BindingFlags.NonPublic | BindingFlags.Static); - HttpClient value = prop.GetValue(snipe.ReqManager) as HttpClient; - - Assert.IsTrue(value.DefaultRequestHeaders.Authorization.Scheme == "Bearer" && - value.DefaultRequestHeaders.Authorization.Parameter == "xxxxx"); + var prop = typeof(RequestManagerRestSharp).GetField("Client", BindingFlags.NonPublic | BindingFlags.Static); + var client = prop.GetValue(snipe.ReqManager) as RestClient; + // NOTE: This test depends on the internal implementation of RestSharp not changing. Check there if you update that dependency! + var value = new PrivateObject(client.Authenticator).GetField("authorizationValue") as string; + + Assert.AreEqual("Bearer xxxxx", value); } } } diff --git a/SnipeSharp.Tests/SnipeSharp.Tests.csproj b/SnipeSharp.Tests/SnipeSharp.Tests.csproj index 4f5b8a1..0cf583d 100644 --- a/SnipeSharp.Tests/SnipeSharp.Tests.csproj +++ b/SnipeSharp.Tests/SnipeSharp.Tests.csproj @@ -47,6 +47,9 @@ ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + + ..\packages\RestSharp.106.1.0\lib\net452\RestSharp.dll + diff --git a/SnipeSharp/Properties/AssemblyInfo.cs b/SnipeSharp/Properties/AssemblyInfo.cs index 4844889..b2ba2dd 100644 --- a/SnipeSharp/Properties/AssemblyInfo.cs +++ b/SnipeSharp/Properties/AssemblyInfo.cs @@ -37,4 +37,4 @@ [assembly: AssemblyFileVersion("0.1.2.0")] [assembly: AssemblyInformationalVersion("0.1.2")] [assembly: NeutralResourcesLanguage("en")] - +[assembly: InternalsVisibleTo("SnipeSharp.Tests")]