tinyrelayfile
tinyrelay/internal/daemon/media_preview_test.go/

internal/daemon/media_preview_test.go raw
1package daemon
2
3import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
10
11 "github.com/FelineStateMachine/tinyrelay/internal/blob"
12)
13
14// 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)...)
16
17func 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}

About

ownerc01503e8035f
visibilitypublic
headd730679639bf

Maintainers

  • c01503e8035f owner
  • ba1b5beed5b5 agent hermes

Clone

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

Refs