示例#1
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);
        }
示例#2
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);
        }
示例#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);
        }
示例#4
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);
        }
示例#5
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);
        }
示例#6
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);
        }
示例#7
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);
        }
示例#8
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);
        }
示例#9
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);
        }
示例#10
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);
        }
示例#11
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);
        }
示例#12
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);
        }
示例#13
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);
        }
示例#14
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);
        }
示例#15
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);
        }
示例#16
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);
        }
示例#17
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);
        }
示例#18
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);
        }