summaryrefslogtreecommitdiff
path: root/internal/href/href_test.go
blob: ed2bc408aba01229f52876ece30dc72b627b6cbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package href

import (
	"io"
	"net/http"
	"net/http/httptest"
	"os"
	"testing"
)

func TestTitle(t *testing.T) {
	testCases := []struct {
		fixture, title, contentType string
	}{
		{
			`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`,
		},
		{
			`testdata/deepnested.html`,
			`!`,
			`text/html`,
		},
	}
	for _, tc := range testCases {
		t.Run(tc.fixture, func(t *testing.T) {
			ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				fd, err := os.Open(tc.fixture)
				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 := Title(ts.URL)
			if err != nil {
				t.Error(tc.fixture, err)
			}
			if title != tc.title {
				t.Errorf("got %v, want %v", title, tc.title)
			}
		})
	}
}