github.com/grafana/pyroscope@v1.18.0/public/execute_template_test.go (about)

     1  package public_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/grafana/pyroscope/public"
    12  )
    13  
    14  func TestInjectingBaseURL(t *testing.T) {
    15  	bin, err := os.ReadFile("testdata/baseurl.html")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  
    20  	for _, tc := range []struct {
    21  		name     string
    22  		basePath string
    23  		expected string
    24  	}{
    25  		{basePath: "", expected: "/"},
    26  		{basePath: "     ", expected: "/"},
    27  		{basePath: "/foobar/", expected: "/foobar/"},
    28  		{basePath: "/foobar", expected: "/foobar/"},
    29  		{basePath: "foobar", expected: "/foobar/"},
    30  		{basePath: "   foobar   ", expected: "/foobar/"},
    31  		{basePath: "http://localhost:8080/foobar/", expected: "http://localhost:8080/foobar/"},
    32  		{basePath: "http://localhost:8080/foobar", expected: "http://localhost:8080/foobar/"},
    33  	} {
    34  		tc := tc
    35  		t.Run(fmt.Sprintf("'%s' -> '%s'", tc.basePath, tc.expected), func(t *testing.T) {
    36  			data, err := public.ExecuteTemplate(bin, public.Params{BasePath: tc.basePath})
    37  			assert.NoError(t, err)
    38  			assert.Equal(t, fmt.Sprintf(`<base href="%s" />`, tc.expected), strings.TrimSpace(string(data)))
    39  		})
    40  	}
    41  }