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
24 changes: 14 additions & 10 deletions dotnet/Trinsic.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ public Tests(ITestOutputHelper testOutputHelper)
private string VaccinationCertificateFrame =>
Path.GetFullPath(Path.Join(TestDataPath, "vaccination-certificate-frame.jsonld"));

[Fact]
public void TestParseURL()
[Theory]
[InlineData("localhost", false)]
[InlineData("localhost:5000", false)]
[InlineData("http://localhost", false)]
[InlineData("http://20.75.134.127", false)]
[InlineData("https://localhost:5000", true)]
[InlineData("http://localhost:5000", true)]
[InlineData("http://localhost:80", true)]
[InlineData("http://20.75.134.127:80", true)]
public void TestParseURL(string url, bool isValid)
{
Assert.Throws<UriFormatException>(() => ServiceBase.CreateChannelIfNeeded("localhost"));
Assert.Throws<ArgumentException>(() => ServiceBase.CreateChannelIfNeeded("localhost:5000"));
// Assert.Throws<ArgumentException>(() => ServiceBase.CreateChannelIfNeeded("http://localhost"));
// Throws because HTTPS is not yet supported.
// Assert.Throws<ArgumentException>(() => ServiceBase.CreateChannelIfNeeded("https://localhost:5000"));

Assert.NotNull(ServiceBase.CreateChannelIfNeeded("http://localhost:5000"));

if (!isValid)
Assert.Throws<ArgumentException>(() => ServiceBase.CreateChannelIfNeeded(url));
else
Assert.NotNull(ServiceBase.CreateChannelIfNeeded(url));
}

[Fact]
Expand Down
23 changes: 19 additions & 4 deletions dotnet/Trinsic/ServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,25 @@ public void SetProfile(WalletProfile profile)

public static GrpcChannel CreateChannelIfNeeded(string serviceAddress)
{
var url = new Uri(serviceAddress);
//if (url.IsDefaultPort) throw new ArgumentException("GRPC Port and scheme required");
if ("https".Equals(url.Scheme)) throw new ArgumentException("HTTPS not yet supported");
return GrpcChannel.ForAddress(serviceAddress, new GrpcChannelOptions());
try
{
var url = new Uri(serviceAddress);
AssertPortIsProvided(serviceAddress, url);
return GrpcChannel.ForAddress(serviceAddress, new GrpcChannelOptions());
}
catch (UriFormatException ufe)
{
throw new ArgumentException("Invalid service address", ufe);
}
}

private static void AssertPortIsProvided(string serviceAddress, Uri url)
{
// If port not provided, it will mismatch as a string
var rebuiltUri = new UriBuilder(url.Scheme, url.Host, url.Port, url.AbsolutePath);
// Remove trailing '/'
if (!serviceAddress.TrimEnd('/').StartsWith(rebuiltUri.ToString().TrimEnd('/')))
throw new ArgumentException("GRPC Port and scheme required");
}
}
}
16 changes: 9 additions & 7 deletions go/services/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ func TestServiceBase_SetProfile(t *testing.T) {
}

func TestCreateChannelIfNeeded(t *testing.T) {
var validHttpAddress = "http://localhost:5000"
var validHttpsAddress = "https://localhost:5000" // Currently, fails due to lack of HTTPS support.
var missingPortAddress = "http://localhost"
var missingProtocolAddress = "localhost:5000"
var blankAddress = ""
testAddresses := []string{validHttpAddress, validHttpsAddress, missingPortAddress, missingProtocolAddress, blankAddress}
throwsException := []bool{false, true, true, true, true}
const validHttpAddress = "http://localhost:5000"
const validHttpsAddress = "https://localhost:5000" // Currently, fails due to lack of HTTPS support.
const validIpAddress = "http://20.75.134.127:80"
const missingPortIpAddress = "http://20.75.134.127"
const missingPortAddress = "http://localhost"
const missingProtocolAddress = "localhost:5000"
const blankAddress = ""
testAddresses := []string{validHttpAddress, validHttpsAddress, validIpAddress, missingPortIpAddress, missingPortAddress, missingProtocolAddress, blankAddress}
throwsException := []bool{false, true, false, true, true, true, true}

for ij := 0; ij < len(testAddresses); ij++ {
channel, err := CreateChannelIfNeeded(testAddresses[ij], nil, false)
Expand Down
9 changes: 6 additions & 3 deletions python/tests/test_trinsic_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ async def test_providerservice_inviteparticipant(self):
def test_url_parse(self):
valid_http_address = "http://localhost:5000"
valid_https_address = "https://localhost:5000"
valid_ip_address = "http://20.75.134.127:80"
missing_port_ip_address = "http://20.75.134.127"
missing_port_address = "http://localhost"
missing_protocol_address = "localhost:5000"
blank_address = ""
addresses = [valid_http_address, valid_https_address, missing_port_address, missing_protocol_address,
addresses = [valid_http_address, valid_https_address, valid_ip_address, missing_port_ip_address,
missing_port_address, missing_protocol_address,
blank_address]
throws_exception = [False, False, True, True, True]
throws_exception = [False, False, False, True, True, True, True]

for ij in range(len(addresses)):
try:
channel = create_channel_if_needed(None, addresses[ij])
create_channel_if_needed(service_address=addresses[ij])
if throws_exception[ij]:
self.fail(f"URL={addresses[ij]} should throw")
except:
Expand Down
2 changes: 1 addition & 1 deletion python/trinsic/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from trinsic.proto.services.verifiablecredentials.v1 import CredentialStub


def create_channel_if_needed(channel: Channel, service_address: str) -> Channel:
def create_channel_if_needed(channel: Channel = None, service_address: str = '') -> Channel:
if not channel:
service_url = urllib.parse.urlsplit(service_address)
is_https = service_url.scheme == "https"
Expand Down