From 9f46bb27ae80b3ec06a63c86210655f302578496 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 23 Jan 2018 18:11:37 +0100 Subject: add fixtures --- href_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) (limited to 'href_test.go') diff --git a/href_test.go b/href_test.go index fe69810..86a0b8b 100644 --- a/href_test.go +++ b/href_test.go @@ -1,21 +1,54 @@ package main -import "testing" +import ( + "io" + "net/http" + "net/http/httptest" + "os" + "testing" +) func TestTitle(t *testing.T) { testCases := []struct { - url, title string + golden, title, contentType string }{ - {`https://www.linux.org.ru`, `LINUX.ORG.RU - Русская информация об ОС Linux`}, - {`http://www.opennet.ru`, `Проект OpenNet - всё, что связано с открытым ПО, открытыми технологиями, Linux, BSD и Unix`}, - {`http://www.openbsd.org`, `OpenBSD`}, - {`http://undeadly.org`, `OpenBSD Journal: A resource for the OpenBSD community`}, + { + `testdata/linux.org.ru`, + `LINUX.ORG.RU - Русская информация об ОС Linux`, + `text/html;charset=utf-8`, + }, + { + `testdata/opennet.ru`, + `Проект OpenNet - всё, что связано с открытым ПО, открытыми технологиями, Linux, BSD и Unix`, + `text/html; charset=koi8-r`, + }, + { + `testdata/openbsd.org`, + `OpenBSD`, + `text/html`, + }, + { + `testdata/undeadly.org`, + `OpenBSD Journal: A resource for the OpenBSD community`, + `text/html`, + }, } for _, tc := range testCases { - t.Run(tc.url, func(t *testing.T) { - title, err := getTitle(tc.url) + t.Run(tc.golden, func(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fd, err := os.Open(tc.golden) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer fd.Close() + w.Header().Set("Content-Type", tc.contentType) + io.Copy(w, fd) + })) + defer ts.Close() + title, err := getTitle(ts.URL) if err != nil { - t.Error(tc.url, err) + t.Error(tc.golden, err) } if title != tc.title { t.Errorf("got %v, want %v", title, tc.title) -- cgit v1.2.3