Пример #1
0
        public void WritesCollectionWithLinks()
        {
            JObject actual;

            var people = new List<Person>
                             {
                                 new Person {Name = "Marvin", Location = "Car Park"},
                                 new Person {Name = "Zaphod", Location = "The Restaurant at the End of the Universe"}
                             };
            var content = new Content(new PeopleHandler(), people);
            var target = new HalJsonMediaTypeHandler();
            using (var stream = new MemoryStream())
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                var text = new StreamReader(stream).ReadToEnd();
                actual = JObject.Parse(text);
            }

            var array = (JArray) actual["collection"];
            Assert.Equal(2, array.Count);
            var marvin = array.First;
            Assert.Equal("Marvin", marvin["name"]);
            Assert.Equal("Car Park", marvin["location"]);
            var marvinLinks = (JObject) marvin["_links"];
            Assert.Equal("/person/Marvin", marvinLinks["self"]["href"]);
            var links = (JObject)actual["_links"];
            Assert.Equal("/people", links["self"]["href"]);
        }
        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);
        }
        public void PicksUpOrdersLinkFromCustomers()
        {
            var content = new Content(new CustomerHandler(), new[] {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("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);
        }
Пример #5
0
        private static void WriteUsingContentTypeHandler(object handler, IContext context)
        {
            IContentTypeHandler contentTypeHandler;
            if (TryGetContentTypeHandler(context, out contentTypeHandler))
            {
                context.Response.ContentType = contentTypeHandler.GetContentType(context.Request.AcceptTypes);

                if (context.Request.HttpMethod.Equals("HEAD")) return;

                var content = new Content(handler, null);
                contentTypeHandler.Write(content, context.Response.OutputStream);
            }
        }
        public void SerializingListToJsonHasExpectedData()
        {
            const string expectedString = "{\"Customers\":[{\"Id\":42,\"Orders\":null}]}";

            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 StringBuilderStream())
            {
                target.Write<CustomerList>(content, stream).Wait();
                actual = stream.StringValue;
            }
            Assert.NotNull(actual);      
            Assert.Equal(expectedString, actual);
        }
Пример #7
0
        public void WritesObjectWithLinks()
        {
            JObject actual;

            var person = new Person {Name = "Marvin", Location = "Car Park"};
            var content = new Content(new PersonHandler(), person);
            var target = new HalJsonMediaTypeHandler();
            using (var stream = new MemoryStream())
            {
                target.Write(content, stream).Wait();
                stream.Position = 0;
                var text = new StreamReader(stream).ReadToEnd();
                actual = JObject.Parse(text);
            }

            Assert.Equal("Marvin", actual["name"]);
            Assert.Equal("Car Park", actual["location"]);
            var links = (JObject)actual["_links"];
            Assert.Equal("/person/Marvin", links["self"]["href"]);
        }
        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);
        }
        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);
        }