public void GetPocoXmlDataContract()
        {
            GenericWebService service = GenericWebService.GetServiceInstance(typeof(MyPocoType), typeof(MyPocoType), useDataContract: true);

            service.OnGetReturnInstance = () => new MyPocoType()
            {
                Id = 1, Name = "Mary"
            };

            XmlMediaTypeFormatter formatter = new XmlMediaTypeFormatter();

            formatter.SetSerializer(typeof(MyPocoType), new DataContractSerializer(typeof(MyPocoType)));

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(GenericWebService.UriTemplate, UriKind.Relative));

            HttpServiceHostAssert.Execute(
                service,
                request,
                (response) =>
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Response status code should have been a 200.");
                MyPocoType pocoResult = response.Content.ReadAs <MyPocoType>(new MediaTypeFormatter[] { formatter });
                Assert.AreEqual("Mary", pocoResult.Name, "Failed to read MyPocoType using DataContract Xml serializer.");
            });
        }
        public void PostPocoJson()
        {
            GenericWebService service = GenericWebService.GetServiceInstance(typeof(MyPocoType), typeof(MyPocoType));

            MyPocoType pocoPosted = new MyPocoType()
            {
                Id = 1, Name = "Mary"
            };

            HttpRequestMessage <MyPocoType> request = new HttpRequestMessage <MyPocoType>(
                pocoPosted,
                HttpMethod.Post,
                new Uri(GenericWebService.UriTemplate, UriKind.Relative),
                new MediaTypeFormatter[0]);

            request.Content.Headers.ContentType = JsonMediaType;
            request.Headers.Accept.Add(JsonMediaType);

            HttpServiceHostAssert.Execute(
                service,
                request,
                (response) =>
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Response status code should have been a 200.");
                MyPocoType pocoResult = response.Content.ReadAs <MyPocoType>();
                Assert.AreEqual(pocoPosted.Name, pocoResult.Name, "Failed to POST then read MyPocoType using Json serializer.");
            });
        }
        public void PostAndReadAsXml()
        {
            IEnumerable <TestData> testData   = TestData.RepresentativeValueAndRefTypeTestDataCollection;
            TestDataVariations     variations = TestDataVariations.AllNonInterfaces;
            XmlMediaTypeFormatter  formatter  = new XmlMediaTypeFormatter();

            MediaTypeFormatter[] formatters = new MediaTypeFormatter[] { formatter };

            TestDataAssert.Execute(
                testData,
                variations,
                "ObjectContent serializing using the Xml DataContractSerializer failed.",
                (type, obj) =>
            {
                GenericWebService service = GenericWebService.GetServiceInstance(type, obj.GetType());
                HttpServiceHostAssert.Execute(
                    service,
                    CreateXmlSerializerPostRequest(type, obj, XmlMediaType),
                    (response) =>
                {
                    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Response status code should have been a 200.");

                    formatter.SetSerializer(type, new DataContractSerializer(type, new Type[] { obj.GetType() }));
                    string stringContent = response.Content.ReadAsString();
                    object readObj       = response.Content.ReadAs(type, formatters);
                    TestDataAssert.AreEqual(obj, readObj, string.Format("Failed to round trip type '{0}'.  Content was:\r\n{1}.", type.Name, stringContent));
                });
            });
        }
        public void PostPocoXmlDataContract()
        {
            GenericWebService     service   = GenericWebService.GetServiceInstance(typeof(MyPocoType), typeof(MyPocoType), useDataContract: true);
            XmlMediaTypeFormatter formatter = new XmlMediaTypeFormatter();

            formatter.SetSerializer(typeof(MyPocoType), new DataContractSerializer(typeof(MyPocoType)));
            IEnumerable <MediaTypeFormatter> formatters = new MediaTypeFormatter[] { formatter };

            MyPocoType pocoPosted = new MyPocoType()
            {
                Id = 1, Name = "Mary"
            };

            HttpRequestMessage <MyPocoType> request = new HttpRequestMessage <MyPocoType>(
                pocoPosted,
                HttpMethod.Post,
                new Uri(GenericWebService.UriTemplate, UriKind.Relative),
                formatters);

            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
            HttpServiceHostAssert.Execute(
                service,
                request,
                (response) =>
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Response status code should have been a 200.");
                MyPocoType pocoResult = response.Content.ReadAs <MyPocoType>(formatters);
                Assert.AreEqual(pocoPosted.Name, pocoResult.Name, "Failed to POST then read MyPocoType using DataContract Xml serializer.");
            });
        }
        public void GetPocoJson()
        {
            GenericWebService service = GenericWebService.GetServiceInstance(typeof(MyPocoType), typeof(MyPocoType));

            service.OnGetReturnInstance = () => new MyPocoType()
            {
                Id = 1, Name = "Mary"
            };

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(GenericWebService.UriTemplate, UriKind.Relative));

            request.Headers.Accept.Add(JsonMediaType);

            HttpServiceHostAssert.Execute(
                service,
                request,
                (response) =>
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Response status code should have been a 200.");
                MyPocoType pocoResult = response.Content.ReadAs <MyPocoType>();
                Assert.AreEqual("Mary", pocoResult.Name, "Failed to read MyPocoType using Json serializer.");
            });
        }