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

Commit ec8a496

Browse files
author
Henrik Elkjær Hagen
committed
Fixed SetQueryParam to set parameters correctly.
1 parent b6b0367 commit ec8a496

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/ServiceStack.Text/HttpUtils.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ public static string SetQueryParam(this string url, string key, string val)
4040
var qsPos = url.IndexOf('?');
4141
if (qsPos != -1)
4242
{
43-
var existingKeyPos = url.IndexOf(key, qsPos, PclExport.Instance.InvariantComparison);
43+
int existingKeyPos;
44+
if(qsPos + 1 == url.IndexOf(key, qsPos, PclExport.Instance.InvariantComparison))
45+
{
46+
existingKeyPos = qsPos + 1;
47+
}
48+
else
49+
{
50+
existingKeyPos = url.IndexOf("&" + key, qsPos, PclExport.Instance.InvariantComparison);
51+
if (existingKeyPos != -1)
52+
existingKeyPos++;
53+
}
54+
4455
if (existingKeyPos != -1)
4556
{
4657
var endPos = url.IndexOf('&', existingKeyPos);

tests/ServiceStack.Text.Tests/HttpUtilsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void Can_SetQueryParam()
2121
Assert.That("http://example.com?s=0".SetQueryParam("f", "1"), Is.EqualTo("http://example.com?s=0&f=1"));
2222
Assert.That("http://example.com?f=1".SetQueryParam("f", "2"), Is.EqualTo("http://example.com?f=2"));
2323
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+
Assert.That("http://example.com?s=rf&f=1".SetQueryParam("f", "2"), Is.EqualTo("http://example.com?s=rf&f=2"));
2425
}
2526

2627
[Test]

0 commit comments

Comments
 (0)