Skip to content

Commit

Permalink
wip: move some more calls
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 24, 2023
1 parent b940fa6 commit 94f5c13
Showing 1 changed file with 18 additions and 49 deletions.
67 changes: 18 additions & 49 deletions gateway/hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (

cid "github.com/ipfs/go-cid"
path "github.com/ipfs/go-path"
"github.com/stretchr/testify/assert"
)

func TestToSubdomainURL(t *testing.T) {
gwAPI, _ := newMockAPI(t)
testCID, err := cid.Decode("bafkqaglimvwgy3zakrsxg5cun5jxkyten5wwc2lokvjeycq")
if err != nil {
t.Fatal(err)
}
assert.Nil(t, err)

gwAPI.namesys["/ipns/dnslink.long-name.example.com"] = path.FromString(testCID.String())
gwAPI.namesys["/ipns/dnslink.too-long.f1siqrebi3vir8sab33hu5vcy008djegvay6atmz91ojesyjs8lx350b7y7i1nvyw2haytfukfyu2f2x4tocdrfa0zgij6p4zpl4u5o.example.com"] = path.FromString(testCID.String())
Expand Down Expand Up @@ -57,11 +56,9 @@ func TestToSubdomainURL(t *testing.T) {
{httpRequest, "localhost", true, "/ipns/dnslink.long-name.example.com", "http://dnslink-long--name-example-com.ipns.localhost/", nil},
{httpRequest, "dweb.link", true, "/ipns/dnslink.long-name.example.com", "http://dnslink-long--name-example-com.ipns.dweb.link/", nil},
} {

url, err := toSubdomainURL(test.gwHostname, test.path, test.request, test.inlineDNSLink, gwAPI)
if url != test.url || !equalError(err, test.err) {
t.Errorf("(%s, %v, %s) returned (%s, %v), expected (%s, %v)", test.gwHostname, test.inlineDNSLink, test.path, url, err, test.url, test.err)
}
assert.Equalf(t, test.url, url, "(%s, %v, %s)", test.gwHostname, test.inlineDNSLink, test.path)
assert.Equalf(t, test.err, err, "(%s, %v, %s)", test.gwHostname, test.inlineDNSLink, test.path)
}
}

Expand All @@ -75,9 +72,8 @@ func TestToDNSLinkDNSLabel(t *testing.T) {
{"dnslink.too-long.f1siqrebi3vir8sab33hu5vcy008djegvay6atmz91ojesyjs8lx350b7y7i1nvyw2haytfukfyu2f2x4tocdrfa0zgij6p4zpl4u5o.example.com", "", errors.New("DNSLink representation incompatible with DNS label length limit of 63: dnslink-too--long-f1siqrebi3vir8sab33hu5vcy008djegvay6atmz91ojesyjs8lx350b7y7i1nvyw2haytfukfyu2f2x4tocdrfa0zgij6p4zpl4u5o-example-com")},
} {
out, err := toDNSLinkDNSLabel(test.in)
if out != test.out || !equalError(err, test.err) {
t.Errorf("(%s) returned (%s, %v), expected (%s, %v)", test.in, out, err, test.out, test.err)
}
assert.Equalf(t, test.out, out, "(%s)", test.in)
assert.Equalf(t, test.err, err, "(%s)", test.in)
}
}

Expand All @@ -91,9 +87,7 @@ func TestToDNSLinkFQDN(t *testing.T) {
{"dnslink-long--name-example-com", "dnslink.long-name.example.com"},
} {
out := toDNSLinkFQDN(test.in)
if out != test.out {
t.Errorf("(%s) returned (%s), expected (%s)", test.in, out, test.out)
}
assert.Equalf(t, test.out, out, "(%s)", test.in)
}
}

Expand All @@ -116,9 +110,7 @@ func TestIsHTTPSRequest(t *testing.T) {
{oddballRequest, false},
} {
out := isHTTPSRequest(test.in)
if out != test.out {
t.Errorf("(%+v): returned %t, expected %t", test.in, out, test.out)
}
assert.Equalf(t, test.out, out, "(%+v)", test.in)
}
}

Expand All @@ -134,9 +126,7 @@ func TestHasPrefix(t *testing.T) {
{[]string{"/version"}, "/version", true},
} {
out := hasPrefix(test.path, test.prefixes...)
if out != test.out {
t.Errorf("(%+v, %s) returned '%t', expected '%t'", test.prefixes, test.path, out, test.out)
}
assert.Equalf(t, test.out, out, "(%+v, %s)", test.prefixes, test.path)
}
}

Expand All @@ -153,9 +143,7 @@ func TestIsDomainNameAndNotPeerID(t *testing.T) {
{"k51qzi5uqu5di608geewp3nqkg0bpujoasmka7ftkyxgcm3fh1aroup0gsdrna", false}, // valid peerid
} {
out := isDomainNameAndNotPeerID(test.hostname)
if out != test.out {
t.Errorf("(%s) returned '%t', expected '%t'", test.hostname, out, test.out)
}
assert.Equalf(t, test.out, out, "(%s)", test.hostname)
}
}

Expand All @@ -173,9 +161,7 @@ func TestPortStripping(t *testing.T) {
{"[::1]:8080", "::1"},
} {
out := stripPort(test.in)
if out != test.out {
t.Errorf("(%s): returned '%s', expected '%s'", test.in, out, test.out)
}
assert.Equalf(t, test.out, out, "(%s)", test.in)
}
}

Expand All @@ -196,11 +182,9 @@ func TestToDNSLabel(t *testing.T) {
} {
inCID, _ := cid.Decode(test.in)
out, err := toDNSLabel(test.in, inCID)
if out != test.out || !equalError(err, test.err) {
t.Errorf("(%s): returned (%s, %v) expected (%s, %v)", test.in, out, err, test.out, test.err)
}
assert.Equalf(t, test.out, out, "(%s)", test.in)
assert.Equalf(t, test.err, err, "(%s)", test.in)
}

}

func TestKnownSubdomainDetails(t *testing.T) {
Expand Down Expand Up @@ -275,25 +259,10 @@ func TestKnownSubdomainDetails(t *testing.T) {
{"bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am.ipfs.sub1.sub2.wildcard2.tld", gwWildcard2, "sub1.sub2.wildcard2.tld", "ipfs", "bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am", true},
} {
gw, hostname, ns, rootID, ok := gateways.knownSubdomainDetails(test.hostHeader)
if ok != test.ok {
t.Errorf("knownSubdomainDetails(%s): ok is %t, expected %t", test.hostHeader, ok, test.ok)
}
if rootID != test.rootID {
t.Errorf("knownSubdomainDetails(%s): rootID is '%s', expected '%s'", test.hostHeader, rootID, test.rootID)
}
if ns != test.ns {
t.Errorf("knownSubdomainDetails(%s): ns is '%s', expected '%s'", test.hostHeader, ns, test.ns)
}
if hostname != test.hostname {
t.Errorf("knownSubdomainDetails(%s): hostname is '%s', expected '%s'", test.hostHeader, hostname, test.hostname)
}
if gw != test.gw {
t.Errorf("knownSubdomainDetails(%s): gw is %+v, expected %+v", test.hostHeader, gw, test.gw)
}
assert.Equalf(t, test.ok, ok, "knownSubdomainDetails(%s)", test.hostHeader)
assert.Equalf(t, test.rootID, rootID, "knownSubdomainDetails(%s)", test.hostHeader)
assert.Equalf(t, test.ns, ns, "knownSubdomainDetails(%s)", test.hostHeader)
assert.Equalf(t, test.hostname, hostname, "knownSubdomainDetails(%s)", test.hostHeader)
assert.Equalf(t, test.gw, gw, "knownSubdomainDetails(%s)", test.hostHeader)
}

}

func equalError(a, b error) bool {
return (a == nil && b == nil) || (a != nil && b != nil && a.Error() == b.Error())
}

0 comments on commit 94f5c13

Please sign in to comment.