From 1ff7f826c2336745a245dd8b16244bfa78784b4f Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 13 Jan 2024 12:46:59 -0300 Subject: [PATCH] nip11: url normalization improvement and Fetch test. --- nip11/fetch.go | 18 ++++-------------- nip11/nip11_test.go | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/nip11/fetch.go b/nip11/fetch.go index 624139c..1bfc1e7 100644 --- a/nip11/fetch.go +++ b/nip11/fetch.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" "strings" "time" ) @@ -20,21 +19,12 @@ func Fetch(ctx context.Context, u string) (info *RelayInformationDocument, err e } // normalize URL to start with http:// or https:// - if !strings.HasPrefix(u, "http") && !strings.HasPrefix(u, "ws") { - u = "wss://" + u + if strings.HasPrefix(u, "ws") { + u = "http" + u[2:] } - p, err := url.Parse(u) - if err != nil { - return nil, fmt.Errorf("cannot parse url: %s", u) - } - if p.Scheme == "ws" { - p.Scheme = "http" - } else if p.Scheme == "wss" { - p.Scheme = "https" - } - p.Path = strings.TrimRight(p.Path, "/") + u = strings.TrimRight(u, "/") - req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.String(), nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) // add the NIP-11 header req.Header.Add("Accept", "application/nostr+json") diff --git a/nip11/nip11_test.go b/nip11/nip11_test.go index a1a0a89..b3abdc2 100644 --- a/nip11/nip11_test.go +++ b/nip11/nip11_test.go @@ -1,6 +1,9 @@ package nip11 -import "testing" +import ( + "context" + "testing" +) func TestAddSupportedNIP(t *testing.T) { info := RelayInformationDocument{} @@ -28,3 +31,14 @@ func TestAddSupportedNIP(t *testing.T) { } } } + +func TestFetch(t *testing.T) { + res, err := Fetch(context.Background(), "wss://relay.nostr.bg") + if err != nil || res.Name == "" { + t.Errorf("failed to fetch from wss") + } + res, err = Fetch(context.Background(), "https://relay.nostr.bg") + if err != nil || res.Name == "" { + t.Errorf("failed to fetch from https") + } +}