Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions go/http/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"regexp"
"strings"
"testing"

"github.com/go-chi/chi/v5"
)

// chdirToRepoRoot finds the repository root (directory containing resources/templates)
Expand Down Expand Up @@ -134,6 +137,44 @@ func TestRenderHTMLYield(t *testing.T) {
}
}

func TestLayoutWebLinksHaveRegisteredRoutes(t *testing.T) {
chdirToRepoRoot(t)
clearContentTemplateCache()

rec := httptest.NewRecorder()
renderHTML(rec, http.StatusOK, "templates/clusters", sampleTemplateData())
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String())
}

router := chi.NewRouter()
web := HttpWeb{}
web.RegisterRequests(router)

registeredGETRoutes := make(map[string]struct{})
err := chi.Walk(router, func(method, route string, _ http.Handler, _ ...func(http.Handler) http.Handler) error {
if method == http.MethodGet {
registeredGETRoutes[route] = struct{}{}
}
return nil
})
if err != nil {
t.Fatal(err)
}

hrefPattern := regexp.MustCompile(`href="(/web/[^"#?]+)"`)
matches := hrefPattern.FindAllStringSubmatch(rec.Body.String(), -1)
if len(matches) == 0 {
t.Fatal("rendered layout contains no constant /web/ links")
}
for _, match := range matches {
href := match[1]
if _, ok := registeredGETRoutes[href]; !ok {
t.Errorf("navbar link %q has no matching GET route", href)
}
}
}

// TestLayoutRequiresYield guards the martini-contrib/render contract: layout.tmpl
// uses {{yield}} to inject page content. Parsing layout without that FuncMap must fail.
func TestLayoutRequiresYield(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions resources/templates/layout.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
Clusters
</a>
<ul class="dropdown-menu" id="dropdown-clusters">
<li><a class="dropdown-item" href="{{.prefix}}/web/clusters/">Dashboard</a></li>
<li><a class="dropdown-item" href="{{.prefix}}/web/clusters-analysis/">Failure analysis</a></li>
<li><a class="dropdown-item" href="{{.prefix}}/web/clusters">Dashboard</a></li>
<li><a class="dropdown-item" href="{{.prefix}}/web/clusters-analysis">Failure analysis</a></li>
<li><a class="dropdown-item" href="{{.prefix}}/web/discover">Discover</a></li>
<li><hr class="dropdown-divider"></li>
</ul>
Expand Down
Loading