github.com/grafana/pyroscope@v1.18.0/pkg/api/index_test.go (about) 1 // SPDX-License-Identifier: AGPL-3.0-only 2 // Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/api/handlers_test.go 3 // Provenance-includes-license: Apache-2.0 4 // Provenance-includes-copyright: The Cortex Authors. 5 6 package api 7 8 import ( 9 "net/http/httptest" 10 "strings" 11 "testing" 12 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestIndexHandlerPrefix(t *testing.T) { 17 c := NewIndexPageContent() 18 c.AddLinks(defaultWeight, "Store Gateway", []IndexPageLink{{Desc: "Ring status", Path: "/store-gateway/ring"}}) 19 20 for _, tc := range []struct { 21 prefix string 22 toBeFound string 23 }{ 24 {prefix: "", toBeFound: "<a href=\"/store-gateway/ring\">"}, 25 {prefix: "/test", toBeFound: "<a href=\"/test/store-gateway/ring\">"}, 26 // All the extra slashed are cleaned up in the result. 27 {prefix: "///test///", toBeFound: "<a href=\"/test/store-gateway/ring\">"}, 28 } { 29 h := indexHandler(tc.prefix, c) 30 31 req := httptest.NewRequest("GET", "/", nil) 32 resp := httptest.NewRecorder() 33 34 h.ServeHTTP(resp, req) 35 36 require.Equal(t, 200, resp.Code) 37 require.True(t, strings.Contains(resp.Body.String(), tc.toBeFound)) 38 } 39 } 40 41 func TestIndexPageContent(t *testing.T) { 42 c := NewIndexPageContent() 43 c.AddLinks(defaultWeight, "Some group", []IndexPageLink{ 44 {Desc: "Some link", Path: "/store-gateway/ring"}, 45 {Dangerous: true, Desc: "Boom!", Path: "/store-gateway/boom"}, 46 }) 47 48 h := indexHandler("", c) 49 50 req := httptest.NewRequest("GET", "/", nil) 51 resp := httptest.NewRecorder() 52 53 h.ServeHTTP(resp, req) 54 55 require.Equal(t, 200, resp.Code) 56 require.True(t, strings.Contains(resp.Body.String(), "Some group")) 57 require.True(t, strings.Contains(resp.Body.String(), "Some link")) 58 require.True(t, strings.Contains(resp.Body.String(), "Dangerous")) 59 require.True(t, strings.Contains(resp.Body.String(), "Boom!")) 60 require.True(t, strings.Contains(resp.Body.String(), "Dangerous")) 61 require.True(t, strings.Contains(resp.Body.String(), "/store-gateway/ring")) 62 require.True(t, strings.Contains(resp.Body.String(), "/store-gateway/boom")) 63 require.False(t, strings.Contains(resp.Body.String(), "/compactor/ring")) 64 }