-
Notifications
You must be signed in to change notification settings - Fork 10
/
beer_info_test.go
134 lines (117 loc) · 3.39 KB
/
beer_info_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
package untappd
import (
"net/http"
"net/url"
"strconv"
"strings"
"testing"
)
// TestClientBeerInfoBadBeer verifies that Client.Beer.Info returns an error when
// an invalid beer is queried.
func TestClientBeerInfoBadBeer(t *testing.T) {
beerID := -1
sBeerID := strconv.Itoa(beerID)
c, done := beerInfoTestClient(t, func(t *testing.T, w http.ResponseWriter, r *http.Request) {
path := "/v4/beer/info/" + sBeerID + "/"
if p := r.URL.Path; p != path {
t.Fatalf("unexpected URL path: %q != %q", p, path)
}
w.WriteHeader(http.StatusInternalServerError)
w.Write(invalidBeerErrJSON)
})
defer done()
_, _, err := c.Beer.Info(beerID, false)
assertInvalidBeerErr(t, err)
}
// TestClientBeerInfoCompactOK verifies that Client.Beer.Info properly requests compact
// beer output.
func TestClientBeerInfoCompactOK(t *testing.T) {
c, done := beerInfoTestClient(t, func(t *testing.T, w http.ResponseWriter, r *http.Request) {
assertParameters(t, r, url.Values{
"compact": []string{"true"},
})
// In the future, we may return compact canned beer data here.
// For now, write a mostly empty JSON object is enough to get
// test coverage.
w.Write([]byte(`{"response":{"beer":{"id":1}}}`))
})
defer done()
if _, _, err := c.Beer.Info(1, true); err != nil {
t.Fatal(err)
}
}
// TestClientBeerInfoOK verifies that Client.Beer.Info returns a valid beer when
// provided with correct input parameters.
func TestClientBeerInfoOK(t *testing.T) {
beerID := 1
sBeerID := strconv.Itoa(beerID)
c, done := beerInfoTestClient(t, func(t *testing.T, w http.ResponseWriter, r *http.Request) {
path := "/v4/beer/info/" + sBeerID + "/"
if p := r.URL.Path; p != path {
t.Fatalf("unexpected URL path: %q != %q", p, path)
}
w.Write(blackNoteBeerJSON)
})
defer done()
b, _, err := c.Beer.Info(beerID, false)
if err != nil {
t.Fatal(err)
}
if id := b.ID; id != beerID {
t.Fatalf("unexpected ID: %d != %d", id, 1)
}
beerName := "Black Note Stout"
if n := b.Name; n != beerName {
t.Fatalf("unexpected Name: %q != %q", n, beerName)
}
breweryName := "Bell's Brewery, Inc."
if n := b.Brewery.Name; n != breweryName {
t.Fatalf("unexpected Brewery.Name: %q != %q", n, breweryName)
}
overallCount := 123
if c := b.OverallCount; c != overallCount {
t.Fatalf("unexpected OverallCount: %q != %q", c, overallCount)
}
}
// beerInfoTestClient builds upon testClient, and adds additional sanity checks
// for tests which target the beer info API.
func beerInfoTestClient(t *testing.T, fn func(t *testing.T, w http.ResponseWriter, r *http.Request)) (*Client, func()) {
return testClient(t, func(t *testing.T, w http.ResponseWriter, r *http.Request) {
// Always GET request
method := "GET"
if m := r.Method; m != method {
t.Fatalf("unexpected HTTP method: %q != %q", m, method)
}
// Always uses specific path prefix
prefix := "/v4/beer/info/"
if p := r.URL.Path; !strings.HasPrefix(p, prefix) {
t.Fatalf("unexpected HTTP path prefix: %q != %q", p, prefix)
}
// Guard against panics
if fn != nil {
fn(t, w, r)
}
})
}
// Canned JSON used in tests
var blackNoteBeerJSON = []byte(`
{
"meta": {
"code": 200,
"response_time": {
"time": 0,
"measure": "seconds"
}
},
"notifications": {},
"response": {
"beer": {
"bid": 1,
"beer_name": "Black Note Stout",
"rating_count": 123,
"brewery": {
"brewery_name": "Bell's Brewery, Inc."
}
}
}
}`)