mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-29 18:11:15 +02:00
use NormalizeURL() on nip11.Fetch()
This commit is contained in:
@ -5,8 +5,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/nbd-wtf/go-nostr"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Fetch fetches the NIP-11 RelayInformationDocument.
|
// Fetch fetches the NIP-11 RelayInformationDocument.
|
||||||
@ -19,14 +20,9 @@ func Fetch(ctx context.Context, u string) (info *RelayInformationDocument, err e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// normalize URL to start with http://, https:// or without protocol
|
// normalize URL to start with http://, https:// or without protocol
|
||||||
if strings.HasPrefix(u, "wss://") || strings.HasPrefix(u, "ws://") {
|
u = "http" + nostr.NormalizeURL(u)[2:]
|
||||||
u = "http" + u[2:]
|
|
||||||
}
|
|
||||||
if !(strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://")) {
|
|
||||||
u = "http://" + u
|
|
||||||
}
|
|
||||||
u = strings.TrimRight(u, "/")
|
|
||||||
|
|
||||||
|
// make request
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||||
|
|
||||||
// add the NIP-11 header
|
// add the NIP-11 header
|
||||||
|
@ -41,4 +41,8 @@ func TestFetch(t *testing.T) {
|
|||||||
if err != nil || res.Name == "" {
|
if err != nil || res.Name == "" {
|
||||||
t.Errorf("failed to fetch from https")
|
t.Errorf("failed to fetch from https")
|
||||||
}
|
}
|
||||||
|
res, err = Fetch(context.Background(), "relay.nostr.bg")
|
||||||
|
if err != nil || res.Name == "" {
|
||||||
|
t.Errorf("failed to fetch without protocol")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user