@@ -7,80 +7,63 @@ import (
77 "testing"
88
99 cid "github.com/ipfs/go-cid"
10+ path "github.com/ipfs/go-path"
1011)
1112
12- // import (
13- // "errors"
14- // "net/http"
15- // "net/http/httptest"
16- // "testing"
13+ func TestToSubdomainURL (t * testing.T ) {
14+ gwAPI := newMockApi ()
15+ testCID , err := cid .Decode ("bafkqaglimvwgy3zakrsxg5cun5jxkyten5wwc2lokvjeycq" )
16+ if err != nil {
17+ t .Fatal (err )
18+ }
19+
20+ gwAPI .ns ["/ipns/dnslink.long-name.example.com" ] = path .FromString (testCID .String ())
21+ gwAPI .ns ["/ipns/dnslink.too-long.f1siqrebi3vir8sab33hu5vcy008djegvay6atmz91ojesyjs8lx350b7y7i1nvyw2haytfukfyu2f2x4tocdrfa0zgij6p4zpl4u5o.example.com" ] = path .FromString (testCID .String ())
22+ httpRequest := httptest .NewRequest ("GET" , "http://127.0.0.1:8080" , nil )
23+ httpsRequest := httptest .NewRequest ("GET" , "https://https-request-stub.example.com" , nil )
24+ httpsProxiedRequest := httptest .NewRequest ("GET" , "http://proxied-https-request-stub.example.com" , nil )
25+ httpsProxiedRequest .Header .Set ("X-Forwarded-Proto" , "https" )
1726
18- // cid "github.com/ipfs/go-cid"
19- // "github.com/ipfs/go-libipfs/files"
20- // path "github.com/ipfs/go-path"
21- // config "github.com/ipfs/kubo/config"
22- // coreapi "github.com/ipfs/kubo/core/coreapi"
23- // )
27+ for _ , test := range []struct {
28+ // in:
29+ request * http.Request
30+ gwHostname string
31+ inlineDNSLink bool
32+ path string
33+ // out:
34+ url string
35+ err error
36+ }{
2437
25- // func TestToSubdomainURL(t *testing.T) {
26- // ns := mockNamesys{}
27- // n, err := newNodeWithMockNamesys(ns)
28- // if err != nil {
29- // t.Fatal(err)
30- // }
31- // coreAPI, err := coreapi.NewCoreAPI(n)
32- // if err != nil {
33- // t.Fatal(err)
34- // }
35- // testCID, err := coreAPI.Unixfs().Add(n.Context(), files.NewBytesFile([]byte("fnord")))
36- // if err != nil {
37- // t.Fatal(err)
38- // }
39- // ns["/ipns/dnslink.long-name.example.com"] = path.FromString(testCID.String())
40- // ns["/ipns/dnslink.too-long.f1siqrebi3vir8sab33hu5vcy008djegvay6atmz91ojesyjs8lx350b7y7i1nvyw2haytfukfyu2f2x4tocdrfa0zgij6p4zpl4u5o.example.com"] = path.FromString(testCID.String())
41- // httpRequest := httptest.NewRequest("GET", "http://127.0.0.1:8080", nil)
42- // httpsRequest := httptest.NewRequest("GET", "https://https-request-stub.example.com", nil)
43- // httpsProxiedRequest := httptest.NewRequest("GET", "http://proxied-https-request-stub.example.com", nil)
44- // httpsProxiedRequest.Header.Set("X-Forwarded-Proto", "https")
38+ // DNSLink
39+ {httpRequest , "localhost" , false , "/ipns/dnslink.io" , "http://dnslink.io.ipns.localhost/" , nil },
40+ // Hostname with port
41+ {httpRequest , "localhost:8080" , false , "/ipns/dnslink.io" , "http://dnslink.io.ipns.localhost:8080/" , nil },
42+ // CIDv0 → CIDv1base32
43+ {httpRequest , "localhost" , false , "/ipfs/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n" , "http://bafybeif7a7gdklt6hodwdrmwmxnhksctcuav6lfxlcyfz4khzl3qfmvcgu.ipfs.localhost/" , nil },
44+ // CIDv1 with long sha512
45+ {httpRequest , "localhost" , false , "/ipfs/bafkrgqe3ohjcjplc6n4f3fwunlj6upltggn7xqujbsvnvyw764srszz4u4rshq6ztos4chl4plgg4ffyyxnayrtdi5oc4xb2332g645433aeg" , "" , errors .New ("CID incompatible with DNS label length limit of 63: kf1siqrebi3vir8sab33hu5vcy008djegvay6atmz91ojesyjs8lx350b7y7i1nvyw2haytfukfyu2f2x4tocdrfa0zgij6p4zpl4u5oj" )},
46+ // PeerID as CIDv1 needs to have libp2p-key multicodec
47+ {httpRequest , "localhost" , false , "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD" , "http://k2k4r8n0flx3ra0y5dr8fmyvwbzy3eiztmtq6th694k5a3rznayp3e4o.ipns.localhost/" , nil },
48+ {httpRequest , "localhost" , false , "/ipns/bafybeickencdqw37dpz3ha36ewrh4undfjt2do52chtcky4rxkj447qhdm" , "http://k2k4r8l9ja7hkzynavdqup76ou46tnvuaqegbd04a4o1mpbsey0meucb.ipns.localhost/" , nil },
49+ // PeerID: ed25519+identity multihash → CIDv1Base36
50+ {httpRequest , "localhost" , false , "/ipns/12D3KooWFB51PRY9BxcXSH6khFXw1BZeszeLDy7C8GciskqCTZn5" , "http://k51qzi5uqu5di608geewp3nqkg0bpujoasmka7ftkyxgcm3fh1aroup0gsdrna.ipns.localhost/" , nil },
51+ {httpRequest , "sub.localhost" , false , "/ipfs/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n" , "http://bafybeif7a7gdklt6hodwdrmwmxnhksctcuav6lfxlcyfz4khzl3qfmvcgu.ipfs.sub.localhost/" , nil },
52+ // HTTPS requires DNSLink name to fit in a single DNS label – see "Option C" from https://github.com/ipfs/in-web-browsers/issues/169
53+ {httpRequest , "dweb.link" , false , "/ipns/dnslink.long-name.example.com" , "http://dnslink.long-name.example.com.ipns.dweb.link/" , nil },
54+ {httpsRequest , "dweb.link" , false , "/ipns/dnslink.long-name.example.com" , "https://dnslink-long--name-example-com.ipns.dweb.link/" , nil },
55+ {httpsProxiedRequest , "dweb.link" , false , "/ipns/dnslink.long-name.example.com" , "https://dnslink-long--name-example-com.ipns.dweb.link/" , nil },
56+ // HTTP requests can also be converted to fit into a single DNS label - https://github.com/ipfs/kubo/issues/9243
57+ {httpRequest , "localhost" , true , "/ipns/dnslink.long-name.example.com" , "http://dnslink-long--name-example-com.ipns.localhost/" , nil },
58+ {httpRequest , "dweb.link" , true , "/ipns/dnslink.long-name.example.com" , "http://dnslink-long--name-example-com.ipns.dweb.link/" , nil },
59+ } {
4560
46- // for _, test := range []struct {
47- // // in:
48- // request *http.Request
49- // gwHostname string
50- // inlineDNSLink bool
51- // path string
52- // // out:
53- // url string
54- // err error
55- // }{
56- // // DNSLink
57- // {httpRequest, "localhost", false, "/ipns/dnslink.io", "http://dnslink.io.ipns.localhost/", nil},
58- // // Hostname with port
59- // {httpRequest, "localhost:8080", false, "/ipns/dnslink.io", "http://dnslink.io.ipns.localhost:8080/", nil},
60- // // CIDv0 → CIDv1base32
61- // {httpRequest, "localhost", false, "/ipfs/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", "http://bafybeif7a7gdklt6hodwdrmwmxnhksctcuav6lfxlcyfz4khzl3qfmvcgu.ipfs.localhost/", nil},
62- // // CIDv1 with long sha512
63- // {httpRequest, "localhost", false, "/ipfs/bafkrgqe3ohjcjplc6n4f3fwunlj6upltggn7xqujbsvnvyw764srszz4u4rshq6ztos4chl4plgg4ffyyxnayrtdi5oc4xb2332g645433aeg", "", errors.New("CID incompatible with DNS label length limit of 63: kf1siqrebi3vir8sab33hu5vcy008djegvay6atmz91ojesyjs8lx350b7y7i1nvyw2haytfukfyu2f2x4tocdrfa0zgij6p4zpl4u5oj")},
64- // // PeerID as CIDv1 needs to have libp2p-key multicodec
65- // {httpRequest, "localhost", false, "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", "http://k2k4r8n0flx3ra0y5dr8fmyvwbzy3eiztmtq6th694k5a3rznayp3e4o.ipns.localhost/", nil},
66- // {httpRequest, "localhost", false, "/ipns/bafybeickencdqw37dpz3ha36ewrh4undfjt2do52chtcky4rxkj447qhdm", "http://k2k4r8l9ja7hkzynavdqup76ou46tnvuaqegbd04a4o1mpbsey0meucb.ipns.localhost/", nil},
67- // // PeerID: ed25519+identity multihash → CIDv1Base36
68- // {httpRequest, "localhost", false, "/ipns/12D3KooWFB51PRY9BxcXSH6khFXw1BZeszeLDy7C8GciskqCTZn5", "http://k51qzi5uqu5di608geewp3nqkg0bpujoasmka7ftkyxgcm3fh1aroup0gsdrna.ipns.localhost/", nil},
69- // {httpRequest, "sub.localhost", false, "/ipfs/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", "http://bafybeif7a7gdklt6hodwdrmwmxnhksctcuav6lfxlcyfz4khzl3qfmvcgu.ipfs.sub.localhost/", nil},
70- // // HTTPS requires DNSLink name to fit in a single DNS label – see "Option C" from https://github.com/ipfs/in-web-browsers/issues/169
71- // {httpRequest, "dweb.link", false, "/ipns/dnslink.long-name.example.com", "http://dnslink.long-name.example.com.ipns.dweb.link/", nil},
72- // {httpsRequest, "dweb.link", false, "/ipns/dnslink.long-name.example.com", "https://dnslink-long--name-example-com.ipns.dweb.link/", nil},
73- // {httpsProxiedRequest, "dweb.link", false, "/ipns/dnslink.long-name.example.com", "https://dnslink-long--name-example-com.ipns.dweb.link/", nil},
74- // // HTTP requests can also be converted to fit into a single DNS label - https://github.com/ipfs/kubo/issues/9243
75- // {httpRequest, "localhost", true, "/ipns/dnslink.long-name.example.com", "http://dnslink-long--name-example-com.ipns.localhost/", nil},
76- // {httpRequest, "dweb.link", true, "/ipns/dnslink.long-name.example.com", "http://dnslink-long--name-example-com.ipns.dweb.link/", nil},
77- // } {
78- // url, err := toSubdomainURL(test.gwHostname, test.path, test.request, test.inlineDNSLink, coreAPI)
79- // if url != test.url || !equalError(err, test.err) {
80- // t.Errorf("(%s, %v, %s) returned (%s, %v), expected (%s, %v)", test.gwHostname, test.inlineDNSLink, test.path, url, err, test.url, test.err)
81- // }
82- // }
83- // }
61+ url , err := toSubdomainURL (test .gwHostname , test .path , test .request , test .inlineDNSLink , gwAPI )
62+ if url != test .url || ! equalError (err , test .err ) {
63+ t .Errorf ("(%s, %v, %s) returned (%s, %v), expected (%s, %v)" , test .gwHostname , test .inlineDNSLink , test .path , url , err , test .url , test .err )
64+ }
65+ }
66+ }
8467
8568func TestToDNSLinkDNSLabel (t * testing.T ) {
8669 for _ , test := range []struct {
0 commit comments