Пример #1
0
        public void AddUserAgentInfoWithVersion()
        {
            string  defaultProductName = "FxVersion";
            string  testProductName = "TestProduct";
            string  testProductVersion = "1.0.0.0";
            Version defaultProductVer, testProductVer;

            FakeServiceClient fakeClient = new FakeServiceClient(new FakeHttpHandler());

            fakeClient.SetUserAgent(testProductName, testProductVersion);
            HttpResponseMessage response = fakeClient.DoStuffSync();
            HttpHeaderValueCollection <ProductInfoHeaderValue> userAgentValueCollection = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent;

            var defaultProduct = userAgentValueCollection.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals(defaultProductName)).FirstOrDefault <ProductInfoHeaderValue>();
            var testProduct    = userAgentValueCollection.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals(testProductName)).FirstOrDefault <ProductInfoHeaderValue>();

            Version.TryParse(defaultProduct.Product.Version, out defaultProductVer);
            Version.TryParse(testProduct.Product.Version, out testProductVer);

            Assert.True(defaultProduct.Product.Name.Equals(defaultProductName));
            Assert.NotNull(defaultProductVer);

            Assert.True(testProduct.Product.Name.Equals(testProductName));
            Assert.True(testProduct.Product.Version.Equals(testProductVersion));
        }
Пример #2
0
        public void AddingSpCharsInUserAgent()
        {
            string sampleProd    = "SampleProdName";
            string newSampleProd = "NewSampleProdName";
            string spChars       = "*()!@#$%^&";
            string sampleVersion = "1.*.0.*";

            string concatProdName  = string.Concat(sampleProd, spChars);
            string cleanedProdName = string.Concat(sampleProd, ".");

            FakeServiceClient fakeClient = new FakeServiceClient(new FakeHttpHandler());

            fakeClient.SetUserAgent(concatProdName);
            HttpHeaderValueCollection <ProductInfoHeaderValue> userAgentValueCollection = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent;

            Assert.Equal(5, userAgentValueCollection.Count);

            var retrievedProdInfo = userAgentValueCollection.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals(cleanedProdName)).FirstOrDefault <ProductInfoHeaderValue>();

            Assert.Equal(retrievedProdInfo?.Product?.Name, cleanedProdName);

            fakeClient.SetUserAgent(newSampleProd, sampleVersion);
            HttpHeaderValueCollection <ProductInfoHeaderValue> userAgentVersion = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent;
            var retrievedVersion = userAgentVersion.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals(newSampleProd)).FirstOrDefault <ProductInfoHeaderValue>();

            Assert.Equal(retrievedVersion?.Product?.Version, "1.0.");
        }
        public void AddHeaderInformationToHttpClient()
        {
            Version    defaultVersion = null;
            HttpClient hc             = new HttpClient(new ContosoMessageHandler());

            hc.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(MY_PRODUCT, MY_VERSION));
            ContosoServiceClient contosoClient = new ContosoServiceClient(hc);
            HttpResponseMessage  response      = contosoClient.DoSyncWork();

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

            Assert.Equal(MY_VERSION, myProduct.Product.Version);

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

            Version.TryParse(fxVer.Product.Version, out defaultVersion);
            Assert.NotNull(defaultVersion);
        }
Пример #4
0
        public void VerifyOsInfoInUserAgent()
        {
            string osInfoProductName = "OSName";

            FakeServiceClient   fakeClient = new FakeServiceClient(new FakeHttpHandler());
            HttpResponseMessage response   = fakeClient.DoStuffSync();
            HttpHeaderValueCollection <ProductInfoHeaderValue> userAgentValueCollection = fakeClient.HttpClient.DefaultRequestHeaders.UserAgent;

            var osProduct = userAgentValueCollection.Where <ProductInfoHeaderValue>((p) => p.Product.Name.Equals(osInfoProductName)).FirstOrDefault <ProductInfoHeaderValue>();

            Assert.NotEmpty(osProduct.Product.Name);
            Assert.NotEmpty(osProduct.Product.Version);
        }
        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);
        }