Beispiel #1
0
        public void TestAutomaticFormatSelectionFromAcceptHeader()
        {
            int    x, y;
            string expectedJson, expectedXml;

            WebHttpBehavior3Tests.CreateInputValuesAndExpectedResults(3, out x, out y, out expectedJson, out expectedXml);

            string xmlInput  = String.Format(CultureInfo.InvariantCulture, "<AddJsonOrXml xmlns=\"http://tempuri.org/\"><x>{0}</x><y>{1}</y></AddJsonOrXml>", x, y);
            string jsonInput = String.Format(CultureInfo.InvariantCulture, "{{\"x\":{0}, \"y\":{1}}}", x, y);

            WebHttpBinding   binding  = new WebHttpBinding();
            WebHttpBehavior3 behavior = new WebHttpBehavior3();

            behavior.AutomaticFormatSelectionEnabled = true;

            using (ServiceHost host = new ServiceHost(typeof(JQueryWCFService), new Uri(WebHttpBehavior3Tests.Endpoint)))
            {
                host.AddServiceEndpoint(typeof(IJQueryWCF), binding, "").Behaviors.Add(behavior);
                try
                {
                    host.Open();

                    foreach (bool useJsonInput in new bool[] { false, true })
                    {
                        foreach (bool useAcceptJson in new bool[] { false, true })
                        {
                            string input = useJsonInput ? jsonInput : xmlInput;
                            string requestContentType          = useJsonInput ? "application/json" : "application/xml";
                            string expectedResponseContentType = useAcceptJson ? WebHttpBehavior3Tests.ApplicationJsonContentTypeWithCharset : "application/xml; charset=utf-8";
                            string expectedResponseBody        = useAcceptJson ? expectedJson : expectedXml;
                            string acceptHeader = useAcceptJson ? "application/json" : "application/xml";

                            Console.WriteLine("Sending {0} request with Accept: {1}", requestContentType, acceptHeader);
                            Dictionary <string, string> headers = new Dictionary <string, string>();
                            headers.Add("Accept", acceptHeader);

                            string          address      = WebHttpBehavior3Tests.Endpoint + "/AddJsonOrXml";
                            HttpWebResponse httpResponse = WebHttpBehavior3Tests.SendRequest("POST", address, requestContentType, input, Encoding.UTF8, headers);
                            WebHttpBehavior3Tests.ValidateHttpResponse(httpResponse, HttpStatusCode.OK, expectedResponseContentType, expectedResponseBody);
                        }
                    }
                }
                catch
                {
                    host.Abort();
                    throw;
                }
            }
        }
Beispiel #2
0
        public void TestAutomaticFormatSelectionFromInput()
        {
            int    x, y;
            string expectedJson, expectedXml;

            WebHttpBehavior3Tests.CreateInputValuesAndExpectedResults(3, out x, out y, out expectedJson, out expectedXml);

            string xmlInput  = String.Format(CultureInfo.InvariantCulture, "<AddJsonOrXml xmlns=\"http://tempuri.org/\"><x>{0}</x><y>{1}</y></AddJsonOrXml>", x, y);
            string jsonInput = String.Format(CultureInfo.InvariantCulture, "{{\"x\":{0}, \"y\":{1}}}", x, y);

            WebHttpBinding   binding  = new WebHttpBinding();
            WebHttpBehavior3 behavior = new WebHttpBehavior3();

            behavior.AutomaticFormatSelectionEnabled = true;

            Test <JQueryWCFService, IJQueryWCF>(binding, behavior, "POST", WebHttpBehavior3Tests.Endpoint + "/AddJsonOrXml", "application/xml; charset=utf-8", xmlInput, HttpStatusCode.OK, "application/xml; charset=utf-8", expectedXml);
            Test <JQueryWCFService, IJQueryWCF>(binding, behavior, "POST", WebHttpBehavior3Tests.Endpoint + "/AddJsonOrXml", "application/json", jsonInput, HttpStatusCode.OK, WebHttpBehavior3Tests.ApplicationJsonContentTypeWithCharset, expectedJson);
        }