Skip to content

Commit 0c2deb8

Browse files
authored
Merge pull request #279 from graphql-dotnet/fix-uri-scheme-comparison
Fix uri conversion and add some tests to prove its working
2 parents 527bf44 + 01367df commit 0c2deb8

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/GraphQL.Client/UriExtensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class UriExtensions
99
/// </summary>
1010
/// <param name="uri"></param>
1111
/// <returns></returns>
12-
public static bool HasWebSocketScheme(this Uri uri) => uri.Scheme.Equals("wss") || uri.Scheme.Equals("ws");
12+
public static bool HasWebSocketScheme(this Uri uri) => uri.Scheme.Equals("wss", StringComparison.OrdinalIgnoreCase) || uri.Scheme.Equals("ws", StringComparison.OrdinalIgnoreCase);
1313

1414
/// <summary>
1515
/// Infers the websocket uri from <paramref name="uri"/>.
@@ -21,8 +21,16 @@ public static Uri GetWebSocketUri(this Uri uri)
2121
if (uri.HasWebSocketScheme())
2222
return uri;
2323

24-
string webSocketScheme = uri.Scheme == "https" ? "wss" : "ws";
25-
return new Uri($"{webSocketScheme}://{uri.Host}:{uri.Port}{uri.PathAndQuery}");
24+
string webSocketScheme;
25+
26+
if (uri.Scheme == Uri.UriSchemeHttps)
27+
webSocketScheme = "wss";
28+
else if (uri.Scheme == Uri.UriSchemeHttp)
29+
webSocketScheme = "ws";
30+
else
31+
throw new NotSupportedException($"cannot infer websocket uri from uri scheme {uri.Scheme}");
32+
33+
return new UriBuilder(uri){Scheme = webSocketScheme}.Uri;
2634
}
2735
}
2836
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using FluentAssertions;
3+
using GraphQL.Client.Http;
4+
using Xunit;
5+
6+
namespace GraphQL.Integration.Tests
7+
{
8+
public class UriExtensionTests
9+
{
10+
[Theory]
11+
[InlineData("http://thats-not-a-websocket-url.net", false)]
12+
[InlineData("https://thats-not-a-websocket-url.net", false)]
13+
[InlineData("ftp://thats-not-a-websocket-url.net", false)]
14+
[InlineData("ws://that-is-a-websocket-url.net", true)]
15+
[InlineData("wss://that-is-a-websocket-url.net", true)]
16+
[InlineData("WS://that-is-a-websocket-url.net", true)]
17+
[InlineData("WSS://that-is-a-websocket-url.net", true)]
18+
public void HasWebSocketSchemaTest(string url, bool result)
19+
{
20+
new Uri(url).HasWebSocketScheme().Should().Be(result);
21+
}
22+
23+
[Theory]
24+
[InlineData("http://this-url-can-be-converted.net", true, "ws://this-url-can-be-converted.net")]
25+
[InlineData("https://this-url-can-be-converted.net", true, "wss://this-url-can-be-converted.net")]
26+
[InlineData("HTTP://this-url-can-be-converted.net", true, "ws://this-url-can-be-converted.net")]
27+
[InlineData("HTTPS://this-url-can-be-converted.net", true, "wss://this-url-can-be-converted.net")]
28+
[InlineData("ws://this-url-can-be-converted.net", true, "ws://this-url-can-be-converted.net")]
29+
[InlineData("wss://this-url-can-be-converted.net", true, "wss://this-url-can-be-converted.net")]
30+
[InlineData("https://this-url-can-be-converted.net/and/all/elements/?are#preserved", true, "wss://this-url-can-be-converted.net/and/all/elements/?are#preserved")]
31+
[InlineData("ftp://this-url-cannot-be-converted.net", false, null)]
32+
// AppSync example
33+
[InlineData("wss://example1234567890000.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=123456789ABCDEF&payload=e30=", true, "wss://example1234567890000.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=123456789ABCDEF&payload=e30=")]
34+
public void GetWebSocketUriTest(string input, bool canConvert, string result)
35+
{
36+
var inputUri = new Uri(input);
37+
if (canConvert)
38+
{
39+
inputUri.GetWebSocketUri().Should().BeEquivalentTo(new Uri(result));
40+
}
41+
else
42+
{
43+
inputUri.Invoking(uri => uri.GetWebSocketUri()).Should().Throw<NotSupportedException>();
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)