10000 [FSSDK-9472] fix: Last-Modified header not respected by mikechu-optimizely · Pull Request #355 · optimizely/csharp-sdk · GitHub
[go: up one dir, main page]

Skip to content

[FSSDK-9472] fix: Last-Modified header not respected #355

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

Merged
merged 8 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test coverage
WIP: new tests succeed in isolation (time-based/brittle)
  • Loading branch information
mikechu-optimizely committed Jun 23, 2023
commit a470f90dcefed94694830e689bdd6ebe5a3ffde9
44 changes: 38 additions & 6 deletions OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
Expand Down Expand Up @@ -92,10 +93,40 @@ public void TestHttpConfigManagerWithInvalidStatus()
}

[Test]
public void TestSettingLastModifiedHeader()
public void TestSettingIfModifiedSinceInRequestHeader()
{
// TODO: Start here
MockSendAsync(statusCode: HttpStatusCode.NotModified);
var t = MockSendAsync(
datafile: string.Empty,
statusCode: HttpStatusCode.NotModified
);

var httpManager = new HttpProjectConfigManager.Builder()
.WithDatafile(string.Empty)
.WithLogger(LoggerMock.Object)
.WithPollingInterval(TimeSpan.FromMilliseconds(1000))
.WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(4000))
.WithStartByDefault()
.Build();
httpManager.LastModifiedSince = new DateTime(2020, 4, 4).ToString("R");
t.Wait(5000);

LoggerMock.Verify(
_ => _.Log(LogLevel.DEBUG, "Set If-Modified-Since in request header."),
Times.AtLeastOnce);

httpManager.Dispose();
}

[Test]
public void TestSettingLastModifiedFromResponseHeader()
{
MockSendAsync(
statusCode: HttpStatusCode.OK,
responseContentHeaders: new Dictionary<string, string>
{
{ "Last-Modified", new DateTime(2050, 10, 10).ToString("R") },
}
);

var httpManager = new HttpProjectConfigManager.Builder()
.WithUrl("https://cdn.optimizely.com/datafiles/QBw9gFM8oTn7ogY9ANCC1z.json")
Expand Down Expand Up @@ -561,12 +592,13 @@ public void TestFormatUrlHigherPriorityThanDefaultUrl()
httpManager.Dispose();
}

public Task MockSendAsync(string datafile = null, TimeSpan? delay = null,
HttpStatusCode statusCode = HttpStatusCode.OK
private Task MockSendAsync(string datafile = null, TimeSpan? delay = null,
HttpStatusCode statusCode = HttpStatusCode.OK,
Dictionary<string, string> responseContentHeaders = null
)
{
return TestHttpProjectConfigManagerUtil.MockSendAsync(HttpClientMock, datafile, delay,
statusCode);
statusCode, responseContentHeaders);
}

#endregion
Expand Down
31 changes: 23 additions & 8 deletions OptimizelySDK.Tests/Utils/TestHttpProjectConfigManagerUtil.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020, Optimizely
* Copyright 2019-2020, 2023 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*/

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -28,27 +29,38 @@ namespace OptimizelySDK.Tests.Utils
/// </summary>
public static class TestHttpProjectConfigManagerUtil
{
public static Task MockSendAsync(Mock<HttpProjectConfigManager.HttpClient> HttpClientMock,
public static Task MockSendAsync(Mock<HttpProjectConfigManager.HttpClient> httpClientMock,
string datafile = null, TimeSpan? delay = null,
HttpStatusCode statusCode = HttpStatusCode.OK
HttpStatusCode statusCode = HttpStatusCode.OK,
Dictionary<string, string> responseContentHeaders = null
)
{
var t = new TaskCompletionSource<bool>();

HttpClientMock.Setup(_ => _.SendAsync(It.IsAny<HttpRequestMessage>())).
httpClientMock.Setup(_ => _.SendAsync(It.IsAny<HttpRequestMessage>())).
Returns(() =>
{
if (delay != null)
{
// This delay mocks the networking delay. And help to see the behavior when get a datafile with some delay.
Task.Delay(delay.Value).Wait();
}

return Task.FromResult<HttpResponseMessage>(new HttpResponseMessage
var responseMessage = new HttpResponseMessage
{
StatusCode = statusCode,
Content = new StringContent(datafile ?? string.Empty),
});
};

if (responseContentHeaders != null)
{
foreach (var header in responseContentHeaders)
{
responseMessage.Content.Headers.Add(header.Key, header.Value);
}
}

return Task.FromResult(responseMessage);
}).
Callback(()
=>
Expand All @@ -69,7 +81,10 @@ public static void SetClientFieldValue(object value)
var field = type.GetField("Client",
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.NonPublic);
field.SetValue(new object(), value);
if (field != null)
{
field.SetValue(new object(), value);
}
}
}
}
15 changes: 9 additions & 6 deletions OptimizelySDK/Config/HttpProjectConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#endif

using System;
using System.Net;
using System.Threading.Tasks;
using OptimizelySDK.ErrorHandler;
using OptimizelySDK.Logger;
Expand All @@ -29,7 +30,7 @@ namespace OptimizelySDK.Config
public class HttpProjectConfigManager : PollingProjectConfigManager
{
private string Url;
private string LastModifiedSince = string.Empty;
internal string LastModifiedSince = string.Empty;
privat AE3F e string DatafileAccessToken = string.Empty;

private HttpProjectConfigManager(TimeSpan period, string url, TimeSpan blockingTimeout,
Expand Down Expand Up @@ -118,6 +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.");
}

if (!string.IsNullOrEmpty(DatafileAccessToken))
Expand All @@ -132,6 +134,12 @@ private string GetRemoteDatafileResponse()

// Return from here if datafile is not modified.
var result = httpResponse.Result;

if (result.StatusCode == HttpStatusCode.NotModified)
{
return null;
}

if (!result.IsSuccessStatusCode)
{
Logger.Log(LogLevel.ERROR, $"Error fetching datafile \"{result.StatusCode}\"");
Expand All @@ -145,11 +153,6 @@ private string GetRemoteDatafileResponse()
Logger.Log(LogLevel.DEBUG, $"Set LastModifiedSince from response header.");
}

if (result.StatusCode == System.Net.HttpStatusCode.NotModified)
{
return null;
}

var content = result.Content.ReadAsStringAsync();
content.Wait();

Expand Down
0