package markup
import (
"strings"
"testing"
)
// Every renderer sends an external image through the proxy, hands the
// proxy the author's URL unescaped, and leaves relative images alone.
func TestExternalImagesGoThroughTheProxy(t *testing.T) {
var seen []string
SetImageProxy(func(src string) string {
seen = append(seen, src)
if strings.HasPrefix(src, "https://skip.example/") {
return ""
}
return "/img?u=" + src + "&s=sig"
})
t.Cleanup(func() { SetImageProxy(nil) })
md, err := Render([]byte("  "), nil)
if err != nil {
t.Fatal(err)
}
got := string(md)
if !strings.Contains(got, `src="/img?u=https://ci.example/badge.svg?branch=main&x=1&s=sig"`) {
t.Errorf("markdown image not proxied: %s", got)
}
if !strings.Contains(got, `src="img/shot.png"`) || !strings.Contains(got, `src="https://skip.example/a.png"`) {
t.Errorf("a relative or refused image was rewritten: %s", got)
}
if len(seen) == 0 || seen[0] != "https://ci.example/badge.svg?branch=main&x=1" {
t.Errorf("the proxy was handed %q, not the author's URL", seen)
}
wiki, err := RenderFossilWiki([]byte(`<img src="https://ci.example/w.png">`), nil)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(wiki), `src="/img?u=https://ci.example/w.png&s=sig"`) {
t.Errorf("fossil wiki image not proxied: %s", wiki)
}
if html := Sanitize(`<img src="http://ci.example/h.png">`); !strings.Contains(html, `src="/img?u=http://ci.example/h.png&s=sig"`) {
t.Errorf("html document image not proxied: %s", html)
}
}
// Off, nothing is rewritten.
func TestNoProxyLeavesImagesAlone(t *testing.T) {
SetImageProxy(nil)
out, err := Render([]byte(""), nil)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(out), `src="https://ci.example/badge.svg"`) {
t.Errorf("rewritten with no proxy: %s", out)
}
}