d2/lib/imgbundler/imgbundler_test.go

329 lines
16 KiB
Go
Raw Normal View History

2022-11-26 23:26:14 +00:00
package imgbundler
import (
2022-11-29 22:20:17 +00:00
"context"
2022-11-29 21:30:22 +00:00
"crypto/rand"
2022-11-26 23:26:14 +00:00
_ "embed"
"fmt"
"net/http"
2022-11-29 18:08:18 +00:00
"net/http/httptest"
2022-11-27 02:14:41 +00:00
"path/filepath"
2022-11-26 23:26:14 +00:00
"strings"
2023-06-06 17:43:52 +00:00
"sync"
2022-11-26 23:26:14 +00:00
"testing"
2022-11-29 21:19:03 +00:00
2023-06-06 17:43:52 +00:00
tassert "github.com/stretchr/testify/assert"
2023-06-06 18:33:00 +00:00
2023-10-19 23:15:08 +00:00
"oss.terrastruct.com/d2/lib/log"
2022-11-26 23:26:14 +00:00
)
//go:embed test_png.png
var testPNGFile []byte
2022-11-29 18:08:18 +00:00
type roundTripFunc func(req *http.Request) *http.Response
2022-11-26 23:26:14 +00:00
2022-11-29 18:08:18 +00:00
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
2022-11-26 23:26:14 +00:00
return f(req), nil
}
2022-11-28 21:25:40 +00:00
func TestRegex(t *testing.T) {
urls := []string{
"https://icons.terrastruct.com/essentials/004-picture.svg",
"http://icons.terrastruct.com/essentials/004-picture.svg",
}
notURLs := []string{
"hi.png",
"./cat.png",
"/cat.png",
}
for _, href := range append(urls, notURLs...) {
str := fmt.Sprintf(`<image href="%s" />`, href)
matches := imageRegex.FindAllStringSubmatch(str, -1)
2022-11-28 21:25:40 +00:00
if len(matches) != 1 {
t.Fatalf("uri regex didn't match %s", str)
}
}
}
2022-11-27 02:14:41 +00:00
func TestInlineRemote(t *testing.T) {
2023-06-06 17:43:52 +00:00
imgCache = sync.Map{}
2023-10-19 23:15:08 +00:00
ctx := log.WithTB(context.Background(), t, nil)
2022-11-26 23:26:14 +00:00
svgURL := "https://icons.terrastruct.com/essentials/004-picture.svg"
pngURL := "https://cdn4.iconfinder.com/data/icons/smart-phones-technologies/512/android-phone.png"
sampleSVG := fmt.Sprintf(`<?xml version="1.0" encoding="utf-8"?>
<svg
2023-01-04 00:42:39 +00:00
id="d2-svg"
2022-11-26 23:26:14 +00:00
style="background: white;"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
<![CDATA[
.shape {
shape-rendering: geometricPrecision;
stroke-linejoin: round;
}
.connection {
stroke-linecap: round;
stroke-linejoin: round;
}
]]>
</style><g id="a"><g class="shape" ><image href="%s" x="0" y="0" width="128" height="128" style="fill:#FFFFFF;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" /></g><text class="text-bold" x="64.000000" y="-15.000000" style="text-anchor:middle;font-size:16px;fill:#0A0F25">a</text></g><g id="b"><g class="shape" ><image href="%s" x="0" y="228" width="128" height="128" style="fill:#FFFFFF;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" /></g><text class="text-bold" x="64.000000" y="213.000000" style="text-anchor:middle;font-size:16px;fill:#0A0F25">b</text></g><g id="(a -&gt; b)[0]"><marker id="mk-3990223579" markerWidth="10.000000" markerHeight="12.000000" refX="7.000000" refY="6.000000" viewBox="0.000000 0.000000 10.000000 12.000000" orient="auto" markerUnits="userSpaceOnUse"> <polygon class="connection" fill="#0D32B2" stroke-width="2" points="0.000000,0.000000 10.000000,6.000000 0.000000,12.000000" /> </marker><path d="M 64.000000 130.000000 C 64.000000 168.000000 64.000000 188.000000 64.000000 224.000000" class="connection" style="fill:none;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" marker-end="url(#mk-3990223579)" /></g><style type="text/css"><![CDATA[
.text-bold {
font-family: "font-bold";
}
@font-face {
font-family: font-bold;
src: url("REMOVED");
}]]></style></svg>
`, svgURL, pngURL)
httpClient.Transport = roundTripFunc(func(req *http.Request) *http.Response {
2022-11-29 18:08:18 +00:00
respRecorder := httptest.NewRecorder()
2022-11-26 23:26:14 +00:00
switch req.URL.String() {
case svgURL:
2022-11-29 18:08:18 +00:00
respRecorder.WriteString(`<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->\r\n<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\r\n\t viewBox=\"0 0 58 58\" style=\"enable-background:new 0 0 58 58;\" xml:space=\"preserve\">\r\n<rect x=\"1\" y=\"7\" style=\"fill:#C3E1ED;stroke:#E7ECED;stroke-width:2;stroke-miterlimit:10;\" width=\"56\" height=\"44\"/>\r\n<circle style=\"fill:#ED8A19;\" cx=\"16\" cy=\"17.569\" r=\"6.569\"/>\r\n<polygon style=\"fill:#1A9172;\" points=\"56,36.111 55,35 43,24 32.5,35.5 37.983,40.983 42,45 56,45 \"/>\r\n<polygon style=\"fill:#1A9172;\" points=\"2,49 26,49 21.983,44.983 11.017,34.017 2,41.956 \"/>\r\n<rect x=\"2\" y=\"45\" style=\"fill:#6B5B4B;\" width=\"54\" height=\"5\"/>\r\n<polygon style=\"fill:#25AE88;\" points=\"37.983,40.983 27.017,30.017 10,45 42,45 \"/>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n</svg>`)
2022-11-26 23:26:14 +00:00
case pngURL:
2022-11-29 18:08:18 +00:00
respRecorder.Write(testPNGFile)
2022-11-26 23:26:14 +00:00
default:
2022-11-29 16:40:05 +00:00
t.Fatal(req.URL)
2022-11-26 23:26:14 +00:00
}
2022-11-29 18:08:18 +00:00
respRecorder.WriteHeader(200)
return respRecorder.Result()
2022-11-26 23:26:14 +00:00
})
out, err := BundleRemote(ctx, []byte(sampleSVG), false)
2022-11-26 23:26:14 +00:00
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(out), "https://") {
t.Fatal("links still exist")
}
if !strings.Contains(string(out), "image/svg+xml") {
t.Fatal("no svg image inserted")
}
if !strings.Contains(string(out), "image/png") {
t.Fatal("no png image inserted")
}
2022-11-29 21:19:03 +00:00
2023-06-06 17:43:52 +00:00
imgCache = sync.Map{}
2022-11-29 21:30:22 +00:00
// Test almost too large response
httpClient.Transport = roundTripFunc(func(req *http.Request) *http.Response {
2022-11-29 21:30:22 +00:00
respRecorder := httptest.NewRecorder()
2022-11-30 00:38:26 +00:00
bytes := make([]byte, maxImageSize)
2022-11-29 21:30:22 +00:00
rand.Read(bytes)
respRecorder.Write(bytes)
respRecorder.WriteHeader(200)
return respRecorder.Result()
})
_, err = BundleRemote(ctx, []byte(sampleSVG), false)
2022-11-29 21:30:22 +00:00
if err != nil {
t.Fatal(err)
}
2023-06-06 17:43:52 +00:00
imgCache = sync.Map{}
2022-11-29 21:30:22 +00:00
// Test too large response
httpClient.Transport = roundTripFunc(func(req *http.Request) *http.Response {
2022-11-29 21:30:22 +00:00
respRecorder := httptest.NewRecorder()
2022-11-29 22:20:17 +00:00
bytes := make([]byte, maxImageSize+1)
2022-11-29 21:30:22 +00:00
rand.Read(bytes)
respRecorder.Write(bytes)
respRecorder.WriteHeader(200)
return respRecorder.Result()
})
_, err = BundleRemote(ctx, []byte(sampleSVG), false)
2022-11-29 21:30:22 +00:00
if err == nil {
t.Fatal("expected error")
}
2023-06-06 17:43:52 +00:00
imgCache = sync.Map{}
2022-11-29 21:19:03 +00:00
// Test error response
httpClient.Transport = roundTripFunc(func(req *http.Request) *http.Response {
2022-11-29 21:19:03 +00:00
respRecorder := httptest.NewRecorder()
respRecorder.WriteHeader(500)
return respRecorder.Result()
})
_, err = BundleRemote(ctx, []byte(sampleSVG), false)
2022-11-29 21:19:03 +00:00
if err == nil {
t.Fatal("expected error")
}
2022-11-26 23:26:14 +00:00
}
2022-11-27 02:14:41 +00:00
func TestInlineLocal(t *testing.T) {
2023-06-06 17:43:52 +00:00
imgCache = sync.Map{}
2023-10-19 23:15:08 +00:00
ctx := log.WithTB(context.Background(), t, nil)
2022-11-27 02:14:41 +00:00
svgURL, err := filepath.Abs("./test_svg.svg")
if err != nil {
t.Fatal(err)
}
pngURL, err := filepath.Abs("./test_png.png")
if err != nil {
t.Fatal(err)
}
sampleSVG := fmt.Sprintf(`<?xml version="1.0" encoding="utf-8"?>
<svg
2023-01-04 00:42:39 +00:00
id="d2-svg"
2022-11-27 02:14:41 +00:00
style="background: white;"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
<![CDATA[
.shape {
shape-rendering: geometricPrecision;
stroke-linejoin: round;
}
.connection {
stroke-linecap: round;
stroke-linejoin: round;
}
]]>
</style><g id="a"><g class="shape" ><image href="%s" x="0" y="0" width="128" height="128" style="fill:#FFFFFF;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" /></g><text class="text-bold" x="64.000000" y="-15.000000" style="text-anchor:middle;font-size:16px;fill:#0A0F25">a</text></g><g id="b"><g class="shape" ><image href="%s" x="0" y="228" width="128" height="128" style="fill:#FFFFFF;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" /></g><text class="text-bold" x="64.000000" y="213.000000" style="text-anchor:middle;font-size:16px;fill:#0A0F25">b</text></g><g id="(a -&gt; b)[0]"><marker id="mk-3990223579" markerWidth="10.000000" markerHeight="12.000000" refX="7.000000" refY="6.000000" viewBox="0.000000 0.000000 10.000000 12.000000" orient="auto" markerUnits="userSpaceOnUse"> <polygon class="connection" fill="#0D32B2" stroke-width="2" points="0.000000,0.000000 10.000000,6.000000 0.000000,12.000000" /> </marker><path d="M 64.000000 130.000000 C 64.000000 168.000000 64.000000 188.000000 64.000000 224.000000" class="connection" style="fill:none;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" marker-end="url(#mk-3990223579)" /></g><style type="text/css"><![CDATA[
.text-bold {
font-family: "font-bold";
}
@font-face {
font-family: font-bold;
src: url("REMOVED");
}]]></style></svg>
`, svgURL, pngURL)
out, err := BundleLocal(ctx, []byte(sampleSVG), false)
2022-11-27 02:14:41 +00:00
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(out), svgURL) {
t.Fatal("links still exist")
}
if !strings.Contains(string(out), "image/svg+xml") {
t.Fatal("no svg image inserted")
}
if !strings.Contains(string(out), "image/png") {
t.Fatal("no png image inserted")
}
}
2023-06-06 17:43:52 +00:00
// TestDuplicateURL ensures that we don't fetch the same image twice
func TestDuplicateURL(t *testing.T) {
imgCache = sync.Map{}
2023-10-19 23:15:08 +00:00
ctx := log.WithTB(context.Background(), t, nil)
2023-06-06 17:43:52 +00:00
url1 := "https://icons.terrastruct.com/essentials/004-picture.svg"
url2 := "https://icons.terrastruct.com/essentials/004-picture.svg"
sampleSVG := fmt.Sprintf(`<?xml version="1.0" encoding="utf-8"?>
<svg
id="d2-svg"
style="background: white;"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
<![CDATA[
.shape {
shape-rendering: geometricPrecision;
stroke-linejoin: round;
}
.connection {
stroke-linecap: round;
stroke-linejoin: round;
}
]]>
</style><g id="a"><g class="shape" ><image href="%s" x="0" y="0" width="128" height="128" style="fill:#FFFFFF;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" /></g><text class="text-bold" x="64.000000" y="-15.000000" style="text-anchor:middle;font-size:16px;fill:#0A0F25">a</text></g><g id="b"><g class="shape" ><image href="%s" x="0" y="228" width="128" height="128" style="fill:#FFFFFF;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" /></g><text class="text-bold" x="64.000000" y="213.000000" style="text-anchor:middle;font-size:16px;fill:#0A0F25">b</text></g><g id="(a -&gt; b)[0]"><marker id="mk-3990223579" markerWidth="10.000000" markerHeight="12.000000" refX="7.000000" refY="6.000000" viewBox="0.000000 0.000000 10.000000 12.000000" orient="auto" markerUnits="userSpaceOnUse"> <polygon class="connection" fill="#0D32B2" stroke-width="2" points="0.000000,0.000000 10.000000,6.000000 0.000000,12.000000" /> </marker><path d="M 64.000000 130.000000 C 64.000000 168.000000 64.000000 188.000000 64.000000 224.000000" class="connection" style="fill:none;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" marker-end="url(#mk-3990223579)" /></g><style type="text/css"><![CDATA[
.text-bold {
font-family: "font-bold";
}
@font-face {
font-family: font-bold;
src: url("REMOVED");
}]]></style></svg>
`, url1, url2)
count := 0
httpClient.Transport = roundTripFunc(func(req *http.Request) *http.Response {
count++
respRecorder := httptest.NewRecorder()
respRecorder.WriteString(`<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->\r\n<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\r\n\t viewBox=\"0 0 58 58\" style=\"enable-background:new 0 0 58 58;\" xml:space=\"preserve\">\r\n<rect x=\"1\" y=\"7\" style=\"fill:#C3E1ED;stroke:#E7ECED;stroke-width:2;stroke-miterlimit:10;\" width=\"56\" height=\"44\"/>\r\n<circle style=\"fill:#ED8A19;\" cx=\"16\" cy=\"17.569\" r=\"6.569\"/>\r\n<polygon style=\"fill:#1A9172;\" points=\"56,36.111 55,35 43,24 32.5,35.5 37.983,40.983 42,45 56,45 \"/>\r\n<polygon style=\"fill:#1A9172;\" points=\"2,49 26,49 21.983,44.983 11.017,34.017 2,41.956 \"/>\r\n<rect x=\"2\" y=\"45\" style=\"fill:#6B5B4B;\" width=\"54\" height=\"5\"/>\r\n<polygon style=\"fill:#25AE88;\" points=\"37.983,40.983 27.017,30.017 10,45 42,45 \"/>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n</svg>`)
respRecorder.WriteHeader(200)
return respRecorder.Result()
})
out, err := BundleRemote(ctx, []byte(sampleSVG), false)
2023-06-06 17:43:52 +00:00
if err != nil {
t.Fatal(err)
}
tassert.Equal(t, 1, count)
if strings.Contains(string(out), url1) {
t.Fatal("links still exist")
}
tassert.Equal(t, 2, strings.Count(string(out), "image/svg+xml"))
}
2023-06-06 18:17:02 +00:00
func TestImgCache(t *testing.T) {
imgCache = sync.Map{}
2023-10-19 23:15:08 +00:00
ctx := log.WithTB(context.Background(), t, nil)
2023-06-06 18:17:02 +00:00
url1 := "https://icons.terrastruct.com/essentials/004-picture.svg"
url2 := "https://icons.terrastruct.com/essentials/004-picture.svg"
sampleSVG := fmt.Sprintf(`<?xml version="1.0" encoding="utf-8"?>
<svg
id="d2-svg"
style="background: white;"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
<![CDATA[
.shape {
shape-rendering: geometricPrecision;
stroke-linejoin: round;
}
.connection {
stroke-linecap: round;
stroke-linejoin: round;
}
]]>
</style><g id="a"><g class="shape" ><image href="%s" x="0" y="0" width="128" height="128" style="fill:#FFFFFF;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" /></g><text class="text-bold" x="64.000000" y="-15.000000" style="text-anchor:middle;font-size:16px;fill:#0A0F25">a</text></g><g id="b"><g class="shape" ><image href="%s" x="0" y="228" width="128" height="128" style="fill:#FFFFFF;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" /></g><text class="text-bold" x="64.000000" y="213.000000" style="text-anchor:middle;font-size:16px;fill:#0A0F25">b</text></g><g id="(a -&gt; b)[0]"><marker id="mk-3990223579" markerWidth="10.000000" markerHeight="12.000000" refX="7.000000" refY="6.000000" viewBox="0.000000 0.000000 10.000000 12.000000" orient="auto" markerUnits="userSpaceOnUse"> <polygon class="connection" fill="#0D32B2" stroke-width="2" points="0.000000,0.000000 10.000000,6.000000 0.000000,12.000000" /> </marker><path d="M 64.000000 130.000000 C 64.000000 168.000000 64.000000 188.000000 64.000000 224.000000" class="connection" style="fill:none;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" marker-end="url(#mk-3990223579)" /></g><style type="text/css"><![CDATA[
.text-bold {
font-family: "font-bold";
}
@font-face {
font-family: font-bold;
src: url("REMOVED");
}]]></style></svg>
`, url1, url2)
count := 0
httpClient.Transport = roundTripFunc(func(req *http.Request) *http.Response {
count++
respRecorder := httptest.NewRecorder()
respRecorder.WriteString(`<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->\r\n<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\r\n\t viewBox=\"0 0 58 58\" style=\"enable-background:new 0 0 58 58;\" xml:space=\"preserve\">\r\n<rect x=\"1\" y=\"7\" style=\"fill:#C3E1ED;stroke:#E7ECED;stroke-width:2;stroke-miterlimit:10;\" width=\"56\" height=\"44\"/>\r\n<circle style=\"fill:#ED8A19;\" cx=\"16\" cy=\"17.569\" r=\"6.569\"/>\r\n<polygon style=\"fill:#1A9172;\" points=\"56,36.111 55,35 43,24 32.5,35.5 37.983,40.983 42,45 56,45 \"/>\r\n<polygon style=\"fill:#1A9172;\" points=\"2,49 26,49 21.983,44.983 11.017,34.017 2,41.956 \"/>\r\n<rect x=\"2\" y=\"45\" style=\"fill:#6B5B4B;\" width=\"54\" height=\"5\"/>\r\n<polygon style=\"fill:#25AE88;\" points=\"37.983,40.983 27.017,30.017 10,45 42,45 \"/>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n<g>\r\n</g>\r\n</svg>`)
respRecorder.WriteHeader(200)
return respRecorder.Result()
})
// Using a cache, imgs are not refetched on multiple runs
_, err := BundleRemote(ctx, []byte(sampleSVG), true)
2023-06-06 18:17:02 +00:00
if err != nil {
t.Fatal(err)
}
_, err = BundleRemote(ctx, []byte(sampleSVG), true)
2023-06-06 18:17:02 +00:00
if err != nil {
t.Fatal(err)
}
tassert.Equal(t, 1, count)
// With cache disabled, it refetches
count = 0
_, err = BundleRemote(ctx, []byte(sampleSVG), false)
2023-06-06 18:17:02 +00:00
if err != nil {
t.Fatal(err)
}
_, err = BundleRemote(ctx, []byte(sampleSVG), false)
2023-06-06 18:17:02 +00:00
if err != nil {
t.Fatal(err)
}
tassert.Equal(t, 2, count)
}