コード例 #1
0
        public async Task ExecuteRequestMessagesAsync_CallsInvokerForEachRequest()
        {
            // Arrange
            RequestDelegate handler = async context =>
            {
                HttpRequest request         = context.Request;
                string      responseContent = request.GetDisplayUrl();
                string      content         = request.ReadBody();
                if (!string.IsNullOrEmpty(content))
                {
                    responseContent += "," + content;
                }

                context.Response.Body = new MemoryStream();
                await context.Response.WriteAsync(responseContent);
            };

            ODataBatchRequestItem[] requests = new ODataBatchRequestItem[]
            {
                new OperationRequestItem(HttpContextHelper.Create("Get", "http://example.com/")),
                new ChangeSetRequestItem(new HttpContext[]
                {
                    HttpContextHelper.Create("Post", "http://example.com/values", "foo", "text/plan")
                })
            };
            DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler();

            // Act
            IList <ODataBatchResponseItem> responses = await batchHandler.ExecuteRequestMessagesAsync(requests, handler);

            // Assert
            Assert.Equal(2, responses.Count);

            // #1
            OperationResponseItem response0 = Assert.IsType <OperationResponseItem>(responses[0]);

            Assert.Equal("http://example.com/", response0.Context.Response.ReadBody());

            // #2
            ChangeSetResponseItem response1  = Assert.IsType <ChangeSetResponseItem>(responses[1]);
            HttpContext           subContext = Assert.Single(response1.Contexts);

            Assert.Equal("http://example.com/values,foo", response1.Contexts.First().Response.ReadBody());
        }
コード例 #2
0
        public async Task ExecuteRequestMessagesAsync_CallsInvokerForEachRequest()
        {
            MockHttpServer server = new MockHttpServer(async request =>
            {
                string responseContent = request.RequestUri.AbsoluteUri;
                if (request.Content != null)
                {
                    string content = await request.Content.ReadAsStringAsync();
                    if (!String.IsNullOrEmpty(content))
                    {
                        responseContent += "," + content;
                    }
                }
                return(new HttpResponseMessage {
                    Content = new StringContent(responseContent)
                });
            });
            DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler(server);

            ODataBatchRequestItem[] requests = new ODataBatchRequestItem[]
            {
                new OperationRequestItem(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")),
                new ChangeSetRequestItem(new HttpRequestMessage[]
                {
                    new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")
                    {
                        Content = new StringContent("foo")
                    }
                })
            };

            var responses = await batchHandler.ExecuteRequestMessagesAsync(requests, CancellationToken.None);

            var response0 = (OperationResponseItem)responses[0];
            var response1 = (ChangeSetResponseItem)responses[1];

            Assert.Equal(2, responses.Count);
            Assert.Equal("http://example.com/", await response0.Response.Content.ReadAsStringAsync());
            Assert.Equal("http://example.com/values,foo", await response1.Responses.First().Content.ReadAsStringAsync());
        }