-
Notifications
You must be signed in to change notification settings - Fork 30
/
repo_test.go
140 lines (120 loc) · 3.17 KB
/
repo_test.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package repo
import (
"bytes"
"fmt"
"io/ioutil"
"sort"
"strings"
"testing"
"context"
"net/http"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/russross/blackfriday"
)
func TestAlphaAndEmbed(t *testing.T) {
query := startQuery()
query.Find("body > ul").Each(func(_ int, s *goquery.Selection) {
testList(t, s)
})
}
func TestDuplicatedLinks(t *testing.T) {
query := startQuery()
links := make(map[string]bool, 0)
query.Find("body a").Each(func(_ int, s *goquery.Selection) {
href, ok := s.Attr("href")
if !ok {
t.Errorf("expected '%s' to have href", s.Text())
}
if links[href] {
t.Fatalf("duplicated link '%s'", href)
}
links[href] = true
})
}
func testList(t *testing.T, list *goquery.Selection) {
list.Find("ul").Each(func(_ int, items *goquery.Selection) {
testList(t, items)
items.RemoveFiltered("ul")
})
cat := list.Prev().Text()
t.Run(fmt.Sprintf("order of [%s]", cat), func(t *testing.T) {
checkAlphabeticOrder(t, list)
})
t.Run(fmt.Sprintf("embeds in [%s]", cat), func(t *testing.T) {
checkEmbeds(t, list)
})
}
func readme() []byte {
input, err := ioutil.ReadFile("./README.md")
if err != nil {
panic(err)
}
html := append([]byte("<body>"), blackfriday.MarkdownCommon(input)...)
html = append(html, []byte("</body>")...)
return html
}
func startQuery() *goquery.Document {
buf := bytes.NewBuffer(readme())
query, err := goquery.NewDocumentFromReader(buf)
if err != nil {
panic(err)
}
return query
}
func checkAlphabeticOrder(t *testing.T, s *goquery.Selection) {
items := s.Find("li > a:first-child").Map(func(_ int, li *goquery.Selection) string {
return strings.ToLower(li.Text())
})
sorted := make([]string, len(items))
copy(sorted, items)
sort.Strings(sorted)
for k, item := range items {
if item != sorted[k] {
t.Errorf("expected '%s' but actual is '%s'", sorted[k], item)
}
}
if t.Failed() {
t.Logf("expected order is:\n%s", strings.Join(sorted, "\n"))
}
}
func checkEmbeds(t *testing.T, s *goquery.Selection) {
s.Find("li > a:first-child").Each(func(idx int, li *goquery.Selection) {
repourl, _ := li.Attr("href")
if strings.Contains(repourl, "#") {
return
}
t.Run(fmt.Sprintf("embed for [%s]", li.Text()), func(t *testing.T) {
checkEmbed(t, li.Parent().Find("a").Next())
})
})
}
func checkEmbed(t *testing.T, embed *goquery.Selection) {
if embed.Text() != "Embed" {
t.Errorf("expected 'Embed' but actual is '%v'", embed)
}
url, ok := embed.Attr("href")
if !ok {
t.Fatalf("expected a url to an embed")
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatalf("failed to make request to %s: %s", url, err.Error())
}
ctx, cancel := context.WithTimeout(req.Context(), 5*time.Second)
defer cancel()
rsp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
t.Fatalf("failed to perform request to %s: %s", url, err.Error())
}
conttype := rsp.Header.Get("Content-Type")
if !strings.Contains(conttype, "text/css") {
rsp.Body.Close()
t.Fatalf("expected content type to contain 'text/css' but actual is '%s' ", conttype)
}
_, err = ioutil.ReadAll(rsp.Body)
rsp.Body.Close()
if err != nil {
t.Fatalf("failed to obtain content: %s", err.Error())
}
}