Skip to content
quarry Artifact [bdf8abac1f]
Login

Artifact [bdf8abac1f]

Artifact bdf8abac1fa06dbd634f56ab20bad3b94d5fb690f605c7c78a566193d5f3ba2c:


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("![build](https://ci.example/badge.svg?branch=main&x=1) ![local](img/shot.png) ![kept](https://skip.example/a.png)"), 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&amp;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&amp;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("![b](https://ci.example/badge.svg)"), 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)
	}
}