Skip to content

Commit

Permalink
Add controller base extensions to get set callback data
Browse files Browse the repository at this point in the history
  • Loading branch information
thebevrishot committed Nov 12, 2019
1 parent a06de99 commit af7459d
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/Ztm.WebApi.Tests/Controllers/ControllerBaseExtensionsTests.cs
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
{
}
}
41 changes: 41 additions & 0 deletions src/Ztm.WebApi/Controllers/ControllerBaseExtensions.cs
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;
}
}
}

0 comments on commit af7459d

Please sign in to comment.