예제 #1
0
        void TestFormEncodedParsing(string encoded, string expectedResult)
        {
            JsonObject result = FormUrlEncodedExtensions.ParseFormUrlEncoded(encoded);

            Assert.IsNotNull(result);
            Assert.AreEqual(expectedResult, result.ToString());
        }
예제 #2
0
        public void LegacyArraysTest()
        {
            ValidateFormsEncodingParsing("a=1&a=hello&a=333", "{\"a\":[\"1\",\"hello\",\"333\"]}");

            // Only valid in shallow serialization
            ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a[z]=2&a[z]=3"));
        }
        HttpResponseMessage Post(HttpRequestMessage req)
        {
            var     body        = req.Content.ReadAsString();
            dynamic formContent = FormUrlEncodedExtensions.ParseFormUrlEncoded(body);
            string  description = formContent.Description;

            if (description == null)
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.BadRequest
                });
            }
            var td = new ToDo(description);

            _repo.Add(td);
            var response = new HttpResponseMessage();

            response.StatusCode = HttpStatusCode.Created;
            string uriString = req.RequestUri.AbsoluteUri + "/" + td.Id.ToString();
            Uri    uri       = new Uri(uriString);

            response.Headers.Location = uri;
            response.Content          = new StringContent(uri.AbsoluteUri, Encoding.UTF8, "text/uri-list");
            return(response);
        }
예제 #4
0
        static void ValidateFormsEncodingParsing(string formUrlEncoded, string expectedJson)
        {
            JsonValue jv = FormUrlEncodedExtensions.ParseFormUrlEncoded(formUrlEncoded);

            Assert.IsNotNull(jv);
            Assert.AreEqual(expectedJson, jv.ToString());
        }
        HttpResponseMessage Put(int id, HttpRequestMessage req)
        {
            ToDo td = _repo.ToDos.Where(t => t.Id == id).FirstOrDefault();

            if (td == null)
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.NotFound
                });
            }
            var     body        = req.Content.ReadAsString();
            dynamic formContent = FormUrlEncodedExtensions.ParseFormUrlEncoded(body);
            string  description = formContent.Description;

            if (description == null)
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.BadRequest
                });
            }
            td.Description = description;
            return(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content =
                    new StringContent(td.ToString())
            });
        }
예제 #6
0
        HttpResponseMessage Post(HttpRequestMessage req)
        {
            var     body        = req.Content.ReadAsString();
            dynamic formContent = FormUrlEncodedExtensions.ParseFormUrlEncoded(body);
            string  imageName   = formContent.Image;

            try
            {
                var proc     = Process.Start(imageName);
                var response = new HttpResponseMessage();
                response.StatusCode = HttpStatusCode.Created;
                string uriString = req.RequestUri.AbsoluteUri + "/" + proc.Id.ToString();
                Console.WriteLine(uriString);
                Uri uri = new Uri(uriString);
                response.Headers.Location = uri;
                response.Content          = new StringContent(uri.AbsoluteUri, Encoding.UTF8, "text/uri-list");
                return(response);
            }
            catch (Win32Exception e)
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.BadRequest
                });
            }
        }
예제 #7
0
        public HttpResponseMessage Post(HttpRequestMessage request)
        {
            var     body        = request.Content.ReadAsString();
            dynamic formContent = FormUrlEncodedExtensions.ParseFormUrlEncoded(body);
            var     person      = (string)formContent.person;

            peopleToSayHelloTo.Add(person);
            var response = new HttpResponseMessage();

            response.Content    = new StringContent(string.Format("Added {0}", person), Encoding.UTF8, "text/plain");
            response.StatusCode = HttpStatusCode.Created;
            return(response);
        }
예제 #8
0
 public void InvalidObjectGraphsTest()
 {
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a[b]=1&a=2"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a[b]=1&a[b][]=2"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a[x][]=1&a[x][0]=2"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a=2&a[b]=1"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("[]=1"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a[][]=0"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a[][x]=0"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a&a=1"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a=1&a"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a&a[b]=1"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a[]=1&a"));
 }
예제 #9
0
        public override object OnReadFromStream(Type type, Stream stream, HttpContentHeaders contentHeaders)
        {
            var reader      = new StreamReader(stream);
            var jsonContent = reader.ReadToEnd();
            var jsonObject  = FormUrlEncodedExtensions.ParseFormUrlEncoded(jsonContent);

            if (typeof(JsonValue) == type)
            {
                return(jsonObject);
            }

            var value = jsonObject.ReadAsType(type);

            return(value);
        }
예제 #10
0
        public void GeneratedJsonValueTest()
        {
            Random rndGen             = new Random(1);
            int    oldMaxArray        = CreatorSettings.MaxArrayLength;
            int    oldMaxList         = CreatorSettings.MaxListLength;
            int    oldMaxStr          = CreatorSettings.MaxStringLength;
            double oldNullProbability = CreatorSettings.NullValueProbability;
            bool   oldCreateAscii     = CreatorSettings.CreateOnlyAsciiChars;

            CreatorSettings.MaxArrayLength       = 5;
            CreatorSettings.MaxListLength        = 3;
            CreatorSettings.MaxStringLength      = 3;
            CreatorSettings.NullValueProbability = 0;
            CreatorSettings.CreateOnlyAsciiChars = true;
            JsonValueCreatorSurrogate jsonValueCreator = new JsonValueCreatorSurrogate();

            try
            {
                for (int i = 0; i < 1000; i++)
                {
                    JsonValue jv = (JsonValue)jsonValueCreator.CreateInstanceOf(typeof(JsonValue), rndGen);
                    if (jv.JsonType == JsonType.Array || jv.JsonType == JsonType.Object)
                    {
                        string    jaStr   = FormUrlEncoding(jv);
                        JsonValue deserJv = FormUrlEncodedExtensions.ParseFormUrlEncoded(jaStr);
                        bool      compare = true;
                        if (deserJv is JsonObject && ((JsonObject)deserJv).ContainsKey("JV"))
                        {
                            compare = JsonValueRoundTripComparer.Compare(jv, deserJv["JV"]);
                        }
                        else
                        {
                            compare = JsonValueRoundTripComparer.Compare(jv, deserJv);
                        }

                        Assert.IsTrue(compare, "Comparison failed for test instance " + i);
                    }
                }
            }
            finally
            {
                CreatorSettings.MaxArrayLength       = oldMaxArray;
                CreatorSettings.MaxListLength        = oldMaxList;
                CreatorSettings.MaxStringLength      = oldMaxStr;
                CreatorSettings.NullValueProbability = oldNullProbability;
                CreatorSettings.CreateOnlyAsciiChars = oldCreateAscii;
            }
        }
예제 #11
0
        public override object ReadFromStream(Stream stream, HttpRequestMessage request)
        {
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var reader      = new StreamReader(stream);
            var jsonContent = reader.ReadToEnd();
            var jsonObject  = FormUrlEncodedExtensions.ParseFormUrlEncoded(jsonContent);

            if (this.isJsonValueParameter)
            {
                return(jsonObject);
            }

            var tempStream = new MemoryStream();

            jsonObject.Save(tempStream);
            tempStream.Position = 0;
            var serializer = new DataContractJsonSerializer(Parameter.ParameterType);

            return(serializer.ReadObject(tempStream));
        }
예제 #12
0
        public void SimpleStringsTest()
        {
            string     anyString = "abc";
            JsonObject jv        = FormUrlEncodedExtensions.ParseFormUrlEncoded(anyString);

            Assert.AreEqual(1, jv.Count);
            Assert.IsTrue(jv.ContainsKey(anyString));
            Assert.AreEqual(anyString, jv.Keys.First());
            Assert.IsNull(jv[anyString]);

            string escaped = "%2eabc%2e";

            ValidateFormsEncodingParsing(escaped, "{\".abc.\":null}");

            ValidateFormsEncodingParsing("", "{}");
            ValidateFormsEncodingParsing("?", "{}");

            string anyFormUrlEncoded = "a=1";

            Assert.IsNotNull(FormUrlEncodedExtensions.ParseFormUrlEncoded(anyFormUrlEncoded));

            ExceptionTestHelper.ExpectException <ArgumentNullException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded((string)null));
        }
예제 #13
0
 private static JsonObject ParseFormUrlEncoded(string data)
 {
     return(FormUrlEncodedExtensions.ParseFormUrlEncoded(data));
 }
예제 #14
0
 public void InvalidFormUrlEncodingTest()
 {
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a[b=2"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a[[b]=2"));
     ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a[b]]=2"));
 }