예제 #1
0
        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");
        }
예제 #2
0
        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");
        }
예제 #3
0
        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);
        }
예제 #4
0
        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());
        }
예제 #5
0
        /// <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));
        }