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

Commit ff9a2f0

Browse files
committed
Fix SetQueryParam / SetHashParam issue
1 parent acc7a79 commit ff9a2f0

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/ServiceStack.Text/HttpUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static string SetQueryParam(this string url, string key, string val)
4646
var qsPos = url.IndexOf('?');
4747
if (qsPos != -1)
4848
{
49-
var existingKeyPos = qsPos + 1 == url.IndexOf(key, qsPos, PclExport.Instance.InvariantComparison)
49+
var existingKeyPos = qsPos + 1 == url.IndexOf(key + "=", qsPos, PclExport.Instance.InvariantComparison)
5050
? qsPos
5151
: url.IndexOf("&" + key, qsPos, PclExport.Instance.InvariantComparison);
5252

@@ -85,7 +85,7 @@ public static string SetHashParam(this string url, string key, string val)
8585
var hPos = url.IndexOf('#');
8686
if (hPos != -1)
8787
{
88-
var existingKeyPos = hPos + 1 == url.IndexOf(key, hPos, PclExport.Instance.InvariantComparison)
88+
var existingKeyPos = hPos + 1 == url.IndexOf(key + "=", hPos, PclExport.Instance.InvariantComparison)
8989
? hPos
9090
: url.IndexOf("/" + key, hPos, PclExport.Instance.InvariantComparison);
9191

tests/ServiceStack.Text.Tests/HttpUtilsTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public void Can_AddQueryParam()
1515
Assert.That("http://example.com?s=rf&f=1".AddQueryParam("f", "2"), Is.EqualTo("http://example.com?s=rf&f=1&f=2"));
1616
Assert.That("http://example.com?".AddQueryParam("f", "1"), Is.EqualTo("http://example.com?f=1"));
1717
Assert.That("http://example.com?f=1&".AddQueryParam("f", "2"), Is.EqualTo("http://example.com?f=1&f=2"));
18+
Assert.That("http://example.com?ab=0".AddQueryParam("a", "1"), Is.EqualTo("http://example.com?ab=0&a=1"));
1819
}
1920

2021
[Test]
@@ -25,6 +26,7 @@ public void Can_SetQueryParam()
2526
Assert.That("http://example.com?f=1".SetQueryParam("f", "2"), Is.EqualTo("http://example.com?f=2"));
2627
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"));
2728
Assert.That("http://example.com?s=rf&f=1".SetQueryParam("f", "2"), Is.EqualTo("http://example.com?s=rf&f=2"));
29+
Assert.That("http://example.com?ab=0".SetQueryParam("a", "1"), Is.EqualTo("http://example.com?ab=0&a=1"));
2830
}
2931

3032
[Test]
@@ -35,6 +37,7 @@ public void Can_AddHashParam()
3537
Assert.That("http://example.com#f=1".AddHashParam("f", "2"), Is.EqualTo("http://example.com#f=1/f=2"));
3638
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"));
3739
Assert.That("http://example.com#s=rf/f=1".AddHashParam("f", "2"), Is.EqualTo("http://example.com#s=rf/f=1/f=2"));
40+
Assert.That("http://example.com#ab=0".AddHashParam("a", "1"), Is.EqualTo("http://example.com#ab=0/a=1"));
3841
}
3942

4043
[Test]
@@ -45,6 +48,7 @@ public void Can_SetHashParam()
4548
Assert.That("http://example.com#f=1".SetHashParam("f", "2"), Is.EqualTo("http://example.com#f=2"));
4649
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"));
4750
Assert.That("http://example.com#s=rf/f=1".SetHashParam("f", "2"), Is.EqualTo("http://example.com#s=rf/f=2"));
51+
Assert.That("http://example.com#ab=0".SetHashParam("a", "1"), Is.EqualTo("http://example.com#ab=0/a=1"));
4852
}
4953
}
5054
}

0 commit comments

Comments
 (0)