Skip to content

Commit 292d541

Browse files
Parse .NET IP address with default port (#159)
1 parent aa3c76d commit 292d541

File tree

5 files changed

+49
-25
lines changed

5 files changed

+49
-25
lines changed

dotnet/Trinsic.Tests/Tests.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,21 @@ public Tests(ITestOutputHelper testOutputHelper)
3030
private string VaccinationCertificateFrame =>
3131
Path.GetFullPath(Path.Join(TestDataPath, "vaccination-certificate-frame.jsonld"));
3232

33-
[Fact]
34-
public void TestParseURL()
33+
[Theory]
34+
[InlineData("localhost", false)]
35+
[InlineData("localhost:5000", false)]
36+
[InlineData("http://localhost", false)]
37+
[InlineData("http://20.75.134.127", false)]
38+
[InlineData("https://localhost:5000", true)]
39+
[InlineData("http://localhost:5000", true)]
40+
[InlineData("http://localhost:80", true)]
41+
[InlineData("http://20.75.134.127:80", true)]
42+
public void TestParseURL(string url, bool isValid)
3543
{
36-
Assert.Throws<UriFormatException>(() => ServiceBase.CreateChannelIfNeeded("localhost"));
37-
Assert.Throws<ArgumentException>(() => ServiceBase.CreateChannelIfNeeded("localhost:5000"));
38-
// Assert.Throws<ArgumentException>(() => ServiceBase.CreateChannelIfNeeded("http://localhost"));
39-
// Throws because HTTPS is not yet supported.
40-
// Assert.Throws<ArgumentException>(() => ServiceBase.CreateChannelIfNeeded("https://localhost:5000"));
41-
42-
Assert.NotNull(ServiceBase.CreateChannelIfNeeded("http://localhost:5000"));
43-
44+
if (!isValid)
45+
Assert.Throws<ArgumentException>(() => ServiceBase.CreateChannelIfNeeded(url));
46+
else
47+
Assert.NotNull(ServiceBase.CreateChannelIfNeeded(url));
4448
}
4549

4650
[Fact]

dotnet/Trinsic/ServiceBase.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,25 @@ public void SetProfile(WalletProfile profile)
6464

6565
public static GrpcChannel CreateChannelIfNeeded(string serviceAddress)
6666
{
67-
var url = new Uri(serviceAddress);
68-
//if (url.IsDefaultPort) throw new ArgumentException("GRPC Port and scheme required");
69-
if ("https".Equals(url.Scheme)) throw new ArgumentException("HTTPS not yet supported");
70-
return GrpcChannel.ForAddress(serviceAddress, new GrpcChannelOptions());
67+
try
68+
{
69+
var url = new Uri(serviceAddress);
70+
AssertPortIsProvided(serviceAddress, url);
71+
return GrpcChannel.ForAddress(serviceAddress, new GrpcChannelOptions());
72+
}
73+
catch (UriFormatException ufe)
74+
{
75+
throw new ArgumentException("Invalid service address", ufe);
76+
}
77+
}
78+
79+
private static void AssertPortIsProvided(string serviceAddress, Uri url)
80+
{
81+
// If port not provided, it will mismatch as a string
82+
var rebuiltUri = new UriBuilder(url.Scheme, url.Host, url.Port, url.AbsolutePath);
83+
// Remove trailing '/'
84+
if (!serviceAddress.TrimEnd('/').StartsWith(rebuiltUri.ToString().TrimEnd('/')))
85+
throw new ArgumentException("GRPC Port and scheme required");
7186
}
7287
}
7388
}

go/services/services_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ func TestServiceBase_SetProfile(t *testing.T) {
6060
}
6161

6262
func TestCreateChannelIfNeeded(t *testing.T) {
63-
var validHttpAddress = "http://localhost:5000"
64-
var validHttpsAddress = "https://localhost:5000" // Currently, fails due to lack of HTTPS support.
65-
var missingPortAddress = "http://localhost"
66-
var missingProtocolAddress = "localhost:5000"
67-
var blankAddress = ""
68-
testAddresses := []string{validHttpAddress, validHttpsAddress, missingPortAddress, missingProtocolAddress, blankAddress}
69-
throwsException := []bool{false, true, true, true, true}
63+
const validHttpAddress = "http://localhost:5000"
64+
const validHttpsAddress = "https://localhost:5000" // Currently, fails due to lack of HTTPS support.
65+
const validIpAddress = "http://20.75.134.127:80"
66+
const missingPortIpAddress = "http://20.75.134.127"
67+
const missingPortAddress = "http://localhost"
68+
const missingProtocolAddress = "localhost:5000"
69+
const blankAddress = ""
70+
testAddresses := []string{validHttpAddress, validHttpsAddress, validIpAddress, missingPortIpAddress, missingPortAddress, missingProtocolAddress, blankAddress}
71+
throwsException := []bool{false, true, false, true, true, true, true}
7072

7173
for ij := 0; ij < len(testAddresses); ij++ {
7274
channel, err := CreateChannelIfNeeded(testAddresses[ij], nil, false)

python/tests/test_trinsic_services.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,19 @@ async def test_providerservice_inviteparticipant(self):
4343
def test_url_parse(self):
4444
valid_http_address = "http://localhost:5000"
4545
valid_https_address = "https://localhost:5000"
46+
valid_ip_address = "http://20.75.134.127:80"
47+
missing_port_ip_address = "http://20.75.134.127"
4648
missing_port_address = "http://localhost"
4749
missing_protocol_address = "localhost:5000"
4850
blank_address = ""
49-
addresses = [valid_http_address, valid_https_address, missing_port_address, missing_protocol_address,
51+
addresses = [valid_http_address, valid_https_address, valid_ip_address, missing_port_ip_address,
52+
missing_port_address, missing_protocol_address,
5053
blank_address]
51-
throws_exception = [False, False, True, True, True]
54+
throws_exception = [False, False, False, True, True, True, True]
5255

5356
for ij in range(len(addresses)):
5457
try:
55-
channel = create_channel_if_needed(None, addresses[ij])
58+
create_channel_if_needed(service_address=addresses[ij])
5659
if throws_exception[ij]:
5760
self.fail(f"URL={addresses[ij]} should throw")
5861
except:

python/trinsic/services.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from trinsic.proto.services.verifiablecredentials.v1 import CredentialStub
1919

2020

21-
def create_channel_if_needed(channel: Channel, service_address: str) -> Channel:
21+
def create_channel_if_needed(channel: Channel = None, service_address: str = '') -> Channel:
2222
if not channel:
2323
service_url = urllib.parse.urlsplit(service_address)
2424
is_https = service_url.scheme == "https"

0 commit comments

Comments
 (0)