示例#1
0
        public void CreateResourceWithPathParameterAndRelWithDots()
        {
            var doc = new HomeDocument();

            doc.AddResource <Link>(l =>
            {
                l.Relation = "vnd.foo.about";
                l.Target   = new Uri("http://example.org:1001/about/{id}");
            });

            doc.AddResource <HelpLink>(l =>
            {
                l.Target = new Uri("http://example.org:1001/help/{id}");
            });

            var ms = new MemoryStream();

            doc.Save(ms);
            ms.Position = 0;

            var st = new StreamReader(ms);
            var s  = st.ReadToEnd();

            Assert.True(s.Contains("href-template"));
        }
示例#2
0
        public HttpResponseMessage Get(HttpRequestMessage requestMessage)
        {
            var homeDocument = new HomeDocument();


            var fooLink = new Link()
            {
                Relation = "http://example.org/rels/foo",
                Target   = new Uri("foo", UriKind.Relative)
            };
            var allowHint = new AllowHint();

            allowHint.AddMethod(HttpMethod.Get);
            allowHint.AddMethod(HttpMethod.Post);
            fooLink.AddHint(allowHint);
            homeDocument.AddResource(fooLink);


            var barLink = new Link()
            {
                Relation = "http://example.org/rels/bar",
                Target   = new Uri("bar", UriKind.Relative)
            };
            var allowHint2 = new AllowHint();

            allowHint2.AddMethod(HttpMethod.Get);
            barLink.AddHint(allowHint2);
            homeDocument.AddResource(barLink);


            var bar2Link = new Link()
            {
                Relation = "http://example.org/rels/bar2",
                Target   = new Uri("bar/{id}", UriKind.Relative)
            };

            //   bar2Link.SetParameter("id","",new Uri("template/params/id", UriKind.Relative));
            homeDocument.AddResource(bar2Link);


            var ms = new MemoryStream();

            homeDocument.Save(ms);
            ms.Position = 0;
            var streamContent = new StreamContent(ms);

            return(new HttpResponseMessage()
            {
                Content = streamContent
            });
        }
示例#3
0
        private HomeDocument GetHomeDocument()
        {
            var home = new HomeDocument();

            string Prefix = Request.RequestUri + "/";

            Prefix = "";
            JToken listFormat = JToken.FromObject(new List <PhysicalTopology>()
            {
                new PhysicalTopology(), new PhysicalTopology()
            });
            JToken itemFormat = JToken.FromObject(new PhysicalTopology()
            {
                Id = 1, Name = "a"
            });
            JToken treeFormat = JToken.FromObject(new PhysicalTopology[]
                                                  { new PhysicalTopology {
                                                        Children = new List <PhysicalTopology> {
                                                        }
                                                    } });

            home.AddResource(GetLink(Prefix, "areaList", "area", listFormat));
            home.AddResource(GetLink(Prefix, "areaItem", "area/{id}", itemFormat, HttpMethod.Get, HttpMethod.Delete));
            home.AddResource(GetLink(Prefix, "areaTreeRoot", "area/tree", treeFormat));
            home.AddResource(GetLink(Prefix, "areaTree", "area/tree/{id}", treeFormat));

            home.AddResource(GetLink(Prefix, "areaSearch", "area/search?name={name}", listFormat));
            home.AddResource(GetLink(Prefix, "areaChildren", "area/{id}/children", listFormat));
            home.AddResource(GetLink(Prefix, "areaPost", "area", itemFormat, HttpMethod.Post));
            home.AddResource(GetLink(Prefix, "areaPut", "area", itemFormat, HttpMethod.Put));
            return(home);
        }
示例#4
0
        public void RoundTripHomeDocumentWithHint()
        {
            var doc       = new HomeDocument();
            var aboutLink = new AboutLink()
            {
                Target = new Uri("about", UriKind.Relative)
            };
            var allowHint = new AllowHint();

            allowHint.AddMethod(HttpMethod.Get);
            aboutLink.AddHint(allowHint);
            aboutLink.AddHint(new FormatsHint());
            doc.AddResource(aboutLink);

            var ms = new MemoryStream();

            doc.Save(ms);
            ms.Position = 0;

            var outDoc = HomeDocument.Parse(ms);

            var link = outDoc.GetResource("about");

            Assert.IsType <AboutLink>(link);
            Assert.IsType <AllowHint>(link.GetHints().First());
            Assert.IsType <FormatsHint>(link.GetHints().Last());
        }
示例#5
0
        public void CreateHomeDocumentWithFormatsHints()
        {
            var doc       = new HomeDocument();
            var aboutLink = new AboutLink()
            {
                Target = new Uri("about", UriKind.Relative)
            };

            aboutLink.AddHint <AllowHint>(h => h.AddMethod(HttpMethod.Get));
            aboutLink.AddHint <FormatsHint>(h => h.AddMediaType("application/json"));

            doc.AddResource(aboutLink);

            var ms = new MemoryStream();

            doc.Save(ms);
            ms.Position = 0;

            var outDoc = HomeDocument.Parse(ms);

            var link = outDoc.GetResource("about");

            Assert.IsType <AboutLink>(link);
            Assert.IsType <AllowHint>(link.GetHints().First());
            Assert.IsType <FormatsHint>(link.GetHints().Last());
            Assert.IsType <AboutLink>(outDoc.Resources.First());
        }
示例#6
0
        public IHttpActionResult Get()
        {
            var home = new HomeDocument();

            home.AddResource(TopicsLinkHelper.CreateLink(Request).WithHints());
            home.AddResource(DaysLinkHelper.CreateLink(Request).WithHints());
            home.AddResource(SessionsLinkHelper.CreateLink(Request).WithHints());
            home.AddResource(SpeakersLinkHelper.CreateLink(Request).WithHints());

            return(new OkResult(Request)
                   .WithCaching(new CacheControlHeaderValue()
            {
                MaxAge = new TimeSpan(0, 0, 60)
            })
                   .WithContent(new HomeContent(home)));
        }
示例#7
0
        public IHttpActionResult Get()
        {
            //var explorer = new ApiExplorer(Configuration);
            //var descriptions = explorer.ApiDescriptions;

            //return new OkResult(Request).WithContent(new StringContent(String.Join("\n",descriptions.Select(d=> d.ID).ToArray())));

            var home = new HomeDocument();

            home.AddResource(TopicsLinkHelper.CreateLink(Request).WithHints());
            home.AddResource(DaysLinkHelper.CreateLink(Request).WithHints());
            home.AddResource(SessionsLinkHelper.CreateLink(Request).WithHints());
            home.AddResource(SpeakersLinkHelper.CreateLink(Request).WithHints());

            return(new OkResult(Request)
                   .WithCaching(new CacheControlHeaderValue()
            {
                MaxAge = new TimeSpan(0, 0, 60)
            })
                   .WithContent(new HomeContent(home)));
        }
示例#8
0
        public void RoundTripHomeDocument()
        {
            var doc = new HomeDocument();

            doc.AddResource(new AboutLink()
            {
                Target = new Uri("about", UriKind.Relative)
            });

            var ms = new MemoryStream();

            doc.Save(ms);
            ms.Position = 0;

            var outDoc = HomeDocument.Parse(ms);

            Assert.NotNull(outDoc.GetResource("about"));
        }
示例#9
0
        public void CreateResource_with_extension_rel_and_template()
        {
            var doc = new HomeDocument();

            doc.AddResource <Link>(l =>
            {
                l.Relation = "http://webapibook.net/rels#issue-processor";
                l.Target   = new Uri("/issueprocessor/{id}{?action}", UriKind.Relative);
            });


            var ms = new MemoryStream();

            doc.Save(ms);
            ms.Position = 0;

            var st = new StreamReader(ms);
            var s  = st.ReadToEnd();

            Assert.True(s.Contains("href-template"));
        }
示例#10
0
        public void CreateResourceWithMultipleQueryParameters()
        {
            var doc = new HomeDocument();

            doc.AddResource <Link>(l =>
            {
                l.Relation = "vnd.foo.about";
                l.Template = new UriTemplate("http://example.org:1001/about{?id,name}");
            });


            var ms = new MemoryStream();

            doc.Save(ms);
            ms.Position = 0;

            var st = new StreamReader(ms);
            var s  = st.ReadToEnd();

            Assert.True(s.Contains("href-template"));
        }
示例#11
0
        public void CreateHomeDocumentWithLotsOfHints()
        {
            var doc = new HomeDocument();

            doc.AddResource <AboutLink>(l =>
            {
                l.Target = new Uri("about", UriKind.Relative);
                l.AddHint <AllowHint>(h => h.AddMethod(HttpMethod.Get));
                l.AddHint <FormatsHint>(h => h.AddMediaType("application/json"));
                l.AddHint <AcceptPostHint>(h => h.AddMediaType("application/vnd.tavis.foo+json"));
                l.AddHint <AcceptPreferHint>(h => h.AddPreference("handling"));
            });

            var ms = new MemoryStream();

            doc.Save(ms);
            ms.Position = 0;

            var st = new StreamReader(ms);
            var s  = st.ReadToEnd();

            Assert.NotNull(s);
        }