Пример #1
0
        public void SerializesOrder()
        {
            var content = new Content(new Uri("http://test.com/order/42"), new OrderHandler(), new Order {
                Id = 54, CustomerId = 42
            });
            var    target = new XmlMediaTypeHandler();
            string actual;

            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream);
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);

            var root = XElement.Parse(actual);

            Assert.Equal("Order", root.Name.LocalName);
            XElement id = root.Element(root.GetDefaultNamespace() + "Id");

            Assert.NotNull(id);
            Assert.Equal("54", id.Value);

            XElement customerId = root.Element(root.GetDefaultNamespace() + "CustomerId");

            Assert.NotNull(customerId);
            Assert.Equal("42", customerId.Value);
        }
        public void PicksUpOrdersLinkFromCustomer()
        {
            var content = new Content(new CustomerHandler(), new Customer { Id = 42 });
            var target = new XmlContentTypeHandler();
            string actual;
            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream);
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);

            var root = XElement.Parse(actual);
            Assert.Equal("Customer", root.Name.LocalName);
            XElement id = root.Element(root.GetDefaultNamespace() + "Id");
            Assert.NotNull(id);
            Assert.Equal("42", id.Value);
            var links = root.Elements("link").ToList();
            Assert.Equal(2, links.Count);
        }
Пример #3
0
        public void PicksUpOrdersLinkFromCustomers()
        {
            var content = new Content(new Uri("http://test.com/customer/42"), new CustomerHandler(), new[] { new Customer {
                                                                                                                 Id = 42
                                                                                                             } });
            var    target = new XmlMediaTypeHandler();
            string actual;

            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream);
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);

            var root = XElement.Parse(actual);

            Assert.Equal("Customers", root.Name.LocalName);
            var      customer = root.Elements().FirstOrDefault(x => x.Name.LocalName == "Customer");
            XElement id       = customer.Element(customer.GetDefaultNamespace() + "Id");

            Assert.NotNull(id);
            Assert.Equal("42", id.Value);
            var links = customer.Elements("link").ToList();

            Assert.Equal(2, links.Count);
        }
        public void SerializesOrder()
        {
            var content = new Content(new OrderHandler(), new Order {Id = 54, CustomerId = 42});
            var target = new XmlContentTypeHandler();
            string actual;
            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream);
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);

            var root = XElement.Parse(actual);
            Assert.Equal("Order", root.Name.LocalName);
            XElement id = root.Element(root.GetDefaultNamespace() + "Id");
            Assert.NotNull(id);
            Assert.Equal("54", id.Value);
            
            XElement customerId = root.Element(root.GetDefaultNamespace() + "CustomerId");
            Assert.NotNull(customerId);
            Assert.Equal("42", customerId.Value);
        }
        public void PicksUpOrdersLinkFromCustomers()
        {
            const string idProperty = @"""Id"":42";
            const string ordersLink =
                @"{""Title"":null,""Href"":""/customer/42/orders"",""Rel"":""customer.orders"",""Type"":""application/vnd.list.order+json""}";
            const string selfLink =
                @"{""Title"":null,""Href"":""/customer/42"",""Rel"":""self"",""Type"":""application/vnd.customer+json""}]}";

            var content = new Content(new CustomerHandler(), new[] {new Customer { Id = 42 }});
            var target = new JsonContentTypeHandler();
            string actual;
            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream);
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);
            Assert.Contains(idProperty, actual);
            Assert.Contains(ordersLink, actual);
            Assert.Contains(selfLink, actual);
        }
Пример #6
0
        public void SerializingListToJsonHasExpectedData()
        {
            const string expectedString = "{\"Customers\":[{\"Id\":42}]}";

            var content = new Content(new Uri("http://test.com/customer/42"), new CustomersListHandler(), new CustomerList()
            {
                Customers = new List <Customer>()
                {
                    new Customer {
                        Id = 42
                    }
                }
            });
            var    target = new JsonMediaTypeHandler();
            string actual;

            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);
            Assert.Equal(expectedString, actual);
        }
Пример #7
0
        public void SerializesOrder()
        {
            var content = new Content(new Uri("http://test.com/order/42"), new OrderHandler(), new Order {
                Id = 54, CustomerId = 42
            });
            var    target = new XmlMediaTypeHandler();
            string actual;

            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream);
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);

            const string expected = "<Order xmlns='http://schemas.datacontract.org/2004/07/Simple.Web.Xml.Tests'" +
                                    "       xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
                                    "  <CustomerId>42</CustomerId>" +
                                    "  <Id>54</Id>" +
                                    "</Order>";

            XElement.Parse(actual).ShouldEqual(expected);
        }
Пример #8
0
        public void PicksUpOrdersLinkFromCustomers()
        {
            var content = new Content(new Uri("http://test.com/customer/42"), new CustomerHandler(), new[] { new Customer {
                                                                                                                 Id = 42
                                                                                                             } });
            var    target = new XmlMediaTypeHandler();
            string actual;

            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream);
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);

            const string expected = "<?xml version='1.0' encoding='utf-8'?>" +
                                    "<Customers>" +
                                    "  <Customer xmlns='http://schemas.datacontract.org/2004/07/Simple.Web.Xml.Tests'" +
                                    "            xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
                                    "    <Id>42</Id>" +
                                    "    <link href='/customer/42/orders' rel='customer.orders' type='application/vnd.list.order+xml' xmlns='' />" +
                                    "    <link href='/customer/42' rel='self' type='application/vnd.customer+xml' xmlns='' />" +
                                    "  </Customer>" +
                                    "</Customers>";

            XElement.Parse(actual).ShouldEqual(expected);
        }
Пример #9
0
        public void PicksUpOrdersLinkFromCustomers()
        {
            const string idProperty = @"""Id"":42";
            const string ordersLink =
                @"{""Title"":null,""Href"":""/customer/42/orders"",""Rel"":""customer.orders"",""Type"":""application/vnd.list.order+json""}";
            const string selfLink =
                @"{""Title"":null,""Href"":""/customer/42"",""Rel"":""self"",""Type"":""application/vnd.customer+json""}]}";

            var content = new Content(new Uri("http://test.com/customer/42"), new CustomerHandler(), new[] { new Customer {
                                                                                                                 Id = 42
                                                                                                             } });
            var    target = new JsonMediaTypeHandler();
            string actual;

            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);
            Assert.Contains(idProperty, actual);
            Assert.Contains(ordersLink, actual);
            Assert.Contains(selfLink, actual);
        }
Пример #10
0
        public void WritesJsonWithEnum()
        {
            var content = new Content(new Uri("http://test.com/EnumCustomer/42"), new EnumCustomerHandler(), new[] { new EnumCustomer()
                                                                                                                     {
                                                                                                                         AnEnum = MyEnum.Ian
                                                                                                                     } });
            var target = new JsonMediaTypeHandler();

            string actual;

            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;

                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }

                stream.ForceDispose();
            }

            Assert.NotNull(actual);
        }
Пример #11
0
        public void AddsSelfLinkToChildCollectionItems()
        {
            var customer = new Customer
            {
                Id     = 42,
                Orders = new List <Order> {
                    new Order {
                        CustomerId = 42, Id = 54
                    }
                }
            };
            var content = new Content(new Uri("http://test.com/customer/42"), new CustomerHandler(), customer);
            var target  = new JsonMediaTypeHandler();

            string actual;

            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);
            var jobj   = JObject.Parse(actual);
            var orders = jobj["orders"] as JArray;

            Assert.NotNull(orders);
            var order = orders[0] as JObject;

            Assert.NotNull(order);
            var links = order["links"] as JArray;

            Assert.NotNull(links);
            var self = links.FirstOrDefault(jt => jt["rel"].Value <string>() == "self");

            Assert.NotNull(self);
            Assert.Equal("/order/54", self["href"].Value <string>());
            Assert.Equal("application/vnd.order+json", self["type"].Value <string>());
        }
        public void SerializingListToJsonHasExpectedData()
        {
            const string expectedString = "{\"Customers\":[{\"Id\":42}]}";

            var content = new Content(new Uri("http://test.com/customer/42"), new CustomersListHandler(), new CustomerList() { Customers = new List<Customer>() { new Customer { Id = 42 } } });
            var target = new JsonMediaTypeHandler();
            string actual;
            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);      
            Assert.Equal(expectedString, actual);
        }
        public void PicksUpContactsLinkFromCustomer()
        {
            const string contactsLink =
                @"{""title"":null,""href"":""/customer/42/contacts"",""rel"":""customer.contacts"",""type"":""application/json""}";

            var content = new Content(new Uri("http://test.com/customer/42"), new CustomerHandler(), new Customer { Id = 42 });
            var target = new JsonMediaTypeHandler();
            string actual;
            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);
            Assert.Contains(contactsLink, actual);
        }
Пример #14
0
        public void SerializesCyrillicText()
        {
            const string russian = "Мыа алиё лаборамюз ед, ведят промпта элыктрам квюо ты.";
            var          content = new Content(new Uri("http://test.com/customer/42"), new ThingHandler(), new Thing {
                Path = russian
            });
            var    target = new JsonMediaTypeHandler();
            string actual;

            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }

            Assert.Contains(russian, actual);
        }
Пример #15
0
        public void PicksUpContactsLinkFromCustomer()
        {
            const string contactsLink =
                @"{""title"":null,""href"":""/customer/42/contacts"",""rel"":""customer.contacts"",""type"":""application/json""}";

            var content = new Content(new Uri("http://test.com/customer/42"), new CustomerHandler(), new Customer {
                Id = 42
            });
            var    target = new JsonMediaTypeHandler();
            string actual;

            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);
            Assert.Contains(contactsLink, actual);
        }
Пример #16
0
        public void PicksUpPathFromThing()
        {
            const string thingLink =
                @"{""title"":null,""href"":""/things?path=%2Ffoo%2Fbar"",""rel"":""self"",""type"":""application/json""}";

            var content = new Content(new Uri("http://test.com/foo/bar"), new ThingHandler(), new Thing {
                Path = "/foo/bar"
            });
            var    target = new JsonMediaTypeHandler();
            string actual;

            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);
            Assert.Contains(thingLink, actual);
        }
        public void AddsSelfLinkToChildCollectionItems()
        {
            var customer = new Customer
                               {
                                   Id = 42,
                                   Orders = new List<Order> {new Order {CustomerId = 42, Id = 54}}
                               };
            var content = new Content(new Uri("http://test.com/customer/42"), new CustomerHandler(), customer);
            var target = new JsonMediaTypeHandler();

            string actual;
            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);
            var jobj = JObject.Parse(actual);
            var orders = jobj["orders"] as JArray;
            Assert.NotNull(orders);
            var order = orders[0] as JObject;
            Assert.NotNull(order);
            var links = order["links"] as JArray;
            Assert.NotNull(links);
            var self = links.FirstOrDefault(jt => jt["rel"].Value<string>() == "self");
            Assert.NotNull(self);
            Assert.Equal("/order/54", self["href"].Value<string>());
            Assert.Equal("application/vnd.order+json", self["type"].Value<string>());
        }
        public void PicksUpPathFromThing()
        {
            const string thingLink =
                @"{""title"":null,""href"":""/things?path=%2Ffoo%2Fbar"",""rel"":""self"",""type"":""application/json""}";

            var content = new Content(new Uri("http://test.com/foo/bar"), new ThingHandler(), new Thing { Path = "/foo/bar" });
            var target = new JsonMediaTypeHandler();
            string actual;
            using (var stream = new NonClosingMemoryStream(new MemoryStream()))
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    actual = reader.ReadToEnd();
                }
                stream.ForceDispose();
            }
            Assert.NotNull(actual);
            Assert.Contains(thingLink, actual);
        }