diff --git a/Tests/Westwind.Utilities.Test/UrlEncodingParserTests.cs b/Tests/Westwind.Utilities.Test/UrlEncodingParserTests.cs index c333759..9fea77b 100644 --- a/Tests/Westwind.Utilities.Test/UrlEncodingParserTests.cs +++ b/Tests/Westwind.Utilities.Test/UrlEncodingParserTests.cs @@ -1,7 +1,9 @@ using System; +using System.Collections.Specialized; using System.Text; using System.Collections.Generic; using System.Linq; +using System.Web; using Microsoft.VisualStudio.TestTools.UnitTesting; using Westwind.Utilities; @@ -19,6 +21,7 @@ public void QueryStringTest() string str = "http://mysite.com/page1?id=3123&format=json&action=edit&text=It's%20a%20brave%20new%20world!"; var query = new UrlEncodingParser(str); + Console.WriteLine(query); Assert.IsTrue(query["id"] == "3123"); Assert.IsTrue(query["format"] == "json", "wrong format " + query["format"]); @@ -29,11 +32,12 @@ public void QueryStringTest() query["id"] = "4123"; query["format"] = "xml"; - query["name"] = "<< It's a brave new world!"; + query["name"] = "<< It's a brave new world! say what?"; - var url = query.Write(); + var url = query.ToString(); Console.WriteLine(url); + Console.Write(query.ToString()); //http://mysite.com/page1?id=4123&format=xml&action=edit& //text=It's%20a%20brave%20new%20world!&name=%3C%3C%20It's%20a%20brave%20new%20world! } @@ -46,25 +50,25 @@ public void QueryStringMultipleTest() var query = new UrlEncodingParser(str); Assert.IsTrue(query["id"] == "3123"); - Assert.IsTrue(query["format"] == "json,xml", "wrong format " + query["format"]); + Assert.IsTrue(query["format"] == "json,xml", "wrong format " + query["format"]); // multiple format strings string[] formats = query.GetValues("format"); Assert.IsTrue(formats.Length == 2); query.SetValues("multiple", new[] - { - "1", - "2", - "3" - }); + { + "1", + "2", + "3" + }); - var url = query.Write(); + var url = query.ToString(); Console.WriteLine(url); Assert.IsTrue(url == - "http://mysite.com/page1?id=3123&format=json&format=xml&multiple=1&multiple=2&multiple=3"); + "http://mysite.com/page1?id=3123&format=json&format=xml&multiple=1&multiple=2&multiple=3"); } @@ -78,7 +82,7 @@ public void WriteUrlTest() query["id"] = "321312"; query["name"] = "rick"; - url = query.Write(); + url = query.ToString(); Console.WriteLine(url); Assert.IsTrue(url.Contains("name=")); @@ -91,7 +95,7 @@ public void WriteUrlTest() query["id"] = "321312"; query["name"] = "rick"; - url = query.Write(); + url = query.ToString(); Console.WriteLine(url); Assert.IsTrue(url.Contains("name=")); @@ -104,7 +108,7 @@ public void WriteUrlTest() query["id"] = "321312"; query["name"] = "rick"; - url = query.Write(); + url = query.ToString(); Console.WriteLine(url); Assert.IsTrue(url.Contains("name=")); @@ -118,7 +122,7 @@ public void WriteUrlTest() query["id"] = "321312"; query["name"] = "rick"; - url = query.Write(); + url = query.ToString(); Console.WriteLine(url); Assert.IsTrue(url.Contains("name=")); @@ -132,11 +136,21 @@ public void WriteUrlTest() query["id"] = "321312"; query["name"] = "rick"; - url = query.Write(); + url = query.ToString(); Console.WriteLine(url); Assert.IsTrue(url.Contains("name=")); Assert.IsTrue(!url.Contains("http://")); } + + [TestMethod] + public void HttpUtilityTest() + { + var nv = HttpUtility.ParseQueryString(""); + nv["id"] = "Rick"; + nv["format"] = "json"; + Console.WriteLine(nv); + } + } } diff --git a/Westwind.Utilities/SupportClasses/UrlEncodingParser.cs b/Westwind.Utilities/SupportClasses/UrlEncodingParser.cs index 43eb50f..1037211 100644 --- a/Westwind.Utilities/SupportClasses/UrlEncodingParser.cs +++ b/Westwind.Utilities/SupportClasses/UrlEncodingParser.cs @@ -5,6 +5,7 @@ namespace Westwind.Utilities { + /// /// A query string or UrlEncoded form parser and editor /// class that allows reading and writing of urlencoded @@ -18,15 +19,12 @@ namespace Westwind.Utilities /// /// Supports multiple values per key /// - public class UrlEncodingParser + public class UrlEncodingParser : NameValueCollection { - /// - /// Internally holds parsed or added query string values - /// - public NameValueCollection Values { get; set; } /// - /// Holds the original Url that was assigned + /// Holds the original Url that was assigned if any + /// Url must contain // to be considered a url /// private string Url { get; set; } @@ -42,7 +40,6 @@ public class UrlEncodingParser /// public UrlEncodingParser(string queryStringOrUrl = null) { - Values = new NameValueCollection(); Url = string.Empty; if (!string.IsNullOrEmpty(queryStringOrUrl)) @@ -51,27 +48,6 @@ public UrlEncodingParser(string queryStringOrUrl = null) } } - /// - /// Indexer that allows access to the query string - /// values. - /// - /// - /// - public string this[string key] - { - get { return Values[key]; } - set { Values[key] = value; } - } - - /// - /// Returns a - /// - /// - /// - public string[] GetValues(string key) - { - return Values.GetValues(key); - } /// /// Assigns multiple values to the same key @@ -81,7 +57,7 @@ public string[] GetValues(string key) public void SetValues(string key, IEnumerable values) { foreach (var val in values) - Values.Add(key, val); + Add(key, val); } /// @@ -95,11 +71,11 @@ public void SetValues(string key, IEnumerable values) /// public NameValueCollection Parse(string query) { - if (query.Contains("//")) + if (Uri.IsWellFormedUriString(query, UriKind.Absolute)) Url = query; - if (query == null) - Values = new NameValueCollection(); + if (string.IsNullOrEmpty(query)) + Clear(); else { int index = query.IndexOf('?'); @@ -115,12 +91,12 @@ public NameValueCollection Parse(string query) int index2 = pair.IndexOf('='); if (index2 > 0) { - Values.Add(pair.Substring(0, index2), pair.Substring(index2 + 1)); + Add(pair.Substring(0, index2), pair.Substring(index2 + 1)); } } } - return Values; + return this; } /// @@ -128,12 +104,12 @@ public NameValueCollection Parse(string query) /// on the internally set values. /// /// urlencoded data or url - public string Write() + public override string ToString() { string query = string.Empty; - foreach (string key in Values.Keys) + foreach (string key in Keys) { - string[] values = Values.GetValues(key); + string[] values = GetValues(key); foreach (var val in values) { query += key + "=" + Uri.EscapeUriString(val) + "&"; diff --git a/libs/Westwind.Data.MongoDb.dll b/libs/Westwind.Data.MongoDb.dll index f29764c..8bc6c45 100644 Binary files a/libs/Westwind.Data.MongoDb.dll and b/libs/Westwind.Data.MongoDb.dll differ diff --git a/libs/Westwind.Data.dll b/libs/Westwind.Data.dll index 2b5a8d8..5afa458 100644 Binary files a/libs/Westwind.Data.dll and b/libs/Westwind.Data.dll differ diff --git a/libs/Westwind.Utilities.dll b/libs/Westwind.Utilities.dll index c85dafa..8477800 100644 Binary files a/libs/Westwind.Utilities.dll and b/libs/Westwind.Utilities.dll differ diff --git a/libs/Westwind.Utilities.xml b/libs/Westwind.Utilities.xml index b45f7e6..1d59e1e 100644 --- a/libs/Westwind.Utilities.xml +++ b/libs/Westwind.Utilities.xml @@ -2744,13 +2744,6 @@ re-written with the new query string. - - - Returns a - - - - Assigns multiple values to the same key @@ -2769,30 +2762,18 @@ - + Writes out the urlencoded data/query string or full URL based on the internally set values. urlencoded data or url - - - Internally holds parsed or added query string values - - - Holds the original Url that was assigned - - - - - Indexer that allows access to the query string - values. + Holds the original Url that was assigned if any + Url must contain // to be considered a url - - diff --git a/libs/Westwind.Web.Mvc.dll b/libs/Westwind.Web.Mvc.dll index 5cbf714..a8966a9 100644 Binary files a/libs/Westwind.Web.Mvc.dll and b/libs/Westwind.Web.Mvc.dll differ diff --git a/libs/Westwind.Web.WebApi.dll b/libs/Westwind.Web.WebApi.dll index d03df20..0f5f0aa 100644 Binary files a/libs/Westwind.Web.WebApi.dll and b/libs/Westwind.Web.WebApi.dll differ diff --git a/libs/Westwind.Web.WebForms.dll b/libs/Westwind.Web.WebForms.dll index cfb60ef..dbd58e7 100644 Binary files a/libs/Westwind.Web.WebForms.dll and b/libs/Westwind.Web.WebForms.dll differ diff --git a/libs/Westwind.Web.dll b/libs/Westwind.Web.dll index 57df07c..dbea2c1 100644 Binary files a/libs/Westwind.Web.dll and b/libs/Westwind.Web.dll differ