Пример #1
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);
        }
Пример #2
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);
        }