Skip to content

Commit

Permalink
[FSSDK-9538] bug: Fix last modified formatting (#361)
Browse files Browse the repository at this point in the history
* Fix non-RFC1123 formatting

* Update tests to ensure expected format

* Add more accurate assert

* Lint fixes
  • Loading branch information
mikechu-optimizely committed Jul 25, 2023
1 parent b658323 commit 962bd68
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
28 changes: 21 additions & 7 deletions OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class HttpProjectConfigManagerTest
private Mock<TestNotificationCallbacks> NotificationCallbackMock =
new Mock<TestNotificationCallbacks>();

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;

[SetUp]
public void Setup()
{
Expand Down Expand Up @@ -100,22 +105,29 @@ public void TestSettingIfModifiedSinceInRequestHeader()
statusCode: HttpStatusCode.NotModified,
responseContentHeaders: new Dictionary<string, string>
{
{ "Last-Modified", new DateTime(2050, 10, 10).ToString("R") },
{ "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 = new DateTime(2020, 4, 4).ToString("R");
httpManager.LastModifiedSince = _pastLastModified.ToString("r");
t.Wait(3000);

HttpClientMock.Verify(_ => _.SendAsync(
It.Is<HttpRequestMessage>(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."),
_ => _.Log(LogLevel.DEBUG,
$"Set If-Modified-Since in request header: {ExpectedRfc1123DateTime}"),
Times.AtLeastOnce);

httpManager.Dispose();
Expand All @@ -125,22 +137,24 @@ public void TestSettingIfModifiedSinceInRequestHeader()
public void TestSettingLastModifiedFromResponseHeader()
{
MockSendAsync(
datafile: TestData.Datafile,
statusCode: HttpStatusCode.OK,
responseContentHeaders: new Dictionary<string, string>
{
{ "Last-Modified", new DateTime(2050, 10, 10).ToString("R") },
{ "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))
.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();
Expand Down
8 changes: 5 additions & 3 deletions OptimizelySDK/Config/HttpProjectConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Logger.Log(LogLevel.DEBUG,
$"Set If-Modified-Since in request header: {LastModifiedSince}");
}

if (!string.IsNullOrEmpty(DatafileAccessToken))
Expand Down Expand Up @@ -149,8 +150,9 @@ 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();
Expand Down

0 comments on commit 962bd68

Please sign in to comment.