public void TestGetResponse(string method, bool isAsync)
        {
            using (ShimsContext.Create())
            {
                // Arrange
                var baseUriMapping = new BaseUriMapping { { "TargetSite", "http://yahoo.com" } };
                var userAgentMapping = new UserAgentMapping { { "IE11", "Mozilla IE11" } };

                var httpRequest = new HttpRequest
                {
                    BaseUri = "TargetSite",
                    UserAgent = "IE11",
                    Method = method,
                    Headers = new NameValueCollection { { "test", "test-value" } },
                    PathInfos = new List<string> { "test.aspx" },
                    Cookies = new NameValueCollection { { "test", "test-value" } },
                };

                var httpContent = new StringContent("Hello World");
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("text/html");
                httpContent.Headers.LastModified = DateTimeOffset.Parse("Tue, 16 Jul 2013 11:50:35 GMT");

                var httpResponseMessage = new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = httpContent,
                    RequestMessage = new HttpRequestMessage { RequestUri = new Uri("http://yahoo.com/test.aspx") }
                };

                httpResponseMessage.Headers.Add("test", "test-value");

                var cookies = new CookieContainer();
                cookies.Add(new Cookie("test", "test-value", "", "yahoo.com"));

                ShimHttpClientHandler.AllInstances.CookieContainerGet = h => cookies;
                ShimHttpClient.AllInstances.GetAsyncUri = (client, task) => Task.FromResult(httpResponseMessage);
                ShimHttpClient.AllInstances.PostAsyncUriHttpContent = (client, task, c) => Task.FromResult(httpResponseMessage);
                ShimHttpClient.AllInstances.PutAsyncUriHttpContent = (client, task, c) => Task.FromResult(httpResponseMessage);
                ShimHttpClient.AllInstances.DeleteAsyncUri = (client, task) => Task.FromResult(httpResponseMessage);

                // Act
                var httpRequestService = new HttpRequestService(baseUriMapping, userAgentMapping, "http://proxy.com");
                var httpResponse = isAsync
                    ? httpRequestService.GetResponseAsync(httpRequest).Result
                    : httpRequestService.GetResponse(httpRequest);

                // Assert
                Assert.AreEqual(1, httpResponse.Headers.Count);
                Assert.AreEqual(1, httpResponse.Cookies.Count);
                Assert.AreEqual(DateTimeOffset.Parse("Tue, 16 Jul 2013 11:50:35 GMT", CultureInfo.InvariantCulture), httpResponse.LastModified);
                Assert.AreEqual("Hello World", httpResponse.Contents);
                Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);
                Assert.AreEqual(new Uri("http://yahoo.com/test.aspx"), httpResponse.Uri);
            }
        }
        public static UserAgentMapping Parse(XElement element)
        {
            if (element.Name != "UserAgentMapping")
            {
                throw new ArgumentException("Invalid element", "element");
            }

            var userAgentMapping = new UserAgentMapping();
            foreach (var current in element.Elements("Item"))
            {
                userAgentMapping.Add(current.Attribute("Key").Value, current.Value);
            }

            return userAgentMapping;
        }
Exemplo n.º 3
0
        public static UserAgentMapping Parse(XElement element)
        {
            if (element.Name != "UserAgentMapping")
            {
                throw new ArgumentException("Invalid element", "element");
            }

            var userAgentMapping = new UserAgentMapping();

            foreach (var current in element.Elements("Item"))
            {
                userAgentMapping.Add(current.Attribute("Key").Value, current.Value);
            }

            return(userAgentMapping);
        }
        public void TestGetResponse(string method, bool isAsync)
        {
            using (ShimsContext.Create())
            {
                // Arrange
                var baseUriMapping = new BaseUriMapping {
                    { "TargetSite", "http://yahoo.com" }
                };
                var userAgentMapping = new UserAgentMapping {
                    { "IE11", "Mozilla IE11" }
                };

                var httpRequest = new HttpRequest
                {
                    BaseUri   = "TargetSite",
                    UserAgent = "IE11",
                    Method    = method,
                    Headers   = new NameValueCollection {
                        { "test", "test-value" }
                    },
                    PathInfos = new List <string> {
                        "test.aspx"
                    },
                    Cookies = new NameValueCollection {
                        { "test", "test-value" }
                    },
                };

                var httpContent = new StringContent("Hello World");
                httpContent.Headers.ContentType  = new MediaTypeHeaderValue("text/html");
                httpContent.Headers.LastModified = DateTimeOffset.Parse("Tue, 16 Jul 2013 11:50:35 GMT");

                var httpResponseMessage = new HttpResponseMessage
                {
                    StatusCode     = HttpStatusCode.OK,
                    Content        = httpContent,
                    RequestMessage = new HttpRequestMessage {
                        RequestUri = new Uri("http://yahoo.com/test.aspx")
                    }
                };

                httpResponseMessage.Headers.Add("test", "test-value");

                var cookies = new CookieContainer();
                cookies.Add(new Cookie("test", "test-value", "", "yahoo.com"));

                ShimHttpClientHandler.AllInstances.CookieContainerGet = h => cookies;
                ShimHttpClient.AllInstances.GetAsyncUri             = (client, task) => Task.FromResult(httpResponseMessage);
                ShimHttpClient.AllInstances.PostAsyncUriHttpContent = (client, task, c) => Task.FromResult(httpResponseMessage);
                ShimHttpClient.AllInstances.PutAsyncUriHttpContent  = (client, task, c) => Task.FromResult(httpResponseMessage);
                ShimHttpClient.AllInstances.DeleteAsyncUri          = (client, task) => Task.FromResult(httpResponseMessage);

                // Act
                var httpRequestService = new HttpRequestService(baseUriMapping, userAgentMapping, "http://proxy.com");
                var httpResponse       = isAsync
                    ? httpRequestService.GetResponseAsync(httpRequest).Result
                    : httpRequestService.GetResponse(httpRequest);

                // Assert
                Assert.AreEqual(1, httpResponse.Headers.Count);
                Assert.AreEqual(1, httpResponse.Cookies.Count);
                Assert.AreEqual(DateTimeOffset.Parse("Tue, 16 Jul 2013 11:50:35 GMT", CultureInfo.InvariantCulture), httpResponse.LastModified);
                Assert.AreEqual("Hello World", httpResponse.Contents);
                Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);
                Assert.AreEqual(new Uri("http://yahoo.com/test.aspx"), httpResponse.Uri);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpRequestService" /> class.
        /// </summary>
        /// <param name="baseUriMapping">baseUriMapping parameter</param>
        /// <param name="userAgentMapping">userAgentMapping parameter</param>
        /// <param name="proxyUri">proxyUri parameter</param>
		public HttpRequestService(BaseUriMapping baseUriMapping, UserAgentMapping userAgentMapping, string proxyUri)
        {
            this.baseUriMapping = baseUriMapping;
            this.userAgentMapping = userAgentMapping;
            this.proxyUri = proxyUri;
        }
Exemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HttpRequestService" /> class.
 /// </summary>
 /// <param name="baseUriMapping">baseUriMapping parameter</param>
 /// <param name="userAgentMapping">userAgentMapping parameter</param>
 /// <param name="proxyUri">proxyUri parameter</param>
 public HttpRequestService(BaseUriMapping baseUriMapping, UserAgentMapping userAgentMapping, string proxyUri)
 {
     this.baseUriMapping   = baseUriMapping;
     this.userAgentMapping = userAgentMapping;
     this.proxyUri         = proxyUri;
 }