Skip to content

Commit

Permalink
🚸 automatically add /rpc suffix for ws protocols (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno authored Dec 12, 2024
1 parent c959e9d commit 46d8b4b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions SurrealDb.Net.Tests/ConstructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ public void ShouldSupportWssProtocol()
func().AbsoluteUri.Should().Be("wss://cloud.surrealdb.com/rpc");
}

[Theory]
[InlineData("ws://127.0.0.1:8000", "ws://127.0.0.1:8000/rpc")]
[InlineData("wss://cloud.SurrealDb.com", "wss://cloud.surrealdb.com/rpc")]
[InlineData("ws://127.0.0.1:8000/", "ws://127.0.0.1:8000/rpc")]
[InlineData("wss://cloud.SurrealDb.com/", "wss://cloud.surrealdb.com/rpc")]
[InlineData("ws://127.0.0.1:8000/bar", "ws://127.0.0.1:8000/bar/rpc")]
[InlineData("wss://cloud.SurrealDb.com/bar", "wss://cloud.surrealdb.com/bar/rpc")]
public void ShouldAutomaticallyAddRpcSuffixForWsProtocols(string endpoint, string expected)
{
Func<Uri> func = () => new SurrealDbClient(endpoint).Uri;

func.Should().NotThrow();
func().AbsoluteUri.Should().Be(expected);
}

[Fact]
public void ShouldRequireDependencyInjectionForMemoryProtocol()
{
Expand Down
8 changes: 8 additions & 0 deletions SurrealDb.Net/SurrealDbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ internal SurrealDbClient(
throw new ArgumentNullException(nameof(configuration), "The endpoint is required.");

Uri = new Uri(configuration.Endpoint);
if (Uri.Scheme is "ws" or "wss" && !Uri.AbsolutePath.EndsWith("/rpc"))
{
string absoluteNakedPath = Uri.AbsolutePath.EndsWith("/")
? Uri.AbsolutePath[..^1]
: Uri.AbsolutePath;
Uri = new Uri(Uri, $"{absoluteNakedPath}/rpc");
}

NamingPolicy = configuration.NamingPolicy;

var protocol = Uri.Scheme;
Expand Down

0 comments on commit 46d8b4b

Please sign in to comment.