-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from twilio-labs/ReloadOnChangeOptionsAndVali…
…dation Support reloadOnChange and add better validation
- Loading branch information
Showing
19 changed files
with
1,355 additions
and
801 deletions.
There are no files selected for viewing
This file contains 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,59 @@ | ||
using System; | ||
using System.Net; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Twilio.AspNet.Core.UnitTests; | ||
|
||
public class ContextMocks | ||
{ | ||
public Moq.Mock<HttpContext> HttpContext { get; set; } | ||
public Moq.Mock<HttpRequest> Request { get; set; } | ||
|
||
public ContextMocks(bool isLocal, FormCollection form = null, bool isProxied = false) : this("", isLocal, form, | ||
isProxied) | ||
{ | ||
} | ||
|
||
public ContextMocks(string urlOverride, bool isLocal, FormCollection form = null, bool isProxied = false) | ||
{ | ||
var headers = new HeaderDictionary(); | ||
headers.Add("X-Twilio-Signature", CalculateSignature(urlOverride, form)); | ||
if (isProxied) | ||
{ | ||
headers.Add("X-Forwarded-For", "1.1.1.1"); | ||
} | ||
|
||
var connectionInfo = new Moq.Mock<ConnectionInfo>(); | ||
connectionInfo.Setup(x => x.RemoteIpAddress).Returns(isLocal ? IPAddress.Loopback : IPAddress.Parse("1.1.1.1")); | ||
|
||
HttpContext = new Moq.Mock<HttpContext>(); | ||
Request = new Moq.Mock<HttpRequest>(); | ||
HttpContext.Setup(x => x.Request).Returns(Request.Object); | ||
HttpContext.Setup(x => x.Connection).Returns(connectionInfo.Object); | ||
Request.Setup(x => x.Headers).Returns(headers); | ||
Request.Setup(x => x.HttpContext).Returns(HttpContext.Object); | ||
|
||
var uri = new Uri(ContextMocks.fakeUrl); | ||
Request.Setup(x => x.QueryString).Returns(new QueryString(uri.Query)); | ||
Request.Setup(x => x.Scheme).Returns(uri.Scheme); | ||
Request.Setup(x => x.Host).Returns(new HostString(uri.Host)); | ||
Request.Setup(x => x.Path).Returns(new PathString(uri.AbsolutePath)); | ||
|
||
if (form != null) | ||
{ | ||
Request.Setup(x => x.Method).Returns("POST"); | ||
Request.Setup(x => x.Form).Returns(form); | ||
Request.Setup(x => x.HasFormContentType).Returns(true); | ||
} | ||
} | ||
|
||
public static string fakeUrl = "https://api.example.com/webhook"; | ||
public static string fakeAuthToken = "thisisafakeauthtoken"; | ||
|
||
private static string CalculateSignature(string urlOverride, FormCollection form) | ||
=> ValidationHelper.CalculateSignature( | ||
string.IsNullOrEmpty(urlOverride) ? fakeUrl : urlOverride, | ||
fakeAuthToken, | ||
form | ||
); | ||
} |
This file contains 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,31 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Twilio.AspNet.Core.UnitTests; | ||
|
||
public static class Extensions | ||
{ | ||
public static Task<T> WaitForOptionChange<T>(this IOptionsMonitor<T> monitor) | ||
{ | ||
var tcs = new TaskCompletionSource<T>(); | ||
var cts = new CancellationTokenSource(); | ||
IDisposable disposable = null; | ||
disposable = monitor.OnChange(options => | ||
{ | ||
disposable.Dispose(); | ||
cts.Cancel(); | ||
tcs.SetResult(options); | ||
}); | ||
|
||
Task.Delay(TimeSpan.FromSeconds(1), cts.Token) | ||
.ContinueWith(_ => | ||
{ | ||
disposable.Dispose(); | ||
tcs.SetException(new Exception("WaitForOptionChange timed out.")); | ||
}, cts.Token); | ||
|
||
return tcs.Task; | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.