示例#1
0
        public async Task AsyncHttpGetRequest()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http    = new DefaultHttpRequester();
                var request = new Request
                {
                    Address = new Url("http://httpbin.org/robots.txt"),
                    Method  = HttpMethod.Get
                };

                using (var response = http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.IsFalse(response.IsCompleted);

                    var result = await response;

                    Assert.IsTrue(response.IsCompleted);
                    Assert.IsTrue(result.Content.CanRead);
                    Assert.IsTrue(result.Headers.Count > 0);

                    var content = new StreamReader(result.Content);
                    Assert.AreEqual("User-agent: *\nDisallow: /deny\n", content.ReadToEnd());
                }
            }
        }
示例#2
0
        public async Task RequestUserAgentString()
        {
            if (Helper.IsNetworkAvailable())
            {
                var agent   = "MyAgent";
                var http    = new DefaultHttpRequester(agent);
                var request = new Request
                {
                    Address = new Url("http://httpbin.org/user-agent"),
                    Method  = HttpMethod.Get
                };

                using (var response = await http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.AreEqual(200, (int)response.StatusCode);
                    Assert.IsTrue(response.Content.CanRead);
                    Assert.IsTrue(response.Headers.Count > 0);

                    var stream = new StreamReader(response.Content);
                    Assert.IsNotNull(stream);

                    var content = stream.ReadToEnd();
                    Assert.IsTrue(content.Length > 0);
                    Assert.AreEqual("{\n  \"user-agent\": \"" + agent + "\"\n}\n", content);
                }
            }
        }
示例#3
0
        public async Task SimpleHttpPutRequest()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http    = new DefaultHttpRequester();
                var request = new Request
                {
                    Address = new Url("http://httpbin.org/put"),
                    Method  = HttpMethod.Put,
                    Content = Helper.StreamFromString("PUT THIS THING BACK")
                };

                using (var response = await http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.AreEqual(200, (int)response.StatusCode);
                    Assert.IsTrue(response.Content.CanRead);
                    Assert.IsTrue(response.Headers.Count > 0);

                    var stream = new StreamReader(response.Content);
                    Assert.IsNotNull(stream);

                    var content = stream.ReadToEnd();
                    Assert.IsTrue(content.Length > 0);
                    Assert.IsTrue(content.Contains("\"data\": \"PUT THIS THING BACK\""));
                }
            }
        }
示例#4
0
        protected override async Task <IResponse> PerformRequestAsync(Request request, CancellationToken cancel)
        {
            var url = request.Address.Href;

            if (_mapping.Contains(url) == false)
            {
                var response = await _default.RequestAsync(request, cancel).ConfigureAwait(false);

                var fileName = await AddResourceAsync(url, response).ConfigureAwait(false);

                _mapping.Add(url, fileName);
            }

            return(await ReadResourceAsync(url).ConfigureAwait(false));
        }
示例#5
0
        public async Task StatusCode403OfHttpGetRequest()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http    = new DefaultHttpRequester();
                var request = new Request
                {
                    Address = new Url("http://httpbin.org/status/403"),
                    Method  = HttpMethod.Get
                };

                using (var response = await http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.AreEqual(403, (int)response.StatusCode);
                    Assert.IsTrue(response.Content.CanRead);
                    Assert.IsTrue(response.Headers.Count > 0);
                }
            }
        }
示例#6
0
        public async Task MethodNotAllowedOnHttpPut()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http    = new DefaultHttpRequester();
                var request = new Request
                {
                    Address = new Url("http://httpbin.org/get"),
                    Method  = HttpMethod.Put,
                    Content = Helper.StreamFromString("Should be ignored")
                };

                using (var response = await http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.AreEqual(405, (int)response.StatusCode);
                    Assert.IsTrue(response.Content.CanRead);
                    Assert.IsTrue(response.Headers.Count > 0);
                }
            }
        }