public async Task Returns_content_if_response_is_OK()
        {
            string content = Guid.NewGuid().ToString();
            var response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StringContent(content);

            var httpClient = new HttpClient(new FakeHandler
            {
                Response = response,
                InnerHandler = new HttpClientHandler()
            });

            var link = new GuidServiceLink();
            Guid guid = Guid.Empty;
            var machine = new HttpResponseMachine();
            machine.AddResponseHandler(async (lr, r) =>
            {
                {
                    
                    guid = Guid.Parse( r.Content.ReadAsStringAsync().Result);
                    return r;
                }
            }, HttpStatusCode.OK);


            await httpClient.FollowLinkAsync(link,machine);
            
            Assert.Equal(content, guid.ToString());



        }
        public Task SpecifyHandlerChainForAboutLink()
        {
            var foo = false;
            var bar = false;
            var baz = false;
  
            var registry = new LinkFactory();
            var grh = new InlineResponseHandler((rel,hrm) => baz = true,
                new InlineResponseHandler((rel, hrm) => foo = true,
                    new InlineResponseHandler((rel, hrm) => bar = true)));

            var machine = new HttpResponseMachine();
            machine.AddResponseHandler(grh.HandleResponseAsync, System.Net.HttpStatusCode.OK);
            

            var link = registry.CreateLink<AboutLink>();
            link.Target = new Uri("http://example.org");
            var httpClient = new HttpClient(new FakeHandler() { Response = new HttpResponseMessage()});

            return httpClient.FollowLinkAsync(link,machine).ContinueWith(t =>
                {
                    Assert.True(foo);
                    Assert.True(bar);
                    Assert.True(baz);
                });

        }
 public void TestLink()
 {
     var link = new GuidServiceLink()
         {
             Target = new Uri("http://localhost/guidservice")
         };
     var httpClient = new HttpClient();
     httpClient.FollowLinkAsync(link);
     
 }
        public Task Test()
        {
            var link = new Link(){Target = new Uri("http://localhost")};

            link.HttpResponseHandler = new NotFoundHandler(new OkHandler(null));

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

            return client.FollowLinkAsync(link);
        }
        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.AddResponseHandler(notFoundHandler.HandleResponseAsync, HttpStatusCode.NotFound);

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

            await client.FollowLinkAsync(link,machine);

            Assert.True(notFoundHandler.NotFound);
        }
        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);
        }
        public void Throws_exception_if_response_not_OK()
        {
            var response = new HttpResponseMessage(HttpStatusCode.BadRequest);
            var httpClient = new HttpClient(new FakeHandler
            {
                Response = response,
                InnerHandler = new HttpClientHandler()
            });
              var machine = new HttpResponseMachine();
              machine.AddResponseHandler((l, r) => { throw new Exception(); }, HttpStatusCode.BadRequest); 

            var task = httpClient.FollowLinkAsync(new GuidServiceLink(),machine);
            
            Assert.Throws<AggregateException>(() => task.Wait());
            
        }
Example #8
0
        private static async Task CodeSearchUsingLink()
        {
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders
                .UserAgent.Add(new ProductInfoHeaderValue("DarrelsGitClient", "1.0"));

            // API Specific type acts as Request Factory
            var codeSearchLink = new CodeSearchLink()
            {
                Template = new UriTemplate("https://api.github.com/search/code?q={query}{&page,per_page,sort_order}"),
                Query = ".Result user:darrelmiller"
            };

            var requestMessage = codeSearchLink.CreateRequest();
           
            var responseMessage = await httpClient.SendAsync(requestMessage);
            
            // ReadAs extension takes advantage of media type defined by API
            var githubdoc = await responseMessage.Content.ReadAsGithubDocumentAsync();

            // API Specific Link Type knows how to interpret generic media type
            var searchResults = CodeSearchLink.InterpretMessageBody(githubdoc);

            foreach (var result in searchResults.Items)
            {
                Console.WriteLine(result.Path);
            }

            // Harvest strongly typed links from strongly typed searchresults
            var fileLinks = searchResults.Items
                .Where(i => i.Name.Contains("Slack") && i.CodeFileLink != null)
                .Select(i => i.CodeFileLink);

            foreach (var fileLink in fileLinks)
            {
                // Following Links is a way of navigating the API's object graph
                var fileResponse = await httpClient.FollowLinkAsync(fileLink);

                // Reuse media type parser
                var fileDoc = await fileResponse.Content.ReadAsGithubDocumentAsync();

                // Use Type Specific Link to interpret results
                var codeFile = CodeFileLink.InterpretMessageBody(fileDoc);
                Console.WriteLine(codeFile.Content);
            }
        }  
Example #9
0
        public Task SpecifyHandlerChainForAboutLink()
        {
            var foo = false;
            var bar = false;
            var baz = false;

            var registry = new LinkFactory();
            var grh = new ActionResponseHandler((hrm) => baz = true,
                new ActionResponseHandler((hrm) => foo = true,
                    new ActionResponseHandler((hrm) => bar = true)));

            registry.SetHandler<AboutLink>(grh);

            var link = registry.CreateLink<AboutLink>();
            link.Target = new Uri("http://example.org");
            var httpClient = new HttpClient(new FakeHandler() { Response = new HttpResponseMessage()});

            return httpClient.FollowLinkAsync(link).ContinueWith(t =>
                {
                    Assert.True(foo);
                    Assert.True(bar);
                    Assert.True(baz);
                });
        }
Example #10
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);
        }
Example #11
0
        private static async Task TypedLinksFromGenericType()
        {
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders
                .UserAgent.Add(new ProductInfoHeaderValue("DarrelsGitClient", "1.0"));

            // Factory for typed links
            var linkFactory = new LinkFactory();
            GitHubHelper.RegisterGitHubLinks(linkFactory);

            // Get Home Document, without strongly typed object
            var homelink = new HomeLink();
            var homeResponse = await httpClient.FollowLinkAsync(homelink);
            var githubDocument = await homeResponse.Content.ReadAsGithubDocumentAsync(linkFactory);

            // Retrieve specific link from generic document
            var codeSearchLink = githubDocument.GetLink<CodeSearchLink>();
            codeSearchLink.Query = ".Result user:darrelmiller";

            var responseMessage = await httpClient.FollowLinkAsync(codeSearchLink);
            var githubDocument2 = await responseMessage.Content.ReadAsGithubDocumentAsync();
            var searchResults = CodeSearchLink.InterpretMessageBody(githubDocument2);

            // Show Results
            foreach (var result in searchResults.Items)
            {
                Console.WriteLine(result.Path);
            }
        }
        public Task Returns_content_if_response_is_OK()
        {
            string content = Guid.NewGuid().ToString();
            var response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StringContent(content);

            var httpClient = new HttpClient(new FakeHandler
            {
                Response = response,
                InnerHandler = new HttpClientHandler()
            });

            var link = new GuidServiceLink();
            return httpClient.FollowLinkAsync(link).ContinueWith(
                t => Assert.Equal(content, link.Guid));
        }
Example #13
0
        public void FollowLink()
        {
            var link = new Link { Target = new Uri("Http://localhost") };
            var client = new HttpClient(new FakeMessageHandler());

            var uri = string.Empty;
            link.HttpResponseHandler = new ActionResponseHandler(r => uri = r.RequestMessage.RequestUri.AbsoluteUri);
            var task = client.FollowLinkAsync(link);

            task.Wait();

            Assert.Equal("http://localhost/", uri);
        }