示例#1
0
        public async Task Handle200AndContentTypeUsingTypedMachine()
        {
            // Arrange
            var model = new Model <JToken>
            {
                Value = null
            };

            var machine = new HttpResponseMachine <Model <JToken> >(model);

            machine.AddResponseHandler(async(m, l, r) =>
            {
                var text = await r.Content.ReadAsStringAsync();
                m.Value  = JToken.Parse(text);
                return(r);
            }, HttpStatusCode.OK, null, new MediaTypeHeaderValue("application/json"));


            machine.AddResponseHandler(async(m, l, r) => { return(r); }, HttpStatusCode.OK, null, new MediaTypeHeaderValue("application/xml"));

            var byteArrayContent = new ByteArrayContent(Encoding.UTF8.GetBytes("{\"hello\" : \"world\"}"));

            byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            // Act
            await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.OK) { Content = byteArrayContent });

            // Assert
            Assert.NotNull(model.Value);
        }
        public async Task GetTenant()
        {
            var httpClient = CreateClient();
            
            var linkFactory = StormPathDocument.CreateLinkFactory();

            var tenantLink = new TenantLink()
            {
                TenantId = "5gG32HDHLSsYAWeh9ADSZo"
            };

            TenantMessage tenantMessage = null;

            var machine = new HttpResponseMachine();
            machine.AddResponseHandler(new RedirectHandler(httpClient, machine).HandleResponseAsync, HttpStatusCode.Redirect);
            machine.AddResponseHandler(async (l, r) =>
            {
                tenantMessage = tenantLink.InterpretMessageBody(r.Content.Headers.ContentType, await r.Content.ReadAsStreamAsync(), linkFactory);
                return r;
            }, HttpStatusCode.OK, LinkHelper.GetLinkRelationTypeName<TenantLink>() ,new MediaTypeHeaderValue("application/json"){CharSet="UTF-8"});


            await httpClient.FollowLinkAsync(tenantLink, machine);

          
            Assert.NotNull(tenantMessage);
        }
示例#3
0
        public async Task Handle200AndContentTypeAndLinkRelation()
        {
            // Arrange
            JToken root    = null;
            var    machine = new HttpResponseMachine();

            // Fallback handler
            machine.AddResponseHandler(async(l, r) =>
            {
                var text = await r.Content.ReadAsStringAsync();
                root     = JToken.Parse(text);
                return(r);
            }, HttpStatusCode.OK);

            // More specific handler
            machine.AddResponseHandler(async(l, r) =>
            {
                var text = await r.Content.ReadAsStringAsync();
                root     = JToken.Parse(text);
                return(r);
            }, HttpStatusCode.OK, linkRelation: "foolink", contentType: new MediaTypeHeaderValue("application/json"), profile: null);

            machine.AddResponseHandler(async(l, r) => { return(r); }, HttpStatusCode.OK, linkRelation: "foolink", contentType: new MediaTypeHeaderValue("application/xml"), profile: null);

            var byteArrayContent = new ByteArrayContent(Encoding.UTF8.GetBytes("{\"hello\" : \"world\"}"));

            byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            // A
            await machine.HandleResponseAsync("foolink", new HttpResponseMessage(HttpStatusCode.OK) { Content = byteArrayContent });

            // Assert
            Assert.NotNull(root);
        }
 public Controller(LoginFormModel model)
 {
     _loginFormModel = model;
     Machine         = new HttpResponseMachine();
     Machine.AddResponseHandler(LoginSuccessful, HttpStatusCode.OK, linkRelation: "login", contentType: null, profile: null);
     Machine.AddResponseHandler(LoginFailed, HttpStatusCode.Unauthorized, linkRelation: "login", contentType: null, profile: null);
     Machine.AddResponseHandler(LoginForbidden, HttpStatusCode.Forbidden, linkRelation: "login", contentType: null, profile: null);
     Machine.AddResponseHandler(FailedRequest, HttpStatusCode.BadRequest, linkRelation: "login", contentType: null, profile: null);
     Machine.AddResponseHandler(ResetForm, HttpStatusCode.OK, linkRelation: "reset", contentType: null, profile: null);
 }
        public Controller(LoginFormModel model)
        {
            _loginFormModel = model;
            Machine = new HttpResponseMachine();
            Machine.AddResponseHandler(LoginSuccessful, HttpStatusCode.OK, linkRelation: "login", contentType: null, profile: null);
            Machine.AddResponseHandler(LoginFailed, HttpStatusCode.Unauthorized, linkRelation: "login", contentType: null, profile: null);
            Machine.AddResponseHandler(LoginForbidden, HttpStatusCode.Forbidden, linkRelation: "login", contentType: null, profile: null);
            Machine.AddResponseHandler(FailedRequest, HttpStatusCode.BadRequest, linkRelation: "login", contentType: null, profile: null);
            Machine.AddResponseHandler(ResetForm, HttpStatusCode.OK, linkRelation: "reset", contentType: null, profile: null);

        }
        public async Task GetCurrentTenant()
        {
            var httpClient = CreateClient();
            bool gotTenant = false;
            var machine = new HttpResponseMachine();
            machine.AddResponseHandler(new RedirectHandler(httpClient, machine).HandleResponseAsync,HttpStatusCode.Redirect);
            machine.AddResponseHandler(async (l, r) => { gotTenant = true; return r; }, HttpStatusCode.OK);

            var tenantLink = new CurrentTenantLink();

            await httpClient.FollowLinkAsync(tenantLink, machine);

            Assert.True(gotTenant);
        }
        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);
                });

        }
示例#8
0
        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);
            }));
        }
示例#9
0
        public async Task TestUsingMachine()
        {
            var link = new Link()
            {
                Target = new Uri("http://localhost")
            };

            var responseMachine = new HttpResponseMachine();

            var notFoundHandler = new NotFoundHandler(new OkHandler(null));

            responseMachine.AddResponseHandler(notFoundHandler.HandleResponseAsync, HttpStatusCode.NotFound);


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

            await client.FollowLinkAsync(link, responseMachine);

            Assert.True(notFoundHandler.NotFound);
        }
示例#10
0
        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 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());



        }
示例#12
0
        public async Task Handle200Only()
        {
            bool ok = false;
            var machine = new HttpResponseMachine();

            machine.AddResponseHandler(async (l, r) => { ok = true; return r;}, System.Net.HttpStatusCode.OK);

            await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.OK));

            Assert.True(ok);
        }
示例#13
0
        public async Task Handle200Only()
        {
            bool ok      = false;
            var  machine = new HttpResponseMachine();

            machine.AddResponseHandler(async(l, r) => { ok = true; return(r); }, System.Net.HttpStatusCode.OK);

            await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.OK));

            Assert.True(ok);
        }
示例#14
0
        public async Task Handle404()
        {
            bool notfound = false;
            var  machine  = new HttpResponseMachine();

            machine.AddResponseHandler(async(l, r) => { notfound = true; return(r); }, HttpStatusCode.NotFound);

            await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.NotFound));

            Assert.True(notfound);
        }
示例#15
0
        public async Task HandleUnknown4XX()
        {
            bool badrequest = false;
            var  machine    = new HttpResponseMachine();

            machine.AddResponseHandler(async(l, r) => { badrequest = true; return(r); }, HttpStatusCode.BadRequest);

            await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.ExpectationFailed));

            Assert.True(badrequest);
        }
示例#16
0
        public async Task HandleUnknown4XX()
        {
            bool badrequest = false;
            var machine = new HttpResponseMachine();

            machine.AddResponseHandler(async (l, r) => { badrequest = true; return r;}, HttpStatusCode.BadRequest);

            await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.ExpectationFailed));

            Assert.True(badrequest);
        }
示例#17
0
        public async Task Handle404()
        {
            bool notfound = false;
            var machine = new HttpResponseMachine();

            machine.AddResponseHandler(async (l, r) => { notfound = true; return r;  }, HttpStatusCode.NotFound);

            await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.NotFound));

            Assert.True(notfound);
        }
        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);
        }
示例#19
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.AddResponseHandler(async(rel, r) => { uri = r.RequestMessage.RequestUri.AbsoluteUri; return(r); }, HttpStatusCode.OK);
            await client.FollowLinkAsync(link, machine);

            Assert.Equal("http://localhost/", uri);
        }
示例#20
0
        public async Task Handle200AndContentType()
        {
            // Arrange
            JToken root = null;
            var machine = new HttpResponseMachine();

            machine.AddResponseHandler(async (l, r) =>
            {
                var text = await r.Content.ReadAsStringAsync();
                root = JToken.Parse(text);
                return r;
            },HttpStatusCode.OK,null, new MediaTypeHeaderValue("application/json"));

            machine.AddResponseHandler(async (l, r) => { return r;}, HttpStatusCode.OK, null,new MediaTypeHeaderValue("application/xml"));

            var byteArrayContent = new ByteArrayContent(Encoding.UTF8.GetBytes("{\"hello\" : \"world\"}"));
            byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            
            // Act
            await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.OK) { Content = byteArrayContent});

            // Assert
            Assert.NotNull(root);
        }
示例#21
0
        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());
        }
        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());
            
        }
示例#23
0
        public async Task HandleCodeSearchResponseInMachine()
        {
            var response    = new HttpResponseMessage();
            var clientState = new ClientState();

            response.RequestMessage = new HttpRequestMessage();
            response.RequestMessage.Properties[HttpClientExtensions.PropertyKeyLinkRelation] = "codesearch";
            response.Content = new StringContent("Fake content");
            var task = Task.FromResult <HttpResponseMessage>(response);

            CodeSearchLink.CodeSearchResults result = null;

            var httpMachine = new HttpResponseMachine();

            httpMachine.AddResponseHandler(clientState.HandleResponseAsync, System.Net.HttpStatusCode.OK);
            await task.ApplyRepresentationToAsync(httpMachine);

            Assert.NotNull(clientState.SearchResult);
        }
示例#24
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.AddResponseHandler(async (rel,r) => {uri = r.RequestMessage.RequestUri.AbsoluteUri; return r;}, HttpStatusCode.OK);
            await client.FollowLinkAsync(link,machine);

            Assert.Equal("http://localhost/", uri);
        }
        public async Task HandleCodeSearchResponseInMachine()
        {
            var response = new HttpResponseMessage();
            var clientState = new ClientState();
            response.RequestMessage = new HttpRequestMessage();
            response.RequestMessage.Properties[HttpClientExtensions.PropertyKeyLinkRelation] = "codesearch";
            response.Content = new StringContent("Fake content");
            var task = Task.FromResult<HttpResponseMessage>(response);
            CodeSearchLink.CodeSearchResults result = null;

            var httpMachine = new HttpResponseMachine();
            httpMachine.AddResponseHandler(clientState.HandleResponseAsync, System.Net.HttpStatusCode.OK);
            await task.ApplyRepresentationToAsync(httpMachine);

            Assert.NotNull(clientState.SearchResult);
        }
        public async Task GetAccounts()
        {
            var httpClient = CreateClient();

            var machine = new HttpResponseMachine();
            machine.AddResponseHandler(new RedirectHandler(httpClient,machine).HandleResponseAsync, HttpStatusCode.Redirect);
            machine.AddResponseHandler(async (l, r) => r , HttpStatusCode.OK);

            var accountsLink = new AccountsLink
            {
                TenantId = "5gG32HDHLSsYAWeh9ADSZo"
            };

            var response = await httpClient.FollowLinkAsync(accountsLink, machine);

            

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

        }