forked from dghubble/go-twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_test.go
92 lines (76 loc) · 3.04 KB
/
users_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
package twitter
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUserService_Show(t *testing.T) {
httpClient, mux, server := testServer()
defer server.Close()
mux.HandleFunc("/1.1/users/show.json", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, "GET", r)
assertQuery(t, map[string]string{"screen_name": "xkcdComic"}, r)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"name": "XKCD Comic", "favourites_count": 2}`)
})
client := NewClient(httpClient)
user, _, err := client.Users.Show(&UserShowParams{ScreenName: "xkcdComic"})
expected := &User{Name: "XKCD Comic", FavouritesCount: 2}
assert.Nil(t, err)
assert.Equal(t, expected, user)
}
func TestUserService_LookupWithIds(t *testing.T) {
httpClient, mux, server := testServer()
defer server.Close()
mux.HandleFunc("/1.1/users/lookup.json", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, "GET", r)
assertQuery(t, map[string]string{"user_id": "113419064,623265148"}, r)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `[{"screen_name": "golang"}, {"screen_name": "dghubble"}]`)
})
client := NewClient(httpClient)
users, _, err := client.Users.Lookup(&UserLookupParams{UserID: []int64{113419064, 623265148}})
expected := []User{User{ScreenName: "golang"}, User{ScreenName: "dghubble"}}
assert.Nil(t, err)
assert.Equal(t, expected, users)
}
func TestUserService_LookupWithScreenNames(t *testing.T) {
httpClient, mux, server := testServer()
defer server.Close()
mux.HandleFunc("/1.1/users/lookup.json", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, "GET", r)
assertQuery(t, map[string]string{"screen_name": "foo,bar"}, r)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `[{"name": "Foo"}, {"name": "Bar"}]`)
})
client := NewClient(httpClient)
users, _, err := client.Users.Lookup(&UserLookupParams{ScreenName: []string{"foo", "bar"}})
expected := []User{User{Name: "Foo"}, User{Name: "Bar"}}
assert.Nil(t, err)
assert.Equal(t, expected, users)
}
func TestUserService_Search(t *testing.T) {
httpClient, mux, server := testServer()
defer server.Close()
mux.HandleFunc("/1.1/users/search.json", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, "GET", r)
assertQuery(t, map[string]string{"count": "11", "q": "news"}, r)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `[{"name": "BBC"}, {"name": "BBC Breaking News"}]`)
})
client := NewClient(httpClient)
users, _, err := client.Users.Search("news", &UserSearchParams{Query: "override me", Count: 11})
expected := []User{User{Name: "BBC"}, User{Name: "BBC Breaking News"}}
assert.Nil(t, err)
assert.Equal(t, expected, users)
}
func TestUserService_SearchHandlesNilParams(t *testing.T) {
httpClient, mux, server := testServer()
defer server.Close()
mux.HandleFunc("/1.1/users/search.json", func(w http.ResponseWriter, r *http.Request) {
assertQuery(t, map[string]string{"q": "news"}, r)
})
client := NewClient(httpClient)
client.Users.Search("news", nil)
}