@@ -23,78 +23,67 @@ namespace UnitTests.Middleware
2323 public class CurrentRequestMiddlewareTests
2424 {
2525 [ Fact ]
26- public async Task ParseUrlBase_UrlHasBaseIdSet_ShouldSetCurrentRequestWithSaidId ( )
26+ public async Task ParseUrlBase_ObfuscatedIdClass_ShouldSetIdCorrectly ( )
2727 {
2828 // Arrange
29- var id = "123 " ;
30- var configuration = GetConfiguration ( $ "/users /{ id } ") ;
29+ var id = "ABC123ABC " ;
30+ var configuration = GetConfiguration ( $ "/obfuscatedIdModel /{ id } ", action : "GetAsync" , id : id ) ;
3131 var currentRequest = configuration . CurrentRequest ;
3232
3333 // Act
3434 await RunMiddlewareTask ( configuration ) ;
3535
3636 // Assert
3737 Assert . Equal ( id , currentRequest . BaseId ) ;
38- }
3938
39+ }
4040 [ Fact ]
41- public async Task ParseUrlBase_UrlHasNoBaseIdSet_ShouldHaveBaseIdSetToNull ( )
41+ public async Task ParseUrlBase_UrlHasBaseIdSet_ShouldSetCurrentRequestWithSaidId ( )
4242 {
4343 // Arrange
44- var configuration = GetConfiguration ( "/users" ) ;
44+ var id = "123" ;
45+ var configuration = GetConfiguration ( $ "/users/{ id } ", id : id ) ;
4546 var currentRequest = configuration . CurrentRequest ;
4647
4748 // Act
4849 await RunMiddlewareTask ( configuration ) ;
4950
5051 // Assert
51- Assert . Null ( currentRequest . BaseId ) ;
52+ Assert . Equal ( id , currentRequest . BaseId ) ;
5253 }
54+
5355 [ Fact ]
54- public async Task ParseUrlRel_UrlHasRelationshipIdSet_ShouldHaveBaseIdAndRelationshipIdSet ( )
56+ public async Task ParseUrlBase_UrlHasNoBaseIdSet_ShouldHaveBaseIdSetToNull ( )
5557 {
5658 // Arrange
57- var baseId = "5" ;
58- var relId = "23" ;
59- var configuration = GetConfiguration ( $ "/users/{ baseId } /relationships/books/{ relId } ", relType : typeof ( TodoItem ) , relIdType : typeof ( int ) ) ;
59+ var configuration = GetConfiguration ( "/users" ) ;
6060 var currentRequest = configuration . CurrentRequest ;
6161
6262 // Act
6363 await RunMiddlewareTask ( configuration ) ;
6464
6565 // Assert
66- Assert . Equal ( baseId , currentRequest . BaseId ) ;
67- Assert . Equal ( relId , currentRequest . RelationshipId ) ;
66+ Assert . Null ( currentRequest . BaseId ) ;
6867 }
6968
7069 [ Fact ]
71- public async Task ParseUrlBase_UrlHasNegativeBaseIdAndTypeIsInt_ShouldThrowJAException ( )
70+ public async Task ParseUrlBase_UrlHasNegativeBaseIdAndTypeIsInt_ShouldNotThrowJAException ( )
7271 {
7372 // Arrange
7473 var configuration = GetConfiguration ( "/users/-5/" ) ;
7574
76- // Act
77- var task = RunMiddlewareTask ( configuration ) ;
78-
79- // Assert
80- var exception = await Assert . ThrowsAsync < JsonApiException > ( async ( ) =>
81- {
82- await task ;
83- } ) ;
84- Assert . Equal ( 500 , exception . GetStatusCode ( ) ) ;
85- Assert . Contains ( "negative" , exception . Message ) ;
75+ // Act / Assert
76+ await RunMiddlewareTask ( configuration ) ;
8677 }
8778
8879 [ Theory ]
89- [ InlineData ( "12315K" , typeof ( int ) , true ) ]
90- [ InlineData ( "12315K" , typeof ( int ) , false ) ]
91- [ InlineData ( "5" , typeof ( Guid ) , true ) ]
92- [ InlineData ( "5" , typeof ( Guid ) , false ) ]
93- public async Task ParseUrlBase_UrlHasIncorrectBaseIdSet_ShouldThrowException ( string baseId , Type idType , bool addSlash )
80+ [ InlineData ( "" , false ) ]
81+ [ InlineData ( "" , true ) ]
82+ public async Task ParseUrlBase_UrlHasIncorrectBaseIdSet_ShouldThrowException ( string baseId , bool addSlash )
9483 {
9584 // Arrange
9685 var url = addSlash ? $ "/users/{ baseId } /" : $ "/users/{ baseId } ";
97- var configuration = GetConfiguration ( url , idType : idType ) ;
86+ var configuration = GetConfiguration ( url , id : baseId ) ;
9887
9988 // Act
10089 var task = RunMiddlewareTask ( configuration ) ;
@@ -104,7 +93,7 @@ public async Task ParseUrlBase_UrlHasIncorrectBaseIdSet_ShouldThrowException(str
10493 {
10594 await task ;
10695 } ) ;
107- Assert . Equal ( 500 , exception . GetStatusCode ( ) ) ;
96+ Assert . Equal ( 400 , exception . GetStatusCode ( ) ) ;
10897 Assert . Contains ( baseId , exception . Message ) ;
10998 }
11099
@@ -126,34 +115,29 @@ private Task RunMiddlewareTask(InvokeConfiguration holder)
126115 var resourceGraph = holder . ResourceGraph . Object ;
127116 return holder . MiddleWare . Invoke ( context , controllerResourceMapping , options , currentRequest , resourceGraph ) ;
128117 }
129- private InvokeConfiguration GetConfiguration ( string path , string resourceName = "users" , Type idType = null , Type relType = null , Type relIdType = null )
118+ private InvokeConfiguration GetConfiguration ( string path , string resourceName = "users" , string action = "" , string id = null , Type relType = null )
130119 {
131- if ( ( relType != null ) != ( relIdType != null ) )
132- {
133- throw new ArgumentException ( "Define both reltype and relidType or dont." ) ;
134- }
135120 if ( path . First ( ) != '/' )
136121 {
137122 throw new ArgumentException ( "Path should start with a '/'" ) ;
138123 }
139- idType ??= typeof ( int ) ;
140124 var middleware = new CurrentRequestMiddleware ( ( context ) =>
141125 {
142126 return Task . Run ( ( ) => Console . WriteLine ( "finished" ) ) ;
143127 } ) ;
144128 var forcedNamespace = "api/v1" ;
145129 var mockMapping = new Mock < IControllerResourceMapping > ( ) ;
146130 Mock < IJsonApiOptions > mockOptions = CreateMockOptions ( forcedNamespace ) ;
147- var mockGraph = CreateMockResourceGraph ( idType , resourceName , relIdType : relIdType ) ;
131+ var mockGraph = CreateMockResourceGraph ( resourceName , includeRelationship : relType != null ) ;
148132 var currentRequest = new CurrentRequest ( ) ;
149- if ( relType != null && relIdType != null )
133+ if ( relType != null )
150134 {
151135 currentRequest . RequestRelationship = new HasManyAttribute
152136 {
153137 RightType = relType
154138 } ;
155139 }
156- var context = CreateHttpContext ( path , isRelationship : relType != null ) ;
140+ var context = CreateHttpContext ( path , isRelationship : relType != null , action : action , id : id ) ;
157141 return new InvokeConfiguration
158142 {
159143 MiddleWare = middleware ,
@@ -172,33 +156,37 @@ private static Mock<IJsonApiOptions> CreateMockOptions(string forcedNamespace)
172156 return mockOptions ;
173157 }
174158
175- private static DefaultHttpContext CreateHttpContext ( string path , bool isRelationship = false )
159+ private static DefaultHttpContext CreateHttpContext ( string path , bool isRelationship = false , string action = "" , string id = null )
176160 {
177161 var context = new DefaultHttpContext ( ) ;
178162 context . Request . Path = new PathString ( path ) ;
179163 context . Response . Body = new MemoryStream ( ) ;
180164 var feature = new RouteValuesFeature ( ) ;
181165 feature . RouteValues [ "controller" ] = "fake!" ;
182- feature . RouteValues [ "action" ] = isRelationship ? "relationships" : "noRel" ;
166+ feature . RouteValues [ "action" ] = isRelationship ? "GetRelationship" : action ;
167+ if ( id != null )
168+ {
169+ feature . RouteValues [ "id" ] = id ;
170+ }
183171 context . Features . Set < IRouteValuesFeature > ( feature ) ;
184172 return context ;
185173 }
186174
187- private Mock < IResourceGraph > CreateMockResourceGraph ( Type idType , string resourceName , Type relIdType = null )
175+ private Mock < IResourceGraph > CreateMockResourceGraph ( string resourceName , bool includeRelationship = false )
188176 {
189177 var mockGraph = new Mock < IResourceGraph > ( ) ;
190178 var resourceContext = new ResourceContext
191179 {
192180 ResourceName = resourceName ,
193- IdentityType = idType
181+ IdentityType = typeof ( string )
194182 } ;
195183 var seq = mockGraph . SetupSequence ( d => d . GetResourceContext ( It . IsAny < Type > ( ) ) ) . Returns ( resourceContext ) ;
196- if ( relIdType != null )
184+ if ( includeRelationship )
197185 {
198186 var relResourceContext = new ResourceContext
199187 {
200188 ResourceName = "todoItems" ,
201- IdentityType = relIdType
189+ IdentityType = typeof ( string )
202190 } ;
203191 seq . Returns ( relResourceContext ) ;
204192 }
0 commit comments