| 1 | package daemon |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/FelineStateMachine/tinyrelay/internal/views" |
| 11 | ) |
| 12 | |
| 13 | func TestCustomViewMediaPathServesStoredType(t *testing.T) { |
| 14 | for _, mediaType := range []string{"image/svg+xml" , "image/png" } { |
| 15 | t.Run(mediaType, func(t *testing.T) { |
| 16 | tenant, server, _ := viewTenant(t, []int{1}, nil) |
| 17 | artifact := svgArtifact(0) |
| 18 | artifact["type" ] = mediaType |
| 19 | if mediaType == "image/png" { |
| 20 | artifact["body" ] = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=" |
| 21 | } |
| 22 | server.answer(http.StatusOK, artifact) |
| 23 | e := signedEvent(t, testOwnerSecret, 1, time.Now().Unix(), nil, "```mermaid\ngraph TD; a-->b;\n```" ) |
| 24 | if err := publishAs(t, tenant, e); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | runPendingView(t, tenant, "diagrams" , e.ID) |
| 28 | _, image, ok := strings.Cut(views.Figure("diagrams" , "mermaid" , "graph TD; a-->b;" ), `<img src="` ) |
| 29 | if !ok { |
| 30 | t.Fatal("custom view has no image" ) |
| 31 | } |
| 32 | path, _, _ := strings.Cut(image, `"` ) |
| 33 | res := getArtifact(t, tenant, path, "" ) |
| 34 | if res.Code != http.StatusOK || res.Header().Get("Content-Type" ) != mediaType { |
| 35 | t.Fatalf("media response: %d %s" , res.Code, res.Body.String()) |
| 36 | } |
| 37 | request := httptest.NewRequest(http.MethodHead, "http://127.0.0.1:8080" +path, nil) |
| 38 | head := httptest.NewRecorder() |
| 39 | tenant.ServeHTTP(head, request) |
| 40 | if head.Code != http.StatusOK || head.Body.Len() != 0 || head.Header().Get("ETag" ) != res.Header().Get("ETag" ) { |
| 41 | t.Fatalf("media HEAD: %d %s" , head.Code, head.Body.String()) |
| 42 | } |
| 43 | }) |
| 44 | } |
| 45 | } |