package main
import (
"bytes"
"image/png"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// A public repository's link preview is its own 1200x630 card, named by
// its pages; a private one has none for a stranger; a page that is not
// a repository's keeps the forge's mark.
func TestARepositoryLinkUnfurlsAsItsOwnCard(t *testing.T) {
g := newSiteRig(t)
get := func(path string, hdr map[string]string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodGet, path, nil)
for k, v := range hdr {
req.Header.Set(k, v)
}
w := httptest.NewRecorder()
g.h.ServeHTTP(w, req)
return w
}
w := get("/r/pub/og.png", nil)
if w.Code != http.StatusOK || w.Header().Get("Content-Type") != "image/png" {
t.Fatalf("FAIL: the card: %d %s", w.Code, w.Header().Get("Content-Type"))
}
img, err := png.Decode(bytes.NewReader(w.Body.Bytes()))
if err != nil || img.Bounds().Dx() != 1200 || img.Bounds().Dy() != 630 {
t.Fatalf("FAIL: the card is not a 1200x630 PNG: %v %v", err, img)
}
if again := get("/r/pub/og.png", map[string]string{"If-None-Match": w.Header().Get("ETag")}); again.Code != http.StatusNotModified {
t.Errorf("FAIL: an unchanged card was sent again: %d", again.Code)
}
if w := get("/r/secret/og.png", nil); w.Code != http.StatusNotFound {
t.Errorf("FAIL: a private repository's card for a stranger: %d", w.Code)
}
page := get("/r/pub/timeline", nil).Body.String() // the listing needs a real store; every repository page shares the head
if !strings.Contains(page, `/r/pub/og.png"`) || !strings.Contains(page, `content="summary_large_image"`) {
t.Error("FAIL: the repository page does not name its card")
}
home := get("/", nil).Body.String()
if !strings.Contains(home, `/assets/quarry.png"`) || strings.Contains(home, "og.png") {
t.Error("FAIL: a page that is not a repository's lost the forge's mark")
}
}