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); }
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); }
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 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()); }
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); }
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 .When(System.Net.HttpStatusCode.OK) .Then((l, r) => grh.HandleResponseAsync(l, r)); 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 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()); }
public async Task Handle404() { bool notfound = false; var machine = new HttpResponseMachine(); machine.When(HttpStatusCode.NotFound) .Then(async(l, r) => { notfound = true; }); await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.NotFound)); Assert.True(notfound); }
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); }
public async Task HandleUnknown4XX() { bool badrequest = false; var machine = new HttpResponseMachine(); machine.When(HttpStatusCode.BadRequest) .Then(async (l, r) => { badrequest = true; }); await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.ExpectationFailed)); Assert.True(badrequest); }
public async Task HandleUnknown4XX() { bool badrequest = false; var machine = new HttpResponseMachine(); machine.When(HttpStatusCode.BadRequest) .Then(async(l, r) => { badrequest = true; }); await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.ExpectationFailed)); Assert.True(badrequest); }
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); }
public async Task Handle404() { bool notfound = false; var machine = new HttpResponseMachine(); machine.When(HttpStatusCode.NotFound) .Then(async (l, r) => { notfound = true; }); await machine.HandleResponseAsync("", new HttpResponseMessage(HttpStatusCode.NotFound)); Assert.True(notfound); }
public Controller(LoginFormModel model) { _loginFormModel = model; Machine = new HttpResponseMachine(); Machine .When(HttpStatusCode.OK, linkRelation: "login", contentType: null, profile: null) .Then(LoginSuccessful); Machine.When(HttpStatusCode.Unauthorized, linkRelation: "login", contentType: null, profile: null) .Then(LoginFailed); Machine.When(HttpStatusCode.Forbidden, linkRelation: "login", contentType: null, profile: null) .Then(LoginForbidden); Machine.When(HttpStatusCode.BadRequest, linkRelation: "login", contentType: null, profile: null) .Then(FailedRequest); Machine.When(HttpStatusCode.OK, linkRelation: "reset", contentType: null, profile: null) .Then(ResetForm); }
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.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); }
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); }
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.When(HttpStatusCode.BadRequest) .Then((l, r) => { throw new Exception(); }); 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.When(HttpStatusCode.BadRequest) .Then((l, r) => { throw new Exception(); }); var task = httpClient.FollowLinkAsync(new GuidServiceLink(), machine); Assert.Throws <AggregateException>(() => task.Wait()); }
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 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 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); }
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); }