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?
        }
        public async Task SendAsync_CorrectlyCopiesHeadersToIndividualRequests(
            IEnumerable <string> batchPreferHeaderValues,
            string getRequest,
            string deleteRequest,
            string postRequest)
        {
            var batchRef               = $"batch_{Guid.NewGuid()}";
            var changesetRef           = $"changeset_{Guid.NewGuid()}";
            var endpoint               = "http://localhost";
            var acceptJsonFullMetadata = "application/json;odata.metadata=minimal";
            var postPayload            = "Bar";

            Type[] controllers = new[] { typeof(BatchTestHeadersCustomersController) };
            var    server      = TestServerFactory.Create(controllers, (config) =>
            {
                var builder = ODataConventionModelBuilderFactory.Create(config);
                builder.EntitySet <BatchTestHeadersCustomer>("BatchTestHeadersCustomers");

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

            var client = TestServerFactory.CreateClient(server);

            var batchRequest = new HttpRequestMessage(HttpMethod.Post, $"{endpoint}/$batch");

            batchRequest.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("multipart/mixed"));
            batchRequest.Headers.Add("Prefer", batchPreferHeaderValues);

            var batchContent = $@"
--{batchRef}
Content-Type: application/http
Content-Transfer-Encoding: binary

GET {endpoint}/BatchTestHeadersCustomers HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8


--{batchRef}
Content-Type: application/http
Content-Transfer-Encoding: binary

DELETE {endpoint}/BatchTestHeadersCustomers(1) HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
Prefer: wait=100,handling=lenient


--{batchRef}
Content-Type: application/http
Content-Transfer-Encoding: binary

POST {endpoint}/BatchTestHeadersCustomers HTTP/1.1
OData-Version: 4.0;NetFx
OData-MaxVersion: 4.0;NetFx
Content-Type: text/plain; charset=utf-8
Content-Length: {postPayload.Length}
Accept: {acceptJsonFullMetadata}
Accept-Charset: UTF-8

{postPayload}
--{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());
            var responseContent = await response.Content.ReadAsStringAsync();

            Assert.Contains(getRequest, responseContent);
            Assert.Contains(deleteRequest, responseContent);
            Assert.Contains(postRequest, responseContent);
        }
        public async Task SendAsync_Works_ForBatchRequestWithInsertedEntityReferencedInAnotherRequest()
        {
            const string acceptJsonFullMetadata = "application/json;odata.metadata=minimal";
            const string acceptJson             = "application/json";

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

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

            var client = TestServerFactory.CreateClient(server);

            var endpoint = "http://localhost";

            var batchRef     = $"batch_{Guid.NewGuid()}";
            var changesetRef = $"changeset_{Guid.NewGuid()}";

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

            var batchRequest = new HttpRequestMessage(HttpMethod.Post, $"{endpoint}/$batch");

            batchRequest.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("multipart/mixed"));
            HttpContent httpContent = new StringContent($@"
--{batchRef}
Content-Type: multipart/mixed; boundary={changesetRef}

--{changesetRef}
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 3

POST {endpoint}/BatchTestOrders HTTP/1.1
OData-Version: 4.0;NetFx
OData-MaxVersion: 4.0;NetFx
Content-Type: {acceptJsonFullMetadata}
Accept: {acceptJsonFullMetadata}
Accept-Charset: UTF-8

{createOrderPayload}
--{changesetRef}
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 4

POST {endpoint}/BatchTestCustomers(2)/Orders/$ref HTTP/1.1
OData-Version: 4.0;NetFx
OData-MaxVersion: 4.0;NetFx
Content-Type: {acceptJsonFullMetadata}
Accept: {acceptJsonFullMetadata}
Accept-Charset: UTF-8

{createRefPayload}
--{changesetRef}--
--{batchRef}--
");

            httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse($"multipart/mixed; boundary={batchRef}");
            batchRequest.Content            = httpContent;

            var response = await client.SendAsync(batchRequest);

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

            HttpRequestMessage customerRequest = new HttpRequestMessage(HttpMethod.Get, $"{endpoint}/BatchTestCustomers(2)?$expand=Orders");

            customerRequest.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(acceptJson));

            var customerResponse = client.SendAsync(customerRequest).Result;
            var objAsJsonString  = await customerResponse.Content.ReadAsStringAsync();

            var customer = JsonConvert.DeserializeObject <BatchTestCustomer>(objAsJsonString);

            Assert.NotNull(customer.Orders?.SingleOrDefault(d => d.Id.Equals(orderId)));
        }
 public CucumberTestsStepDefinitions() : base()
 {
     factory = new TestServerFactory();
     client  = factory.CreateClient();
 }
Example #5
0
        public async Task Get_OpenEntityTypeWithMultiplePropertySelect(string select)
        {
            // Arrange
            string requestUri  = "http://localhost/odata/SimpleOpenCustomers?" + select;
            var    controllers = new[] { typeof(SimpleOpenCustomersController) };
            var    server      = TestServerFactory.Create(controllers, (config) =>
            {
                config.Select();
                config.MapODataServiceRoute("odata", "odata", GetEdmModel());
            });
            var client = TestServerFactory.CreateClient(server);

            // Act
            HttpRequestMessage  request  = new HttpRequestMessage(HttpMethod.Get, requestUri);
            HttpResponseMessage response = await client.SendAsync(request);

            // Assert
            //  Assert.True(response.IsSuccessStatusCode);
            JObject result      = JObject.Parse(await response.Content.ReadAsStringAsync());
            var     resultArray = result["value"] as JArray;

            Assert.Equal(6, resultArray.Count);
            Assert.Equal(@"[
  {
    ""Address"": {
      ""Street"": ""Street 0"",
      ""City"": ""City 0""
    }
  },
  {
    ""Address"": {
      ""Street"": ""Street 1"",
      ""City"": ""City 1""
    }
  },
  {
    ""Address"": {
      ""Street"": ""Street 2"",
      ""City"": ""City 2""
    }
  },
  {
    ""Address"": {
      ""Street"": ""Street 3"",
      ""City"": ""City 3""
    }
  },
  {
    ""Address"": {
      ""Street"": ""Street 4"",
      ""City"": ""City 4""
    }
  },
  {
    ""@odata.type"": ""#Microsoft.AspNet.OData.Test.Common.SimpleVipCustomer"",
    ""Address"": {
      ""Street"": ""Vip Street "",
      ""City"": ""Vip City ""
    }
  }
]", resultArray.ToString());
        }
Example #6
0
 public static void Setup(TestContext tc)
 {
     factory = new TestServerFactory();
     client  = factory.CreateClient();
 }