clone

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

main
tinyrelay / tinygit / private_transport_test.go
private_transport_test.go 2.3K
1package tinygit23import (4 "context"5 "io"6 "net/http"7 "net/http/httptest"8 "strings"9 "testing"10)1112func TestPrivateProxySignsAndStreamsGitRequest(t *testing.T) {13 var authHeader string14 var body string15 remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {16 authHeader = r.Header.Get("Authorization")17 data, _ := io.ReadAll(r.Body)18 body = string(data)19 w.Header().Set("Content-Type", "application/x-git-upload-pack-result")20 _, _ = w.Write([]byte("pack-response"))21 }))22 t.Cleanup(remote.Close)23 proxy, err := newPrivateProxy(context.Background(), remote.URL+"/r/owner/repo.git", func(_ context.Context, method, target, payload string) (string, error) {24 if method != http.MethodGet || target != remote.URL+"/r/owner/repo.git" || payload != "" {25 return "", &testError{"unexpected signed request"}26 }27 return "Nostr signed:" + payload, nil28 }, true)29 if err != nil {30 t.Fatal(err)31 }32 t.Cleanup(func() { _ = proxy.Close(context.Background()) })33 req, err := http.NewRequest(http.MethodPost, proxy.URL()+"/info/refs", strings.NewReader("git-request"))34 if err != nil {35 t.Fatal(err)36 }37 res, err := http.DefaultClient.Do(req)38 if err != nil {39 t.Fatal(err)40 }41 defer res.Body.Close()42 if res.StatusCode != http.StatusOK {43 t.Fatalf("status=%d", res.StatusCode)44 }45 if got, _ := io.ReadAll(res.Body); string(got) != "pack-response" {46 t.Fatalf("response=%q", got)47 }48 if body != "git-request" || !strings.HasPrefix(authHeader, "Nostr signed:") {49 t.Fatalf("forwarded body/auth=%q/%q", body, authHeader)50 }51}5253func TestProbePrivatePeerRequiresGRASP08(t *testing.T) {54 for _, tc := range []struct {55 name string56 body string57 want bool58 }{59 {name: "private", body: `{"supported_grasps":["GRASP-08"]}`, want: true},60 {name: "public", body: `{"supported_grasps":["GRASP-01"]}`, want: false},61 } {62 t.Run(tc.name, func(t *testing.T) {63 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {64 w.Header().Set("Content-Type", "application/nostr+json")65 _, _ = w.Write([]byte(tc.body))66 }))67 t.Cleanup(server.Close)68 err := probePrivatePeer(context.Background(), server.URL, true)69 if (err == nil) != tc.want {70 t.Fatalf("probe error=%v", err)71 }72 })73 }74}7576type testError struct{ text string }7778func (e *testError) Error() string { return e.text }