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.When(HttpStatusCode.OK)
            .Then(async(lr, r) =>
            {
                {
                    guid = Guid.Parse(r.Content.ReadAsStringAsync().Result);
                }
            });


            await httpClient.FollowLinkAsync(link, machine);

            Assert.Equal(content, guid.ToString());
        }
Example #2
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);
            }));
        }
Example #3
0
        public async Task Handle200AndContentType()
        {
            // Arrange
            JToken root = null;
            var machine = new HttpResponseMachine();

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

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

            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);
        }
Example #4
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 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());



        }
Example #6
0
        public async Task Handle200AndContentType()
        {
            // Arrange
            JToken root    = null;
            var    machine = new HttpResponseMachine();

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

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

            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);
        }
Example #7
0
        public async Task DispatchBasedOnMediaTypeWithParser()
        {
            JToken value = null;

            var parserStore = new ParserStore();

            parserStore.AddMediaTypeParser <JToken>("application/json", async(content) =>
            {
                var stream = await content.ReadAsStreamAsync();
                return(JToken.Load(new JsonTextReader(new StreamReader(stream))));
            });

            var machine = new HttpResponseMachine(parserStore);

            machine.When(HttpStatusCode.OK)
            .Then <JToken>((m, l, jt) => { value = jt; });

            var jsonContent = new StringContent("{ \"Hello\" : \"world\" }");

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

            machine.HandleResponseAsync("", new HttpResponseMessage()
            {
                Content = jsonContent
            });

            Assert.NotNull(value);
        }
        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 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);
        }
Example #10
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
            .When(HttpStatusCode.NotFound)
            .Then((l, r) => notFoundHandler.HandleResponseAsync(l, r));

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

            await client.FollowLinkAsync(link, responseMachine);

            Assert.True(notFoundHandler.NotFound);
        }
Example #11
0
        public async Task Handle200AndContentTypeUsingTypedMachine()
        {
            // Arrange
            var appmodel = new Model <JToken>
            {
                Value = null
            };

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

            machine.When(HttpStatusCode.OK, null, new MediaTypeHeaderValue("application/json"))
            .Then(async(model, linkrelation, response) =>
            {
                var text    = await response.Content.ReadAsStringAsync();
                model.Value = JToken.Parse(text);
            });

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


            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(appmodel.Value);
        }
 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);

        }
Example #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);
        }
Example #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);
        }
Example #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);
        }
Example #17
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);
        }
Example #18
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);
        }
Example #19
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);
        }
Example #20
0
        public async Task Handle200Only()
        {
            bool ok = false;
            var machine = new HttpResponseMachine();

            machine.When(HttpStatusCode.OK)
                .Then(async (l, r) => { ok = true; });

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

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

            machine.When(HttpStatusCode.OK)
            .Then(async(l, r) => { ok = true; });

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

            Assert.True(ok);
        }
Example #22
0
        public async Task DispatchBasedOnStatusCodeMediaTypeAndProfile()
        {
            Person testPerson = null;

            var parserStore = new ParserStore();

            // Define method to translate response body into DOM for specified media type
            parserStore.AddMediaTypeParser <JToken>("application/json", async(content) =>
            {
                var stream = await content.ReadAsStreamAsync();
                return(JToken.Load(new JsonTextReader(new StreamReader(stream))));
            });

            // Define method to translate media type DOM into application domain object instance based on profile
            parserStore.AddProfileParser <JToken, Person>(new Uri("http://example.org/person"), (jt) =>
            {
                var person       = new Person();
                var jobject      = (JObject)jt;
                person.FirstName = (string)jobject["FirstName"];
                person.LastName  = (string)jobject["LastName"];

                return(person);
            });

            var machine = new HttpResponseMachine(parserStore);

            // Define action in HttpResponseMachine for all responses that return 200 OK and can be translated somehow to a Person
            machine
            .When(HttpStatusCode.OK)
            .Then <Person>((m, l, p) => { testPerson = p; });


            // Create a sample body
            var jsonContent = new StringContent("{ \"FirstName\" : \"Bob\", \"LastName\" : \"Bang\"  }");

            jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            jsonContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("profile", "\"http://example.org/person\""));

            // Create a sample response
            var httpResponseMessage = new HttpResponseMessage()
            {
                Content = jsonContent
            };

            // Allow machine to dispatch response
            machine.HandleResponseAsync("", httpResponseMessage);

            Assert.NotNull(testPerson);
            Assert.Equal("Bob", testPerson.FirstName);
            Assert.Equal("Bang", testPerson.LastName);
        }
        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 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);
        }
Example #25
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 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 #27
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 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);
        }
Example #29
0
        public async Task DispatchBasedOnStatusCodeAndLinkRelationAndParseProfile()
        {
            Model<Person> test = new Model<Person>();

            var parserStore = new ParserStore();
            // Define method to translate response body into DOM for specified media type 
            parserStore.AddMediaTypeParser<JToken>("application/json", async (content) =>
            {
                var stream = await content.ReadAsStreamAsync();
                return JToken.Load(new JsonTextReader(new StreamReader(stream)));
            });

            // Define method to translate media type DOM into application domain object instance based on profile
            parserStore.AddLinkRelationParser<JToken, Person>("person-link", (jt) =>
            {
                var person = new Person();
                var jobject = (JObject)jt;
                person.FirstName = (string)jobject["FirstName"];
                person.LastName = (string)jobject["LastName"];

                return person;
            });

            var machine = new HttpResponseMachine<Model<Person>>(test,parserStore);


            // Define action in HttpResponseMachine for all responses that return 200 OK and can be translated somehow to a Person
            machine.When(HttpStatusCode.OK)
                   .Then<Person>((m, l, p) => { m.Value = p; });

            // Create a sample body
            var jsonContent = new StringContent("{ \"FirstName\" : \"Bob\", \"LastName\" : \"Bang\"  }");
            jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
      
            // Create a sample response 
            var httpResponseMessage = new HttpResponseMessage()
            {
                Content = jsonContent
            };

            // Allow machine to dispatch response
            machine.HandleResponseAsync("person-link", httpResponseMessage);

            Assert.NotNull(test.Value);
            Assert.Equal("Bob", test.Value.FirstName);
            Assert.Equal("Bang", test.Value.LastName);
        }
        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);
        }
Example #31
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 #32
0
        public async Task DispatchBasedOnMediaTypeWithParser()
        {
            JToken value = null;

            var parserStore = new ParserStore();

            parserStore.AddMediaTypeParser<JToken>("application/json", async (content) =>
            {
                var stream = await content.ReadAsStreamAsync();
                return JToken.Load(new JsonTextReader(new StreamReader(stream)));
            });

            var machine = new HttpResponseMachine(parserStore);

            machine.When(HttpStatusCode.OK)
                .Then<JToken>((m, l, jt) => { value = jt; } );

            var jsonContent = new StringContent("{ \"Hello\" : \"world\" }");
            jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            machine.HandleResponseAsync("", new HttpResponseMessage()
            {
                Content = jsonContent
            });

            Assert.NotNull(value);
        }
        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);

        }
Example #34
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);
        }
Example #35
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 RedirectHandler(HttpClient httpClient, HttpResponseMachine machine)
 {
     _httpClient = httpClient;
     _machine = machine;
 }