Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/GraphQL.Client/UriExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class UriExtensions
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public static bool HasWebSocketScheme(this Uri uri) => uri.Scheme.Equals("wss") || uri.Scheme.Equals("ws");
public static bool HasWebSocketScheme(this Uri uri) => uri.Scheme.Equals("wss", StringComparison.OrdinalIgnoreCase) || uri.Scheme.Equals("ws", StringComparison.OrdinalIgnoreCase);

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

string webSocketScheme = uri.Scheme == "https" ? "wss" : "ws";
return new Uri($"{webSocketScheme}://{uri.Host}:{uri.Port}{uri.PathAndQuery}");
string webSocketScheme;

if (uri.Scheme == Uri.UriSchemeHttps)
webSocketScheme = "wss";
else if (uri.Scheme == Uri.UriSchemeHttp)
webSocketScheme = "ws";
else
throw new NotSupportedException($"cannot infer websocket uri from uri scheme {uri.Scheme}");

return new UriBuilder(uri){Scheme = webSocketScheme}.Uri;
}
}
}
47 changes: 47 additions & 0 deletions tests/GraphQL.Integration.Tests/UriExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using FluentAssertions;
using GraphQL.Client.Http;
using Xunit;

namespace GraphQL.Integration.Tests
{
public class UriExtensionTests
{
[Theory]
[InlineData("http://thats-not-a-websocket-url.net", false)]
[InlineData("https://thats-not-a-websocket-url.net", false)]
[InlineData("ftp://thats-not-a-websocket-url.net", false)]
[InlineData("ws://that-is-a-websocket-url.net", true)]
[InlineData("wss://that-is-a-websocket-url.net", true)]
[InlineData("WS://that-is-a-websocket-url.net", true)]
[InlineData("WSS://that-is-a-websocket-url.net", true)]
public void HasWebSocketSchemaTest(string url, bool result)
{
new Uri(url).HasWebSocketScheme().Should().Be(result);
}

[Theory]
[InlineData("http://this-url-can-be-converted.net", true, "ws://this-url-can-be-converted.net")]
[InlineData("https://this-url-can-be-converted.net", true, "wss://this-url-can-be-converted.net")]
[InlineData("HTTP://this-url-can-be-converted.net", true, "ws://this-url-can-be-converted.net")]
[InlineData("HTTPS://this-url-can-be-converted.net", true, "wss://this-url-can-be-converted.net")]
[InlineData("ws://this-url-can-be-converted.net", true, "ws://this-url-can-be-converted.net")]
[InlineData("wss://this-url-can-be-converted.net", true, "wss://this-url-can-be-converted.net")]
[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")]
[InlineData("ftp://this-url-cannot-be-converted.net", false, null)]
// AppSync example
[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=")]
public void GetWebSocketUriTest(string input, bool canConvert, string result)
{
var inputUri = new Uri(input);
if (canConvert)
{
inputUri.GetWebSocketUri().Should().BeEquivalentTo(new Uri(result));
}
else
{
inputUri.Invoking(uri => uri.GetWebSocketUri()).Should().Throw<NotSupportedException>();
}
}
}
}