This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
brick_test.go
155 lines (126 loc) · 3.81 KB
/
brick_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
// Tests a request to Brick with an invalid root request (/)
func TestRoot(t *testing.T) {
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://brick.im/", nil)
if err != nil {
t.Fatal(err)
}
handler(res, req)
if res.Code != 400 || len(res.Body.String()) > 0 {
t.Fail()
}
}
// Tests a request for a single font
func TestSingle(t *testing.T) {
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://brick.im/EB+Garamond:400", nil)
if err != nil {
t.Fatal(err)
}
handler(res, req)
if res.Code != 200 {
t.Fail()
}
var expected = formatRule("EB Garamond", "normal", "400", "EB Garamond 12 Regular", "ebgaramond", "400")
if res.Body.String() != expected {
t.Fail()
}
}
// Tests a request for multiple weights and styles of a font
func TestVariants(t *testing.T) {
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://brick.im/Raleway:400,400i,700", nil)
if err != nil {
t.Fatal(err)
}
handler(res, req)
if res.Code != 200 {
t.Fail()
}
var expected = formatRule("Raleway", "normal", "400", "Raleway Regular", "raleway", "400") +
formatRule("Raleway", "italic", "400", "Raleway Italic", "raleway", "400i") +
formatRule("Raleway", "normal", "700", "Raleway Bold", "raleway", "700")
if res.Body.String() != expected {
t.Fail()
}
}
// Tests a request for multiple font families
func TestFamilies(t *testing.T) {
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://brick.im/Open+Sans:400,700/Merriweather:400", nil)
if err != nil {
t.Fatal(err)
}
handler(res, req)
if res.Code != 200 {
t.Fail()
}
var expected = formatRule("Open Sans", "normal", "400", "Open Sans Regular", "opensans", "400") +
formatRule("Open Sans", "normal", "700", "Open Sans Bold", "opensans", "700") +
formatRule("Merriweather", "normal", "400", "Merriweather", "merriweather", "400")
if res.Body.String() != expected {
t.Fail()
}
}
// Tests a request including non-existing variants
func TestNonexistentVariant(t *testing.T) {
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://brick.im/Open+Sans:400,800", nil)
if err != nil {
t.Fatal(err)
}
handler(res, req)
if res.Code != 200 {
t.Fail()
}
var expected = formatRule("Open Sans", "normal", "400", "Open Sans Regular", "opensans", "400")
if res.Body.String() != expected {
t.Fail()
}
}
// Tests a request including non-existing families
func TestNonexistentFamily(t *testing.T) {
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://brick.im/Foo+Sans:400/Open+Sans:400", nil)
if err != nil {
t.Fatal(err)
}
handler(res, req)
if res.Code != 200 {
t.Fail()
}
var expected = formatRule("Open Sans", "normal", "400", "Open Sans Regular", "opensans", "400")
if res.Body.String() != expected {
t.Fail()
}
}
// Tests a request using the force flag (preventing the browser from
// loading the font from the local computer)
func TestForce(t *testing.T) {
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://brick.im/Roboto:400:f", nil)
if err != nil {
t.Fatal(err)
}
handler(res, req)
if res.Code != 200 {
t.Fail()
}
var expected = "@font-face{font-family:'Roboto';font-style:normal;font-weight:400;src:url(//brick.freetls.fastly.net/fonts/roboto/400.woff) format('woff')}"
if res.Body.String() != expected {
t.Fail()
}
}
// Templating function to build the expected CSS @font-face rules
func formatRule(family string, style string, weight string, local string, slug string, file string) string {
template := "@font-face{font-family:'%s';font-style:%s;font-weight:%s;" +
"src:local('%s'),url(//brick.freetls.fastly.net/fonts/%s/%s.woff) format('woff')}"
return fmt.Sprintf(template, family, style, weight, local, slug, file)
}