public void InitializeServiceClientWithHttpClient()
        {
            HttpClient           hc            = new HttpClient();
            ContosoServiceClient contosoClient = new ContosoServiceClient(hc);
            HttpResponseMessage  response      = contosoClient.DoSyncWork();

            Assert.NotNull(response);
        }
        public void ProvideHttpClientAfterInitialization()
        {
            ContosoServiceClient contosoClient = new ContosoServiceClient(overRideDefaultHandler: true);
            HttpResponseMessage  response      = contosoClient.DoSyncWork();
            string cont = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.Equal("Delayed User Provided HttpClient after initialization", cont);
        }
        public void InitializeMessageHandlerPostContructor()
        {
            HttpClient           hc            = new HttpClient(new ContosoMessageHandler());
            ContosoServiceClient contosoClient = new ContosoServiceClient(hc);
            HttpResponseMessage  response      = contosoClient.DoSyncWork();
            string cont = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.Equal("Contoso Rocks", cont);
        }
        public void NullReferenceExceptionAfterClientDispose()
        {
            ContosoServiceClient contosoClient = new ContosoServiceClient(null);
            HttpResponseMessage  response      = contosoClient.DoSyncWork();
            string cont = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.NotNull(response);

            contosoClient.Dispose();

            Assert.ThrowsAny <NullReferenceException>(() => SendAndReceiveResponse(contosoClient.HttpClient));
        }
        public void AddFxVersionHeaderInformation()
        {
            Version    defaultVersion = null;
            HttpClient hc             = new HttpClient(new ContosoMessageHandler());

            hc.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("FxVersion", "1.0.0.0"));
            ContosoServiceClient contosoClient = new ContosoServiceClient(hc);
            HttpResponseMessage  response      = contosoClient.DoSyncWork();

            HttpHeaderValueCollection <ProductInfoHeaderValue> userAgentValueCollection = contosoClient.HttpClient.DefaultRequestHeaders.UserAgent;
            ProductInfoHeaderValue fxVer = userAgentValueCollection.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals("FxVersion", StringComparison.OrdinalIgnoreCase)).First <ProductInfoHeaderValue>();

            Version.TryParse(fxVer.Product.Version, out defaultVersion);
            Assert.Equal(defaultVersion.ToString(), "1.0.0.0");
        }
        public void UseHttpClientBeforeConstructingServiceClient()
        {
            HttpClient hc = new HttpClient(new ContosoMessageHandler());

            hc.BaseAddress = new Uri(DEFAULT_URI);
            HttpResponseMessage resMsg = SendAndReceiveResponse(hc);

            Assert.Equal(HttpStatusCode.OK, resMsg.StatusCode);

            ContosoServiceClient contosoClient = new ContosoServiceClient(hc);
            HttpResponseMessage  response      = contosoClient.DoSyncWork();
            string cont = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.Equal("Contoso Rocks", cont);
            Assert.Equal(new Uri(DEFAULT_URI), contosoClient.HttpClient.BaseAddress);
        }
        public void GetBaseClassHttpClient()
        {
            string  defaultProductName = "FxVersion";
            Version defaultProductVer;

            ContosoServiceClient contosoClient = new ContosoServiceClient(null);
            HttpResponseMessage  response      = contosoClient.DoSyncWork();
            string cont = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.NotNull(response);

            HttpHeaderValueCollection <ProductInfoHeaderValue> userAgentValueCollection = contosoClient.HttpClient.DefaultRequestHeaders.UserAgent;
            var dP = userAgentValueCollection.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals(defaultProductName)).FirstOrDefault <ProductInfoHeaderValue>();

            Version.TryParse(dP.Product.Version, out defaultProductVer);
            Assert.Equal(defaultProductName, dP.Product.Name);
            Assert.NotNull(defaultProductVer);
        }
        public void DisposeServiceClientWhileProcessingRequest()
        {
            HttpClient hc = new HttpClient(new DelayedHandler("DelayingResponse", TimeSpan.FromSeconds(5)));

            hc.BaseAddress = new Uri(DEFAULT_URI);
            HttpResponseMessage resMsg = SendAndReceiveResponse(hc);
            string resStr = resMsg.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.Equal("DelayingResponse", resStr);

            ContosoServiceClient contosoClient = new ContosoServiceClient(hc, false);

            var result = Task.Run <HttpResponseMessage>(async() =>
            {
                return(await contosoClient.DoAsyncWork());
            });

            contosoClient.Dispose();

            HttpResponseMessage delayedResponse = result.ConfigureAwait(false).GetAwaiter().GetResult();
            string delayedContent = delayedResponse.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.Equal("DelayingResponse", delayedContent);
        }