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

[FSSDK-9538] bug: Fix last modified formatting #361

Merged
merged 4 commits into from
Jul 25, 2023
Merged
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
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
Loading