@@ -1154,6 +1154,71 @@ public async Task OnActionExecuted_SingleResult_WithMoreThanASingleQueryResult_R
11541154 responseString ) ;
11551155 }
11561156
1157+ #if ! NETCORE
1158+ [ Fact ]
1159+ public void OnActionExecuted_UseCachedODataQueryOptions ( )
1160+ {
1161+ var model = new CustomersModelWithInheritance ( ) ;
1162+ model . Model . SetAnnotationValue ( model . Customer , new ClrTypeAnnotation ( typeof ( Customer ) ) ) ;
1163+
1164+ Customer customer = new Customer ( ) ;
1165+ SingleResult singleResult = new SingleResult < Customer > ( new Customer [ ] { customer } . AsQueryable ( ) ) ;
1166+ HttpActionExecutedContext actionExecutedContext = GetActionExecutedContext ( "http://localhost/" , singleResult ) ;
1167+
1168+ ODataQueryOptions actualQueryOptions = null ;
1169+ ODataQueryOptions expectedQueryOptions = new ODataQueryOptions (
1170+ new ODataQueryContext ( model . Model , typeof ( Customer ) ) ,
1171+ actionExecutedContext . Request ) ;
1172+
1173+ actionExecutedContext . Request . SetODataQueryOptions ( expectedQueryOptions ) ;
1174+
1175+ var mockAttribute = new Mock < EnableQueryAttribute >
1176+ {
1177+ CallBase = true ,
1178+ } ;
1179+ mockAttribute
1180+ . Setup ( x => x . ValidateQuery ( It . IsAny < HttpRequestMessage > ( ) , It . IsAny < ODataQueryOptions > ( ) ) )
1181+ . Callback < HttpRequestMessage , ODataQueryOptions > ( ( r , o ) => { actualQueryOptions = o ; } ) ;
1182+
1183+ mockAttribute . Object . OnActionExecuted ( actionExecutedContext ) ;
1184+
1185+ Assert . Same ( expectedQueryOptions , actualQueryOptions ) ;
1186+ }
1187+
1188+ [ Fact ]
1189+ public void OnActionExecuted_UseCachedODataQueryOptionsDisabled ( )
1190+ {
1191+ var model = new CustomersModelWithInheritance ( ) ;
1192+ model . Model . SetAnnotationValue ( model . Customer , new ClrTypeAnnotation ( typeof ( Customer ) ) ) ;
1193+
1194+ Customer customer = new Customer ( ) ;
1195+ SingleResult singleResult = new SingleResult < Customer > ( new Customer [ ] { customer } . AsQueryable ( ) ) ;
1196+ HttpActionExecutedContext actionExecutedContext = GetActionExecutedContext (
1197+ "http://localhost/" ,
1198+ singleResult ,
1199+ CompatibilityOptions . DisableODataQueryOptionsReuse ) ;
1200+
1201+ ODataQueryOptions actualQueryOptions = null ;
1202+ ODataQueryOptions expectedQueryOptions = new ODataQueryOptions (
1203+ new ODataQueryContext ( model . Model , typeof ( Customer ) ) ,
1204+ actionExecutedContext . Request ) ;
1205+
1206+ actionExecutedContext . Request . SetODataQueryOptions ( expectedQueryOptions ) ;
1207+
1208+ var mockAttribute = new Mock < EnableQueryAttribute >
1209+ {
1210+ CallBase = true ,
1211+ } ;
1212+ mockAttribute
1213+ . Setup ( x => x . ValidateQuery ( It . IsAny < HttpRequestMessage > ( ) , It . IsAny < ODataQueryOptions > ( ) ) )
1214+ . Callback < HttpRequestMessage , ODataQueryOptions > ( ( r , o ) => { actualQueryOptions = o ; } ) ;
1215+
1216+ mockAttribute . Object . OnActionExecuted ( actionExecutedContext ) ;
1217+
1218+ Assert . NotSame ( expectedQueryOptions , actualQueryOptions ) ;
1219+ }
1220+ #endif
1221+
11571222 [ Theory ]
11581223 [ InlineData ( "$filter=ID eq 1" ) ]
11591224 [ InlineData ( "$orderby=ID" ) ]
@@ -1238,14 +1303,23 @@ private void SomeAction()
12381303 {
12391304 }
12401305
1241- private static HttpActionExecutedContext GetActionExecutedContext < TResponse > ( string uri , TResponse result )
1306+ private static HttpActionExecutedContext GetActionExecutedContext < TResponse > (
1307+ string uri ,
1308+ TResponse result ,
1309+ CompatibilityOptions ? compatibilityOptions = null )
12421310 {
12431311 var request = new HttpRequestMessage ( HttpMethod . Get , uri ) ;
12441312 request . EnableODataDependencyInjectionSupport ( ) ;
12451313 var actionContext = ContextUtil . CreateActionContext ( ContextUtil . CreateControllerContext ( request : request ) ) ;
12461314 var response = request . CreateResponse < TResponse > ( HttpStatusCode . OK , result ) ;
12471315 var actionExecutedContext = new HttpActionExecutedContext { ActionContext = actionContext , Response = response } ;
12481316 actionContext . ActionDescriptor . Configuration = request . GetConfiguration ( ) ;
1317+
1318+ if ( compatibilityOptions . HasValue )
1319+ {
1320+ actionContext . ActionDescriptor . Configuration . SetCompatibilityOptions ( compatibilityOptions . GetValueOrDefault ( ) ) ;
1321+ }
1322+
12491323 return actionExecutedContext ;
12501324 }
12511325#endif
0 commit comments