Beispiel #1
0
        public async Task RequestUserAgentString()
        {
            if (Helper.IsNetworkAvailable())
            {
                var agent   = "MyAgent";
                var http    = new HttpRequester(agent);
                var request = new Request();
                request.Address = new Url("http://httpbin.org/user-agent");
                request.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);
                }
            }
        }
Beispiel #2
0
        public async Task AsyncHttpGetRequest()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http    = new HttpRequester();
                var request = new Request();
                request.Address = new Url("http://httpbin.org/robots.txt");
                request.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());
                }
            }
        }
Beispiel #3
0
        public async Task SimpleHttpPutRequest()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http    = new HttpRequester();
                var request = new Request();
                request.Address = new Url("http://httpbin.org/put");
                request.Method  = HttpMethod.Put;
                request.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\""));
                }
            }
        }
 public async Task <StatsResponse> GetPlayerStatsAsync(string playerName, Region region = Region.AGG)
 {
     if (string.IsNullOrEmpty(playerName))
     {
         throw new ArgumentException("Player name cannot be empty.");
     }
     return(await _httpRequester.RequestAsync(playerName, region).ConfigureAwait(false));
 }
Beispiel #5
0
 public async Task <StatsResponse> GetPlayerStatsAsync(string playerName, string region = "agg")
 {
     if (string.IsNullOrEmpty(playerName))
     {
         throw new ArgumentException("Player name cannot be empty.");
     }
     return(await _httpRequester.RequestAsync(playerName, region));
 }
Beispiel #6
0
        public async Task <IResponse> RequestAsync(IRequest request, CancellationToken cancel)
        {
            var response = await _default.RequestAsync(request, cancel).ConfigureAwait(false);

            if (response == null)
            {
                return(null);
            }

            var bytes = await GetContentAsync(response).ConfigureAwait(false);

            Console.WriteLine(response.Address.Href);

            await SetResource(bytes, response.Headers["Content-Type"]).ConfigureAwait(false);

            return(await GetResponseAsync(bytes).ConfigureAwait(false));
        }
Beispiel #7
0
        public async Task <IResponse> RequestAsync(IRequest 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));
        }
Beispiel #8
0
        public async Task StatusCode404OfHttpGetRequest()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http    = new HttpRequester();
                var request = new Request();
                request.Address = new Url("http://httpbin.org/status/404");
                request.Method  = HttpMethod.Get;

                using (var response = await http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.AreEqual(404, (int)response.StatusCode);
                    Assert.IsTrue(response.Content.CanRead);
                    Assert.IsTrue(response.Headers.Count > 0);
                }
            }
        }
Beispiel #9
0
        public async Task MethodNotAllowedOnHttpPut()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http    = new HttpRequester();
                var request = new Request();
                request.Address = new Url("http://httpbin.org/get");
                request.Method  = HttpMethod.Put;
                request.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);
                }
            }
        }
Beispiel #10
0
        public async Task SimpleHttpDeleteRequest()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http    = new HttpRequester();
                var request = new Request
                {
                    Address = new Url("http://httpbin.org/delete"),
                    Method  = HttpMethod.Delete,
                    Content = Helper.StreamFromString("Should be ignored")
                };

                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);
                }
            }
        }