Beispiel #1
0
        public async Task ProcessBatchAsync_CallsRegisterForDispose()
        {
            // Arrange
            List <IDisposable> expectedResourcesForDisposal = new List <IDisposable>();
            MockHttpServer     server = new MockHttpServer(request =>
            {
                var tmpContent = new StringContent(String.Empty);
                request.RegisterForDispose(tmpContent);
                expectedResourcesForDisposal.Add(tmpContent);
                return(new HttpResponseMessage {
                    Content = new StringContent(request.RequestUri.AbsoluteUri)
                });
            });
            DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler(server);
            HttpRequestMessage       batchRequest = new HttpRequestMessage(HttpMethod.Post, "http://example.com/$batch")
            {
                Content = new MultipartContent("mixed")
                {
                    ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/"))
                }
            };

            batchRequest.EnableHttpDependencyInjectionSupport();

            // Act
            var response = await batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None);

            var resourcesForDisposal = batchRequest.GetResourcesForDisposal();

            // Assert
            foreach (var expectedResource in expectedResourcesForDisposal)
            {
                Assert.Contains(expectedResource, resourcesForDisposal);
            }
        }
Beispiel #2
0
 public async Task ProcessBatchAsync_Throws_IfRequestIsNull()
 {
     DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler(new HttpServer());
     await ExceptionAssert.ThrowsArgumentNullAsync(
         () => batchHandler.ProcessBatchAsync(null, CancellationToken.None),
         "request");
 }
        public async Task ProcessBatchAsync_Throws_IfContextIsNull()
        {
            // Arrange
            DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler();

            // Act & Assert
            await ExceptionAssert.ThrowsArgumentNullAsync(() => batchHandler.ProcessBatchAsync(null, null), "context");
        }
        public void ProcessBatchAsync_Throws_IfRequestIsNull()
        {
            DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler(new HttpServer());

            Assert.ThrowsArgumentNull(
                () => batchHandler.ProcessBatchAsync(null, CancellationToken.None).Wait(),
                "request");
        }
Beispiel #5
0
        public async Task ProcessBatchAsync_DoesNotCopyContentHeadersToGetAndDelete()
        {
            MockHttpServer server = new MockHttpServer(request =>
            {
                string responseContent = $"{request.Method},{request.Content?.Headers.ContentLength},{request.Content?.Headers.ContentType}";
                return(new HttpResponseMessage {
                    Content = new StringContent(responseContent)
                });
            });
            DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler(server);
            HttpRequestMessage       batchRequest = new HttpRequestMessage(HttpMethod.Post, "http://example.com/$batch")
            {
                Content = new MultipartContent("mixed")
                {
                    ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")),
                    ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Delete, "http://example.com/")),
                    ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")
                    {
                        Content = new StringContent("bar")
                    }),
                }
            };

            batchRequest.EnableHttpDependencyInjectionSupport();

            var response = await batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None);

            var batchContent   = Assert.IsType <ODataBatchContent>(response.Content);
            var subResponses   = batchContent.Responses.ToArray();
            var getResponse    = (OperationResponseItem)subResponses[0];
            var deleteResponse = (OperationResponseItem)subResponses[1];
            var postResponse   = (OperationResponseItem)subResponses[2];

            Assert.Equal(3, subResponses.Length);
            Assert.Equal("GET,0,", await getResponse.Response.Content.ReadAsStringAsync());
            Assert.Equal("DELETE,0,", await deleteResponse.Response.Content.ReadAsStringAsync());
            Assert.Equal("POST,3,text/plain; charset=utf-8", await postResponse.Response.Content.ReadAsStringAsync());
        }
        public async Task ProcessBatchAsync_DoesNotCopyContentHeadersToGetAndDelete()
        {
            // Arrange
            string batchRequest = @"
--40e2c6b6-e6ce-43aa-9985-ddc12dc4bb9b
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 483818399

GET / HTTP/1.1
Host: example.com


--40e2c6b6-e6ce-43aa-9985-ddc12dc4bb9b
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1035669256

DELETE / HTTP/1.1
Host: example.com


--40e2c6b6-e6ce-43aa-9985-ddc12dc4bb9b
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 632310651

POST /values HTTP/1.1
Host: example.com
Content-Type: text/plain; charset=utf-8

bar
--40e2c6b6-e6ce-43aa-9985-ddc12dc4bb9b--
";

            RequestDelegate handler = async context =>
            {
                HttpRequest request         = context.Request;
                string      responseContent = $"{request.Method},{request.ContentLength},{request.ContentType}";
                await context.Response.WriteAsync(responseContent);
            };

            HttpContext httpContext = HttpContextHelper.Create("Post", "http://example.com/$batch");

            byte[] requestBytes = Encoding.UTF8.GetBytes(batchRequest);
            httpContext.Request.Body          = new MemoryStream(requestBytes);
            httpContext.Request.ContentType   = "multipart/mixed;boundary=40e2c6b6-e6ce-43aa-9985-ddc12dc4bb9b";
            httpContext.Request.ContentLength = requestBytes.Length;

            IEdmModel model = new EdmModel();

            httpContext.ODataFeature().PrefixName = "odata";
            httpContext.RequestServices = BuildServiceProvider(opt => opt.AddModel("odata", model));

            DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler();

            httpContext.Response.Body = new MemoryStream();
            batchHandler.PrefixName   = "odata";

            // Act
            await batchHandler.ProcessBatchAsync(httpContext, handler);

            string responseBody = httpContext.Response.ReadBody();

            // Assert
            Assert.NotNull(responseBody);
            Assert.Contains("GET,,", responseBody);
            Assert.Contains("DELETE,,", responseBody);
            Assert.Contains("POST,3,text/plain; charset=utf-8", responseBody);
        }
        public async Task ProcessBatchAsync_ContinueOnError(bool enableContinueOnError, string preferenceHeader, bool hasThirdResponse)
        {
            // Arrange
            RequestDelegate handler = async context =>
            {
                HttpRequest request         = context.Request;
                string      responseContent = request.GetDisplayUrl();
                string      content         = request.ReadBody();
                if (!string.IsNullOrEmpty(content))
                {
                    responseContent += "," + content;
                }

                HttpResponse response = context.Response;
                if (content.Equals("foo"))
                {
                    response.StatusCode = StatusCodes.Status400BadRequest;
                }

                await response.WriteAsync(responseContent);
            };

            DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler();

            HttpContext httpContext = HttpContextHelper.Create("Post", "http://example.com/$batch");

            string batchRequest = @"--d3df74a8-8212-4c2a-b4fb-d713a4ba383e
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1948857409

GET / HTTP/1.1
Host: example.com


--d3df74a8-8212-4c2a-b4fb-d713a4ba383e
Content-Type: multipart/mixed; boundary=""3c4d5753-d325-4806-8c80-38f4a5fbe523""

--3c4d5753-d325-4806-8c80-38f4a5fbe523
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1856004745

POST /values HTTP/1.1
Host: example.com
Content-Type: text/plain; charset=utf-8

foo
--3c4d5753-d325-4806-8c80-38f4a5fbe523--

--d3df74a8-8212-4c2a-b4fb-d713a4ba383e
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: -2010824035

POST /values HTTP/1.1
Host: example.com
Content-Type: text/plain; charset=utf-8

bar
--d3df74a8-8212-4c2a-b4fb-d713a4ba383e--
";

            byte[] requestBytes = Encoding.UTF8.GetBytes(batchRequest);
            httpContext.Request.Body          = new MemoryStream(requestBytes);
            httpContext.Request.ContentType   = "multipart/mixed;boundary=\"d3df74a8-8212-4c2a-b4fb-d713a4ba383e\"";
            httpContext.Request.ContentLength = 827;

            IEdmModel model = new EdmModel();

            httpContext.ODataFeature().PrefixName = "odata";
            httpContext.RequestServices = BuildServiceProvider(opt => opt.AddModel("odata", model).EnableContinueOnErrorHeader = enableContinueOnError);

            if (preferenceHeader != null)
            {
                httpContext.Request.Headers.Add("prefer", preferenceHeader);
            }

            httpContext.Response.Body = new MemoryStream();
            batchHandler.PrefixName   = "odata";

            // Act
            await batchHandler.ProcessBatchAsync(httpContext, handler);

            string responseBody = httpContext.Response.ReadBody();

            // Assert
            Assert.NotNull(responseBody);

            // #1 response
            Assert.Contains("http://example.com/", responseBody);

            // #2 bad response
            Assert.Contains("Bad Request", responseBody);
            Assert.Contains("http://example.com/values,foo", responseBody);

            // #3 response
            if (hasThirdResponse)
            {
                Assert.Contains("http://example.com/values,bar", responseBody);
            }
            else
            {
                Assert.DoesNotContain("http://example.com/values,bar", responseBody);
            }
        }
Beispiel #8
0
        public async Task ProcessBatchAsync_ContinueOnError(bool enableContinueOnError, string preferenceHeader, int expectedResponses)
        {
            // Arrange
            MockHttpServer server = new MockHttpServer(async request =>
            {
                string responseContent = request.RequestUri.AbsoluteUri;
                string content         = "";
                if (request.Content != null)
                {
                    content = await request.Content.ReadAsStringAsync();
                    if (!String.IsNullOrEmpty(content))
                    {
                        responseContent += "," + content;
                    }
                }
                HttpResponseMessage responseMessage = new HttpResponseMessage {
                    Content = new StringContent(responseContent)
                };
                if (content.Equals("foo"))
                {
                    responseMessage.StatusCode = HttpStatusCode.BadRequest;
                }
                return(responseMessage);
            });
            DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler(server);
            HttpRequestMessage       batchRequest = new HttpRequestMessage(HttpMethod.Post, "http://example.com/$batch")
            {
                Content = new MultipartContent("mixed")
                {
                    ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")),
                    new MultipartContent("mixed") // ChangeSet
                    {
                        ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")
                        {
                            Content = new StringContent("foo")
                        })
                    },
                    ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")
                    {
                        Content = new StringContent("bar")
                    }),
                }
            };
            var enableContinueOnErrorconfig = new HttpConfiguration();

            enableContinueOnErrorconfig.EnableODataDependencyInjectionSupport();
            enableContinueOnErrorconfig.EnableContinueOnErrorHeader();
            if (enableContinueOnError)
            {
                batchRequest.SetConfiguration(enableContinueOnErrorconfig);
            }
            if (!string.IsNullOrEmpty(preferenceHeader))
            {
                batchRequest.Headers.Add("prefer", preferenceHeader);
            }
            batchRequest.EnableHttpDependencyInjectionSupport();

            // Act
            var response = await batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None);

            var batchContent   = Assert.IsType <ODataBatchContent>(response.Content);
            var batchResponses = batchContent.Responses.ToArray();

            // Assert
            Assert.Equal(expectedResponses, batchResponses.Length);
        }
        public void ProcessBatchAsync_ContinueOnError(bool enableContinueOnError)
        {
            // Arrange
            MockHttpServer server = new MockHttpServer(request =>
            {
                string responseContent = request.RequestUri.AbsoluteUri;
                string content         = "";
                if (request.Content != null)
                {
                    content = request.Content.ReadAsStringAsync().Result;
                    if (!String.IsNullOrEmpty(content))
                    {
                        responseContent += "," + content;
                    }
                }
                HttpResponseMessage responseMessage = new HttpResponseMessage {
                    Content = new StringContent(responseContent)
                };
                if (content.Equals("foo"))
                {
                    responseMessage.StatusCode = HttpStatusCode.BadRequest;
                }
                return(responseMessage);
            });
            DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler(server);
            HttpRequestMessage       batchRequest = new HttpRequestMessage(HttpMethod.Post, "http://example.com/$batch")
            {
                Content = new MultipartContent("mixed")
                {
                    ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")),
                    new MultipartContent("mixed") // ChangeSet
                    {
                        ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")
                        {
                            Content = new StringContent("foo")
                        })
                    },
                    ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")
                    {
                        Content = new StringContent("bar")
                    }),
                }
            };
            var enableContinueOnErrorconfig = new HttpConfiguration();

            enableContinueOnErrorconfig.EnableContinueOnErrorHeader();
            batchRequest.SetConfiguration(enableContinueOnErrorconfig);
            HttpRequestMessage batchRequestWithPrefContinueOnError = new HttpRequestMessage(HttpMethod.Post, "http://example.com/$batch")
            {
                Content = new MultipartContent("mixed")
                {
                    ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")),
                    new MultipartContent("mixed") // ChangeSet
                    {
                        ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")
                        {
                            Content = new StringContent("foo")
                        })
                    },
                    ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Post, "http://example.com/values")
                    {
                        Content = new StringContent("bar")
                    }),
                }
            };

            if (enableContinueOnError)
            {
                batchRequestWithPrefContinueOnError.SetConfiguration(enableContinueOnErrorconfig);
                batchRequestWithPrefContinueOnError.Headers.Add("prefer", "odata.continue-on-error");
            }
            else
            {
                batchRequestWithPrefContinueOnError.SetConfiguration(new HttpConfiguration());
            }

            // Act
            var response       = batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None).Result;
            var batchContent   = Assert.IsType <ODataBatchContent>(response.Content);
            var batchResponses = batchContent.Responses.ToArray();
            var responseWithPrefContinueOnError       = batchHandler.ProcessBatchAsync(batchRequestWithPrefContinueOnError, CancellationToken.None).Result;
            var batchContentWithPrefContinueOnError   = Assert.IsType <ODataBatchContent>(responseWithPrefContinueOnError.Content);
            var batchResponsesWithPrefContinueOnError = batchContentWithPrefContinueOnError.Responses.ToArray();

            // Assert
            Assert.Equal(2, batchResponses.Length);
            Assert.Equal(3, batchResponsesWithPrefContinueOnError.Length);
        }