private static void ValidateFormUrlEncoded(string encoded, string expectedResult) { byte[] data = Encoding.UTF8.GetBytes(encoded); for (var cnt = 1; cnt <= data.Length; cnt++) { ICollection <KeyValuePair <string, string> > collection; FormUrlEncodedParser parser = FormUrlEncodedParserTests.CreateParser( data.Length + 1, out collection ); Assert.NotNull(parser); int totalBytesConsumed; ParserState state = FormUrlEncodedParserTests.ParseBufferInSteps( parser, data, cnt, out totalBytesConsumed ); Assert.Equal(ParserState.Done, state); Assert.Equal(data.Length, totalBytesConsumed); JObject result = FormUrlEncodedJson.Parse(collection); Assert.NotNull(result); Assert.Equal(expectedResult, result.ToString(Newtonsoft.Json.Formatting.None)); } }
/// <summary> /// Called during deserialization to read an object of the specified <paramref name="type"/> /// from the specified <paramref name="stream"/>. /// </summary> /// <param name="type">The type of object to read.</param> /// <param name="stream">The <see cref="Stream"/> from which to read.</param> /// <param name="contentHeaders">The <see cref="HttpContentHeaders"/> for the content being read.</param> /// <param name="formatterLogger">The <see cref="IFormatterLogger"/> to log events to.</param> /// <returns>A <see cref="Task"/> whose result will be the object instance that has been read.</returns> public override Task <object> ReadFromStreamAsync(Type type, Stream stream, HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger) { if (type == null) { throw new ArgumentNullException("type"); } if (stream == null) { throw new ArgumentNullException("stream"); } return(TaskHelpers.RunSynchronously <object>(() => { IEnumerable <KeyValuePair <string, string> > nameValuePairs = ReadFormUrlEncoded(stream, ReadBufferSize); if (type == typeof(FormDataCollection)) { return new FormDataCollection(nameValuePairs); } if (FormattingUtilities.IsJTokenType(type)) { return FormUrlEncodedJson.Parse(nameValuePairs, _maxDepth); } // Passed us an unsupported type. Should have called CanReadType() first. throw new InvalidOperationException( RS.Format(Properties.Resources.SerializerCannotSerializeType, GetType().Name, type.Name)); })); }
private static void ParseInvalidFormUrlEncoded(string encoded) { byte[] data = Encoding.UTF8.GetBytes(encoded); for (var cnt = 1; cnt <= data.Length; cnt++) { ICollection <KeyValuePair <string, string> > collection; FormUrlEncodedParser parser = FormUrlEncodedParserTests.CreateParser( data.Length + 1, out collection ); Assert.NotNull(parser); int totalBytesConsumed; ParserState state = FormUrlEncodedParserTests.ParseBufferInSteps( parser, data, cnt, out totalBytesConsumed ); Assert.Equal(ParserState.Done, state); Assert.Equal(data.Length, totalBytesConsumed); Assert.ThrowsArgument( () => { FormUrlEncodedJson.Parse(collection); }, null ); } }
private object ReadFromStream(Type type, Stream readStream) { object result; IEnumerable <KeyValuePair <string, string> > nameValuePairs = ReadFormUrlEncoded( readStream, ReadBufferSize ); if (type == typeof(FormDataCollection)) { result = new FormDataCollection(nameValuePairs); } else if (FormattingUtilities.IsJTokenType(type)) { result = FormUrlEncodedJson.Parse(nameValuePairs, _maxDepth); } else { // Passed us an unsupported type. Should have called CanReadType() first. throw Error.InvalidOperation( Properties.Resources.SerializerCannotSerializeType, GetType().Name, type.Name ); } return(result); }
public void ParseThrowsMaxDepthExceeded() { // Depth of 'a[b]=1' is 3 IEnumerable <KeyValuePair <string, string> > query = CreateQuery(new KeyValuePair <string, string>("a[b]", "1")); Assert.ThrowsArgument(() => { FormUrlEncodedJson.Parse(query, 2); }, null); // This should succeed Assert.NotNull(FormUrlEncodedJson.Parse(query, 3)); }
public void ParseThrowsInvalidMaxDepth() { Assert.ThrowsArgumentGreaterThanOrEqualTo( () => FormUrlEncodedJson.Parse(CreateQuery(), -1), "maxDepth", "1", -1 ); Assert.ThrowsArgumentGreaterThanOrEqualTo( () => FormUrlEncodedJson.Parse(CreateQuery(), 0), "maxDepth", "1", 0 ); }
public void ParseThrowsOnNull() { Assert.ThrowsArgumentNull(() => FormUrlEncodedJson.Parse(null), null); }
public void GeneratedJTokenTest() { 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; JTokenCreatorSurrogate jsonValueCreator = new JTokenCreatorSurrogate(); try { for (int i = 0; i < 1000; i++) { JToken jv = (JToken)jsonValueCreator.CreateInstanceOf(typeof(JToken), rndGen); if (jv.Type == JTokenType.Array || jv.Type == JTokenType.Object) { string jaStr = FormUrlEncoding(jv); byte[] data = Encoding.UTF8.GetBytes(jaStr); for (var cnt = 1; cnt <= data.Length; cnt += 4) { ICollection <KeyValuePair <string, string> > collection; FormUrlEncodedParser parser = FormUrlEncodedParserTests.CreateParser(data.Length + 1, out collection); Assert.NotNull(parser); int totalBytesConsumed; ParserState state = FormUrlEncodedParserTests.ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed); Assert.Equal(ParserState.Done, state); Assert.Equal(data.Length, totalBytesConsumed); JObject deserJo = FormUrlEncodedJson.Parse(collection); Assert.NotNull(deserJo); bool compare = true; if (((IDictionary <string, JToken>)deserJo).ContainsKey("JV")) { compare = JTokenRoundTripComparer.Compare(jv, deserJo["JV"]); } else { compare = JTokenRoundTripComparer.Compare(jv, deserJo); } Assert.True(compare, "Comparison failed for test instance " + i); } } } } finally { CreatorSettings.MaxArrayLength = oldMaxArray; CreatorSettings.MaxListLength = oldMaxList; CreatorSettings.MaxStringLength = oldMaxStr; CreatorSettings.NullValueProbability = oldNullProbability; CreatorSettings.CreateOnlyAsciiChars = oldCreateAscii; } }