-
Notifications
You must be signed in to change notification settings - Fork 0
/
hkn_test.go
147 lines (110 loc) · 3.06 KB
/
hkn_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
package hkn
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
var (
mux *http.ServeMux
client *Client
server *httptest.Server
)
func setup() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
client = NewClient()
client.BaseURL = server.URL
}
func teardown() {
server.Close()
}
func TestGetItem(t *testing.T) {
setup()
defer teardown()
jsonItem := `{
"by" : "dhouston",
"descendants" : 71,
"id" : 8863,
"kids" : [9224, 8952, 8917],
"score" : 104,
"time" : 1175714200,
"title" : "My YC app: Dropbox - Throw away your USB drive",
"type" : "story",
"url" : "http://www.getdropbox.com/u/2/screencast.html"
}`
mux.HandleFunc("/item/8863.json", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, jsonItem)
})
expected := Item{}
_ = json.Unmarshal([]byte(jsonItem), &expected)
item, err := client.GetItem(8863)
if err != nil {
t.Errorf("Error for GetItem(8863) should have been nil. Was: %v", err)
}
if !reflect.DeepEqual(item, expected) {
t.Errorf("GetItem(8863) returned %+v, was expecting %+v", item, expected)
}
}
func TestGetUser(t *testing.T) {
setup()
defer teardown()
jsonUser := `{
"about" : "This is a test",
"created" : 1173923446,
"id" : "jl",
"karma" : 4094,
"submitted": [18498213, 16659709, 16659632, 16659556]
}`
mux.HandleFunc("/user/jl.json", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, jsonUser)
})
expected := User{}
_ = json.Unmarshal([]byte(jsonUser), &expected)
user, err := client.GetUser("jl")
if err != nil {
t.Errorf("Error for GetUser('jl') should have been nil. Was: %v", err)
}
if !reflect.DeepEqual(user, expected) {
t.Errorf("GetUser('jl') returned %+v, was expecting %+v", user, expected)
}
}
func TestGetMaxItemId(t *testing.T) {
setup()
defer teardown()
maxItem := `123456`
mux.HandleFunc("/maxitem.json", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, maxItem)
})
var expected int
_ = json.Unmarshal([]byte(maxItem), &expected)
id, err := client.GetMaxItemID()
if err != nil {
t.Errorf("Error for GetMaxItemId() should have been nil. Was: %v", err)
}
if !reflect.DeepEqual(id, expected) {
t.Errorf("GetMaxItemId() returned %+v, was expecting %+v", id, expected)
}
}
func TestGetUpdates(t *testing.T) {
setup()
defer teardown()
jsonUpdates := `{
"items" : [8423305, 8420805, 8423379, 8422504],
"profiles" : ["thefox", "mdda", "plinkplonk", "GBond", "rqebmm", "neom"]
}`
mux.HandleFunc("/updates.json", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, jsonUpdates)
})
var expected Updates
_ = json.Unmarshal([]byte(jsonUpdates), &expected)
updates, err := client.GetUpdates()
if err != nil {
t.Errorf("Error for GetUpdates() should have been nil. Was: %v", err)
}
if !reflect.DeepEqual(updates, expected) {
t.Errorf("GetUpdates() returned %+v, was expecting %+v", updates, expected)
}
}