Add sample and functional test for custom inline constraints
This commit is contained in:
parent
e7728dde3f
commit
21031a3aa8
|
|
@ -478,6 +478,26 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
// Testing custom inline constraint updated in ConstraintMap
|
||||||
|
[InlineData("1234567890128", "13 Digit ISBN Number")]
|
||||||
|
// Testing custom inline constraint configured via MapRoute
|
||||||
|
[InlineData("1-234-56789-X", "10 Digit ISBN Number")]
|
||||||
|
public async Task CustomInlineConstraint_Add_Update(string isbn, string expectedBody)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
|
||||||
|
var client = server.CreateClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.GetAsync("http://localhost/book/index/" + isbn);
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
Assert.Contains(expectedBody, body);
|
||||||
|
}
|
||||||
|
|
||||||
public static IEnumerable<object[]> QueryParameters
|
public static IEnumerable<object[]> QueryParameters
|
||||||
{
|
{
|
||||||
// The first four parameters are controller name, action name, parameters in the query and their values.
|
// The first four parameters are controller name, action name, parameters in the query and their values.
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,115 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.Routing;
|
||||||
|
|
||||||
|
namespace InlineConstraintsWebSite.Constraints
|
||||||
|
{
|
||||||
|
public class IsbnDigitScheme10Constraint : IRouteConstraint
|
||||||
|
{
|
||||||
|
private readonly bool _allowDashes;
|
||||||
|
|
||||||
|
public IsbnDigitScheme10Constraint(bool allowDashes)
|
||||||
|
{
|
||||||
|
_allowDashes = allowDashes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Match(
|
||||||
|
HttpContext httpContext,
|
||||||
|
IRouter route,
|
||||||
|
string routeKey,
|
||||||
|
IDictionary<string, object> values,
|
||||||
|
RouteDirection routeDirection)
|
||||||
|
{
|
||||||
|
object value;
|
||||||
|
|
||||||
|
if (!values.TryGetValue(routeKey, out value))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var inputString = value as string;
|
||||||
|
string isbnNumber;
|
||||||
|
|
||||||
|
if (inputString == null
|
||||||
|
|| !TryGetIsbn10(inputString, _allowDashes, out isbnNumber))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum = 0;
|
||||||
|
Func<char, int> convertToInt = (char n) => n - '0';
|
||||||
|
|
||||||
|
for (int i = 0; i < isbnNumber.Length - 1; ++i)
|
||||||
|
{
|
||||||
|
sum += convertToInt(isbnNumber[i]) * (i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
var checkSum = sum % 11;
|
||||||
|
var lastDigit = isbnNumber.Last();
|
||||||
|
|
||||||
|
if (checkSum == 10)
|
||||||
|
{
|
||||||
|
return char.ToUpperInvariant(lastDigit) == 'X';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return checkSum == convertToInt(lastDigit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool TryGetIsbn10(string value, bool allowDashes, out string isbnNumber)
|
||||||
|
{
|
||||||
|
if (!allowDashes)
|
||||||
|
{
|
||||||
|
if (CheckIsbn10Characters(value))
|
||||||
|
{
|
||||||
|
isbnNumber = value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
isbnNumber = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var isbnParts = value.Split(
|
||||||
|
new char[] { '-' },
|
||||||
|
StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
if (isbnParts.Length == 4)
|
||||||
|
{
|
||||||
|
value = value.Replace("-", string.Empty);
|
||||||
|
if (CheckIsbn10Characters(value))
|
||||||
|
{
|
||||||
|
isbnNumber = value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
isbnNumber = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool CheckIsbn10Characters(string value)
|
||||||
|
{
|
||||||
|
if (value.Length != 10)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var digits = value.Substring(0, 9);
|
||||||
|
var checksum = value.Last();
|
||||||
|
|
||||||
|
return digits.All(n => '0' <= n && n <= '9')
|
||||||
|
&& ('0' <= checksum && checksum <= '9'
|
||||||
|
|| 'X' == char.ToUpperInvariant(checksum));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.Routing;
|
||||||
|
|
||||||
|
namespace InlineConstraintsWebSite.Constraints
|
||||||
|
{
|
||||||
|
public class IsbnDigitScheme13Constraint : IRouteConstraint
|
||||||
|
{
|
||||||
|
private static readonly int[] _isbn13Weights = new int[] { 1, 3 };
|
||||||
|
|
||||||
|
public bool Match(
|
||||||
|
HttpContext httpContext,
|
||||||
|
IRouter route,
|
||||||
|
string routeKey,
|
||||||
|
IDictionary<string, object> values,
|
||||||
|
RouteDirection routeDirection)
|
||||||
|
{
|
||||||
|
object value;
|
||||||
|
|
||||||
|
if (!values.TryGetValue(routeKey, out value))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var isbnNumber = value as string;
|
||||||
|
|
||||||
|
if (isbnNumber == null
|
||||||
|
|| isbnNumber.Length != 13
|
||||||
|
|| isbnNumber.Any(n => n < '0' || n > '9'))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum = 0;
|
||||||
|
Func<char, int> convertToInt = (char n) => n - '0';
|
||||||
|
|
||||||
|
for (int i = 0; i < isbnNumber.Length - 1; ++i)
|
||||||
|
{
|
||||||
|
sum +=
|
||||||
|
convertToInt(isbnNumber[i]) * _isbn13Weights[i % 2];
|
||||||
|
}
|
||||||
|
|
||||||
|
var checkSum = 10 - sum % 10;
|
||||||
|
|
||||||
|
if (checkSum == convertToInt(isbnNumber.Last()))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace InlineConstraintsWebSite.Controllers
|
||||||
|
{
|
||||||
|
public class InlineConstraints_Isbn10Controller : Controller
|
||||||
|
{
|
||||||
|
public string Index(string isbnNumber)
|
||||||
|
{
|
||||||
|
return "10 Digit ISBN Number " + isbnNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace InlineConstraintsWebSite.Controllers
|
||||||
|
{
|
||||||
|
[Route("book/[action]")]
|
||||||
|
public class InlineConstraints_Isbn13Controller : Controller
|
||||||
|
{
|
||||||
|
[HttpGet("{isbnNumber:IsbnDigitScheme13}")]
|
||||||
|
public string Index(string isbnNumber)
|
||||||
|
{
|
||||||
|
return "13 Digit ISBN Number " + isbnNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using InlineConstraintsWebSite.Constraints;
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.Routing.Constraints;
|
using Microsoft.AspNet.Routing.Constraints;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
|
|
@ -12,6 +13,27 @@ namespace InlineConstraints
|
||||||
// Set up application services
|
// Set up application services
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
services.ConfigureRouting(
|
||||||
|
routeOptions => routeOptions.ConstraintMap.Add(
|
||||||
|
"IsbnDigitScheme10",
|
||||||
|
typeof(IsbnDigitScheme10Constraint)));
|
||||||
|
|
||||||
|
services.ConfigureRouting(
|
||||||
|
routeOptions => routeOptions.ConstraintMap.Add(
|
||||||
|
"IsbnDigitScheme13",
|
||||||
|
typeof(IsbnDigitScheme10Constraint)));
|
||||||
|
|
||||||
|
// Update an existing constraint from ConstraintMap for test purpose.
|
||||||
|
services.ConfigureRouting(
|
||||||
|
routeOptions =>
|
||||||
|
{
|
||||||
|
if (routeOptions.ConstraintMap.ContainsKey("IsbnDigitScheme13"))
|
||||||
|
{
|
||||||
|
routeOptions.ConstraintMap["IsbnDigitScheme13"] =
|
||||||
|
typeof(IsbnDigitScheme13Constraint);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Add MVC services to the services container
|
// Add MVC services to the services container
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
}
|
}
|
||||||
|
|
@ -24,6 +46,11 @@ namespace InlineConstraints
|
||||||
|
|
||||||
app.UseMvc(routes =>
|
app.UseMvc(routes =>
|
||||||
{
|
{
|
||||||
|
routes.MapRoute(
|
||||||
|
name: "isbn10",
|
||||||
|
template: "book/{action}/{isbnNumber:IsbnDigitScheme10(true)}",
|
||||||
|
defaults: new { controller = "InlineConstraints_Isbn10" });
|
||||||
|
|
||||||
routes.MapRoute("StoreId",
|
routes.MapRoute("StoreId",
|
||||||
"store/{action}/{id:guid?}",
|
"store/{action}/{id:guid?}",
|
||||||
defaults: new { controller = "InlineConstraints_Store" });
|
defaults: new { controller = "InlineConstraints_Store" });
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue