Skip to content

Commit 82c5260

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Add tests for options setup
1 parent 9914482 commit 82c5260

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace Microsoft.AspNetCore.Mvc.Versioning
2+
{
3+
using FluentAssertions;
4+
using Microsoft.AspNetCore.Mvc.ModelBinding;
5+
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
6+
using Microsoft.Extensions.Options;
7+
using Moq;
8+
using System.Linq;
9+
using Xunit;
10+
11+
public class ApiVersioningMvcOptionsSetupTest
12+
{
13+
[Fact]
14+
public void post_configure_should_not_register_report_filter_by_default()
15+
{
16+
// arrange
17+
var versioningOptions = Options.Create( new ApiVersioningOptions() );
18+
var mvcOptions = new MvcOptions();
19+
var setup = new ApiVersioningMvcOptionsSetup( versioningOptions );
20+
21+
// act
22+
setup.PostConfigure( default, mvcOptions );
23+
24+
// assert
25+
mvcOptions.Filters.Should().BeEmpty();
26+
}
27+
28+
[Fact]
29+
public void post_configure_should_register_report_filter()
30+
{
31+
// arrange
32+
var versioningOptions = Options.Create( new ApiVersioningOptions() { ReportApiVersions = true } );
33+
var mvcOptions = new MvcOptions();
34+
var setup = new ApiVersioningMvcOptionsSetup( versioningOptions );
35+
36+
// act
37+
setup.PostConfigure( default, mvcOptions );
38+
39+
// assert
40+
mvcOptions.Filters.OfType<ServiceFilterAttribute>().Single().ServiceType.Should().Be( typeof( ReportApiVersionsAttribute ) );
41+
}
42+
43+
[Fact]
44+
public void post_configure_should_register_model_binder_provider()
45+
{
46+
// arrange
47+
var versioningOptions = Options.Create( new ApiVersioningOptions() );
48+
var mvcOptions = new MvcOptions();
49+
var setup = new ApiVersioningMvcOptionsSetup( versioningOptions );
50+
var metadata = new Mock<ModelMetadata>( ModelMetadataIdentity.ForType( typeof( ApiVersion ) ) );
51+
var context = new Mock<ModelBinderProviderContext>();
52+
53+
context.SetupGet( c => c.Metadata ).Returns( metadata.Object );
54+
55+
// act
56+
setup.PostConfigure( default, mvcOptions );
57+
58+
// assert
59+
mvcOptions.ModelBinderProviders.First().GetBinder( context.Object ).Should().BeOfType( typeof( ApiVersionModelBinder ) );
60+
}
61+
}
62+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace Microsoft.AspNetCore.Mvc.Versioning
2+
{
3+
using FluentAssertions;
4+
using Microsoft.AspNetCore.Mvc.Routing;
5+
using Microsoft.AspNetCore.Routing;
6+
using Microsoft.Extensions.Options;
7+
using Xunit;
8+
9+
public class ApiVersioningRouteOptionsSetupTest
10+
{
11+
[Fact]
12+
public void post_configure_should_add_route_contraint_with_default_name()
13+
{
14+
// arrange
15+
var versioningOptions = Options.Create( new ApiVersioningOptions() );
16+
var routeOptions = new RouteOptions();
17+
var setup = new ApiVersioningRouteOptionsSetup( versioningOptions );
18+
19+
// act
20+
setup.PostConfigure( default, routeOptions );
21+
22+
// assert
23+
routeOptions.ConstraintMap["apiVersion"].Should().Be( typeof( ApiVersionRouteConstraint ) );
24+
}
25+
26+
[Fact]
27+
public void post_configure_should_add_route_contraint_with_custom_name()
28+
{
29+
// arrange
30+
const string key = "api-version";
31+
var versioningOptions = Options.Create( new ApiVersioningOptions() { RouteConstraintName = key } );
32+
var routeOptions = new RouteOptions();
33+
var setup = new ApiVersioningRouteOptionsSetup( versioningOptions );
34+
35+
// act
36+
setup.PostConfigure( default, routeOptions );
37+
38+
// assert
39+
routeOptions.ConstraintMap[key].Should().Be( typeof( ApiVersionRouteConstraint ) );
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)