public void PicksUpOrdersLinkFromCustomers()
        {
            var content = new Content(new Uri("http://test.com/customer/42"), new CustomerHandler(),
                                      new[] {new Customer {Id = 42}});
            var target = new DataContractXmlMediaTypeHandler();
            string actual;
            using (var stream = new StringBuilderStream())
            {
                target.Write<IEnumerable<Customer>>(content, stream).Wait();
                actual = stream.StringValue;
            }
            Assert.NotNull(actual);

            const string expected = "<?xml version='1.0' encoding='utf-8'?>" +
                                    "<Customers>" +
                                    "  <Customer xmlns='http://schemas.datacontract.org/2004/07/Simple.Web.TestHelpers.Sample'" +
                                    "            xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
                                    "    <Id>42</Id>" +
                                    "    <Orders i:nil='true' />" +
                                    "    <link href='/customer/42/contacts' rel='customer.contacts' type='application/vnd.contact+xml' xmlns='' />" +
                                    "    <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);
        }
        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 Uri("http://test.com/people"), new PeopleHandler(), people);
            var target = new HalJsonMediaTypeHandler();
            using (var stream = new MemoryStream())
            {
                target.Write<IEnumerable<Person>>(content, stream).Wait();
                stream.Position = 0;
                string text = new StreamReader(stream).ReadToEnd();
                actual = JObject.Parse(text);
            }

            var array = (JArray) actual["collection"];
            Assert.Equal(2, array.Count);
            JToken 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 SerializesOrder()
        {
            var content = new Content(new Uri("http://test.com/order/42"), new OrderHandler(),
                                      new Order {Id = 54, CustomerId = 42});
            var target = new ExplicitXmlMediaTypeHandler();
            string actual;
            using (var stream = new StringBuilderStream())
            {
                target.Write<Order>(content, stream).Wait();
                actual = stream.StringValue;
            }
            Assert.NotNull(actual);

            const string expected = "<Order Id='54' CustomerId='42'>" +
                                    "  <link href='/order/54' rel='self' type='application/vnd.order+xml' />" +
                                    "</Order>";

            XElement.Parse(actual).ShouldEqual(expected);
        }
        public void WritesObjectWithLinks()
        {
            JObject actual;

            var person = new Person {Name = "Marvin", Location = "Car Park"};
            var content = new Content(new Uri("http://test.com/person/Marvin"), new PersonHandler(), person);
            var target = new HalJsonMediaTypeHandler();
            using (var stream = new MemoryStream())
            {
                target.Write<Person>(content, stream).Wait();
                stream.Position = 0;
                string 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 PicksUpOrdersLinkFromCustomer()
        {
            var content = new Content(new Uri("http://test.com/customer/42"), new CustomerHandler(),
                                      new Customer {Id = 42});
            var target = new ExplicitXmlMediaTypeHandler();
            string actual;
            using (var stream = new StringBuilderStream())
            {
                target.Write<Customer>(content, stream).Wait();
                actual = stream.StringValue;
            }
            Assert.NotNull(actual);

            const string expected = "<Customer Id='42'>" +
                                    "  <link href='/customer/42/contacts' rel='customer.contacts' type='application/vnd.contact+xml' />" +
                                    "  <link href='/customer/42/orders' rel='customer.orders' type='application/vnd.list.order+xml' />" +
                                    "  <link href='/customer/42' rel='self' type='application/vnd.customer+xml' />" +
                                    "</Customer>";

            XElement.Parse(actual).ShouldEqual(expected);
        }
        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 DataContractXmlMediaTypeHandler();
            string actual;
            using (var stream = new StringBuilderStream())
            {
                target.Write<Order>(content, stream).Wait();
                actual = stream.StringValue;
            }
            Assert.NotNull(actual);

            const string expected =
                "<Order xmlns='http://schemas.datacontract.org/2004/07/Simple.Web.TestHelpers.Sample'" +
                "       xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
                "  <CustomerId>42</CustomerId>" +
                "  <Id>54</Id>" +
                "  <link href='/order/54' rel='self' type='application/vnd.order+xml' xmlns='' />" +
                "</Order>";

            XElement.Parse(actual).ShouldEqual(expected);
        }