clone

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

feat/tinyagent
tinyrelay / internal / daemon / media_preview_test.go
media_preview_test.go 2.2K
1package daemon23import (4 "context"5 "encoding/json"6 "net/http"7 "net/http/httptest"8 "strings"9 "testing"1011 "github.com/FelineStateMachine/tinyrelay/internal/blob"12)1314// pngHeader is the start of a valid PNG file: enough for type detection.15var pngHeader = append([]byte{0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'}, make([]byte, 64)...)1617func TestUntypedImageUploadsAreRecognisedAndServedInline(t *testing.T) {18 _, tenant := testTenant(t)19 ctx := context.Background()20 entry, err := tenant.blobs.Put(ctx, blob.PutOptions{Reader: strings.NewReader(string(pngHeader)), Type: "application/octet-stream", Uploader: tenant.Policy().Owner})21 if err != nil {22 t.Fatal(err)23 }24 if entry.Type != "image/png" {25 t.Fatalf("stored type = %q, want image/png", entry.Type)26 }27 value, err := tenant.Execute(ctx, tenant.Policy().Owner, "browsefile", []json.RawMessage{json.RawMessage(`{"hash":"` + entry.SHA256 + `"}`)})28 if err != nil {29 t.Fatal(err)30 }31 if page, _ := value.(map[string]any); page["type"] != "image/png" || page["binary"] != true {32 t.Fatalf("browsefile = %v", value)33 }34 req := httptest.NewRequest(http.MethodGet, "http://relay.test/files/raw?hash="+entry.SHA256, nil)35 res := httptest.NewRecorder()36 tenant.ServeHTTP(res, req)37 if res.Code != 200 || res.Header().Get("Content-Type") != "image/png" || !strings.HasPrefix(res.Header().Get("Content-Disposition"), "inline") {38 t.Fatalf("image download: %d type=%q disposition=%q", res.Code, res.Header().Get("Content-Type"), res.Header().Get("Content-Disposition"))39 }40 // Text that merely claims to be an image is still not rendered as a page.41 html, err := tenant.blobs.Put(ctx, blob.PutOptions{Reader: strings.NewReader("<html>hi</html>"), Type: "application/octet-stream", Uploader: tenant.Policy().Owner})42 if err != nil {43 t.Fatal(err)44 }45 res = httptest.NewRecorder()46 tenant.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "http://relay.test/files/raw?hash="+html.SHA256, nil))47 if res.Header().Get("Content-Type") != "application/octet-stream" || !strings.HasPrefix(res.Header().Get("Content-Disposition"), "attachment") {48 t.Fatalf("html blob served as %q %q", res.Header().Get("Content-Type"), res.Header().Get("Content-Disposition"))49 }50}