From 427773d56dc94f676a386f07c0e6f867afdef959 Mon Sep 17 00:00:00 2001 From: Mike Chu Date: Mon, 24 Jul 2023 16:38:18 -0400 Subject: [PATCH 1/4] Fix non-RFC1123 formatting --- OptimizelySDK/Config/HttpProjectConfigManager.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/OptimizelySDK/Config/HttpProjectConfigManager.cs b/OptimizelySDK/Config/HttpProjectConfigManager.cs index 6a73e2ec..b0faab46 100644 --- a/OptimizelySDK/Config/HttpProjectConfigManager.cs +++ b/OptimizelySDK/Config/HttpProjectConfigManager.cs @@ -119,7 +119,7 @@ private string GetRemoteDatafileResponse() if (!string.IsNullOrEmpty(LastModifiedSince)) { request.Headers.Add("If-Modified-Since", LastModifiedSince); - Logger.Log(LogLevel.DEBUG, $"Set If-Modified-Since in request header."); + Logger.Log(LogLevel.DEBUG, $"Set If-Modified-Since in request header: {LastModifiedSince}"); } if (!string.IsNullOrEmpty(DatafileAccessToken)) @@ -149,8 +149,8 @@ private string GetRemoteDatafileResponse() // Update Last-Modified header if provided. if (result.Content.Headers.LastModified.HasValue) { - LastModifiedSince = result.Content.Headers.LastModified.ToString(); - Logger.Log(LogLevel.DEBUG, $"Set LastModifiedSince from response header."); + LastModifiedSince = result.Content.Headers.LastModified?.UtcDateTime.ToString("r"); + Logger.Log(LogLevel.DEBUG, $"Set LastModifiedSince from response header: {LastModifiedSince}"); } var content = result.Content.ReadAsStringAsync(); From e9fa6298622f602a413d751e158f9b9043b41e6d Mon Sep 17 00:00:00 2001 From: Mike Chu Date: Mon, 24 Jul 2023 16:45:11 -0400 Subject: [PATCH 2/4] Update tests to ensure expected format --- .../ConfigTest/HttpProjectConfigManagerTest.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs b/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs index 4d218706..8c331758 100644 --- a/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs +++ b/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs @@ -36,6 +36,9 @@ public class HttpProjectConfigManagerTest private Mock NotificationCallbackMock = new Mock(); + + private const string ExpectedRfc1123DateTime = "Thu, 03 Nov 2022 16:00:00 GMT"; + private readonly DateTime _pastLastModified = new DateTime(2022, 11, 3, 16, 0, 0, DateTimeKind.Utc); [SetUp] public void Setup() @@ -95,12 +98,13 @@ public void TestHttpConfigManagerWithInvalidStatus() [Test] public void TestSettingIfModifiedSinceInRequestHeader() { + var formattedDateTime = new DateTimeOffset(_pastLastModified).UtcDateTime.ToString("r"); var t = MockSendAsync( datafile: string.Empty, statusCode: HttpStatusCode.NotModified, responseContentHeaders: new Dictionary { - { "Last-Modified", new DateTime(2050, 10, 10).ToString("R") }, + { "Last-Modified", formattedDateTime }, } ); @@ -111,11 +115,11 @@ public void TestSettingIfModifiedSinceInRequestHeader() .WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(2000)) .WithStartByDefault() .Build(defer: true); - httpManager.LastModifiedSince = new DateTime(2020, 4, 4).ToString("R"); + httpManager.LastModifiedSince = formattedDateTime; t.Wait(3000); LoggerMock.Verify( - _ => _.Log(LogLevel.DEBUG, "Set If-Modified-Since in request header."), + _ => _.Log(LogLevel.DEBUG, $"Set If-Modified-Since in request header: {ExpectedRfc1123DateTime}"), Times.AtLeastOnce); httpManager.Dispose(); @@ -124,11 +128,12 @@ public void TestSettingIfModifiedSinceInRequestHeader() [Test] public void TestSettingLastModifiedFromResponseHeader() { + var formattedDateTime = new DateTimeOffset(_pastLastModified).UtcDateTime.ToString("r"); MockSendAsync( statusCode: HttpStatusCode.OK, responseContentHeaders: new Dictionary { - { "Last-Modified", new DateTime(2050, 10, 10).ToString("R") }, + { "Last-Modified", formattedDateTime }, } ); var httpManager = new HttpProjectConfigManager.Builder() @@ -138,9 +143,9 @@ public void TestSettingLastModifiedFromResponseHeader() .WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(500)) .WithStartByDefault() .Build(); - + LoggerMock.Verify( - _ => _.Log(LogLevel.DEBUG, "Set LastModifiedSince from response header."), + _ => _.Log(LogLevel.DEBUG, $"Set LastModifiedSince from response header: {ExpectedRfc1123DateTime}"), Times.AtLeastOnce); httpManager.Dispose(); From 0f138e84a1fd4c0fae89f454b88aeca5c0777e26 Mon Sep 17 00:00:00 2001 From: Mike Chu Date: Mon, 24 Jul 2023 17:08:52 -0400 Subject: [PATCH 3/4] Add more accurate assert --- .../HttpProjectConfigManagerTest.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs b/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs index 8c331758..dae8c4b0 100644 --- a/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs +++ b/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs @@ -38,7 +38,7 @@ public class HttpProjectConfigManagerTest new Mock(); private const string ExpectedRfc1123DateTime = "Thu, 03 Nov 2022 16:00:00 GMT"; - private readonly DateTime _pastLastModified = new DateTime(2022, 11, 3, 16, 0, 0, DateTimeKind.Utc); + private readonly DateTime _pastLastModified = new DateTimeOffset(new DateTime(2022, 11, 3, 16, 0, 0, DateTimeKind.Utc)).UtcDateTime; [SetUp] public void Setup() @@ -98,26 +98,30 @@ public void TestHttpConfigManagerWithInvalidStatus() [Test] public void TestSettingIfModifiedSinceInRequestHeader() { - var formattedDateTime = new DateTimeOffset(_pastLastModified).UtcDateTime.ToString("r"); var t = MockSendAsync( datafile: string.Empty, statusCode: HttpStatusCode.NotModified, responseContentHeaders: new Dictionary { - { "Last-Modified", formattedDateTime }, + { "Last-Modified", _pastLastModified.ToString("r") }, } ); var httpManager = new HttpProjectConfigManager.Builder() - .WithDatafile(string.Empty) + .WithSdkKey("QBw9gFM8oTn7ogY9ANCC1z") .WithLogger(LoggerMock.Object) .WithPollingInterval(TimeSpan.FromMilliseconds(1000)) .WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(2000)) .WithStartByDefault() .Build(defer: true); - httpManager.LastModifiedSince = formattedDateTime; + httpManager.LastModifiedSince = _pastLastModified.ToString("r"); t.Wait(3000); + HttpClientMock.Verify(_ => _.SendAsync( + It.Is(requestMessage => + requestMessage.Headers.IfModifiedSince.HasValue && + requestMessage.Headers.IfModifiedSince.Value.UtcDateTime.ToString("r") == ExpectedRfc1123DateTime + )), Times.Once); LoggerMock.Verify( _ => _.Log(LogLevel.DEBUG, $"Set If-Modified-Since in request header: {ExpectedRfc1123DateTime}"), Times.AtLeastOnce); @@ -128,16 +132,16 @@ public void TestSettingIfModifiedSinceInRequestHeader() [Test] public void TestSettingLastModifiedFromResponseHeader() { - var formattedDateTime = new DateTimeOffset(_pastLastModified).UtcDateTime.ToString("r"); MockSendAsync( + datafile: TestData.Datafile, statusCode: HttpStatusCode.OK, responseContentHeaders: new Dictionary { - { "Last-Modified", formattedDateTime }, + { "Last-Modified", _pastLastModified.ToString("r") }, } ); var httpManager = new HttpProjectConfigManager.Builder() - .WithUrl("https://cdn.optimizely.com/datafiles/QBw9gFM8oTn7ogY9ANCC1z.json") + .WithSdkKey("QBw9gFM8oTn7ogY9ANCC1z") .WithLogger(LoggerMock.Object) .WithPollingInterval(TimeSpan.FromMilliseconds(1000)) .WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(500)) From 84caaa9881b77648bda7291bb84c666704b4c2c4 Mon Sep 17 00:00:00 2001 From: Mike Chu Date: Mon, 24 Jul 2023 17:15:01 -0400 Subject: [PATCH 4/4] Lint fixes --- .../ConfigTest/HttpProjectConfigManagerTest.cs | 17 +++++++++++------ .../Config/HttpProjectConfigManager.cs | 6 ++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs b/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs index dae8c4b0..884d9b92 100644 --- a/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs +++ b/OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs @@ -36,9 +36,11 @@ public class HttpProjectConfigManagerTest private Mock NotificationCallbackMock = new Mock(); - + private const string ExpectedRfc1123DateTime = "Thu, 03 Nov 2022 16:00:00 GMT"; - private readonly DateTime _pastLastModified = new DateTimeOffset(new DateTime(2022, 11, 3, 16, 0, 0, DateTimeKind.Utc)).UtcDateTime; + + private readonly DateTime _pastLastModified = + new DateTimeOffset(new DateTime(2022, 11, 3, 16, 0, 0, DateTimeKind.Utc)).UtcDateTime; [SetUp] public void Setup() @@ -120,10 +122,12 @@ public void TestSettingIfModifiedSinceInRequestHeader() HttpClientMock.Verify(_ => _.SendAsync( It.Is(requestMessage => requestMessage.Headers.IfModifiedSince.HasValue && - requestMessage.Headers.IfModifiedSince.Value.UtcDateTime.ToString("r") == ExpectedRfc1123DateTime + requestMessage.Headers.IfModifiedSince.Value.UtcDateTime.ToString("r") == + ExpectedRfc1123DateTime )), Times.Once); LoggerMock.Verify( - _ => _.Log(LogLevel.DEBUG, $"Set If-Modified-Since in request header: {ExpectedRfc1123DateTime}"), + _ => _.Log(LogLevel.DEBUG, + $"Set If-Modified-Since in request header: {ExpectedRfc1123DateTime}"), Times.AtLeastOnce); httpManager.Dispose(); @@ -147,9 +151,10 @@ public void TestSettingLastModifiedFromResponseHeader() .WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(500)) .WithStartByDefault() .Build(); - + LoggerMock.Verify( - _ => _.Log(LogLevel.DEBUG, $"Set LastModifiedSince from response header: {ExpectedRfc1123DateTime}"), + _ => _.Log(LogLevel.DEBUG, + $"Set LastModifiedSince from response header: {ExpectedRfc1123DateTime}"), Times.AtLeastOnce); httpManager.Dispose(); diff --git a/OptimizelySDK/Config/HttpProjectConfigManager.cs b/OptimizelySDK/Config/HttpProjectConfigManager.cs index b0faab46..fecd0a92 100644 --- a/OptimizelySDK/Config/HttpProjectConfigManager.cs +++ b/OptimizelySDK/Config/HttpProjectConfigManager.cs @@ -119,7 +119,8 @@ private string GetRemoteDatafileResponse() if (!string.IsNullOrEmpty(LastModifiedSince)) { request.Headers.Add("If-Modified-Since", LastModifiedSince); - Logger.Log(LogLevel.DEBUG, $"Set If-Modified-Since in request header: {LastModifiedSince}"); + Logger.Log(LogLevel.DEBUG, + $"Set If-Modified-Since in request header: {LastModifiedSince}"); } if (!string.IsNullOrEmpty(DatafileAccessToken)) @@ -150,7 +151,8 @@ private string GetRemoteDatafileResponse() if (result.Content.Headers.LastModified.HasValue) { LastModifiedSince = result.Content.Headers.LastModified?.UtcDateTime.ToString("r"); - Logger.Log(LogLevel.DEBUG, $"Set LastModifiedSince from response header: {LastModifiedSince}"); + Logger.Log(LogLevel.DEBUG, + $"Set LastModifiedSince from response header: {LastModifiedSince}"); } var content = result.Content.ReadAsStringAsync();