Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 5a9faf3

Browse files
committed
Add SetHashParam and test
1 parent bccffc4 commit 5a9faf3

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/ServiceStack.Text/HttpUtils.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ public static string AddHashParam(this string url, string key, string val)
6868
return url + prefix + key + "=" + val.UrlEncode();
6969
}
7070

71+
public static string SetHashParam(this string url, string key, string val)
72+
{
73+
if (string.IsNullOrEmpty(url)) return null;
74+
var hPos = url.IndexOf('#');
75+
if (hPos != -1)
76+
{
77+
var existingKeyPos = url.IndexOf(key, hPos, PclExport.Instance.InvariantComparison);
78+
if (existingKeyPos != -1)
79+
{
80+
var endPos = url.IndexOf('/', existingKeyPos);
81+
if (endPos == -1) endPos = url.Length;
82+
83+
var newUrl = url.Substring(0, existingKeyPos + key.Length + 1)
84+
+ val.UrlEncode()
85+
+ url.Substring(endPos);
86+
return newUrl;
87+
}
88+
}
89+
var prefix = url.IndexOf('#') == -1 ? "#" : "/";
90+
return url + prefix + key + "=" + val.UrlEncode();
91+
}
92+
7193
public static string GetJsonFromUrl(this string url,
7294
Action<HttpWebRequest> requestFilter = null, Action<HttpWebResponse> responseFilter = null)
7395
{
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using NUnit.Framework;
2+
3+
namespace ServiceStack.Text.Tests
4+
{
5+
[TestFixture]
6+
public class HttpUtilsTests
7+
{
8+
[Test]
9+
public void Can_AddQueryParam()
10+
{
11+
Assert.That("http://example.com".AddQueryParam("f", "1"), Is.EqualTo("http://example.com?f=1"));
12+
Assert.That("http://example.com?s=0".AddQueryParam("f", "1"), Is.EqualTo("http://example.com?s=0&f=1"));
13+
Assert.That("http://example.com?f=1".AddQueryParam("f", "2"), Is.EqualTo("http://example.com?f=1&f=2"));
14+
Assert.That("http://example.com?s=0&f=1&s=1".AddQueryParam("f", "2"), Is.EqualTo("http://example.com?s=0&f=1&s=1&f=2"));
15+
}
16+
17+
[Test]
18+
public void Can_SetQueryParam()
19+
{
20+
Assert.That("http://example.com".SetQueryParam("f", "1"), Is.EqualTo("http://example.com?f=1"));
21+
Assert.That("http://example.com?s=0".SetQueryParam("f", "1"), Is.EqualTo("http://example.com?s=0&f=1"));
22+
Assert.That("http://example.com?f=1".SetQueryParam("f", "2"), Is.EqualTo("http://example.com?f=2"));
23+
Assert.That("http://example.com?s=0&f=1&s=1".SetQueryParam("f", "2"), Is.EqualTo("http://example.com?s=0&f=2&s=1"));
24+
}
25+
26+
[Test]
27+
public void Can_AddHashParam()
28+
{
29+
Assert.That("http://example.com".AddHashParam("f", "1"), Is.EqualTo("http://example.com#f=1"));
30+
Assert.That("http://example.com#s=0".AddHashParam("f", "1"), Is.EqualTo("http://example.com#s=0/f=1"));
31+
Assert.That("http://example.com#f=1".AddHashParam("f", "2"), Is.EqualTo("http://example.com#f=1/f=2"));
32+
Assert.That("http://example.com#s=0/f=1/s=1".AddHashParam("f", "2"), Is.EqualTo("http://example.com#s=0/f=1/s=1/f=2"));
33+
}
34+
35+
[Test]
36+
public void Can_SetHashParam()
37+
{
38+
Assert.That("http://example.com".SetHashParam("f", "1"), Is.EqualTo("http://example.com#f=1"));
39+
Assert.That("http://example.com#s=0".SetHashParam("f", "1"), Is.EqualTo("http://example.com#s=0/f=1"));
40+
Assert.That("http://example.com#f=1".SetHashParam("f", "2"), Is.EqualTo("http://example.com#f=2"));
41+
Assert.That("http://example.com#s=0/f=1/s=1".SetHashParam("f", "2"), Is.EqualTo("http://example.com#s=0/f=2/s=1"));
42+
}
43+
}
44+
}

tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<Compile Include="EnumerableTests.cs" />
180180
<Compile Include="AttributeTests.cs" />
181181
<Compile Include="CsvTypeTests.cs" />
182+
<Compile Include="HttpUtilsTests.cs" />
182183
<Compile Include="JsonTests\JsonEnumTests.cs" />
183184
<Compile Include="JsonTests\InvalidJsonTests.cs" />
184185
<Compile Include="JsonTests\OnDeserializationErrorTests.cs" />

0 commit comments

Comments
 (0)