Skip to content

Commit

Permalink
Merge pull request #28 from Kentico/tracking-header-test
Browse files Browse the repository at this point in the history
Tracking header test
  • Loading branch information
petrsvihlik authored Oct 5, 2018
2 parents 546c062 + 2018374 commit ded1797
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

using KenticoCloud.ContentManagement.Exceptions;
using KenticoCloud.ContentManagement.Modules.HttpClient;
using KenticoCloud.ContentManagement.Modules.Extensions;

using NSubstitute;
using Xunit;
Expand All @@ -26,31 +25,33 @@ public ContentManagementHttpClientTests()
}

[Fact]
public void CorrectSdkVersionHeaderAdded()
public async Task SendAsync_AddCorrectSDKTreackingHeader()
{
// Assamble
var assembly = typeof(ContentManagementHttpClient).Assembly;
var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
var sdkVersion = fileVersionInfo.ProductVersion;
var sdkPackageId = assembly.GetName().Name;

var httpRequestMessage = new HttpRequestMessage();
IHttpClient httpClient = Substitute.For<IHttpClient>();
IDelay delay = Substitute.For<IDelay>();
HttpRequestMessage mockRequestMessage = Substitute.For<HttpRequestMessage>();
var successfulResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
httpClient.SendAsync(Arg.Is(mockRequestMessage)).Returns(successfulResponse);

// Act
httpRequestMessage.Headers.AddSdkTrackingHeader();
IEnumerable<string> headerContent = new List<string>();
httpRequestMessage.Headers.TryGetValues("X-KC-SDKID", out headerContent);
ContentManagementHttpClient client = new ContentManagementHttpClient(delay, httpClient);
var response = await client.SendAsync(mockRequestMessage);

// Assert
Assert.True(httpRequestMessage.Headers.Contains("X-KC-SDKID"));
IEnumerable<string> headerContent = new List<string>();
mockRequestMessage.Headers.TryGetValues("X-KC-SDKID", out headerContent);
Assert.True(mockRequestMessage.Headers.Contains("X-KC-SDKID"));
Assert.Contains($"nuget.org;{sdkPackageId};{sdkVersion}", headerContent);
}

[Fact]
public async Task SendAsync_SendsMessageOnSuccessReturnsResponse()
{
var successfulMessage = new HttpRequestMessage();
var successfulResponse = new HttpResponseMessage {StatusCode = System.Net.HttpStatusCode.OK};
var successfulResponse = new HttpResponseMessage { StatusCode = System.Net.HttpStatusCode.OK };
httpClient.SendAsync(Arg.Is(successfulMessage)).Returns(successfulResponse);

var response = await _client.SendAsync(successfulMessage);
Expand Down Expand Up @@ -80,9 +81,9 @@ public async Task SendAsync_SendsMessageOnTooManyRequestsWaitsRetriesThenSuccess
var retryDelta = System.TimeSpan.FromMilliseconds(retryDeltaMilliseconds);
delay.DelayByTimeSpan(Arg.Any<System.TimeSpan>()).Returns(Task.CompletedTask);
var successfulMessage = new HttpRequestMessage();
var tooManyRequestsResponse = new HttpResponseMessage { StatusCode = (HttpStatusCode) 429 };
var tooManyRequestsResponse = new HttpResponseMessage { StatusCode = (HttpStatusCode)429 };
tooManyRequestsResponse.Headers.RetryAfter = new RetryConditionHeaderValue(retryDelta);
var successResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
var successResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
httpClient.SendAsync(Arg.Is(successfulMessage)).Returns(x => tooManyRequestsResponse, x => tooManyRequestsResponse, x => successResponse);

var response = await _client.SendAsync(successfulMessage);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;

using KenticoCloud.ContentManagement.Modules.HttpClient;
using KenticoCloud.ContentManagement.Modules.Extensions;

using Xunit;

namespace KenticoCloud.ContentManagement.Tests
{
public class HttpRequestHeadersExtensionsTests
{
[Fact]
public void AddSdkTrackingHeader_CorrectSdkVersionHeaderAdded()
{
var assembly = typeof(ContentManagementHttpClient).Assembly;
var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
var sdkVersion = fileVersionInfo.ProductVersion;
var sdkPackageId = assembly.GetName().Name;
var httpRequestMessage = new HttpRequestMessage();

httpRequestMessage.Headers.AddSdkTrackingHeader();

IEnumerable<string> headerContent = new List<string>();
httpRequestMessage.Headers.TryGetValues("X-KC-SDKID", out headerContent);

Assert.True(httpRequestMessage.Headers.Contains("X-KC-SDKID"));
Assert.Contains($"nuget.org;{sdkPackageId};{sdkVersion}", headerContent);
}
}
}

0 comments on commit ded1797

Please sign in to comment.