Ejemplo n.º 1
0
        public void ProcessBatchAsync_CallsRegisterForDispose()
        {
            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)
                });
            });
            UnbufferedODataBatchHandler batchHandler = new UnbufferedODataBatchHandler(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/"))
                    }
                }
            };

            var response             = batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None).Result;
            var resourcesForDisposal = batchRequest.GetResourcesForDisposal();

            foreach (var expectedResource in expectedResourcesForDisposal)
            {
                Assert.Contains(expectedResource, resourcesForDisposal);
            }
        }
Ejemplo n.º 2
0
 public async Task ProcessBatchAsync_Throws_IfRequestIsNull()
 {
     UnbufferedODataBatchHandler batchHandler = new UnbufferedODataBatchHandler(new HttpServer());
     await ExceptionAssert.ThrowsArgumentNullAsync(
         () => batchHandler.ProcessBatchAsync(null, CancellationToken.None),
         "request");
 }
Ejemplo n.º 3
0
        public void ProcessBatchAsync_Throws_IfRequestIsNull()
        {
            UnbufferedODataBatchHandler batchHandler = new UnbufferedODataBatchHandler(new HttpServer());

            Assert.ThrowsArgumentNull(
                () => batchHandler.ProcessBatchAsync(null, CancellationToken.None).Wait(),
                "request");
        }
Ejemplo n.º 4
0
        public async Task ProcessBatchAsync_Throws_IfContextIsNull()
        {
            // Arrange
            UnbufferedODataBatchHandler batchHandler = new UnbufferedODataBatchHandler();
            RequestDelegate             next         = null;

            // Act & Assert
            await ExceptionAssert.ThrowsArgumentNullAsync(() => batchHandler.ProcessBatchAsync(null, next), "context");
        }
Ejemplo n.º 5
0
        public async Task ProcessBatchAsync_CallsInvokerForEachRequest()
        {
            // Arrange
            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)
                });
            });
            UnbufferedODataBatchHandler batchHandler = new UnbufferedODataBatchHandler(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")
                        })
                    }
                }
            };

            batchRequest.EnableHttpDependencyInjectionSupport();

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

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

            Assert.Equal(2, batchResponses.Length);
            var response0 = (OperationResponseItem)batchResponses[0];
            var response1 = (ChangeSetResponseItem)batchResponses[1];

            Assert.Equal("http://example.com/", await response0.Response.Content.ReadAsStringAsync());
            Assert.Equal("http://example.com/values,foo", await response1.Responses.First().Content.ReadAsStringAsync());
        }
Ejemplo n.º 6
0
        public async Task ProcessBatchAsync_DisposesResponseInCaseOfException()
        {
            // Arrange
            List <MockHttpResponseMessage> responses = new List <MockHttpResponseMessage>();
            MockHttpServer server = new MockHttpServer(request =>
            {
                if (request.Method == HttpMethod.Put)
                {
                    throw new InvalidOperationException();
                }
                var response = new MockHttpResponseMessage();
                responses.Add(response);
                return(response);
            });
            UnbufferedODataBatchHandler batchHandler = new UnbufferedODataBatchHandler(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")),
                        ODataBatchRequestHelper.CreateODataRequestContent(new HttpRequestMessage(HttpMethod.Put, "http://example.com/values"))
                    }
                }
            };

            batchRequest.EnableHttpDependencyInjectionSupport();

            // Act & Assert
            await ExceptionAssert.ThrowsAsync <InvalidOperationException>(
                () => batchHandler.ProcessBatchAsync(batchRequest, CancellationToken.None));

            Assert.Equal(2, responses.Count);
            foreach (var response in responses)
            {
                Assert.True(response.IsDisposed);
            }
        }
Ejemplo n.º 7
0
        public async Task ProcessBatchAsync_ContinueOnError(bool enableContinueOnError)
        {
            // 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);
            });
            UnbufferedODataBatchHandler batchHandler = new UnbufferedODataBatchHandler(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();
            batchRequest.SetConfiguration(enableContinueOnErrorconfig);
            batchRequest.EnableHttpDependencyInjectionSupport();
            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")
                    }),
                }
            };

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

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

            var batchContent   = Assert.IsType <ODataBatchContent>(response.Content);
            var batchResponses = batchContent.Responses.ToArray();
            var responseWithPrefContinueOnError = await batchHandler.ProcessBatchAsync(batchRequestWithPrefContinueOnError, CancellationToken.None);

            var batchContentWithPrefContinueOnError   = Assert.IsType <ODataBatchContent>(responseWithPrefContinueOnError.Content);
            var batchResponsesWithPrefContinueOnError = batchContentWithPrefContinueOnError.Responses.ToArray();

            // Assert
            Assert.Equal(2, batchResponses.Length);
            Assert.Equal(3, batchResponsesWithPrefContinueOnError.Length);
        }
Ejemplo n.º 8
0
        public async Task ProcessBatchAsync_CallsInvokerForEachRequest()
        {
            // Arrange
            RequestDelegate next = async context =>
            {
                HttpRequest request         = context.Request;
                string      responseContent = request.GetDisplayUrl();
                string      content         = request.ReadBody();
                if (!string.IsNullOrEmpty(content))
                {
                    responseContent += "," + content;
                }

                await context.Response.WriteAsync(responseContent);
            };

            string batchRequest = @"--2d958200-beb1-4437-97c5-9d19f7a1d538
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1301336816

GET / HTTP/1.1
Host: example.com


--2d958200-beb1-4437-97c5-9d19f7a1d538
Content-Type: multipart/mixed; boundary=""6aef4f89-41ba-4da5-b733-9100d2318dfa""

--6aef4f89-41ba-4da5-b733-9100d2318dfa
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1557260198

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

foo
--6aef4f89-41ba-4da5-b733-9100d2318dfa--

--2d958200-beb1-4437-97c5-9d19f7a1d538--
";


            HttpRequest request     = RequestFactory.Create("Post", "http://example.com/$batch", opt => opt.AddModel("odata", EdmCoreModel.Instance));
            HttpContext httpContext = request.HttpContext;

            byte[] requestBytes = Encoding.UTF8.GetBytes(batchRequest);
            request.Body          = new MemoryStream(requestBytes);
            request.ContentType   = "multipart/mixed; boundary=\"2d958200-beb1-4437-97c5-9d19f7a1d538\"";
            request.ContentLength = 603;
            httpContext.ODataFeature().PrefixName = "odata";
            httpContext.Response.Body = new MemoryStream();

            // Act
            UnbufferedODataBatchHandler batchHandler = new UnbufferedODataBatchHandler();

            batchHandler.PrefixName = "odata";
            await batchHandler.ProcessBatchAsync(httpContext, next);

            // Assert
            string responseBody = httpContext.Response.ReadBody();

            Assert.Contains("http://example.com/", responseBody);
            Assert.Contains("http://example.com/values,foo", responseBody);
        }
Ejemplo n.º 9
0
        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);
            };

            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--
";

            HttpRequest request = RequestFactory.Create("Post", "http://example.com/$batch",
                                                        opt => opt.AddModel(EdmCoreModel.Instance).EnableContinueOnErrorHeader = enableContinueOnError);
            HttpContext httpContext = request.HttpContext;

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

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

            httpContext.Response.Body = new MemoryStream();
            UnbufferedODataBatchHandler batchHandler = new UnbufferedODataBatchHandler();

            batchHandler.PrefixName = "";

            // 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);
            }
        }
Ejemplo n.º 10
0
        public async Task ProcessBatchAsync_DisposesResponseInCaseOfException()
        {
            // Arrange
            RequestDelegate handler = context =>
            {
                HttpRequest request = context.Request;
                if (request.Method.ToLowerInvariant() == "put")
                {
                    context.Response.StatusCode = StatusCodes.Status400BadRequest;
                }
                else if (request.Method.ToLowerInvariant() == "post")
                {
                    context.Response.WriteAsync("POSTED VALUE");
                }
                else
                {
                    context.Response.WriteAsync("OTHER VALUE");
                }

                return(Task.FromResult(context.Response));
            };

            UnbufferedODataBatchHandler batchHandler = new UnbufferedODataBatchHandler();
            string      batchRequest = @"
--97a4482b-26c6-4551-aed3-85f8b500bbbf
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 794393547

GET / HTTP/1.1
Host: example.com


--97a4482b-26c6-4551-aed3-85f8b500bbbf
Content-Type: multipart/mixed;boundary=""be13321d-3c7b-4126-aa20-958b2c7a87e0""

--be13321d-3c7b-4126-aa20-958b2c7a87e0
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: -583046506

POST /values HTTP/1.1
Host: example.com


--be13321d-3c7b-4126-aa20-958b2c7a87e0
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: -811158921

PUT /values HTTP/1.1
Host: example.com


--be13321d-3c7b-4126-aa20-958b2c7a87e0--

--97a4482b-26c6-4551-aed3-85f8b500bbbf--
";
            HttpRequest request      = RequestFactory.Create("Post", "http://example.com/$batch", opt => opt.AddModel(EdmCoreModel.Instance));
            HttpContext httpContext  = request.HttpContext;

            byte[] requestBytes = Encoding.UTF8.GetBytes(batchRequest);
            request.Body          = new MemoryStream(requestBytes);
            request.ContentType   = "multipart/mixed;boundary=\"be13321d-3c7b-4126-aa20-958b2c7a87e0\"";
            request.ContentLength = 736;
            httpContext.ODataFeature().PrefixName = "";
            httpContext.Response.Body = new MemoryStream();
            batchHandler.PrefixName   = "";

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

            // Assert
            string responseBody = httpContext.Response.ReadBody();

            Assert.Contains("POSTED VALUE", responseBody);
            Assert.Contains("400 Bad Request", responseBody);
            Assert.DoesNotContain("OTHER VALUE", responseBody);
        }