Skip to content

Commit e18111d

Browse files
authored
Fix behaviour for Preflight requests on the ODataBatchMiddleware. (#2356)
1 parent d52c95b commit e18111d

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

src/Microsoft.AspNetCore.OData/Batch/ODataBatchMiddleware.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ public ODataBatchMiddleware(RequestDelegate next)
3434
/// <returns>A task that can be awaited.</returns>
3535
public async Task Invoke(HttpContext context)
3636
{
37-
// Attempt to match the path to a bach route.
37+
HttpRequest request = context.Request;
38+
bool isPreFlight = HttpMethods.IsOptions(request.Method);
39+
40+
// Attempt to match the path to a batch route.
3841
ODataBatchPathMapping batchMapping = context.RequestServices.GetRequiredService<ODataBatchPathMapping>();
3942

40-
string routeName;
41-
if (batchMapping.TryGetRouteName(context, out routeName))
43+
if (!isPreFlight && batchMapping.TryGetRouteName(context, out string routeName))
4244
{
43-
// Get the per-route continer and retrieve the batch handler.
45+
// Get the per-route container and retrieve the batch handler.
4446
IPerRouteContainer perRouteContainer = context.RequestServices.GetRequiredService<IPerRouteContainer>();
4547
if (perRouteContainer == null)
4648
{
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Microsoft.AspNet.OData.Test.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<Compile Include="$(MSBuildThisFileDirectory)Batch\MockHttpServer.cs" />
1818
<Compile Include="$(MSBuildThisFileDirectory)Batch\ODataBatchContentTest.cs" />
1919
<Compile Include="$(MSBuildThisFileDirectory)Batch\ODataBatchHttpRequestMessageExtensionsTest.cs" />
20+
<Compile Include="$(MSBuildThisFileDirectory)Batch\ODataBatchMiddlewareTests.cs" />
2021
<Compile Include="$(MSBuildThisFileDirectory)Batch\ODataBatchReaderExtensionsTest.cs" />
2122
<Compile Include="$(MSBuildThisFileDirectory)Batch\ODataBatchRequestHelper.cs" />
2223
<Compile Include="$(MSBuildThisFileDirectory)Batch\ODataBatchRequestItemTest.cs" />

0 commit comments

Comments
 (0)