55// </copyright>
66//------------------------------------------------------------------------------
77
8+ #if NETCORE
9+ using System ;
10+ using System . Collections . Generic ;
11+ using System . Linq ;
12+ using System . Net ;
13+ using System . Net . Http ;
14+ using System . Text ;
15+ using System . Threading . Tasks ;
16+ using Microsoft . AspNetCore . Mvc ;
17+ using Microsoft . AspNet . OData . Builder ;
18+ using Microsoft . AspNet . OData . Extensions ;
19+ using Microsoft . AspNet . OData . Query ;
20+ using Microsoft . AspNet . OData . Test . Abstraction ;
21+ using Microsoft . AspNet . OData . Test . Common ;
22+ using Microsoft . OData . Edm ;
23+ using Xunit ;
24+ #else
825using System ;
926using System . Collections . Generic ;
1027using System . Linq ;
1128using System . Net ;
1229using System . Net . Http ;
30+ using System . Text ;
1331using System . Threading . Tasks ;
32+ using System . Web . Http ;
1433using Microsoft . AspNet . OData . Builder ;
1534using Microsoft . AspNet . OData . Extensions ;
1635using Microsoft . AspNet . OData . Query ;
1736using Microsoft . AspNet . OData . Test . Abstraction ;
1837using Microsoft . AspNet . OData . Test . Common ;
1938using Microsoft . OData . Edm ;
2039using Xunit ;
40+ #endif
2141
2242namespace Microsoft . AspNet . OData . Test
2343{
@@ -452,13 +472,17 @@ public async Task EnableQuery_DoesNotBlockQueries_WhenEverythingIsAllowed(string
452472
453473 [ Theory ]
454474 [ MemberData ( nameof ( AutoExpandedTestData ) ) ]
455- public async Task EnableQuery_Works_WithAutoExpanded ( string queryString )
475+ public async Task EnableQuery_Works_WithAutoExpand ( string queryString )
456476 {
457477 // Arrange
458478 string url = "http://localhost/odata/AutoExpandedCustomers" ;
459479 Type [ ] controllers = new Type [ ] { typeof ( AutoExpandedCustomersController ) } ;
480+
460481 ODataModelBuilder builder = ODataConventionModelBuilderFactory . Create ( ) ;
461482 builder . EntitySet < AutoExpandedCustomer > ( "AutoExpandedCustomers" ) ;
483+ builder . EntitySet < EnableQueryCategory > ( "EnableQueryCategories" ) ;
484+ builder . EntityType < PremiumEnableQueryCategory > ( ) ;
485+
462486 IEdmModel model = builder . GetEdmModel ( ) ;
463487 var server = TestServerFactory . Create ( controllers , ( config ) =>
464488 {
@@ -478,6 +502,59 @@ public async Task EnableQuery_Works_WithAutoExpanded(string queryString)
478502 Assert . Contains ( "5678" , responseString ) ;
479503 }
480504
505+ [ Theory ]
506+ [ InlineData ( false , false ) ]
507+ [ InlineData ( false , true ) ]
508+ [ InlineData ( true , false ) ]
509+ [ InlineData ( true , true ) ]
510+ public async Task EnableQuery_Works_WithAutoExpandAndOperation ( bool useAction , bool includeQueryString )
511+ {
512+ // Arrange
513+ string baseUrl = "http://localhost/odata/AutoExpandedCustomers/" ;
514+ Type [ ] controllers = new Type [ ] { typeof ( AutoExpandedCustomersController ) } ;
515+
516+ ODataModelBuilder builder = ODataConventionModelBuilderFactory . Create ( ) ;
517+ builder . EntitySet < AutoExpandedCustomer > ( "AutoExpandedCustomers" ) ;
518+ builder . EntitySet < EnableQueryCategory > ( "EnableQueryCategories" ) ;
519+ builder . EntityType < PremiumEnableQueryCategory > ( ) ;
520+
521+ builder . EntityType < AutoExpandedCustomer > ( ) . Collection . Action ( "GetCategoryViaAction" )
522+ . ReturnsFromEntitySet < EnableQueryCategory > ( "EnableQueryCategories" )
523+ . Parameter ( typeof ( int ) , "id" ) ;
524+ builder . EntityType < AutoExpandedCustomer > ( ) . Collection . Function ( "GetCategoryViaFunction" )
525+ . ReturnsFromEntitySet < EnableQueryCategory > ( "EnableQueryCategories" )
526+ . Parameter ( typeof ( int ) , "id" ) ;
527+
528+ IEdmModel model = builder . GetEdmModel ( ) ;
529+ var server = TestServerFactory . Create ( controllers , ( config ) =>
530+ {
531+ config . MapODataServiceRoute ( "odata" , "odata" , model ) ;
532+ config . Count ( ) . OrderBy ( ) . Filter ( ) . Expand ( ) . MaxTop ( null ) . Select ( ) ;
533+ } ) ;
534+
535+ HttpClient client = TestServerFactory . CreateClient ( server ) ;
536+
537+ // Act
538+ HttpResponseMessage response = null ;
539+ if ( useAction )
540+ {
541+ response = await client . PostAsync (
542+ baseUrl + "GetCategoryViaAction" + ( includeQueryString ? "?a=b" : "" ) ,
543+ new StringContent ( "{\" id\" :1}" , Encoding . UTF8 , "application/json" ) ) ;
544+ }
545+ else
546+ {
547+ response = await client . GetAsync ( baseUrl + "GetCategoryViaFunction(id=1)" + ( includeQueryString ? "?a=b" : "" ) ) ;
548+ }
549+
550+ string responseString = await response . Content . ReadAsStringAsync ( ) ;
551+
552+ // Assert
553+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
554+ Assert . Contains ( "1234" , responseString ) ;
555+ Assert . Contains ( "5678" , responseString ) ;
556+ }
557+
481558 [ Theory ]
482559 [ MemberData ( nameof ( UnsupportedDateTimeFunctionsTestData ) ) ]
483560 public async Task EnableQuery_ReturnsBadRequest_ForUnsupportedFunctions ( string queryString , string expectedElement )
@@ -608,6 +685,19 @@ public IQueryable<AutoExpandedCustomer> Get()
608685 return _autoCustomers ;
609686 }
610687
688+ [ EnableQuery ]
689+ [ HttpPost ]
690+ public IQueryable < EnableQueryCategory > GetCategoryViaAction ( ODataActionParameters parameters )
691+ {
692+ return _autoCustomers . Where ( x => x . Id == ( int ) parameters [ "id" ] ) . Select ( x => x . Category ) . AsQueryable ( ) ;
693+ }
694+
695+ [ EnableQuery ]
696+ public IQueryable < EnableQueryCategory > GetCategoryViaFunction ( int id )
697+ {
698+ return _autoCustomers . Where ( x => x . Id == id ) . Select ( x => x . Category ) . AsQueryable ( ) ;
699+ }
700+
611701 static AutoExpandedCustomersController ( )
612702 {
613703 _autoCustomers = CreateAutoExpandedCustomers ( ) . AsQueryable ( ) ;
0 commit comments