-
Notifications
You must be signed in to change notification settings - Fork 5
/
xkcd_test.go
183 lines (160 loc) · 4.33 KB
/
xkcd_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package xkcd
import (
"bytes"
"context"
"flag"
"fmt"
"io/ioutil"
"math"
"net/http"
"net/http/httptest"
"os"
"testing"
)
var (
responseLatest = []byte(`{"month":"4","num":2293,"link":"","year":"2020","news":"","safe_title":"RIP John Conway","transcript":"","alt":"1937-2020","img":"https://imgs.xkcd.com/comics/rip_john_conway.gif","title":"RIP John Conway","day":"13"}`)
response144 = []byte(`{"month":"8","num":144,"link":"","year":"2006","news":"","safe_title":"Parody Week: A Softer World","transcript":"when we open the lab each morning, we tell the robot to kill\nit's our little joke\nbut secretly\nwe're just afraid\nto tell it to love","alt":"The robot is pregnant. It isn't mine.","img":"https://imgs.xkcd.com/comics/a_softer_robot.jpg","title":"Parody Week: A Softer World","day":"17"}`)
comicLatest = Comic{
Month: 4,
Number: 2293,
Year: 2020,
SafeTitle: "RIP John Conway",
Alt: "1937-2020",
ImageURL: "https://imgs.xkcd.com/comics/rip_john_conway.gif",
Title: "RIP John Conway",
Day: 13,
}
comic144 = Comic{
Month: 8,
Number: 144,
Year: 2006,
SafeTitle: "Parody Week: A Softer World",
Transcript: "when we open the lab each morning, we tell the robot to kill\nit's our little joke\nbut secretly\nwe're just afraid\nto tell it to love",
Alt: "The robot is pregnant. It isn't mine.",
ImageURL: "https://imgs.xkcd.com/comics/a_softer_robot.jpg",
Title: "Parody Week: A Softer World",
Day: 17,
}
)
const nonExistentComic = math.MaxUint32
type testClient struct {
desc string
client *Client
}
var (
localClient, liveClient *Client
clients []testClient
)
func localServer() *httptest.Server {
writeJson := func(b []byte) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write(b)
})
}
mux := http.NewServeMux()
mux.Handle("/info.0.json", writeJson(responseLatest))
mux.Handle("/144/info.0.json", writeJson(response144))
mux.HandleFunc(fmt.Sprintf("/%d/info.0.json", nonExistentComic), func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
return httptest.NewUnstartedServer(mux)
}
func TestMain(m *testing.M) {
flag.Parse()
svr := localServer()
svr.Start()
defer svr.Close()
localClient = &Client{
HTTPClient: http.DefaultClient,
BaseURL: svr.URL,
}
liveClient = NewClient()
clients = []testClient{
{"local", localClient},
{"live", liveClient},
}
os.Exit(m.Run())
}
func TestLatest(t *testing.T) {
t.Parallel()
comic, err := localClient.Latest(context.Background())
if err != nil {
t.Errorf("%v", err)
return
}
if comicLatest != comic {
t.Errorf("comics do not match:\nexpected=%+v\n got=%+v", comicLatest, comic)
return
}
_, err = liveClient.Latest(context.Background())
if err != nil {
t.Errorf("%v", err)
return
}
}
func TestGet(t *testing.T) {
t.Parallel()
for _, c := range clients {
t.Run(c.desc, func(t *testing.T) {
comic, err := c.client.Get(context.Background(), 144)
if err != nil {
t.Errorf("%v", err)
return
}
if comic144 != comic {
t.Errorf("comics do not match:\nexpected=%+v\n got=%+v", comic144, comic)
return
}
})
}
}
func TestImage(t *testing.T) {
t.Parallel()
expect, err := ioutil.ReadFile("testdata/a_softer_robot.jpg")
if err != nil {
t.Fatalf("failed to read file")
}
for _, c := range clients {
t.Run(c.desc, func(t *testing.T) {
img, contentType, err := c.client.Image(context.Background(), 144)
if err != nil {
t.Errorf("%v", err)
return
}
b, err := ioutil.ReadAll(img)
if err != nil {
t.Errorf("failed to read image")
return
}
if !bytes.Equal(expect, b) {
t.Errorf("images do not match")
return
}
if contentType != "image/jpeg" {
t.Errorf("wrong content type: %s", contentType)
return
}
})
}
}
func TestStatusError(t *testing.T) {
t.Parallel()
for _, c := range clients {
t.Run(c.desc, func(t *testing.T) {
_, err := c.client.Get(context.Background(), math.MaxUint32)
if err == nil {
t.Errorf("error unexpectedly nil")
return
}
statusErr, ok := err.(StatusError)
if !ok {
t.Errorf("wrong error type")
return
}
if statusErr.Code != 404 {
t.Errorf("wrong status code:\nexpected=%d\n got=%d", 404, statusErr.Code)
return
}
})
}
}