blob: fe1e7f7df2dfcd99bcff4f3b6cc1ec8a77d00c49 [file] [log] [blame]
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package frontend
import (
"net/http/httptest"
"strings"
"testing"
)
func TestBadgeHandler_ServeSVG(t *testing.T) {
_, handler, _ := newTestServer(t, nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, httptest.NewRequest("GET", "/badge/net/http", nil))
if got, want := w.Result().Header.Get("Content-Type"), "image/svg+xml"; got != want {
t.Errorf("Content-Type = %q, want %q", got, want)
}
}
func TestBadgeHandler_ServeBadgeTool(t *testing.T) {
_, handler, _ := newTestServer(t, nil)
tests := []struct {
url string
want string
}{
{
"/badge/",
"<p>Type a pkg.go.dev URL above to create a badge link.</p>",
},
{
"/badge/?path=net/http",
`<a href="https://example.com/net/http"><img src="https://example.com/badge/net/http.svg" alt="Go Reference"></a>`,
},
{
"/badge/?path=https://pkg.go.dev/net/http",
`<a href="https://example.com/net/http"><img src="https://example.com/badge/net/http.svg" alt="Go Reference"></a>`,
},
{
"/badge/?path=github.com/google/uuid",
"[![Go Reference](https://example.com/badge/github.com/google/uuid.svg)](https://example.com/github.com/google/uuid)",
},
{
"/badge/?path=https://pkg.go.dev/github.com/google/uuid",
"[![Go Reference](https://example.com/badge/github.com/google/uuid.svg)](https://example.com/github.com/google/uuid)",
},
{
"/badge/?path=https://google.com",
"<p>Type a pkg.go.dev URL above to create a badge link.</p>",
},
{
"/badge/?path=https://google.com/github.com/google/uuid",
"[![Go Reference](https://example.com/badge/github.com/google/uuid.svg)](https://example.com/github.com/google/uuid)",
},
}
for _, test := range tests {
t.Run(test.url, func(t *testing.T) {
w := httptest.NewRecorder()
handler.ServeHTTP(w, httptest.NewRequest("GET", test.url, nil))
got := w.Body.String()
if !strings.Contains(w.Body.String(), test.want) {
t.Errorf("Expected html substring not found, want %s, got %s", test.want, got)
}
})
}
}