private async Task <HttpResponseMessage> SendInternalAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (_useCookies)
            {
                string cookieHeader = _cookieContainer.GetCookieHeader(request.RequestUri);
                if (!string.IsNullOrEmpty(cookieHeader))
                {
                    request.Headers.Add("Cookie", cookieHeader);
                }
            }

            var state = new RequestState(request, cancellationToken);

            HttpContent requestContent = request.Content ?? new StreamContent(Stream.Null);
            Stream      body           = await requestContent.ReadAsStreamAsync().NotOnCapturedContext();

            if (body.CanSeek)
            {
                // This body may have been consumed before, rewind it.
                body.Seek(0, SeekOrigin.Begin);
            }
            state.OwinContext.Request.Body = body;
            CancellationTokenRegistration registration = cancellationToken.Register(state.Abort);

            // Async offload, don't let the test code block the caller.
            Task offload = Task.Run(async() =>
            {
                try
                {
                    await _appFunc(state.Environment).NotOnCapturedContext();
                    state.CompleteResponse();
                }
                catch (Exception ex)
                {
                    state.Abort(ex);
                }
                finally
                {
                    registration.Dispose();
                    state.Dispose();
                }
            }, cancellationToken);

            HttpResponseMessage response = await state.ResponseTask.NotOnCapturedContext();

            if (_useCookies && response.Headers.Contains("Set-Cookie"))
            {
                string cookieHeader = string.Join(",", response.Headers.GetValues("Set-Cookie"));
                _cookieContainer.SetCookies(request.RequestUri, cookieHeader);
            }
            return(response);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This adapts HttpRequestMessages to OWIN requests, dispatches them through the OWIN pipeline, and returns the
        /// associated HttpResponseMessage.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var         state          = new RequestState(request, cancellationToken);
            HttpContent requestContent = request.Content ?? new StreamContent(Stream.Null);

            return(requestContent.ReadAsStreamAsync().Then(
                       body =>
            {
                if (body.CanSeek)
                {
                    // This body may have been consumed before, rewind it.
                    body.Seek(0, SeekOrigin.Begin);
                }
                state.OwinContext.Request.Body = body;
                CancellationTokenRegistration registration = cancellationToken.Register(state.Abort);

                // Async offload, don't let the test code block the caller.
                Task.Factory.StartNew(() =>
                {
                    _next(state.Environment)
                    .Then(() =>
                    {
                        state.CompleteResponse();
                    })
                    .Catch(errorInfo =>
                    {
                        state.Abort(errorInfo.Exception);
                        return errorInfo.Handled();
                    })
                    .Finally(() =>
                    {
                        registration.Dispose();
                        state.Dispose();
                    });
                })
                .Catch(errorInfo =>
                {
                    state.Abort(errorInfo.Exception);
                    state.Dispose();
                    return errorInfo.Handled();
                });

                return state.ResponseTask;
            }));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This adapts HttpRequestMessages to ASP.NET Core requests, dispatches them through the pipeline, and returns the
        /// associated HttpResponseMessage.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var state          = new RequestState(request, _pathBase, _application);
            var requestContent = request.Content ?? new StreamContent(Stream.Null);
            var body           = await requestContent.ReadAsStreamAsync();

            if (body.CanSeek)
            {
                // This body may have been consumed before, rewind it.
                body.Seek(0, SeekOrigin.Begin);
            }
            state.Context.HttpContext.Request.Body = body;
            var registration = cancellationToken.Register(state.AbortRequest);

            // Async offload, don't let the test code block the caller.
            var offload = Task.Factory.StartNew(async() =>
            {
                try
                {
                    await _application.ProcessRequestAsync(state.Context);
                    state.CompleteResponse();
                    state.ServerCleanup(exception: null);
                }
                catch (Exception ex)
                {
                    state.Abort(ex);
                    state.ServerCleanup(ex);
                }
                finally
                {
                    registration.Dispose();
                }
            });

            return(await state.ResponseTask.ConfigureAwait(false));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This adapts HttpRequestMessages to OWIN requests, dispatches them through the OWIN pipeline, and returns the
        /// associated HttpResponseMessage.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var         state          = new RequestState(request, cancellationToken);
            HttpContent requestContent = request.Content ?? new StreamContent(Stream.Null);
            Stream      body           = await requestContent.ReadAsStreamAsync();

            if (body.CanSeek)
            {
                // This body may have been consumed before, rewind it.
                body.Seek(0, SeekOrigin.Begin);
            }
            state.OwinContext.Request.Body = body;
            CancellationTokenRegistration registration = cancellationToken.Register(state.Abort);

            // Async offload, don't let the test code block the caller.
            Task offload = Task.Factory.StartNew(async() =>
            {
                try
                {
                    await _next(state.Environment);
                    state.CompleteResponse();
                }
                catch (Exception ex)
                {
                    state.Abort(ex);
                }
                finally
                {
                    registration.Dispose();
                    state.Dispose();
                }
            });

            return(await state.ResponseTask);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// This adapts HttpRequestMessages to ASP.NET requests, dispatches them through the pipeline, and returns the
        /// associated HttpResponseMessage.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var state = new RequestState(request, _pathBase, _application);
            var requestContent = request.Content ?? new StreamContent(Stream.Null);
            var body = await requestContent.ReadAsStreamAsync();
            if (body.CanSeek)
            {
                // This body may have been consumed before, rewind it.
                body.Seek(0, SeekOrigin.Begin);
            }
            state.Context.HttpContext.Request.Body = body;
            var registration = cancellationToken.Register(state.AbortRequest);

            // Async offload, don't let the test code block the caller.
            var offload = Task.Factory.StartNew(async () =>
                {
                    try
                    {
                        await _application.ProcessRequestAsync(state.Context);
                        state.CompleteResponse();
                        state.ServerCleanup(exception: null);
                    }
                    catch (Exception ex)
                    {
                        state.Abort(ex);
                        state.ServerCleanup(ex);
                    }
                    finally
                    {
                        registration.Dispose();
                    }
                });

            return await state.ResponseTask.ConfigureAwait(false);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// This adapts HttpRequestMessages to OWIN requests, dispatches them through the OWIN pipeline, and returns the
        /// associated HttpResponseMessage.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var state = new RequestState(request, cancellationToken);
            HttpContent requestContent = request.Content ?? new StreamContent(Stream.Null);
            Stream body = await requestContent.ReadAsStreamAsync();
            if (body.CanSeek)
            {
                // This body may have been consumed before, rewind it.
                body.Seek(0, SeekOrigin.Begin);
            }
            state.OwinContext.Request.Body = body;
            CancellationTokenRegistration registration = cancellationToken.Register(state.Abort);

            // Async offload, don't let the test code block the caller.
            Task offload = Task.Factory.StartNew(async () =>
                {
                    try
                    {
                        await _next(state.Environment);
                        state.CompleteResponse();
                    }
                    catch (Exception ex)
                    {
                        state.Abort(ex);
                    }
                    finally
                    {
                        registration.Dispose();
                        state.Dispose();
                    }
                });

            return await state.ResponseTask;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// This adapts HttpRequestMessages to ASP.NET requests, dispatches them through the pipeline, and returns the
        /// associated HttpResponseMessage.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task <HttpResponseMessage> SendAsync(
            [NotNull] HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            var state          = new RequestState(request, _pathBase, cancellationToken);
            var requestContent = request.Content ?? new StreamContent(Stream.Null);
            var body           = await requestContent.ReadAsStreamAsync();

            if (body.CanSeek)
            {
                // This body may have been consumed before, rewind it.
                body.Seek(0, SeekOrigin.Begin);
            }
            state.HttpContext.Request.Body = body;
            var registration = cancellationToken.Register(state.Abort);

            // Async offload, don't let the test code block the caller.
            var offload = Task.Factory.StartNew(async() =>
            {
                try
                {
                    await _next(state.FeatureCollection);
                    state.CompleteResponse();
                }
                catch (Exception ex)
                {
                    state.Abort(ex);
                }
                finally
                {
                    registration.Dispose();
                    state.Dispose();
                }
            });

            return(await state.ResponseTask);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// This adapts HttpRequestMessages to ASP.NET requests, dispatches them through the pipeline, and returns the
        /// associated HttpResponseMessage.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task<HttpResponseMessage> SendAsync(
            [NotNull] HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            var state = new RequestState(request, _pathBase, cancellationToken);
            var requestContent = request.Content ?? new StreamContent(Stream.Null);
            var body = await requestContent.ReadAsStreamAsync();
            if (body.CanSeek)
            {
                // This body may have been consumed before, rewind it.
                body.Seek(0, SeekOrigin.Begin);
            }
            state.HttpContext.Request.Body = body;
            var registration = cancellationToken.Register(state.Abort);

            // Async offload, don't let the test code block the caller.
            var offload = Task.Factory.StartNew(async () =>
                {
                    try
                    {
                        await _next(state.FeatureCollection);
                        state.CompleteResponse();
                    }
                    catch (Exception ex)
                    {
                        state.Abort(ex);
                    }
                    finally
                    {
                        registration.Dispose();
                        state.Dispose();
                    }
                });

            return await state.ResponseTask;
        }
        private async Task<HttpResponseMessage> SendInternalAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            if (_useCookies)
            {
                string cookieHeader = _cookieContainer.GetCookieHeader(request.RequestUri);
                if (!string.IsNullOrEmpty(cookieHeader))
                {
                    request.Headers.Add("Cookie", cookieHeader);
                }
            }

            var state = new RequestState(request, cancellationToken);
            HttpContent requestContent = request.Content ?? new StreamContent(Stream.Null);
            Stream body = await requestContent.ReadAsStreamAsync().NotOnCapturedContext();
            if (body.CanSeek)
            {
                // This body may have been consumed before, rewind it.
                body.Seek(0, SeekOrigin.Begin);
            }
            state.OwinContext.Request.Body = body;
            CancellationTokenRegistration registration = cancellationToken.Register(state.Abort);

            // Async offload, don't let the test code block the caller.
            Task offload = Task.Run(async () =>
            {
                try
                {
                    await _appFunc(state.Environment).NotOnCapturedContext();
                    state.CompleteResponse();
                }
                catch (Exception ex)
                {
                    state.Abort(ex);
                }
                finally
                {
                    registration.Dispose();
                    state.Dispose();
                }
            }, cancellationToken);

            HttpResponseMessage response = await state.ResponseTask.NotOnCapturedContext();
            if (_useCookies && response.Headers.Contains("Set-Cookie"))
            {
                string cookieHeader = string.Join(",", response.Headers.GetValues("Set-Cookie"));
                _cookieContainer.SetCookies(request.RequestUri, cookieHeader);
            }
            return response;
        }