clone

a self-hosted multitenant nostr relay with sites, file storage, git hosting, and inbox and outbox delivery

feat/ui-updates
tinyrelay / cmd / tinyclient / main_test.go
main_test.go 1.7K
1package main23import (4 "bytes"5 "net/http"6 "net/http/httptest"7 "strings"8 "testing"910 "github.com/FelineStateMachine/tinyrelay/tinyclient"11)1213func TestCommandRequiresBackendAndPublicOrigin(t *testing.T) {14 for _, args := range [][]string{nil, {"-backend", "http://relay.example"}, {"-backend", "http://relay.example", "-public-url", "not-a-url"}, {"-backend", "http://relay.example/r/one", "-public-url", "http://client.example/r/two"}} {15 if _, err := configure(args, &bytes.Buffer{}); err == nil {16 t.Fatalf("accepted incomplete or invalid configuration: %v", args)17 }18 }19}2021func TestCommandServesEmbeddedClient(t *testing.T) {22 upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {23 if r.URL.Path != tinyclient.BackendPath {24 t.Errorf("unexpected upstream page %s", r.URL.Path)25 http.NotFound(w, r)26 return27 }28 w.Header().Set("Content-Type", "application/json")29 if r.URL.Query().Get("method") != "" {30 _, _ = w.Write([]byte("null"))31 return32 }33 _, _ = w.Write([]byte(`{"policy":{"name":"Standalone frontend","features":{"pages":true}},"slug":"test","url":"http://client.example"}`))34 }))35 defer upstream.Close()36 server, err := configure([]string{"-listen", "127.0.0.1:0", "-backend", upstream.URL, "-public-url", "http://client.example"}, &bytes.Buffer{})37 if err != nil {38 t.Fatal(err)39 }40 response := httptest.NewRecorder()41 server.Handler.ServeHTTP(response, httptest.NewRequest("GET", "http://client.example/", nil))42 if response.Code != 200 || !strings.Contains(response.Body.String(), "Standalone frontend") {43 t.Fatalf("standalone command: %d %s", response.Code, response.Body.String())44 }45 if server.Addr != "127.0.0.1:0" {46 t.Fatalf("listen=%s", server.Addr)47 }48}