-
Notifications
You must be signed in to change notification settings - Fork 20
feat: ODP REST API for Sending ODP Events #315
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
467cbcc
WIP REST API Manager
mikechu-optimizely 15b9398
Add docs
mikechu-optimizely 54a61de
Fix GraphQL tests
mikechu-optimizely adaf5bc
WIP Fixing unit tests...
mikechu-optimizely 7fb27be
Completed unit tests
mikechu-optimizely f8bce3a
Clean up usings; add missing Copyright notices
mikechu-optimizely 0c4885e
Add 'QL' to DotSettings; Disable Enum InconsistentNaming
mikechu-optimizely c98ee82
Code review changes
mikechu-optimizely 7bfb3bf
Code review changes
mikechu-optimizely 6269a4c
Copyright notice & static
mikechu-optimizely 6ce4d49
Bitwise OR TLS12 into SystemDefault instead of hard-coding to 1.2
mikechu-optimizely 8f23e03
Remove ServicePointManager.SecurityProtocol enum declarations
mikechu-optimizely ca90ccb
Code review changes
mikechu-optimizely ea7c2ce
More Pull Request code changes
mikechu-optimizely File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2022 Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using Moq; | ||
using Moq.Protected; | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace OptimizelySDK.Tests.OdpTests | ||
{ | ||
/// <summary> | ||
/// Shared utility methods used for stubbing HttpClient instances | ||
/// </summary> | ||
public static class HttpClientTestUtil | ||
{ | ||
/// <summary> | ||
/// Create an HttpClient instance that returns an expected response | ||
/// </summary> | ||
/// <param name="statusCode">Http Status Code to return</param> | ||
/// <param name="content">Message body content to return</param> | ||
/// <returns>HttpClient instance that will return desired HttpResponseMessage</returns> | ||
public static HttpClient MakeHttpClient(HttpStatusCode statusCode, | ||
string content = default | ||
) | ||
{ | ||
var response = new HttpResponseMessage(statusCode); | ||
|
||
if (!string.IsNullOrWhiteSpace(content)) | ||
{ | ||
response.Content = new StringContent(content); | ||
} | ||
|
||
var mockedHandler = new Mock<HttpMessageHandler>(); | ||
mockedHandler.Protected().Setup<Task<HttpResponseMessage>>( | ||
"SendAsync", | ||
ItExpr.IsAny<HttpRequestMessage>(), | ||
ItExpr.IsAny<CancellationToken>()). | ||
ReturnsAsync(response); | ||
return new HttpClient(mockedHandler.Object); | ||
} | ||
|
||
/// <summary> | ||
/// Create an HttpClient instance that will timeout for SendAsync calls | ||
/// </summary> | ||
/// <returns>HttpClient instance that throw TimeoutException</returns> | ||
public static HttpClient MakeHttpClientWithTimeout() | ||
{ | ||
var mockedHandler = new Mock<HttpMessageHandler>(); | ||
mockedHandler.Protected().Setup<Task<HttpResponseMessage>>( | ||
"SendAsync", | ||
ItExpr.IsAny<HttpRequestMessage>(), | ||
ItExpr.IsAny<CancellationToken>()). | ||
Throws<TimeoutException>(); | ||
return new HttpClient(mockedHandler.Object); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright 2022 Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using Moq; | ||
using NUnit.Framework; | ||
using OptimizelySDK.ErrorHandler; | ||
using OptimizelySDK.Logger; | ||
using OptimizelySDK.Odp; | ||
using OptimizelySDK.Odp.Entity; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
|
||
namespace OptimizelySDK.Tests.OdpTests | ||
{ | ||
[TestFixture] | ||
public class OdpEventApiManagerTest | ||
{ | ||
private const string VALID_ODP_PUBLIC_KEY = "a-valid-odp-public-key"; | ||
private const string ODP_REST_API_HOST = "https://api.example.com"; | ||
|
||
private readonly List<OdpEvent> _odpEvents = new List<OdpEvent>(); | ||
|
||
private Mock<IErrorHandler> _mockErrorHandler; | ||
private Mock<ILogger> _mockLogger; | ||
|
||
[TestFixtureSetUp] | ||
public void Setup() | ||
{ | ||
_mockErrorHandler = new Mock<IErrorHandler>(); | ||
_mockLogger = new Mock<ILogger>(); | ||
_mockLogger.Setup(i => i.Log(It.IsAny<LogLevel>(), It.IsAny<string>())); | ||
|
||
_odpEvents.Add(new OdpEvent("t1", "a1", | ||
new Dictionary<string, string> | ||
{ | ||
{ | ||
"id-key-1", "id-value-1" | ||
}, | ||
}, | ||
new Dictionary<string, object> | ||
{ | ||
{ | ||
"key11", "value-1" | ||
}, | ||
{ | ||
"key12", true | ||
}, | ||
{ | ||
"key13", 3.5 | ||
}, | ||
{ | ||
"key14", null | ||
}, | ||
} | ||
)); | ||
_odpEvents.Add(new OdpEvent("t2", "a2", | ||
new Dictionary<string, string> | ||
{ | ||
{ | ||
"id-key-2", "id-value-2" | ||
}, | ||
}, new Dictionary<string, object> | ||
{ | ||
{ | ||
"key2", "value-2" | ||
}, | ||
} | ||
)); | ||
} | ||
|
||
[Test] | ||
public void ShouldSendEventsSuccessfullyAndNotSuggestRetry() | ||
{ | ||
var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.OK); | ||
var manger = | ||
new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient); | ||
|
||
var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST, | ||
_odpEvents); | ||
|
||
Assert.IsFalse(shouldRetry); | ||
} | ||
|
||
[Test] | ||
public void ShouldNotSuggestRetryFor400HttpResponse() | ||
{ | ||
var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.BadRequest); | ||
var manger = | ||
new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient); | ||
|
||
var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST, | ||
_odpEvents); | ||
|
||
Assert.IsFalse(shouldRetry); | ||
} | ||
|
||
[Test] | ||
public void ShouldSuggestRetryFor500HttpResponse() | ||
{ | ||
var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.InternalServerError); | ||
var manger = | ||
new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient); | ||
|
||
var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST, | ||
_odpEvents); | ||
|
||
Assert.IsTrue(shouldRetry); | ||
} | ||
|
||
[Test] | ||
public void ShouldSuggestRetryForNetworkTimeout() { | ||
var httpClient = HttpClientTestUtil.MakeHttpClientWithTimeout(); | ||
var manger = | ||
new OdpEventApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient); | ||
|
||
var shouldRetry = manger.SendEvents(VALID_ODP_PUBLIC_KEY, ODP_REST_API_HOST, | ||
_odpEvents); | ||
|
||
Assert.IsTrue(shouldRetry);} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.