-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add controller base extensions to get set callback data
- Loading branch information
thebevrishot
committed
Nov 12, 2019
1 parent
a06de99
commit af7459d
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/Ztm.WebApi.Tests/Controllers/ControllerBaseExtensionsTests.cs
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,88 @@ | ||
using System; | ||
using System.Net; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Xunit; | ||
using Ztm.WebApi.Controllers; | ||
|
||
namespace Ztm.WebApi.Tests.Controllers | ||
{ | ||
public sealed class ControllerBaseExtensionsTests | ||
{ | ||
readonly static string CallbackUrlKey = "X-Callback-URL"; | ||
readonly static string CallbackIdKey = "X-Callback-ID"; | ||
|
||
readonly ControllerBase subject; | ||
|
||
public ControllerBaseExtensionsTests() | ||
{ | ||
this.subject = new TestControllerBase(); | ||
this.subject.ControllerContext.HttpContext = new DefaultHttpContext(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("http://zcoin.io/")] | ||
[InlineData("https://zcoin.io/")] | ||
public void TryGetCallbackUrl_WithValidUrl_ShouldSuccess(string rawUrl) | ||
{ | ||
// Arrange. | ||
this.subject.HttpContext.Request.Headers.Add(CallbackUrlKey, rawUrl); | ||
|
||
// Act. | ||
var success = this.subject.TryGetCallbackUrl(out var url); | ||
|
||
// Assert. | ||
Assert.True(success); | ||
Assert.Equal(new Uri(rawUrl), url); | ||
} | ||
|
||
[Theory] | ||
[InlineData("urn:isbn:0451450523")] | ||
[InlineData("urn:lsid:zoobank.org:pub:CDC8D258-8F57-41DC-B560-247E17D3DC8C")] | ||
[InlineData("Foo")] | ||
public void TryGetCallbackUrl_WithInvalidUrl_ShouldReturnFalse(string invalidUrl) | ||
{ | ||
// Arrange. | ||
this.subject.HttpContext.Request.Headers.Add(CallbackUrlKey, invalidUrl); | ||
|
||
// Act. | ||
var success = this.subject.TryGetCallbackUrl(out var url); | ||
|
||
// Assert. | ||
Assert.False(success); | ||
Assert.Null(url); | ||
} | ||
|
||
[Fact] | ||
public void TryGetCallbackUrl_WithUnsetHeader_ShouldReturnFalse() | ||
{ | ||
// Act. | ||
var success = this.subject.TryGetCallbackUrl(out var url); | ||
|
||
// Assert. | ||
Assert.False(success); | ||
Assert.Null(url); | ||
} | ||
|
||
[Fact] | ||
public void SetCallbackId_WithValidGuid_ShouldSuccess() | ||
{ | ||
// Arrange. | ||
var id = Guid.NewGuid(); | ||
|
||
// Act. | ||
this.subject.SetCallbackId(id); | ||
|
||
// Assert. | ||
Assert.True( | ||
this.subject.HttpContext.Response.Headers.TryGetValue(CallbackIdKey, out var retreived) | ||
); | ||
Assert.Equal(id.ToString(), retreived); | ||
Assert.Equal(this.subject.HttpContext.Response.StatusCode, (int)HttpStatusCode.Accepted); | ||
} | ||
} | ||
|
||
class TestControllerBase : ControllerBase | ||
{ | ||
} | ||
} |
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,41 @@ | ||
using System; | ||
using System.Net; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Ztm.WebApi.Controllers | ||
{ | ||
public static class ControllerBaseExtensions | ||
{ | ||
readonly static string CallbackUrlKey = "X-Callback-URL"; | ||
readonly static string CallbackIdKey = "X-Callback-ID"; | ||
|
||
public static bool TryGetCallbackUrl(this ControllerBase controller, out Uri url) | ||
{ | ||
if (controller.HttpContext.Request.Headers.TryGetValue(CallbackUrlKey, out var rawUrl)) | ||
{ | ||
try | ||
{ | ||
url = new Uri(rawUrl); | ||
|
||
if (url.Scheme == Uri.UriSchemeHttp | ||
|| url.Scheme == Uri.UriSchemeHttps) | ||
{ | ||
return true; | ||
} | ||
} | ||
catch (UriFormatException) | ||
{ | ||
} | ||
} | ||
|
||
url = null; | ||
return false; | ||
} | ||
|
||
public static void SetCallbackId(this ControllerBase controller, Guid id) | ||
{ | ||
controller.HttpContext.Response.Headers.Add(CallbackIdKey, id.ToString()); | ||
controller.HttpContext.Response.StatusCode = (int)HttpStatusCode.Accepted; | ||
} | ||
} | ||
} |