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 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); }