Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="link"></param>
        /// <param name="configureHint"></param>
        public static void AddHint <T>(this Link link, Action <T> configureHint) where T : Hint, new()
        {
            var hint = new T();

            configureHint(hint);
            link.AddHint(hint);
        }
Esempio n. 2
0
        public void UseAllowHintToSetRequestMethodOnLink()
        {
            // Arrange
            var hint = new AllowHint();

            hint.AddMethod(HttpMethod.Post);
            hint.ConfigureRequest = (h, r) =>
            {
                // Really not sure if this is a good idea to allow hints to actually change the request.
                // Should they just validate the current request?
                // How do we know if someone has explicitly set link.method or if it is just a default Get.
                var allowHint = ((AllowHint)h);
                if (!allowHint.Methods.Contains(r.Method))
                {
                    r.Method = allowHint.Methods.First();
                }
                return(r);
            };

            var link = new Link();

            link.AddHint(hint);

            // Act
            var request = link.CreateRequest();

            //Asset
            Assert.Equal(HttpMethod.Post, request.Method);
        }
Esempio n. 3
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
            });
        }
Esempio n. 4
0
        private Link GetLink(string prefix, string relation, string target, JToken format, params HttpMethod[] methods)
        {
            var link = new Link()
            {
                Relation = prefix + "" + relation,
                Target   = new Uri("/" + target, UriKind.Relative)
            };

            link.AddHint <AllowHint>(h =>
            {
                foreach (var item in methods)
                {
                    h.AddMethod(HttpMethod.Get);
                }
            }
                                     );
            link.AddHint <FormatsHint>(h =>
            {
                h.AddMediaType("application/json");
                //h.AddMediaType("application/vnd.area+json");
                h.Content = JToken.FromObject(new PhysicalTopology());
            });
            return(link);
        }
Esempio n. 5
0
        public void SpecifyHandlerForAllowhint()
        {
            var foo = false;

            var registry = new HintFactory();

            registry.SetHandler <AllowHint>((h, r) =>
                                            { foo = true;
                                              return(r); });

            var hint = registry.CreateHint <AllowHint>();

            var link = new Link();

            link.AddHint(hint);

            // Act
            var request = link.CreateRequest();

            Assert.Equal(foo, true);
        }
        public HttpResponseMessage Get()
        {
            var home = new Tavis.Home.HomeDocument();

            var issueLink = new Link()
            {
                Relation = IssueLinkFactory.Rels.Issue,
                Target   = new Uri("/issue/{id}", UriKind.Relative)
            };

            issueLink.AddHint <AllowHint>(h => h.AddMethod(HttpMethod.Get));
            issueLink.AddHint <FormatsHint>(h =>
            {
                h.AddMediaType("application/json");
                h.AddMediaType("application/vnd.issue+json");
            });



            home.AddResource((Link)issueLink);

            var issuesLink = new Link()
            {
                Relation = IssueLinkFactory.Rels.Issues,
                Target   = new Uri("/issue", UriKind.Relative)
            };

            issuesLink.AddHint <AllowHint>(h => h.AddMethod(HttpMethod.Get));
            issuesLink.AddHint <FormatsHint>(h =>
            {
                h.AddMediaType("application/json");
                h.AddMediaType("application/vnd.collection+json");
            });

            home.AddResource(issuesLink);

            var searchLink = new Link()
            {
                Relation = IssueLinkFactory.Rels.SearchQuery,
                Target   = new Uri("/issue{?searchtext}", UriKind.Relative)
            };

            searchLink.AddHint <AllowHint>(h => h.AddMethod(HttpMethod.Get));
            searchLink.AddHint <FormatsHint>(h =>
            {
                h.AddMediaType("application/json");
                h.AddMediaType("application/vnd.collection+json");
            });

            home.AddResource(searchLink);



            var issueProcessorLink = new Link()
            {
                Relation = IssueLinkFactory.Rels.IssueProcessor,
                Target   = new Uri("/issueprocessor/{id}{?action}", UriKind.Relative)
            };

            issueProcessorLink.AddHint <AllowHint>(h => h.AddMethod(HttpMethod.Post));

            home.AddResource(issueProcessorLink);

            return(new HttpResponseMessage()
            {
                RequestMessage = Request,
                Content = new HomeContent(home)
            });
        }