Add sample and functional test for custom inline constraints
This commit is contained in:
parent
e7728dde3f
commit
21031a3aa8
|
|
@ -158,7 +158,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
var client = server.CreateClient();
|
var client = server.CreateClient();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response =
|
var response =
|
||||||
await client.GetAsync(@"http://localhost/products/GetProductByManufacturingDate/2014-10-11T13:45:30");
|
await client.GetAsync(@"http://localhost/products/GetProductByManufacturingDate/2014-10-11T13:45:30");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
|
|
@ -176,7 +176,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
|
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
|
||||||
var client = server.CreateClient();
|
var client = server.CreateClient();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await client.GetAsync("http://localhost/products/GetProductByCategoryName/Sports");
|
var response = await client.GetAsync("http://localhost/products/GetProductByCategoryName/Sports");
|
||||||
|
|
||||||
|
|
@ -196,7 +196,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
var client = server.CreateClient();
|
var client = server.CreateClient();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response =
|
var response =
|
||||||
await client.GetAsync("http://localhost/products/GetProductByCategoryName/SportsSportsSportsSports");
|
await client.GetAsync("http://localhost/products/GetProductByCategoryName/SportsSportsSportsSports");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
|
|
@ -226,7 +226,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
|
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
|
||||||
var client = server.CreateClient();
|
var client = server.CreateClient();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await client.GetAsync("http://localhost/products/GetProductByCategoryId/40");
|
var response = await client.GetAsync("http://localhost/products/GetProductByCategoryId/40");
|
||||||
|
|
||||||
|
|
@ -478,10 +478,30 @@ 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.
|
||||||
// These are used to generate a link, the last parameter is expected generated link
|
// These are used to generate a link, the last parameter is expected generated link
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
// Attribute Route, id:int? constraint
|
// Attribute Route, id:int? constraint
|
||||||
|
|
@ -495,7 +515,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attribute Route, id:int? constraint
|
// Attribute Route, id:int? constraint
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
"InlineConstraints_Products",
|
"InlineConstraints_Products",
|
||||||
"GetProductById",
|
"GetProductById",
|
||||||
|
|
@ -514,7 +534,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attribute Route, name:length(1,20)? constraint
|
// Attribute Route, name:length(1,20)? constraint
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
"InlineConstraints_Products",
|
"InlineConstraints_Products",
|
||||||
"GetProductByCategoryName",
|
"GetProductByCategoryName",
|
||||||
|
|
@ -524,7 +544,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attribute Route, name:length(1,20)? constraint
|
// Attribute Route, name:length(1,20)? constraint
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
"InlineConstraints_Products",
|
"InlineConstraints_Products",
|
||||||
"GetProductByCategoryName",
|
"GetProductByCategoryName",
|
||||||
|
|
@ -534,7 +554,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attribute Route, catId:int:range(10, 100) constraint
|
// Attribute Route, catId:int:range(10, 100) constraint
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
"InlineConstraints_Products",
|
"InlineConstraints_Products",
|
||||||
"GetProductByCategoryId",
|
"GetProductByCategoryId",
|
||||||
|
|
@ -544,7 +564,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attribute Route, catId:int:range(10, 100) constraint
|
// Attribute Route, catId:int:range(10, 100) constraint
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
"InlineConstraints_Products",
|
"InlineConstraints_Products",
|
||||||
"GetProductByCategoryId",
|
"GetProductByCategoryId",
|
||||||
|
|
@ -554,7 +574,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attribute Route, name:length(1,20)? constraint
|
// Attribute Route, name:length(1,20)? constraint
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
"InlineConstraints_Products",
|
"InlineConstraints_Products",
|
||||||
"GetProductByPrice",
|
"GetProductByPrice",
|
||||||
|
|
@ -564,7 +584,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attribute Route, price:float? constraint
|
// Attribute Route, price:float? constraint
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
"InlineConstraints_Products",
|
"InlineConstraints_Products",
|
||||||
"GetProductByManufacturerId",
|
"GetProductByManufacturerId",
|
||||||
|
|
@ -574,7 +594,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attribute Route, manId:int:min(10)? constraint
|
// Attribute Route, manId:int:min(10)? constraint
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
"InlineConstraints_Products",
|
"InlineConstraints_Products",
|
||||||
"GetProductByManufacturerId",
|
"GetProductByManufacturerId",
|
||||||
|
|
@ -584,7 +604,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attribute Route, manId:int:min(10)? constraint
|
// Attribute Route, manId:int:min(10)? constraint
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
"InlineConstraints_Products",
|
"InlineConstraints_Products",
|
||||||
"GetProductByManufacturerId",
|
"GetProductByManufacturerId",
|
||||||
|
|
@ -594,7 +614,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attribute Route, dateTime:datetime constraint
|
// Attribute Route, dateTime:datetime constraint
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
"InlineConstraints_Products",
|
"InlineConstraints_Products",
|
||||||
"GetProductByManufacturingDate",
|
"GetProductByManufacturingDate",
|
||||||
|
|
@ -604,7 +624,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
};
|
};
|
||||||
|
|
||||||
// Conventional Route, id:guid? constraint
|
// Conventional Route, id:guid? constraint
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
"InlineConstraints_Store",
|
"InlineConstraints_Store",
|
||||||
"GetStoreById",
|
"GetStoreById",
|
||||||
|
|
@ -618,10 +638,10 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
[Theory]
|
[Theory]
|
||||||
[MemberData(nameof(QueryParameters))]
|
[MemberData(nameof(QueryParameters))]
|
||||||
public async Task GetGeneratedLink(
|
public async Task GetGeneratedLink(
|
||||||
string controller,
|
string controller,
|
||||||
string action,
|
string action,
|
||||||
string parameterName,
|
string parameterName,
|
||||||
string parameterValue,
|
string parameterValue,
|
||||||
string expectedLink)
|
string expectedLink)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
|
|
@ -647,12 +667,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
controller,
|
controller,
|
||||||
action,
|
action,
|
||||||
parameterName,
|
parameterName,
|
||||||
parameterValue);
|
parameterValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = await client.GetAsync(url);
|
var response = await client.GetAsync(url);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var body = await response.Content.ReadAsStringAsync();
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
Assert.Equal(expectedLink, body);
|
Assert.Equal(expectedLink, body);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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