Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
Refactored UrlEncodingParser to be based directly of NameValueCollect…
Browse files Browse the repository at this point in the history
…ion to maximize code reuse. Mimic existing interface behavior with slight enhancements for returning full URLs in ToString().
  • Loading branch information
RickStrahl committed Sep 7, 2014
1 parent b149f19 commit 3b8ed8d
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 74 deletions.
44 changes: 29 additions & 15 deletions Tests/Westwind.Utilities.Test/UrlEncodingParserTests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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"]);
Expand All @@ -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!
}
Expand All @@ -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");

}

Expand All @@ -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="));
Expand All @@ -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="));
Expand All @@ -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="));
Expand All @@ -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="));
Expand All @@ -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);
}

}
}
50 changes: 13 additions & 37 deletions Westwind.Utilities/SupportClasses/UrlEncodingParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Westwind.Utilities
{

/// <summary>
/// A query string or UrlEncoded form parser and editor
/// class that allows reading and writing of urlencoded
Expand All @@ -18,15 +19,12 @@ namespace Westwind.Utilities
/// <remarks>
/// Supports multiple values per key
/// </remarks>
public class UrlEncodingParser
public class UrlEncodingParser : NameValueCollection
{
/// <summary>
/// Internally holds parsed or added query string values
/// </summary>
public NameValueCollection Values { get; set; }

/// <summary>
/// Holds the original Url that was assigned
/// Holds the original Url that was assigned if any
/// Url must contain // to be considered a url
/// </summary>
private string Url { get; set; }

Expand All @@ -42,7 +40,6 @@ public class UrlEncodingParser
/// </param>
public UrlEncodingParser(string queryStringOrUrl = null)
{
Values = new NameValueCollection();
Url = string.Empty;

if (!string.IsNullOrEmpty(queryStringOrUrl))
Expand All @@ -51,27 +48,6 @@ public UrlEncodingParser(string queryStringOrUrl = null)
}
}

/// <summary>
/// Indexer that allows access to the query string
/// values.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string this[string key]
{
get { return Values[key]; }
set { Values[key] = value; }
}

/// <summary>
/// Returns a
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string[] GetValues(string key)
{
return Values.GetValues(key);
}

/// <summary>
/// Assigns multiple values to the same key
Expand All @@ -81,7 +57,7 @@ public string[] GetValues(string key)
public void SetValues(string key, IEnumerable<string> values)
{
foreach (var val in values)
Values.Add(key, val);
Add(key, val);
}

/// <summary>
Expand All @@ -95,11 +71,11 @@ public void SetValues(string key, IEnumerable<string> values)
/// <returns></returns>
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('?');
Expand All @@ -115,25 +91,25 @@ 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;
}

/// <summary>
/// Writes out the urlencoded data/query string or full URL based
/// on the internally set values.
/// </summary>
/// <returns>urlencoded data or url</returns>
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) + "&";
Expand Down
Binary file modified libs/Westwind.Data.MongoDb.dll
Binary file not shown.
Binary file modified libs/Westwind.Data.dll
Binary file not shown.
Binary file modified libs/Westwind.Utilities.dll
Binary file not shown.
25 changes: 3 additions & 22 deletions libs/Westwind.Utilities.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified libs/Westwind.Web.Mvc.dll
Binary file not shown.
Binary file modified libs/Westwind.Web.WebApi.dll
Binary file not shown.
Binary file modified libs/Westwind.Web.WebForms.dll
Binary file not shown.
Binary file modified libs/Westwind.Web.dll
Binary file not shown.

0 comments on commit 3b8ed8d

Please sign in to comment.