Skip to content

Commit 5e07a97

Browse files
committed
Fix more query-string handling
1 parent 1f58899 commit 5e07a97

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

src/KubernetesClient/Kubernetes.Watch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public partial class Kubernetes
8787
Utilities.AddQueryParameter(query, "resourceVersion", resourceVersion);
8888
}
8989

90-
uriBuilder.Query = query.ToString(1, query.Length-1); // UriBuilder.Query doesn't like leading '?' chars, so trim it
90+
uriBuilder.Query = query.Length == 0 ? "" : query.ToString(1, query.Length-1); // UriBuilder.Query doesn't like leading '?' chars, so trim it
9191

9292
// Create HTTP transport objects
9393
var httpRequest = new HttpRequestMessage(HttpMethod.Get, uriBuilder.ToString());

src/KubernetesClient/Kubernetes.WebSocket.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Threading;
1515
using System.Threading.Tasks;
1616
using System.Text;
17+
using System.Globalization;
1718

1819
namespace k8s
1920
{
@@ -167,14 +168,13 @@ public partial class Kubernetes
167168

168169
uriBuilder.Path += $"api/v1/namespaces/{@namespace}/pods/{name}/portforward";
169170

170-
var q = "";
171+
var q = new StringBuilder();
171172
foreach (var port in ports)
172173
{
173-
q = QueryHelpers.AddQueryString(q, "ports", $"{port}");
174+
if (q.Length != 0) q.Append('&');
175+
q.Append("ports=").Append(port.ToString(CultureInfo.InvariantCulture));
174176
}
175-
uriBuilder.Query = q.TrimStart('?');
176-
177-
177+
uriBuilder.Query = q.ToString();
178178

179179
return StreamConnectAsync(uriBuilder.Uri, _invocationId, webSocketSubProtocol, customHeaders, cancellationToken);
180180
}
@@ -222,14 +222,13 @@ public partial class Kubernetes
222222

223223
uriBuilder.Path += $"api/v1/namespaces/{@namespace}/pods/{name}/attach";
224224

225-
uriBuilder.Query = QueryHelpers.AddQueryString(string.Empty, new Dictionary<string, string>
226-
{
227-
{ "container", container},
228-
{ "stderr", stderr ? "1": "0"},
229-
{ "stdin", stdin ? "1": "0"},
230-
{ "stdout", stdout ? "1": "0"},
231-
{ "tty", tty ? "1": "0"}
232-
}).TrimStart('?');
225+
var query = new StringBuilder();
226+
query.Append("?stderr=").Append(stderr ? '1' : '0');
227+
query.Append("&stdin=").Append(stdin ? '1' : '0');
228+
query.Append("&stdout=").Append(stdout ? '1' : '0');
229+
query.Append("&tty=").Append(tty ? '1' : '0');
230+
Utilities.AddQueryParameter(query, "container", container);
231+
uriBuilder.Query = query.ToString(1, query.Length-1); // UriBuilder.Query doesn't like leading '?' chars, so trim it
233232

234233
return StreamConnectAsync(uriBuilder.Uri, _invocationId, webSocketSubProtol, customHeaders, cancellationToken);
235234
}

tests/KubernetesClient.Tests/Kubernetes.WebSockets.Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public async Task WebSocketNamespacedPodExecAsync()
5858
};
5959

6060
Assert.Equal(mockWebSocketBuilder.PublicWebSocket, webSocket); // Did the method return the correct web socket?
61-
Assert.Equal(new Uri("ws://localhost/api/v1/namespaces/mynamespace/pods/mypod/exec?command=%2Fbin%2Fbash&command=-c&command=echo%20Hello,%20World%0Aexit%200%0A&container=mycontainer&stderr=1&stdin=1&stdout=1&tty=1"), mockWebSocketBuilder.Uri); // Did we connect to the correct URL?
61+
Assert.Equal(new Uri("ws://localhost/api/v1/namespaces/mynamespace/pods/mypod/exec?command=%2Fbin%2Fbash&command=-c&command=echo%20Hello%2C%20World%0Aexit%200%0A&container=mycontainer&stderr=1&stdin=1&stdout=1&tty=1"), mockWebSocketBuilder.Uri); // Did we connect to the correct URL?
6262
Assert.Empty(mockWebSocketBuilder.Certificates); // No certificates were used in this test
6363
Assert.Equal(expectedHeaders, mockWebSocketBuilder.RequestHeaders); // Did we use the expected headers
6464
}
@@ -136,7 +136,7 @@ public async Task WebSocketNamespacedPodAttachAsync()
136136
};
137137

138138
Assert.Equal(mockWebSocketBuilder.PublicWebSocket, webSocket); // Did the method return the correct web socket?
139-
Assert.Equal(new Uri("ws://localhost:80/api/v1/namespaces/mynamespace/pods/mypod/attach?container=my-container&stderr=1&stdin=1&stdout=1&tty=1"), mockWebSocketBuilder.Uri); // Did we connect to the correct URL?
139+
Assert.Equal(new Uri("ws://localhost:80/api/v1/namespaces/mynamespace/pods/mypod/attach?stderr=1&stdin=1&stdout=1&tty=1&container=my-container"), mockWebSocketBuilder.Uri); // Did we connect to the correct URL?
140140
Assert.Empty(mockWebSocketBuilder.Certificates); // No certificates were used in this test
141141
Assert.Equal(expectedHeaders, mockWebSocketBuilder.RequestHeaders); // Did we use the expected headers
142142
}

0 commit comments

Comments
 (0)