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

            handler2.Should().BeSameAs(handler1);
        }
        public void Should_return_different_handlers_for_different_settings()
        {
            var settings2 = new SocketsTransportSettings {
                ConnectionLifetime = 5.Hours()
            };
            var provider2 = new SocketsHandlerProvider(settings2);

            var handler1 = provider.Obtain(null);
            var handler2 = provider2.Obtain(null);

            handler2.Should().NotBeSameAs(handler1);
        }
Exemple #3
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);
                    if (state.Request.Content is GenericContent content && socketTuner.CanTune)
                    {
                        state.Request.Content = new SocketTuningContent(content, socketTuner, log);
                    }

                    var handler = handlerProvider.Obtain(connectionTimeout);

                    state.Response = await SocketsHandlerInvoker.Invoke(handler, state.Request, 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, connectionTimeout);
                if (errorResponse == null)
                {
                    throw;
                }

                return(errorResponse);
            }
        }
        public void Should_return_a_handler_with_proxy_from_settings()
        {
            var proxy = Substitute.For <IWebProxy>();

            settings.Proxy = proxy;

            provider.Obtain(null).Proxy.Should().BeSameAs(proxy);
        }