Esempio n. 1
0
        public void TestJsonInputWithJsonValue()
        {
            List <string> jsonInputs = new List <string>
            {
                "null",
                "123",
                "123.45",
                "\"hello\"",
                "true",
                "false",
                "[]",
                "{}",
                "[1,2,3]",
                "[[[1],[2,2]],3,[4,5,6]]",
                "{\"a\":123,\"b\":null,\"c\":[1,2],\"d\":{\"e\":1}}",
            };

            string[] contentTypes = new string[]
            {
                "text/json",
                "text/json; charset=utf-8",
                "application/json",
                ApplicationJsonContentTypeWithCharset,
            };

            int contentTypeIndex = 0;

            foreach (string json in jsonInputs)
            {
                string contentType = contentTypes[contentTypeIndex];
                Console.WriteLine("Sending JSON {0} with content-type {1}", json, contentType);
                WebHttpBehavior3Tests.Test("POST", Endpoint + "/Echo", contentType, json, HttpStatusCode.OK, json == "null" ? "" : ApplicationJsonContentTypeWithCharset, json == "null" ? "" : json);
                contentTypeIndex = (contentTypeIndex + 1) % contentTypes.Length;
            }
        }
Esempio n. 2
0
        public void WebGetTest()
        {
            string queryString = "/JQueryGet/Foo/Bar?Address=Capital+Hill&customer%5BName%5D=Pete&customer%5BAddress%5D=Redmond&" +
                                 "customer%5BAge%5D%5B0%5D%5B%5D=23&customer%5BAge%5D%5B0%5D%5B%5D=24&customer%5BAge%5D%5B1%5D%5B%5D=25&" +
                                 "customer%5BAge%5D%5B1%5D%5B%5D=26&customer%5BPhones%5D%5B%5D=425+888+1111&customer%5BPhones%5D%5B%5D=425+345+7777&" +
                                 "customer%5BPhones%5D%5B%5D=425+888+4564&customer%5BEnrolmentDate%5D=%2FDate(1277243030667)%2F&role=PM&changeDate=3&count=15";
            string expectedReturn = @"{""Name"":""Yavor"",""Address"":""Capital Hill"",""Age"":[[""23"",""24""],[""25"",""26""]],""Phones"":[""425 888 1111"",""425 345 7777"",""425 888 4564""],""EnrolmentDate"":""\/Date(1277243030667)\/""}";

            WebHttpBehavior3Tests.Test("GET", Endpoint + queryString, null, null, HttpStatusCode.OK, ApplicationJsonContentTypeWithCharset, expectedReturn);
        }
Esempio n. 3
0
        public void TestJsonInputWithNoJsonValue()
        {
            int    x, y;
            string expectedJson, expectedXml;

            CreateInputValuesAndExpectedResults(1, out x, out y, out expectedJson, out expectedXml);

            string json = String.Format(CultureInfo.InvariantCulture, "{{\"x\":{0}, \"y\":{1}}}", x, y);

            WebHttpBehavior3Tests.Test("POST", Endpoint + "/AddJson", "application/json", json, HttpStatusCode.OK, ApplicationJsonContentTypeWithCharset, expectedJson);
            WebHttpBehavior3Tests.Test("POST", Endpoint + "/AddXml", "application/json", json, HttpStatusCode.OK, ApplicationJsonContentTypeWithCharset, expectedJson);
        }
Esempio n. 4
0
        public void TestXmlInputWithNoJsonValue()
        {
            int    x, y;
            string expectedJson, expectedXml;

            CreateInputValuesAndExpectedResults(2, out x, out y, out expectedJson, out expectedXml);

            string addXmlInput  = String.Format(CultureInfo.InvariantCulture, "<AddXml xmlns=\"http://tempuri.org/\"><x>{0}</x><y>{1}</y></AddXml>", x, y);
            string addJsonInput = addXmlInput.Replace("AddXml", "AddJson");

            WebHttpBehavior3Tests.Test("POST", Endpoint + "/AddJson", "text/xml", addJsonInput, HttpStatusCode.OK, "text/xml; charset=utf-8", expectedXml);
            WebHttpBehavior3Tests.Test("POST", Endpoint + "/AddXml", "text/xml", addXmlInput, HttpStatusCode.OK, "text/xml; charset=utf-8", expectedXml);
        }
Esempio n. 5
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;
                }
            }
        }
Esempio n. 6
0
 static void Test <TService, IContract>(WebHttpBinding binding, WebHttpBehavior3 behavior, string method, string address, string contentType, string body, HttpStatusCode expectedHttpStatus, string expectedResponseContentType, string expectedResponseBody)
 {
     using (ServiceHost host = new ServiceHost(typeof(TService), new Uri(WebHttpBehavior3Tests.Endpoint)))
     {
         host.AddServiceEndpoint(typeof(IContract), binding, "").Behaviors.Add(behavior);
         try
         {
             host.Open();
             WebHttpBehavior3Tests.Test(method, address, contentType, body, expectedHttpStatus, expectedResponseContentType, expectedResponseBody);
         }
         catch
         {
             host.Abort();
             throw;
         }
     }
 }
Esempio n. 7
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);
        }
Esempio n. 8
0
        static void WebInvokeTestWithEncoding(Encoding encoding, string contentType)
        {
            string jsonString = @"customer%5BName%5D=Pete&customer%5BAddress%5D=Redmond&customer%5BAge%5D%5B0%5D%5B%5D=23&" +
                                "customer%5BAge%5D%5B0%5D%5B%5D=24&customer%5BAge%5D%5B1%5D%5B%5D=25&customer%5BAge%5D%5B1%5D%5B%5D=26&" +
                                "customer%5BPhones%5D%5B%5D=425+888+1111&customer%5BPhones%5D%5B%5D=425+345+7777&" +
                                "customer%5BPhones%5D%5B%5D=425+888+4564&customer%5BEnrolmentDate%5D=%2FDate(1277243030664)%2F&" +
                                "customers%5B0%5D%5BName%5D=Pete2&customers%5B0%5D%5BAddress%5D=Redmond2&customers%5B0%5D%5BAge%5D%5B0%5D%5B%5D=23&" +
                                "customers%5B0%5D%5BAge%5D%5B0%5D%5B%5D=24&customers%5B0%5D%5BAge%5D%5B1%5D%5B%5D=25&" +
                                "customers%5B0%5D%5BAge%5D%5B1%5D%5B%5D=26&customers%5B0%5D%5BPhones%5D%5B%5D=425+888+1111&" +
                                "customers%5B0%5D%5BPhones%5D%5B%5D=425+345+7777&customers%5B0%5D%5BPhones%5D%5B%5D=425+888+4564&" +
                                "customers%5B0%5D%5BEnrolmentDate%5D=%2FDate(1277243030664)%2F&customers%5B1%5D%5BName%5D=Pete3&" +
                                "customers%5B1%5D%5BAddress%5D=Redmond3&customers%5B1%5D%5BAge%5D%5B0%5D%5B%5D=23&customers%5B1%5D%5BAge%5D%5B0%5D%5B%5D=24&" +
                                "customers%5B1%5D%5BAge%5D%5B1%5D%5B%5D=25&customers%5B1%5D%5BAge%5D%5B1%5D%5B%5D=26&" +
                                "customers%5B1%5D%5BPhones%5D%5B%5D=425+888+1111&customers%5B1%5D%5BPhones%5D%5B%5D=425+345+7777&" +
                                "customers%5B1%5D%5BPhones%5D%5B%5D=425+888+4564&customers%5B1%5D%5BEnrolmentDate%5D=%2FDate(1277243030664)%2F&role=NewRole&changeDate=3&count=15";
            string expectedReturn = @"{""Name"":""Yavor"",""Address"":""Redmond"",""Age"":[[""23"",""24""],[""25"",""26""]],""Phones"":[""425 888 1111"",""425 345 7777"",""425 888 4564""],""EnrolmentDate"":""\/Date(1277243030664)\/""}";

            WebHttpBehavior3Tests.Test("POST", Endpoint + "/JQuery/Foo/Bar", contentType, jsonString, encoding, HttpStatusCode.OK, ApplicationJsonContentTypeWithCharset, expectedReturn);
        }
Esempio n. 9
0
 public void TestJsonValueNotLastParameter()
 {
     WebHttpBehavior3Tests.Test("POST", Endpoint + "/GetKeyValue/b", FormUrlEncodedContentType, "a=1&b=2", HttpStatusCode.OK, ApplicationJsonContentTypeWithCharset, "\"2\"");
 }
Esempio n. 10
0
 public void WebInvokeWithWebOperationContextChangingContentType()
 {
     WebHttpBehavior3Tests.Test("POST", Endpoint + "/PostSettingHttpHeaders", FormUrlEncodedContentType, "Content-Type=text%2Fplain", HttpStatusCode.OK, "text/plain", "{\"Content-Type\":\"text\\/plain\"}");
 }
Esempio n. 11
0
 public void WebInvokeWithWebOperationContext()
 {
     WebHttpBehavior3Tests.Test("POST", Endpoint + "/PostSettingHttpHeaders", FormUrlEncodedContentType, "Cache-Control=no-cache", HttpStatusCode.OK, ApplicationJsonContentTypeWithCharset, "{\"Cache-Control\":\"no-cache\"}");
 }
Esempio n. 12
0
 public void WebGetWithWebOperationContext()
 {
     WebHttpBehavior3Tests.Test("GET", Endpoint + "/GetSettingHttpHeaders?X-Foo=something", null, null, HttpStatusCode.OK, ApplicationJsonContentTypeWithCharset, "{\"X-Foo\":\"something\"}");
 }