Example #1
0
        public void ShouldPOSTWithFormDataObject()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                var bodyExtractor = new UrlEncodedBodyExtractor();
                var extractor     = new RequestExtractor(bodyExtractor);
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client   = new RestClient();
                var        response = client.Post("http://localhost:8080/api/customers")
                                      .WithUrlEncodedBody(new
                {
                    Name  = "Bob Smith",
                    Age   = 31,
                    Title = "Mr."
                })
                                      .Execute();

                string name  = bodyExtractor.Parameters["Name"];
                string age   = bodyExtractor.Parameters["Age"];
                string title = bodyExtractor.Parameters["Title"];

                Assert.AreEqual("Bob Smith", name, "The name was not sent.");
                Assert.AreEqual("31", age, "The age was not sent.");
                Assert.AreEqual("Mr.", title, "The title was not sent.");
            }
        }
Example #2
0
        public void ShouldGETWithQueryParametersObject()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                var extractor = new RequestExtractor();
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client = new RestClient();
                var response = client.Get("http://localhost:8080/api/customers")
                    .WithQueryParameters(new { customerid = 123 })
                    .Execute();

                Assert.AreEqual("123", extractor.QueryString["customerid"], "The ID was not passed.");
            }
        }
Example #3
0
        public void ShouldGETWithQueryParametersObject()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                var extractor = new RequestExtractor();
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client   = new RestClient();
                var        response = client.Get("http://localhost:8080/api/customers")
                                      .WithQueryParameters(new { customerid = 123 })
                                      .Execute();

                Assert.AreEqual("123", extractor.QueryString["customerid"], "The ID was not passed.");
            }
        }
Example #4
0
        public void ShouldGETWithCustomHeader()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                var extractor = new RequestExtractor();
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client   = new RestClient();
                var        response = client.Get("http://localhost:8080/api/customers")
                                      .WithHeader("test_header", "test_value")
                                      .Execute();

                var value = extractor.Headers.Get("test_header");
                Assert.AreEqual("test_value", value, "The header was not passed to the server.");
            }
        }
Example #5
0
        public void ShouldGETWithSimpleTemplate()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                var extractor = new RequestExtractor();
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client   = new RestClient();
                var        response = client.Get("http://localhost:8080/api/customers/{customerId}", new
                {
                    customerId = 123
                }).Execute();

                Assert.IsTrue(extractor.Url.ToString().EndsWith("123"), "The ID was not passed.");
            }
        }
Example #6
0
        public void ShouldPOSTWithNoBody()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                RequestExtractor extractor = new RequestExtractor();
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client   = new RestClient();
                var        response = client.Post("http://localhost:8080/api/customers")
                                      .WhenError(r => { throw new Exception(r.FromString <string>()); })
                                      .Execute();

                string contentLength = extractor.Headers["Content-Length"];
                Assert.AreEqual("0", contentLength, "The content length was not specified.");
            }
        }
Example #7
0
        public void ShouldGETWithSimpleTemplate()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                var extractor = new RequestExtractor();
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client = new RestClient();
                var response = client.Get("http://localhost:8080/api/customers/{customerId}", new 
                    {
                        customerId = 123
                    }).Execute();

                Assert.IsTrue(extractor.Url.ToString().EndsWith("123"), "The ID was not passed.");
            }
        }
Example #8
0
        /// <summary>
        /// 解析数据结果, 解析目标链接
        /// </summary>
        /// <param name="page">页面数据</param>
        public void Process(Page page)
        {
            var properties = page.Selectable(RemoveOutboundLinks).Properties;

            properties[Env.UrlPropertyKey]       = page.Request.Url;
            properties[Env.TargetUrlPropertyKey] = page.TargetUrl;

            if (!(page.Request.GetProperty(Page.Depth) == 1 && !Env.FilterDefaultRequest))
            {
                if (Filter != null && !Filter.IsMatch(page.Request))
                {
                    return;
                }
            }

            Handle(page);

            if (LastPageChecker != null && LastPageChecker.IsLastPage(page))
            {
                return;
            }

            IEnumerable <Request> requests;

            if (RequestExtractor != null && (requests = RequestExtractor.Extract(page)) != null)
            {
                foreach (var link in requests)
                {
                    if (Filter != null && !Filter.IsMatch(link))
                    {
                        continue;
                    }

                    if (CleanPound)
                    {
                        link.Url = link.Url.Split('#')[0];
                    }

                    page.AddTargetRequest(link);
                }
            }
        }
Example #9
0
        public void ShouldPOSTWithArrayFormData()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                var bodyExtractor = new UrlEncodedBodyExtractor();
                var extractor     = new RequestExtractor(bodyExtractor);
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client   = new RestClient();
                var        response = client.Post("http://localhost:8080/api/customers")
                                      .WithUrlEncodedBody(b => b
                                                          .WithParameter("CustomerId", 1)
                                                          .WithParameter("CustomerId", 2)
                                                          .WithParameter("CustomerId", 3))
                                      .Execute();

                string[] ids         = bodyExtractor.Parameters.GetValues("CustomerId");
                string[] expectedIds = new string[] { "1", "2", "3" };
                CollectionAssert.AreEquivalent(expectedIds, ids, "The array of values were not sent.");
            }
        }
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            var container             = Bootstrapper.Container;
            var authenticationService = container.Resolve <IAccountService>();

            var token = RequestExtractor.GetToken(actionContext.Request);

            if (string.IsNullOrEmpty(token))
            {
                throw new UnauthorizedException(Messages.Forbidden);
            }

            var authorization = authenticationService.GetUserAuthorization(token);

            if (authorization == null)
            {
                throw new UnauthorizedException(Messages.Forbidden);
            }

            if (!authorization.IsAdmin && _requiresAdmin)
            {
                throw new ForbiddenException(Messages.Forbidden);
            }
        }
Example #11
0
 public SongsDiscoverModel Discover(string name, int count)
 {
     return(_songService.DiscoverRelatedSongs(name, RequestExtractor.GetToken(Request), count));
 }
Example #12
0
        public void ShouldGETWithCustomHeader()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                var extractor = new RequestExtractor();
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client = new RestClient();
                var response = client.Get("http://localhost:8080/api/customers")
                    .WithHeader("test_header", "test_value")
                    .Execute();

                var value = extractor.Headers.Get("test_header");
                Assert.AreEqual("test_value", value, "The header was not passed to the server.");
            }
        }
Example #13
0
 public void Logout()
 {
     _accountService.Logout(RequestExtractor.GetToken(Request));
 }
Example #14
0
 public bool TogglePreference([FromUri] string id)
 {
     return(_songService.ToggleUserSongPreference(id, RequestExtractor.GetToken(Request)));
 }
Example #15
0
        public void ShouldPOSTWithNoBody()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                RequestExtractor extractor = new RequestExtractor();
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client = new RestClient();
                var response = client.Post("http://localhost:8080/api/customers")
                    .WhenError(r => { throw new Exception(r.FromString<string>()); })
                    .Execute();

                string contentLength = extractor.Headers["Content-Length"];
                Assert.AreEqual("0", contentLength, "The content length was not specified.");
            }
        }
Example #16
0
        public void ShouldPOSTWithArrayFormData()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                var bodyExtractor = new UrlEncodedBodyExtractor();
                var extractor = new RequestExtractor(bodyExtractor);
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client = new RestClient();
                var response = client.Post("http://localhost:8080/api/customers")
                    .WithUrlEncodedBody(b => b
                        .WithParameter("CustomerId", 1)
                        .WithParameter("CustomerId", 2)
                        .WithParameter("CustomerId", 3))
                    .Execute();

                string[] ids = bodyExtractor.Parameters.GetValues("CustomerId");
                string[] expectedIds = new string[] { "1", "2", "3" };
                CollectionAssert.AreEquivalent(expectedIds, ids, "The array of values were not sent.");
            }
        }
Example #17
0
        public void ShouldPOSTWithFormDataObject()
        {
            using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
            {
                var bodyExtractor = new UrlEncodedBodyExtractor();
                var extractor = new RequestExtractor(bodyExtractor);
                server.UseBodyExtractor(extractor);
                server.Listen();

                RestClient client = new RestClient();
                var response = client.Post("http://localhost:8080/api/customers")
                    .WithUrlEncodedBody(new
                    {
                        Name = "Bob Smith",
                        Age = 31,
                        Title = "Mr."
                    })
                    .Execute();

                string name = bodyExtractor.Parameters["Name"];
                string age = bodyExtractor.Parameters["Age"];
                string title = bodyExtractor.Parameters["Title"];

                Assert.AreEqual("Bob Smith", name, "The name was not sent.");
                Assert.AreEqual("31", age, "The age was not sent.");
                Assert.AreEqual("Mr.", title, "The title was not sent.");
            }
        }
Example #18
0
 public IEnumerable <SongItemModel> GetRecommendationsById([FromUri] string id, int count)
 {
     return(_songService.GetSongRecommendations(id, RequestExtractor.GetToken(Request), count));
 }