dumb/handlers/article_test.go
2024-06-23 17:03:18 -06:00

56 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package handlers
import (
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/PuerkitoBio/goquery"
"github.com/rramiachraf/dumb/utils"
)
func TestArticle(t *testing.T) {
url := "/a/genius-celebrates-hip-hops-50th-anniversary-with-a-look-back-at-the-music-thats-defined-this-site"
title := "Genius Celebrates Hip-Hops 50th Anniversary With A Look Back At The Music Thats Defined This Site"
subtitle := "The first post in a yearlong look at the genres storied history."
r, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
l := utils.NewLogger(os.Stdout)
m := New(l, &assets{})
m.ServeHTTP(rr, r)
defer rr.Result().Body.Close()
if rr.Result().StatusCode != http.StatusOK {
t.Fatalf("expected %d, got %d\n", http.StatusOK, rr.Result().StatusCode)
}
doc, err := goquery.NewDocumentFromReader(rr.Result().Body)
if err != nil {
t.Fatal(err)
}
articleTitle := doc.Find("#article-title").First().Text()
if articleTitle != title {
t.Fatalf("expected %q, got %q\n", title, articleTitle)
}
articleSubtitle := doc.Find("#article-subtitle").First().Text()
if articleSubtitle != subtitle {
t.Fatalf("expected %q, got %q\n", subtitle, articleSubtitle)
}
articleBody := doc.Find("#article-body").First().Text()
if len(articleBody) == 0 {
t.Fatal("missing article body\n")
}
}