Skip to content

Commit 122cab1

Browse files
Tsar Nikolayxuzhg
authored andcommitted
Fix duplicate batch Cookie header.
1 parent e18111d commit 122cab1

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ private static HttpContext CreateHttpContext(HttpContext originalContext)
269269
context.Request.Headers.Add(header.Key, preferencesToInherit);
270270
}
271271
}
272-
else
272+
// do not copy already existing headers, such as Cookie
273+
else if (!context.Request.Headers.ContainsKey(header.Key))
273274
{
274275
context.Request.Headers.Add(header);
275276
}

test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Batch/DefaultODataBatchHandlerTest.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,72 @@ public async Task SendAsync_CorrectlyCopiesHeadersToIndividualRequests(
709709
Assert.Contains(deleteRequest, responseContent);
710710
Assert.Contains(postRequest, responseContent);
711711
}
712+
713+
[Fact]
714+
public async Task SendAsync_CorrectlyHandlesCookieHeader()
715+
{
716+
var batchRef = $"batch_{Guid.NewGuid()}";
717+
var changesetRef = $"changeset_{Guid.NewGuid()}";
718+
var endpoint = "http://localhost";
719+
720+
Type[] controllers = new[] { typeof(BatchTestCustomersController), typeof(BatchTestOrdersController), };
721+
var server = TestServerFactory.Create(controllers, (config) =>
722+
{
723+
var builder = ODataConventionModelBuilderFactory.Create(config);
724+
builder.EntitySet<BatchTestOrder>("BatchTestOrders");
725+
726+
config.MapODataServiceRoute("odata", null, builder.GetEdmModel(), new DefaultODataBatchHandler());
727+
config.Expand();
728+
config.EnableDependencyInjection();
729+
});
730+
731+
var client = TestServerFactory.CreateClient(server);
732+
733+
var orderId = 2;
734+
var createOrderPayload = $@"{{""@odata.type"":""Microsoft.AspNet.OData.Test.Batch.BatchTestOrder"",""Id"":{orderId},""Amount"":50}}";
735+
736+
var batchRequest = new HttpRequestMessage(HttpMethod.Post, $"{endpoint}/$batch");
737+
batchRequest.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("text/plain"));
738+
739+
// Add cookie (for example IdentityServer adds antiforgery after login)
740+
batchRequest.Headers.TryAddWithoutValidation("Cookie", ".AspNetCore.Antiforgery.9TtSrW0hzOs=" + Guid.NewGuid());
741+
742+
var batchContent = $@"
743+
--{batchRef}
744+
Content-Type: multipart/mixed;boundary={changesetRef}
745+
746+
--{changesetRef}
747+
Content-Type: application/http
748+
Content-Transfer-Encoding: binary
749+
Content-ID: 1
750+
751+
POST {endpoint}/BatchTestOrders HTTP/1.1
752+
Content-Type: application/json;type=entry
753+
Prefer: return=representation
754+
755+
{createOrderPayload}
756+
--{changesetRef}--
757+
--{batchRef}
758+
Content-Type: application/http
759+
Content-Transfer-Encoding: binary
760+
761+
GET {endpoint}/BatchTestOrders({orderId}) HTTP/1.1
762+
Content-Type: application/json;type=entry
763+
Prefer: return=representation
764+
765+
--{batchRef}--
766+
";
767+
768+
var httpContent = new StringContent(batchContent);
769+
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse($"multipart/mixed;boundary={batchRef}");
770+
httpContent.Headers.ContentLength = batchContent.Length;
771+
batchRequest.Content = httpContent;
772+
var response = await client.SendAsync(batchRequest);
773+
774+
ExceptionAssert.DoesNotThrow(() => response.EnsureSuccessStatusCode());
775+
776+
// TODO: assert somehow?
777+
}
712778
#endif
713779
}
714780

@@ -810,6 +876,12 @@ public IEnumerable<BatchTestOrder> Get()
810876
return BatchTestOrder.Orders;
811877
}
812878

879+
[EnableQuery]
880+
public SingleResult<BatchTestOrder> Get([FromODataUri]int key)
881+
{
882+
return SingleResult.Create(BatchTestOrder.Orders.Where(d => d.Id.Equals(key)).AsQueryable());
883+
}
884+
813885
public ITestActionResult Post([FromBody]BatchTestOrder order)
814886
{
815887
BatchTestOrder.Orders.Add(order);

0 commit comments

Comments
 (0)