1
+ #if NETCORE
2
+ using System . Net . Http ;
3
+ using System . Threading . Tasks ;
4
+ using Microsoft . AspNet . OData . Batch ;
5
+ using Microsoft . AspNet . OData . Extensions ;
6
+ using Microsoft . AspNet . OData . Test . Abstraction ;
7
+ using Microsoft . AspNetCore . Http ;
8
+ using Microsoft . AspNetCore . Routing ;
9
+ using Microsoft . Extensions . DependencyInjection ;
10
+ using Microsoft . OData ;
11
+ using Xunit ;
12
+ using ServiceLifetime = Microsoft . OData . ServiceLifetime ;
13
+
14
+ namespace Microsoft . AspNet . OData . Test . Batch
15
+ {
16
+ public class ODataBatchMiddlewareTests
17
+ {
18
+ private const string URI = "http://localhost/$batch" ;
19
+
20
+ private readonly ODataBatchMiddleware _batchMiddleware ;
21
+ private readonly IRouteBuilder _routeBuilder ;
22
+
23
+ public ODataBatchMiddlewareTests ( )
24
+ {
25
+ _batchMiddleware = new ODataBatchMiddleware ( CorsDelegate ) ;
26
+ _routeBuilder = RoutingConfigurationFactory . CreateWithRootContainer ( "odata" ) ;
27
+ _routeBuilder . MapODataServiceRoute ( "odata" , "odata" , b =>
28
+ {
29
+ b . AddService < ODataBatchHandler > ( ServiceLifetime . Singleton ,
30
+ implementationFactory => new TestODataBatchHandler ( ) ) ;
31
+ } ) ;
32
+ }
33
+
34
+ private HttpRequest CreateRequest ( HttpMethod method )
35
+ {
36
+
37
+ HttpRequest request = RequestFactory . Create ( method , URI , this . _routeBuilder , "odata" ) ;
38
+ ODataBatchPathMapping batchMapping =
39
+ request . HttpContext . RequestServices . GetRequiredService < ODataBatchPathMapping > ( ) ;
40
+ batchMapping . AddRoute ( "odata" , "/$batch" ) ;
41
+ return request ;
42
+ }
43
+
44
+ [ Fact ]
45
+ public async Task BatchMiddlewareShouldNotHandlePreflightRequests ( )
46
+ {
47
+ var request = CreateRequest ( HttpMethod . Options ) ;
48
+
49
+ await _batchMiddleware . Invoke ( request . HttpContext ) ;
50
+
51
+ Assert . True ( request . HttpContext . Items . ContainsKey ( "TestKey" ) ) ;
52
+ }
53
+
54
+ [ Fact ]
55
+ public async Task BatchMiddlewareShouldWorkNormallyForNonPreflightRequests ( )
56
+ {
57
+ var request = CreateRequest ( HttpMethod . Post ) ;
58
+ await _batchMiddleware . Invoke ( request . HttpContext ) ;
59
+
60
+ Assert . False ( request . HttpContext . Items . ContainsKey ( "TestKey" ) ) ;
61
+ }
62
+
63
+
64
+
65
+ private Task CorsDelegate ( HttpContext context )
66
+ {
67
+ // This mocks the execution of the cors middleware which should be registered for cors to work normally.
68
+ // the ODatabatch middleware should not process the request but instead pass it on to the next middleware for cors to work as expected.
69
+ // https://github.com/dotnet/aspnetcore/blob/master/src/Middleware/CORS/src/Infrastructure/CorsMiddleware.cs
70
+ context . Items . Add ( "TestKey" , "TestValue" ) ;
71
+ return Task . CompletedTask ;
72
+ }
73
+
74
+ }
75
+
76
+ public class TestODataBatchHandler : DefaultODataBatchHandler
77
+ {
78
+ public override Task ProcessBatchAsync ( HttpContext context , RequestDelegate nextHandler )
79
+ {
80
+ return Task . CompletedTask ;
81
+ }
82
+ }
83
+ }
84
+ #endif
0 commit comments