mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-28 18:51:53 +01:00
nip11: url normalization improvement and Fetch test.
This commit is contained in:
parent
fed7e4c3f2
commit
1ff7f826c2
@ -5,7 +5,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -20,21 +19,12 @@ func Fetch(ctx context.Context, u string) (info *RelayInformationDocument, err e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// normalize URL to start with http:// or https://
|
// normalize URL to start with http:// or https://
|
||||||
if !strings.HasPrefix(u, "http") && !strings.HasPrefix(u, "ws") {
|
if strings.HasPrefix(u, "ws") {
|
||||||
u = "wss://" + u
|
u = "http" + u[2:]
|
||||||
}
|
}
|
||||||
p, err := url.Parse(u)
|
u = strings.TrimRight(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, "/")
|
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.String(), nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||||
|
|
||||||
// add the NIP-11 header
|
// add the NIP-11 header
|
||||||
req.Header.Add("Accept", "application/nostr+json")
|
req.Header.Add("Accept", "application/nostr+json")
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package nip11
|
package nip11
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestAddSupportedNIP(t *testing.T) {
|
func TestAddSupportedNIP(t *testing.T) {
|
||||||
info := RelayInformationDocument{}
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user