public async Task ExecuteRequestMessagesAsync_DisposesResponseInCaseOfException() { 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); }); DefaultODataBatchHandler batchHandler = new DefaultODataBatchHandler(server); ODataBatchRequestItem[] requests = new ODataBatchRequestItem[] { new OperationRequestItem(new HttpRequestMessage(HttpMethod.Get, "http://example.com/")), new OperationRequestItem(new HttpRequestMessage(HttpMethod.Post, "http://example.com/")), new OperationRequestItem(new HttpRequestMessage(HttpMethod.Put, "http://example.com/")), }; await ExceptionAssert.ThrowsAsync <InvalidOperationException>( () => batchHandler.ExecuteRequestMessagesAsync(requests, CancellationToken.None)); Assert.Equal(2, responses.Count); foreach (var response in responses) { Assert.True(response.IsDisposed); } }
public async Task SendRequestAsync_Throws_WhenRequestIsNull() { // Arrange Mock <RequestDelegate> handler = new Mock <RequestDelegate>(); // Act & Assert await ExceptionAssert.ThrowsArgumentNullAsync( () => ODataBatchRequestItem.SendRequestAsync(handler.Object, null, null), "context"); }
public async Task SendRequestAsync_Throws_WhenInvokerIsNull() { // Arrange Mock <HttpContext> httpContext = new Mock <HttpContext>(); // Act & Assert await ExceptionAssert.ThrowsArgumentNullAsync( () => ODataBatchRequestItem.SendRequestAsync(null, httpContext.Object, null), "handler"); }
private void fixRequestUri(ODataBatchRequestItem request) { foreach (HttpRequestMessage req in ((ChangeSetRequestItem)request).Requests) { var oldUri = req.RequestUri; var newUriBuilder = new UriBuilder(oldUri); newUriBuilder.Path = "/odata" + newUriBuilder.Path; req.RequestUri = newUriBuilder.Uri; } }
public void SendMessageAsync_CallsInvoker() { HttpResponseMessage response = new HttpResponseMessage(); HttpMessageInvoker invoker = new HttpMessageInvoker(new MockHttpServer((request) => { return(response); })); var result = ODataBatchRequestItem.SendMessageAsync(invoker, new HttpRequestMessage(HttpMethod.Get, "http://example.com"), CancellationToken.None, new Dictionary <string, string>()).Result; Assert.Same(response, result); }
public async Task SendRequestAsync_CallsHandler() { // Arrange HttpContext context = HttpContextHelper.Create("Get", "http://example.com"); RequestDelegate handler = context => { context.Response.StatusCode = StatusCodes.Status201Created; return(Task.FromResult(context.Response)); }; // Act await ODataBatchRequestItem.SendRequestAsync(handler, context, new Dictionary <string, string>()); // Assert Assert.Equal(StatusCodes.Status201Created, context.Response.StatusCode); }
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()); }
public async Task SendMessageAsync_Resolves_Uri_From_ContentId() { // Arrange DefaultHttpContext context = new DefaultHttpContext(); HttpResponseMessage response = new HttpResponseMessage(); RequestDelegate handler = (c) => { return(Task.FromResult(response)); }; Dictionary <string, string> contentIdLocationMappings = new Dictionary <string, string>(); contentIdLocationMappings.Add("1", "http://localhost:12345/odata/Customers(42)"); Uri unresolvedUri = new Uri("http://localhost:12345/odata/$1/Orders"); context.Request.CopyAbsoluteUrl(unresolvedUri); // Act await ODataBatchRequestItem.SendRequestAsync(handler, context, contentIdLocationMappings); // Assert Assert.Equal("/odata/Customers(42)/Orders", context.Request.Path.ToString()); }
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()); }
public void SendMessageAsync_Throws_WhenRequestIsNull() { Assert.ThrowsArgumentNull( () => ODataBatchRequestItem.SendMessageAsync(new HttpMessageInvoker(new HttpServer()), null, CancellationToken.None, null).Wait(), "request"); }
public void SendMessageAsync_Throws_WhenInvokerIsNull() { Assert.ThrowsArgumentNull( () => ODataBatchRequestItem.SendMessageAsync(null, new HttpRequestMessage(), CancellationToken.None, null).Wait(), "invoker"); }
/// <summary> /// Asynchronously sends the request. /// </summary> /// <param name="handler">The handler for processing a message.</param> /// <returns>The task object that contains the batch response.</returns> public override async Task <ODataBatchResponseItem> SendRequestAsync(RequestDelegate handler) { Ensure.NotNull(handler, nameof(handler)); var changeSetProperty = new RestierChangeSetProperty(this) { ChangeSet = new ChangeSet(), }; SetChangeSetProperty(changeSetProperty); var contentIdToLocationMapping = new Dictionary <string, string>(); var responseTasks = new List <Task <Task <HttpContext> > >(); foreach (var context in this.Contexts) { // Since exceptions may occure before the request is sent to RestierController, // we must catch the exceptions here and call OnChangeSetCompleted, // so as to avoid deadlock mentioned in Github Issue #82. var tcs = new TaskCompletionSource <HttpContext>(); var task = ODataBatchRequestItem.SendRequestAsync(handler, context, contentIdToLocationMapping) .ContinueWith( t => { if (t.Exception != null) { var taskEx = (t.Exception.InnerExceptions != null && t.Exception.InnerExceptions.Count == 1) ? t.Exception.InnerExceptions.First() : t.Exception; changeSetProperty.Exceptions.Add(taskEx); changeSetProperty.OnChangeSetCompleted(); tcs.SetException(taskEx.Demystify()); } else { tcs.SetResult(context); } return(tcs.Task); }, context.RequestAborted); responseTasks.Add(task); } // the responseTasks will be complete after: // - the ChangeSet is submitted // - the responses are created and // - the controller actions have returned await Task.WhenAll(responseTasks).ConfigureAwait(false); var returnContexts = new List <HttpContext>(); foreach (var responseTask in responseTasks) { var returnContext = responseTask.Result.Result; if (returnContext.Response.IsSuccessStatusCode()) { returnContexts.Add(returnContext); } else { returnContexts.Clear(); returnContexts.Add(returnContext); return(new ChangeSetResponseItem(returnContexts)); } } return(new ChangeSetResponseItem(returnContexts)); }
public async Task SendMessageAsync_Throws_WhenRequestIsNull() { await ExceptionAssert.ThrowsArgumentNullAsync( () => ODataBatchRequestItem.SendMessageAsync(new HttpMessageInvoker(new HttpServer()), null, CancellationToken.None, null), "request"); }
public async Task SendMessageAsync_Throws_WhenInvokerIsNull() { await ExceptionAssert.ThrowsArgumentNullAsync( () => ODataBatchRequestItem.SendMessageAsync(null, new HttpRequestMessage(), CancellationToken.None, null), "invoker"); }