-
Notifications
You must be signed in to change notification settings - Fork 8
/
api_test.go
75 lines (60 loc) · 1.53 KB
/
api_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
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestLinkHeader(t *testing.T) {
url, _ := url.Parse("/cards?foo=bar")
header := LinkHeader("http://localhost:3000", url, 0)
expected := "<http://localhost:3000/cards?foo=bar&page=1>; rel=\"next\""
if header != expected {
t.Errorf("Expected %s not %s", expected, header)
}
url, _ = url.Parse("/cards?foo=bar&page=1")
header = LinkHeader("http://localhost:3000", url, 1)
expected = "<http://localhost:3000/cards?foo=bar&page=0>; rel=\"prev\", <http://localhost:3000/cards?foo=bar&page=2>; rel=\"next\""
if header != expected {
t.Errorf("Expected %s not %s", expected, header)
}
}
func TestApi(t *testing.T) {
db, err := GetDatabase()
if err != nil {
t.Fatal(err)
}
m := NewApi()
m.Map(db)
ts := httptest.NewServer(m)
defer ts.Close()
urls := []string{
"/mtg/cards",
"/mtg/cards?type=creature",
"/mtg/cards?subtype=zombie",
"/mtg/cards?supertype=legendary",
"/mtg/cards?color=red",
"/mtg/cards?name=rats",
"/mtg/cards?set=UNH",
"/mtg/cards?rarity=mythic",
"/mtg/cards?rarity=basic",
"/mtg/cards?oracle=you+win+the+game",
"/mtg/cards/1cdf2b87355ed978c0c5fe64bfc6a38c",
"/mtg/cards/typeahead?q=nessian",
"/mtg/sets",
"/mtg/sets/UNH",
"/mtg/colors",
"/mtg/types",
"/mtg/supertypes",
"/mtg/subtypes",
}
for _, url := range urls {
res, err := http.Get(ts.URL + url)
if err != nil {
t.Error(err)
}
if res.StatusCode != 200 {
t.Errorf("Expected %s to return 200, not %d", url, res.StatusCode)
}
}
}