Esempio n. 1
0
    public async Task HttpOptionsSetOntoHttpClientHandler()
    {
        var testHttpHandler = TestHttpMessageHandler.CreateDefault();

        var negotiateUrlTcs = new TaskCompletionSource <string>();

        testHttpHandler.OnNegotiate((request, cancellationToken) =>
        {
            negotiateUrlTcs.TrySetResult(request.RequestUri.ToString());
            return(ResponseUtils.CreateResponse(HttpStatusCode.OK,
                                                ResponseUtils.CreateNegotiationContent()));
        });

        HttpClientHandler httpClientHandler = null;

        var httpOptions = new HttpConnectionOptions();

        httpOptions.Url = new Uri("http://fakeuri.org/");
        httpOptions.HttpMessageHandlerFactory = inner =>
        {
            httpClientHandler = (HttpClientHandler)inner;
            return(testHttpHandler);
        };
        httpOptions.Cookies.Add(new Cookie("Name", "Value", string.Empty, "fakeuri.org"));
        var clientCertificate = new X509Certificate(Array.Empty <byte>());

        httpOptions.ClientCertificates.Add(clientCertificate);
        httpOptions.UseDefaultCredentials = false;
        httpOptions.Credentials           = Mock.Of <ICredentials>();
        httpOptions.Proxy      = Mock.Of <IWebProxy>();
        httpOptions.Transports = HttpTransportType.LongPolling;

        await WithConnectionAsync(
            CreateConnection(httpOptions),
            async (connection) =>
        {
            await connection.StartAsync().DefaultTimeout();
        });

        Assert.NotNull(httpClientHandler);
        Assert.Equal(1, httpClientHandler.CookieContainer.Count);
        Assert.Single(httpClientHandler.ClientCertificates);
        Assert.Same(clientCertificate, httpClientHandler.ClientCertificates[0]);
        Assert.False(httpClientHandler.UseDefaultCredentials);
        Assert.Same(httpOptions.Proxy, httpClientHandler.Proxy);
        Assert.Same(httpOptions.Credentials, httpClientHandler.Credentials);
    }
    public async Task SendsDeleteRequestWhenTransportCompleted()
    {
        var handler = TestHttpMessageHandler.CreateDefault();

        using (var httpClient = new HttpClient(handler))
        {
            var longPollingTransport = new LongPollingTransport(httpClient);

            await longPollingTransport.StartAsync(TestUri, TransferFormat.Binary);

            await longPollingTransport.StopAsync();

            var deleteRequest = handler.ReceivedRequests.SingleOrDefault(r => r.Method == HttpMethod.Delete);
            Assert.NotNull(deleteRequest);
            Assert.Equal(TestUri, deleteRequest.RequestUri);
        }
    }
Esempio n. 3
0
    private static HttpConnection CreateConnection(
        HttpMessageHandler httpHandler = null,
        ILoggerFactory loggerFactory   = null,
        string url           = null,
        ITransport transport = null,
        ITransportFactory transportFactory        = null,
        HttpTransportType?transportType           = null,
        TransferFormat transferFormat             = TransferFormat.Text,
        Func <Task <string> > accessTokenProvider = null)
    {
        var httpOptions = new HttpConnectionOptions
        {
            Transports = transportType ?? HttpTransportType.LongPolling,
            HttpMessageHandlerFactory = (httpMessageHandler) => httpHandler ?? TestHttpMessageHandler.CreateDefault(),
            AccessTokenProvider       = accessTokenProvider,
        };

        if (url != null)
        {
            httpOptions.Url = new Uri(url);
        }

        return(CreateConnection(httpOptions, loggerFactory, transport, transportFactory, transferFormat));
    }