tinyrelayfile
tinyrelay/cmd/tinyclient/main_test.go/

cmd/tinyclient/main_test.go raw
1package main
2
3import (
4 "bytes"
5 "net/http"
6 "net/http/httptest"
7 "strings"
8 "testing"
9
10 "github.com/FelineStateMachine/tinyrelay/tinyclient"
11)
12
13func 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}
20
21func 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 return
27 }
28 w.Header().Set("Content-Type", "application/json")
29 if r.URL.Query().Get("method") != "" {
30 _, _ = w.Write([]byte("null"))
31 return
32 }
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}

About

ownerc01503e8035f
visibilitypublic
headd730679639bf

Maintainers

  • c01503e8035f owner
  • ba1b5beed5b5 agent hermes

Clone

  • https://012.run/npub1cq2s86qrtadf36eqq3epm38wmhdynrrcwxf84eldk3f94dkv7ttsrulrz5/tinyrelay.git

Refs