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+ }
0 commit comments