relayinfo_test.go raw
1 package relayinfo
2
3 import (
4 "context"
5 "net/http"
6 "net/http/httptest"
7 "strings"
8 "testing"
9 )
10
11 // TestFetchReadsWhatARelayAdvertises: the relay's own answer says who it admits, so a gateway or
12 // an operator can act on it without contacting anyone.
13 func TestFetchReadsWhatARelayAdvertises(t *testing.T) {
14 const key = "8964ed8cae7cb41286f9dc56344154f9491eace55ad6a18d7fb025ac02ec18a7"
15
16 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17 if accept := r.Header.Get("Accept"); !strings.Contains(accept, "application/nostr+json") {
18 t.Errorf("the document was asked for with Accept %q", accept)
19 }
20
21 w.Header().Set("content-type", "application/nostr+json")
22 _, _ = w.Write([]byte(`{"name":"swaprelay","contact":"blake@smesh.lol","gateways":["` + key + `"]}`))
23 }))
24
25 defer server.Close()
26
27 doc, err := Fetch(context.Background(), server.Client(), "ws"+strings.TrimPrefix(server.URL, "http"))
28
29 if err != nil {
30 t.Fatalf("fetch: %v", err)
31 }
32
33 if doc.Name != "swaprelay" || doc.Contact != "blake@smesh.lol" {
34 t.Fatalf("document: %+v", doc)
35 }
36
37 if !doc.Authorizes(strings.ToUpper(key)) {
38 t.Fatal("the advertised key was not recognised")
39 }
40
41 if doc.Authorizes(strings.Repeat("0", 64)) {
42 t.Fatal("a key that is not advertised was accepted")
43 }
44 }
45
46 // TestARelayThatDoesNotAnswerIsAnError: "this relay lists nobody" and "this relay did not answer"
47 // are different answers, and a caller deciding whether it may connect needs to tell them apart.
48 func TestARelayThatDoesNotAnswerIsAnError(t *testing.T) {
49 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
50 w.WriteHeader(http.StatusNotFound)
51 }))
52
53 defer server.Close()
54
55 if _, err := Fetch(context.Background(), server.Client(), "ws"+strings.TrimPrefix(server.URL, "http")); err == nil {
56 t.Fatal("a relay that does not serve NIP-11 was reported as advertising no gateways")
57 }
58
59 server.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
60 _, _ = w.Write([]byte("<html>not a relay</html>"))
61 })
62
63 if _, err := Fetch(context.Background(), server.Client(), "ws"+strings.TrimPrefix(server.URL, "http")); err == nil {
64 t.Fatal("a non-JSON page was accepted as a NIP-11 document")
65 }
66 }
67
68 // TestHTTPURL: the relay is dialled over WebSocket and asked about itself over HTTP.
69 func TestHTTPURL(t *testing.T) {
70 cases := map[string]string{
71 "wss://swaprelay.smesh.lol": "https://swaprelay.smesh.lol/",
72 "ws://127.0.0.1:3337/nostrino": "http://127.0.0.1:3337/",
73 "wss://relay.example/path?x=1": "https://relay.example/",
74 }
75
76 for in, want := range cases {
77 got, err := HTTPURL(in)
78
79 if err != nil {
80 t.Fatalf("%s: %v", in, err)
81 }
82
83 if got != want {
84 t.Fatalf("%s: got %s, want %s", in, got, want)
85 }
86 }
87
88 if _, err := HTTPURL("ftp://relay"); err == nil {
89 t.Fatal("an unsupported scheme was accepted")
90 }
91 }
92