public void Should_cache_handler_in_one_instance()
        {
            var handler1 = provider.Obtain(connectionTimeout);
            var handler2 = provider.Obtain(connectionTimeout);

            handler2.Should().BeSameAs(handler1);
        }
Exemple #2
0
        private async Task <Response> SendAsync(Request request, TimeSpan?connectionTimeout, CancellationToken token)
        {
            try
            {
                using (var state = new DisposableState())
                {
                    state.Request = RequestMessageFactory.Create(request, token, log);

                    var client = clientProvider.Obtain(connectionTimeout);

                    state.Response = await client.SendAsync(state.Request, HttpCompletionOption.ResponseHeadersRead, token).ConfigureAwait(false);

                    var responseCode    = (ResponseCode)(int)state.Response.StatusCode;
                    var responseHeaders = ResponseHeadersConverter.Convert(state.Response);

                    if (request.Method == RequestMethods.Head)
                    {
                        return(new Response(responseCode, headers: responseHeaders));
                    }

                    var bodyReadResult = await bodyReader.ReadAsync(state.Response, token).ConfigureAwait(false);

                    if (bodyReadResult.ErrorCode.HasValue)
                    {
                        return(new Response(bodyReadResult.ErrorCode.Value));
                    }

                    if (bodyReadResult.Stream == null)
                    {
                        return(new Response(responseCode, bodyReadResult.Content, responseHeaders));
                    }

                    state.PreventNextDispose();

                    return(new Response(responseCode, null, responseHeaders, new DisposableBodyStream(bodyReadResult.Stream, state)));
                }
            }
            catch (Exception error)
            {
                var errorResponse = errorHandler.TryHandle(request, error, token);
                if (errorResponse == null)
                {
                    throw;
                }

                return(errorResponse);
            }
        }