Ejemplo n.º 1
0
        public void RoundTripUsingJsonNet()
        {
            //Arrange
            var formatters = new MediaTypeFormatterCollection() {new JsonNetFormatter()};
            var fromContact = new Contact { FirstName = "Brad", LastName = "Abrams" };

            // Act
            var fromContent = new ObjectContent<Contact>(fromContact, new MediaTypeHeaderValue("application/json"), formatters);
            var contentString = fromContent.ReadAsStringAsync().Result;

            var toContent = new ObjectContent<Contact>(new StringContent(contentString), formatters);
            toContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var toContact = toContent.ReadAsAsync<Contact>().Result;

            //Arrange
            Assert.AreEqual(fromContact.FirstName, toContact.FirstName);
            Assert.AreEqual(fromContact.LastName, toContact.LastName);
        }
Ejemplo n.º 2
0
        public void RoundTripUsingBson()
        {
            //Arrange
            var formatters = new[] { new JsonNetFormatter() };
            var fromContact = new Contact { FirstName = "Brad", LastName = "Abrams" };

            // Act
            var fromContent = new ObjectContent<Contact>(fromContact, "application/bson", formatters);
            var contentReadStream = fromContent.ReadAsStreamAsync().Result;
            var newStream = new MemoryStream();
            contentReadStream.Position = 0;
            contentReadStream.CopyTo(newStream);
            newStream.Position = 0;
            var toContent = new ObjectContent<Contact>(new StreamContent(newStream), formatters);
            toContent.Headers.ContentType = new MediaTypeHeaderValue("application/bson");
            var toContact = toContent.ReadAsAsync<Contact>().Result;

            //Arrange
            Assert.AreEqual(fromContact.FirstName, toContact.FirstName);
            Assert.AreEqual(fromContact.LastName, toContact.LastName);
        }
Ejemplo n.º 3
0
 public void Post(Contact contact)
 {
     _Contacts.Add(contact);
 }