Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated tests to reflect the use of RequestManagerRestSharp over RequestManager. #11

Open
wants to merge 3 commits into
base: Develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions SnipeSharp.Tests/RequestManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using SnipeSharp.Common;
using System.Reflection;
using System.Net.Http;
using RestSharp;
using RestSharp.Authenticators;

namespace SnipeSharp.Tests
{
Expand Down Expand Up @@ -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<Uri>(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<string>("Bearer xxxxx", value);
}
}
}
3 changes: 3 additions & 0 deletions SnipeSharp.Tests/SnipeSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="RestSharp, Version=106.1.0.0, Culture=neutral, PublicKeyToken=598062e77f915f75, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.106.1.0\lib\net452\RestSharp.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
Expand Down
2 changes: 1 addition & 1 deletion SnipeSharp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
[assembly: AssemblyFileVersion("0.1.2.0")]
[assembly: AssemblyInformationalVersion("0.1.2")]
[assembly: NeutralResourcesLanguage("en")]

[assembly: InternalsVisibleTo("SnipeSharp.Tests")]