Skip to content

Commit

Permalink
Fix duplicate batch Cookie header.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsar Nikolay authored and xuzhg committed Dec 23, 2020
1 parent e18111d commit 122cab1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ private static HttpContext CreateHttpContext(HttpContext originalContext)
context.Request.Headers.Add(header.Key, preferencesToInherit);
}
}
else
// do not copy already existing headers, such as Cookie
else if (!context.Request.Headers.ContainsKey(header.Key))
{
context.Request.Headers.Add(header);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,72 @@ public async Task SendAsync_CorrectlyCopiesHeadersToIndividualRequests(
Assert.Contains(deleteRequest, responseContent);
Assert.Contains(postRequest, responseContent);
}

[Fact]
public async Task SendAsync_CorrectlyHandlesCookieHeader()
{
var batchRef = $"batch_{Guid.NewGuid()}";
var changesetRef = $"changeset_{Guid.NewGuid()}";
var endpoint = "http://localhost";

Type[] controllers = new[] { typeof(BatchTestCustomersController), typeof(BatchTestOrdersController), };
var server = TestServerFactory.Create(controllers, (config) =>
{
var builder = ODataConventionModelBuilderFactory.Create(config);
builder.EntitySet<BatchTestOrder>("BatchTestOrders");

config.MapODataServiceRoute("odata", null, builder.GetEdmModel(), new DefaultODataBatchHandler());
config.Expand();
config.EnableDependencyInjection();
});

var client = TestServerFactory.CreateClient(server);

var orderId = 2;
var createOrderPayload = $@"{{""@odata.type"":""Microsoft.AspNet.OData.Test.Batch.BatchTestOrder"",""Id"":{orderId},""Amount"":50}}";

var batchRequest = new HttpRequestMessage(HttpMethod.Post, $"{endpoint}/$batch");
batchRequest.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("text/plain"));

// Add cookie (for example IdentityServer adds antiforgery after login)
batchRequest.Headers.TryAddWithoutValidation("Cookie", ".AspNetCore.Antiforgery.9TtSrW0hzOs=" + Guid.NewGuid());

var batchContent = $@"
--{batchRef}
Content-Type: multipart/mixed;boundary={changesetRef}
--{changesetRef}
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1
POST {endpoint}/BatchTestOrders HTTP/1.1
Content-Type: application/json;type=entry
Prefer: return=representation
{createOrderPayload}
--{changesetRef}--
--{batchRef}
Content-Type: application/http
Content-Transfer-Encoding: binary
GET {endpoint}/BatchTestOrders({orderId}) HTTP/1.1
Content-Type: application/json;type=entry
Prefer: return=representation
--{batchRef}--
";

var httpContent = new StringContent(batchContent);
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse($"multipart/mixed;boundary={batchRef}");
httpContent.Headers.ContentLength = batchContent.Length;
batchRequest.Content = httpContent;
var response = await client.SendAsync(batchRequest);

ExceptionAssert.DoesNotThrow(() => response.EnsureSuccessStatusCode());

// TODO: assert somehow?
}
#endif
}

Expand Down Expand Up @@ -810,6 +876,12 @@ public IEnumerable<BatchTestOrder> Get()
return BatchTestOrder.Orders;
}

[EnableQuery]
public SingleResult<BatchTestOrder> Get([FromODataUri]int key)
{
return SingleResult.Create(BatchTestOrder.Orders.Where(d => d.Id.Equals(key)).AsQueryable());
}

public ITestActionResult Post([FromBody]BatchTestOrder order)
{
BatchTestOrder.Orders.Add(order);
Expand Down

0 comments on commit 122cab1

Please sign in to comment.