44using System . Collections . ObjectModel ;
55using System . Linq . Expressions ;
66using System . Reflection ;
7+ using System . Security . Claims ;
78using System . Text ;
9+ using System . Text . Json ;
810using Microsoft . AspNetCore . Authentication ;
911using Microsoft . AspNetCore . Routing ;
1012using Microsoft . Net . Http . Headers ;
@@ -126,13 +128,17 @@ public void BadRequest_WithNoArgs_ResultHasCorrectValues()
126128
127129 [ Theory ]
128130 [ MemberData ( nameof ( Bytes_ResultHasCorrectValues_Data ) ) ]
129- public void Bytes_ResultHasCorrectValues ( string contentType , string fileDownloadName , bool enableRangeProcessing , DateTimeOffset lastModified , EntityTagHeaderValue entityTag )
131+ public void BytesOrFile_ResultHasCorrectValues ( int bytesOrFile , string contentType , string fileDownloadName , bool enableRangeProcessing , DateTimeOffset lastModified , EntityTagHeaderValue entityTag )
130132 {
131133 // Arrange
132134 var contents = new byte [ 0 ] ;
133135
134136 // Act
135- var result = Results . Bytes ( contents , contentType , fileDownloadName , enableRangeProcessing , lastModified , entityTag ) as FileContentHttpResult ;
137+ var result = bytesOrFile switch
138+ {
139+ 0 => Results . Bytes ( contents , contentType , fileDownloadName , enableRangeProcessing , lastModified , entityTag ) ,
140+ _ => Results . File ( contents , contentType , fileDownloadName , enableRangeProcessing , lastModified , entityTag )
141+ } as FileContentHttpResult ;
136142
137143 // Assert
138144 Assert . Equal ( contents , result . FileContents ) ;
@@ -145,12 +151,14 @@ public void Bytes_ResultHasCorrectValues(string contentType, string fileDownload
145151
146152 public static IEnumerable < object [ ] > Bytes_ResultHasCorrectValues_Data => new List < object [ ] >
147153 {
148- new object [ ] { "text/plain" , "testfile" , true , new DateTimeOffset ( 2022 , 1 , 1 , 0 , 0 , 1 , TimeSpan . FromHours ( - 8 ) ) , EntityTagHeaderValue . Any } ,
149- new object [ ] { default ( string ) , default ( string ) , default ( bool ) , default ( DateTimeOffset ? ) , default ( EntityTagHeaderValue ) } ,
154+ new object [ ] { 0 , "text/plain" , "testfile" , true , new DateTimeOffset ( 2022 , 1 , 1 , 0 , 0 , 1 , TimeSpan . FromHours ( - 8 ) ) , EntityTagHeaderValue . Any } ,
155+ new object [ ] { 0 , default ( string ) , default ( string ) , default ( bool ) , default ( DateTimeOffset ? ) , default ( EntityTagHeaderValue ) } ,
156+ new object [ ] { 1 , "text/plain" , "testfile" , true , new DateTimeOffset ( 2022 , 1 , 1 , 0 , 0 , 1 , TimeSpan . FromHours ( - 8 ) ) , EntityTagHeaderValue . Any } ,
157+ new object [ ] { 1 , default ( string ) , default ( string ) , default ( bool ) , default ( DateTimeOffset ? ) , default ( EntityTagHeaderValue ) } ,
150158 } ;
151159
152160 [ Theory ]
153- [ MemberData ( nameof ( Challenge_ResultHasCorrectValues_Data ) ) ]
161+ [ MemberData ( nameof ( ChallengeForbidSignInOut_ResultHasCorrectValues_Data ) ) ]
154162 public void Challenge_ResultHasCorrectValues ( AuthenticationProperties properties , IList < string > authenticationSchemes )
155163 {
156164 // Act
@@ -161,14 +169,60 @@ public void Challenge_ResultHasCorrectValues(AuthenticationProperties properties
161169 Assert . Equal ( authenticationSchemes ?? new ReadOnlyCollection < string > ( new List < string > ( ) ) , result . AuthenticationSchemes ) ;
162170 }
163171
164- public static IEnumerable < object [ ] > Challenge_ResultHasCorrectValues_Data => new List < object [ ] >
172+ [ Theory ]
173+ [ MemberData ( nameof ( ChallengeForbidSignInOut_ResultHasCorrectValues_Data ) ) ]
174+ public void Forbid_ResultHasCorrectValues ( AuthenticationProperties properties , IList < string > authenticationSchemes )
175+ {
176+ // Act
177+ var result = Results . Forbid ( properties , authenticationSchemes ) as ForbidHttpResult ;
178+
179+ // Assert
180+ Assert . Equal ( properties , result . Properties ) ;
181+ Assert . Equal ( authenticationSchemes ?? new ReadOnlyCollection < string > ( new List < string > ( ) ) , result . AuthenticationSchemes ) ;
182+ }
183+
184+ [ Theory ]
185+ [ MemberData ( nameof ( ChallengeForbidSignInOut_ResultHasCorrectValues_Data ) ) ]
186+ public void SignOut_ResultHasCorrectValues ( AuthenticationProperties properties , IList < string > authenticationSchemes )
187+ {
188+ // Act
189+ var result = Results . SignOut ( properties , authenticationSchemes ) as SignOutHttpResult ;
190+
191+ // Assert
192+ Assert . Equal ( properties , result . Properties ) ;
193+ Assert . Equal ( authenticationSchemes ?? new ReadOnlyCollection < string > ( new List < string > ( ) ) , result . AuthenticationSchemes ) ;
194+ }
195+
196+ [ Theory ]
197+ [ MemberData ( nameof ( ChallengeForbidSignInOut_ResultHasCorrectValues_Data ) ) ]
198+ public void SignIn_ResultHasCorrectValues ( AuthenticationProperties properties , IList < string > authenticationSchemes )
199+ {
200+ // Arrange
201+ var principal = new ClaimsPrincipal ( ) ;
202+
203+ // Act
204+ var result = Results . SignIn ( principal , properties , authenticationSchemes ? . First ( ) ) as SignInHttpResult ;
205+
206+ // Assert
207+ Assert . Equal ( principal , result . Principal ) ;
208+ Assert . Equal ( properties , result . Properties ) ;
209+ Assert . Equal ( authenticationSchemes ? . First ( ) , result . AuthenticationScheme ) ;
210+ }
211+
212+ public static IEnumerable < object [ ] > ChallengeForbidSignInOut_ResultHasCorrectValues_Data => new List < object [ ] >
165213 {
166214 new object [ ] { new AuthenticationProperties ( ) , new List < string > { "TestScheme" } } ,
167215 new object [ ] { new AuthenticationProperties ( ) , default ( IList < string > ) } ,
168216 new object [ ] { default ( AuthenticationProperties ) , new List < string > { "TestScheme" } } ,
169217 new object [ ] { default ( AuthenticationProperties ) , default ( IList < string > ) } ,
170218 } ;
171219
220+ [ Fact ]
221+ public void SignIn_WithNullPrincipal_ThrowsArgNullException ( )
222+ {
223+ Assert . Throws < ArgumentNullException > ( "principal" , ( ) => Results . SignIn ( null ) ) ;
224+ }
225+
172226 [ Fact ]
173227 public void Conflict_WithValue_ResultHasCorrectValues ( )
174228 {
@@ -298,6 +352,179 @@ public void Created_WithNullUri_ThrowsArgNullException()
298352 Assert . Throws < ArgumentNullException > ( "uri" , ( ) => Results . Created ( default ( Uri ) , null ) ) ;
299353 }
300354
355+ [ Fact ]
356+ public void CreatedAtRoute_WithRouteNameAndRouteValuesAndValue_ResultHasCorrectValues ( )
357+ {
358+ // Arrange
359+ var routeName = "routeName" ;
360+ var routeValues = new { foo = 123 } ;
361+ var value = new { } ;
362+
363+ // Act
364+ var result = Results . CreatedAtRoute ( routeName , routeValues , value ) as CreatedAtRoute < object > ;
365+
366+ // Assert
367+ Assert . Equal ( StatusCodes . Status201Created , result . StatusCode ) ;
368+ Assert . Equal ( routeName , result . RouteName ) ;
369+ Assert . Equal ( new RouteValueDictionary ( routeValues ) , result . RouteValues ) ;
370+ Assert . Equal ( value , result . Value ) ;
371+ }
372+
373+ [ Fact ]
374+ public void CreatedAtRoute_WithRouteNameAndValue_ResultHasCorrectValues ( )
375+ {
376+ // Arrange
377+ var routeName = "routeName" ;
378+ var value = new { } ;
379+
380+ // Act
381+ var result = Results . CreatedAtRoute ( routeName , null , value ) as CreatedAtRoute < object > ;
382+
383+ // Assert
384+ Assert . Equal ( StatusCodes . Status201Created , result . StatusCode ) ;
385+ Assert . Equal ( routeName , result . RouteName ) ;
386+ Assert . Equal ( new RouteValueDictionary ( ) , result . RouteValues ) ;
387+ Assert . Equal ( value , result . Value ) ;
388+ }
389+
390+ [ Fact ]
391+ public void CreatedAtRoute_WithRouteName_ResultHasCorrectValues ( )
392+ {
393+ // Arrange
394+ var routeName = "routeName" ;
395+
396+ // Act
397+ var result = Results . CreatedAtRoute ( routeName , null , null ) as CreatedAtRoute ;
398+
399+ // Assert
400+ Assert . Equal ( StatusCodes . Status201Created , result . StatusCode ) ;
401+ Assert . Equal ( routeName , result . RouteName ) ;
402+ Assert . Equal ( new RouteValueDictionary ( ) , result . RouteValues ) ;
403+ }
404+
405+ [ Fact ]
406+ public void CreatedAtRoute_WithNoArgs_ResultHasCorrectValues ( )
407+ {
408+ // Act
409+ var result = Results . CreatedAtRoute ( ) as CreatedAtRoute ;
410+
411+ // Assert
412+ Assert . Equal ( StatusCodes . Status201Created , result . StatusCode ) ;
413+ Assert . Null ( result . RouteName ) ;
414+ Assert . Equal ( new RouteValueDictionary ( ) , result . RouteValues ) ;
415+ }
416+
417+ [ Fact ]
418+ public void Empty_IsEmptyInstance ( )
419+ {
420+ // Act
421+ var result = Results . Empty as EmptyHttpResult ;
422+
423+ // Assert
424+ Assert . Equal ( EmptyHttpResult . Instance , result ) ;
425+ }
426+
427+ [ Fact ]
428+ public void Json_WithAllArgs_ResultHasCorrectValues ( )
429+ {
430+ // Arrange
431+ var data = new { } ;
432+ var options = new JsonSerializerOptions ( ) ;
433+ var contentType = "application/custom+json" ;
434+ var statusCode = StatusCodes . Status208AlreadyReported ;
435+
436+ // Act
437+ var result = Results . Json ( data , options , contentType , statusCode ) as JsonHttpResult < object > ;
438+
439+ // Assert
440+ Assert . Equal ( data , result . Value ) ;
441+ Assert . Equal ( options , result . JsonSerializerOptions ) ;
442+ Assert . Equal ( contentType , result . ContentType ) ;
443+ Assert . Equal ( statusCode , result . StatusCode ) ;
444+ }
445+
446+ [ Fact ]
447+ public void Json_WithNoArgs_ResultHasCorrectValues ( )
448+ {
449+ // Act
450+ var result = Results . Json ( null ) as JsonHttpResult < object > ;
451+
452+ // Assert
453+ Assert . Null ( result . Value ) ;
454+ Assert . Null ( result . JsonSerializerOptions ) ;
455+ Assert . Null ( result . ContentType ) ;
456+ Assert . Null ( result . StatusCode ) ;
457+ }
458+
459+ [ Fact ]
460+ public void LocalRedirect_WithUrl_ResultHasCorrectValues ( )
461+ {
462+ // Arrange
463+ var localUrl = "test/path" ;
464+
465+ // Act
466+ var result = Results . LocalRedirect ( localUrl ) as RedirectHttpResult ;
467+
468+ // Assert
469+ Assert . Equal ( localUrl , result . Url ) ;
470+ Assert . True ( result . AcceptLocalUrlOnly ) ;
471+ Assert . False ( result . Permanent ) ;
472+ Assert . False ( result . PreserveMethod ) ;
473+ }
474+
475+ [ Fact ]
476+ public void LocalRedirect_WithUrlAndPermanentTrue_ResultHasCorrectValues ( )
477+ {
478+ // Arrange
479+ var localUrl = "test/path" ;
480+ var permanent = true ;
481+
482+ // Act
483+ var result = Results . LocalRedirect ( localUrl , permanent ) as RedirectHttpResult ;
484+
485+ // Assert
486+ Assert . Equal ( localUrl , result . Url ) ;
487+ Assert . True ( result . AcceptLocalUrlOnly ) ;
488+ Assert . True ( result . Permanent ) ;
489+ Assert . False ( result . PreserveMethod ) ;
490+ }
491+
492+ [ Fact ]
493+ public void LocalRedirect_WithUrlAndPermanentTrueAndPreserveTrue_ResultHasCorrectValues ( )
494+ {
495+ // Arrange
496+ var localUrl = "test/path" ;
497+ var permanent = true ;
498+ var preserveMethod = true ;
499+
500+ // Act
501+ var result = Results . LocalRedirect ( localUrl , permanent , preserveMethod ) as RedirectHttpResult ;
502+
503+ // Assert
504+ Assert . Equal ( localUrl , result . Url ) ;
505+ Assert . True ( result . AcceptLocalUrlOnly ) ;
506+ Assert . True ( result . Permanent ) ;
507+ Assert . True ( result . PreserveMethod ) ;
508+ }
509+
510+ [ Fact ]
511+ public void LocalRedirect_WithNonLocalUrlAndPermanentTrueAndPreserveTrue_ResultHasCorrectValues ( )
512+ {
513+ // Arrange
514+ var localUrl = "https://example.com/non-local-url/example" ;
515+ var permanent = true ;
516+ var preserveMethod = true ;
517+
518+ // Act
519+ var result = Results . LocalRedirect ( localUrl , permanent , preserveMethod ) as RedirectHttpResult ;
520+
521+ // Assert
522+ Assert . Equal ( localUrl , result . Url ) ;
523+ Assert . True ( result . AcceptLocalUrlOnly ) ;
524+ Assert . True ( result . Permanent ) ;
525+ Assert . True ( result . PreserveMethod ) ;
526+ }
527+
301528 [ Theory ]
302529 [ MemberData ( nameof ( FactoryMethodsFromTuples ) ) ]
303530 public void FactoryMethod_ReturnsCorrectResultType ( Expression < Func < IResult > > expression , Type expectedReturnType )
0 commit comments