Link class augments the base LinkAttributes class with abilities to: - create HttpRequestMessage - attach link hints - attach response handling behaviour - support for URI templates This class can be subclassed with attributes and behaviour that is specific to a particular link relation
Наследование: LinkAttributes, IRequestFactory
Пример #1
0
        public void DefaultLinkShouldCreateAGetRequest()
        {
            var link = new Link() { Target = new Uri("Http://localhost") };

            var request = link.CreateRequest();

            Assert.Equal(HttpMethod.Get,request.Method);
        }
Пример #2
0
        public void UseLinkToMakeRequest()
        {
            var link = new Link { Target = new Uri("Http://localhost") };
            var client = new HttpClient(new FakeMessageHandler());

            var response = client.SendAsync(link.CreateRequest()).Result;

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
Пример #3
0
        public void SettingContentShouldBePassedToTheRequest()
        {
            var link = new Link
            {
                Target = new Uri("Http://localhost"),
                Method = HttpMethod.Post,
                Content = new StringContent("Hello World")
            };

            var request = link.CreateRequest();

            Assert.Equal(request.Content.ReadAsStringAsync().Result, "Hello World");
        }
Пример #4
0
        public async Task Test()
        {
            var link = new Link(){Target = new Uri("http://localhost")};

            var notFoundHandler = new NotFoundHandler(new OkHandler(null));
            var machine = new HttpResponseMachine();
            machine.When(HttpStatusCode.NotFound)
                .Then(async (l, r) => { notFoundHandler.HandleResponseAsync(l, r); });

            var client = new HttpClient(new FakeHandler() {Response = new HttpResponseMessage() {StatusCode = HttpStatusCode.NotFound}});

            await client.FollowLinkAsync(link,machine);

            Assert.True(notFoundHandler.NotFound);
        }
Пример #5
0
        public async Task FollowAndApplyInDistinctSteps()
        {
            var link = new Link() {Target = new Uri("http://example.org/")};

            var client = new HttpClient(new FakeHandler() { Response = new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.NotFound
            } });
            
            var clientState = new ClientApplicationState();

            await client.FollowLinkAsync(link)
                        .ApplyRepresentationToAsync(clientState);

            Assert.Equal(HttpStatusCode.NotFound, clientState.StatusCode);
        }
Пример #6
0
 private static Dictionary<string, ParameterInfo> GetBindingProperties(Link link)
 {
     var props = link.GetType()
         .GetTypeInfo()
         .DeclaredProperties 
         .Select(p => new ParameterInfo()
         {
             PropertyInfo = p,
             Attribute =  p.GetCustomAttributes<LinkParameterAttribute>()
             .FirstOrDefault()
         })
         .ToDictionary(pi =>
         {
             return pi.Attribute == null ? pi.PropertyInfo.Name.ToLowerInvariant() : pi.Attribute.Name;
         }, pr => pr);
     return props;
 }
Пример #7
0
        public void SettingAnAcceptHeaderShouldBePassedToTheRequest()
        {
            var link = new Link
            {
                Target = new Uri("Http://localhost"),
                
            };
            link.AddRequestBuilder(new InlineRequestBuilder((r) =>
            {
                r.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.hal"));
                r.Headers.UserAgent.Add(new ProductInfoHeaderValue("foo", "1.1"));
                return r;
            }));


            var request = link.CreateRequest();

            Assert.True(request.Headers.Accept.Any(h => h.MediaType == "application/vnd.hal"));
        }
Пример #8
0
 public void CreateLinkHeaderWithMediaTypeAndLanguages()
 {
     var link = new Link()
     {
         Target = new Uri("http://localhost/{?foo}"),
         Type = "application/foo"
     };
     link.HrefLang.Add(new CultureInfo("en-GB"));
     link.HrefLang.Add(new CultureInfo("en-CA"));
     Assert.Equal("<http://localhost/{?foo}>;rel=\"related\";hreflang=en-GB;hreflang=en-CA;type=\"application/foo\"", link.AsLinkHeader());
 }
Пример #9
0
 public void CreateLinkHeaderWithRelation()
 {
     var link = new Link() { Target = new Uri("http://localhost/{?foo}"),
         Relation = "related",
         Title = "foo"
     };
     Assert.Equal("<http://localhost/{?foo}>;rel=\"related\";title=\"foo\"", link.AsLinkHeader());
 }
Пример #10
0
        public void UnsetParameterInLink()
        {
            var link = new Link() { Template = new UriTemplate("http://localhost/{?foo}") };

            var request = link.CreateRequest();

            Assert.Equal("http://localhost/", request.RequestUri.AbsoluteUri);
        }
Пример #11
0
        public void IdentifyParametersInTemplate()
        {
            var link = new Link() { Template = new UriTemplate("http://localhost/api/{dataset}/customer{?foo,bar,baz}") };

            var parameters = link.Template.GetParameterNames();

            Assert.Equal(4, parameters.Count());
            Assert.True(parameters.Contains("dataset"));
            Assert.True(parameters.Contains("foo"));
            Assert.True(parameters.Contains("bar"));
            Assert.True(parameters.Contains("baz"));
        }
Пример #12
0
        public void AddMultipleParametersToLink()
        {
            var link = new Link() { Template = new UriTemplate("http://localhost/api/{dataset}/customer{?foo,bar,baz}") };

            link.Template.ApplyParametersToTemplate(new Dictionary<string, object>
            {
                {"foo", "bar"},
                {"baz", "99"},
                {"dataset", "bob"}
            });

            var request = link.CreateRequest();
            
            Assert.Equal("http://localhost/api/bob/customer?foo=bar&baz=99", request.RequestUri.AbsoluteUri);
        }
Пример #13
0
        public void AddParameterToLink()
        {
            var link = new Link(){ Template = new UriTemplate("http://localhost/{?foo}")};
        
            var client = new HttpClient(new FakeMessageHandler());

            link.Template.AddParameter("foo", "bar");
            

            var response = client.SendAsync(link.CreateRequest()).Result;

            Assert.Equal("http://localhost/?foo=bar", response.RequestMessage.RequestUri.AbsoluteUri);
        }
Пример #14
0
        public async Task FollowLink()
        {
            var link = new Link { Target = new Uri("Http://localhost") };
            var client = new HttpClient(new FakeMessageHandler());

            var uri = string.Empty;
            var machine = new HttpResponseMachine();
            machine.When(HttpStatusCode.OK)
                .Then(async (rel,r) => {uri = r.RequestMessage.RequestUri.AbsoluteUri; });
            await client.FollowLinkAsync(link,machine);

            Assert.Equal("http://localhost/", uri);
        }
Пример #15
0
        public void UseURITemplateAsLinkSource()
        {
            var link = new Link { Target = new Uri("Http://localhost") }; 
            var client = new HttpClient(new FakeMessageHandler());

            var response = client.SendAsync(link.CreateRequest()).Result;

            Assert.Equal("http://localhost/", response.RequestMessage.RequestUri.AbsoluteUri);
        }
Пример #16
0
        public void RelativeLinkTemplate()
        {
            var link = new Link { Template = new UriTemplate("/foo/{bar}") };

            link.Template.AddParameter("bar", "baz");

            var req = link.CreateRequest();

            Assert.Equal("/foo/baz", req.RequestUri.OriginalString);
            
        }
Пример #17
0
 public void CreateLinkHeader()
 {
     var link = new Link() { Target = new Uri("http://localhost/{?foo}") };
     Assert.Equal("<http://localhost/{?foo}>;rel=\"related\"", link.AsLinkHeader());
 }