1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "net/http"
11 "testing"
12
13 "github.com/google/go-cmp/cmp"
14 )
15
16 func TestActivityService_List(t *testing.T) {
17 client, mux, _, teardown := setup()
18 defer teardown()
19
20 mux.HandleFunc("/feeds", func(w http.ResponseWriter, r *http.Request) {
21 testMethod(t, r, "GET")
22
23 w.WriteHeader(http.StatusOK)
24 w.Write(feedsJSON)
25 })
26
27 ctx := context.Background()
28 got, _, err := client.Activity.ListFeeds(ctx)
29 if err != nil {
30 t.Errorf("Activity.ListFeeds returned error: %v", err)
31 }
32 if want := wantFeeds; !cmp.Equal(got, want) {
33 t.Errorf("Activity.ListFeeds = %+v, want %+v", got, want)
34 }
35
36 const methodName = "ListFeeds"
37 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
38 got, resp, err := client.Activity.ListFeeds(ctx)
39 if got != nil {
40 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
41 }
42 return resp, err
43 })
44 }
45
46 var feedsJSON = []byte(`{
47 "timeline_url": "https://github.com/timeline",
48 "user_url": "https://github.com/{user}",
49 "current_user_public_url": "https://github.com/defunkt",
50 "current_user_url": "https://github.com/defunkt.private?token=abc123",
51 "current_user_actor_url": "https://github.com/defunkt.private.actor?token=abc123",
52 "current_user_organization_url": "",
53 "current_user_organization_urls": [
54 "https://github.com/organizations/github/defunkt.private.atom?token=abc123"
55 ],
56 "_links": {
57 "timeline": {
58 "href": "https://github.com/timeline",
59 "type": "application/atom+xml"
60 },
61 "user": {
62 "href": "https://github.com/{user}",
63 "type": "application/atom+xml"
64 },
65 "current_user_public": {
66 "href": "https://github.com/defunkt",
67 "type": "application/atom+xml"
68 },
69 "current_user": {
70 "href": "https://github.com/defunkt.private?token=abc123",
71 "type": "application/atom+xml"
72 },
73 "current_user_actor": {
74 "href": "https://github.com/defunkt.private.actor?token=abc123",
75 "type": "application/atom+xml"
76 },
77 "current_user_organization": {
78 "href": "",
79 "type": ""
80 },
81 "current_user_organizations": [
82 {
83 "href": "https://github.com/organizations/github/defunkt.private.atom?token=abc123",
84 "type": "application/atom+xml"
85 }
86 ]
87 }
88 }`)
89
90 var wantFeeds = &Feeds{
91 TimelineURL: String("https://github.com/timeline"),
92 UserURL: String("https://github.com/{user}"),
93 CurrentUserPublicURL: String("https://github.com/defunkt"),
94 CurrentUserURL: String("https://github.com/defunkt.private?token=abc123"),
95 CurrentUserActorURL: String("https://github.com/defunkt.private.actor?token=abc123"),
96 CurrentUserOrganizationURL: String(""),
97 CurrentUserOrganizationURLs: []string{
98 "https://github.com/organizations/github/defunkt.private.atom?token=abc123",
99 },
100 Links: &FeedLinks{
101 Timeline: &FeedLink{
102 HRef: String("https://github.com/timeline"),
103 Type: String("application/atom+xml"),
104 },
105 User: &FeedLink{
106 HRef: String("https://github.com/{user}"),
107 Type: String("application/atom+xml"),
108 },
109 CurrentUserPublic: &FeedLink{
110 HRef: String("https://github.com/defunkt"),
111 Type: String("application/atom+xml"),
112 },
113 CurrentUser: &FeedLink{
114 HRef: String("https://github.com/defunkt.private?token=abc123"),
115 Type: String("application/atom+xml"),
116 },
117 CurrentUserActor: &FeedLink{
118 HRef: String("https://github.com/defunkt.private.actor?token=abc123"),
119 Type: String("application/atom+xml"),
120 },
121 CurrentUserOrganization: &FeedLink{
122 HRef: String(""),
123 Type: String(""),
124 },
125 CurrentUserOrganizations: []*FeedLink{
126 {
127 HRef: String("https://github.com/organizations/github/defunkt.private.atom?token=abc123"),
128 Type: String("application/atom+xml"),
129 },
130 },
131 },
132 }
133
134 func TestFeedLink_Marshal(t *testing.T) {
135 testJSONMarshal(t, &FeedLink{}, "{}")
136
137 u := &FeedLink{
138 HRef: String("h"),
139 Type: String("t"),
140 }
141
142 want := `{
143 "href": "h",
144 "type": "t"
145 }`
146
147 testJSONMarshal(t, u, want)
148 }
149
150 func TestFeeds_Marshal(t *testing.T) {
151 testJSONMarshal(t, &Feeds{}, "{}")
152
153 u := &Feeds{
154 TimelineURL: String("t"),
155 UserURL: String("u"),
156 CurrentUserPublicURL: String("cupu"),
157 CurrentUserURL: String("cuu"),
158 CurrentUserActorURL: String("cuau"),
159 CurrentUserOrganizationURL: String("cuou"),
160 CurrentUserOrganizationURLs: []string{"a"},
161 Links: &FeedLinks{
162 Timeline: &FeedLink{
163 HRef: String("h"),
164 Type: String("t"),
165 },
166 User: &FeedLink{
167 HRef: String("h"),
168 Type: String("t"),
169 },
170 CurrentUserPublic: &FeedLink{
171 HRef: String("h"),
172 Type: String("t"),
173 },
174 CurrentUser: &FeedLink{
175 HRef: String("h"),
176 Type: String("t"),
177 },
178 CurrentUserActor: &FeedLink{
179 HRef: String("h"),
180 Type: String("t"),
181 },
182 CurrentUserOrganization: &FeedLink{
183 HRef: String("h"),
184 Type: String("t"),
185 },
186 CurrentUserOrganizations: []*FeedLink{
187 {
188 HRef: String("h"),
189 Type: String("t"),
190 },
191 },
192 },
193 }
194
195 want := `{
196 "timeline_url": "t",
197 "user_url": "u",
198 "current_user_public_url": "cupu",
199 "current_user_url": "cuu",
200 "current_user_actor_url": "cuau",
201 "current_user_organization_url": "cuou",
202 "current_user_organization_urls": ["a"],
203 "_links": {
204 "timeline": {
205 "href": "h",
206 "type": "t"
207 },
208 "user": {
209 "href": "h",
210 "type": "t"
211 },
212 "current_user_public": {
213 "href": "h",
214 "type": "t"
215 },
216 "current_user": {
217 "href": "h",
218 "type": "t"
219 },
220 "current_user_actor": {
221 "href": "h",
222 "type": "t"
223 },
224 "current_user_organization": {
225 "href": "h",
226 "type": "t"
227 },
228 "current_user_organizations": [
229 {
230 "href": "h",
231 "type": "t"
232 }
233 ]
234 }
235 }`
236
237 testJSONMarshal(t, u, want)
238 }
239
240 func TestFeedLinks_Marshal(t *testing.T) {
241 testJSONMarshal(t, &FeedLinks{}, "{}")
242
243 u := &FeedLinks{
244 Timeline: &FeedLink{
245 HRef: String("h"),
246 Type: String("t"),
247 },
248 User: &FeedLink{
249 HRef: String("h"),
250 Type: String("t"),
251 },
252 CurrentUserPublic: &FeedLink{
253 HRef: String("h"),
254 Type: String("t"),
255 },
256 CurrentUser: &FeedLink{
257 HRef: String("h"),
258 Type: String("t"),
259 },
260 CurrentUserActor: &FeedLink{
261 HRef: String("h"),
262 Type: String("t"),
263 },
264 CurrentUserOrganization: &FeedLink{
265 HRef: String("h"),
266 Type: String("t"),
267 },
268 CurrentUserOrganizations: []*FeedLink{
269 {
270 HRef: String("h"),
271 Type: String("t"),
272 },
273 },
274 }
275
276 want := `{
277 "timeline": {
278 "href": "h",
279 "type": "t"
280 },
281 "user": {
282 "href": "h",
283 "type": "t"
284 },
285 "current_user_public": {
286 "href": "h",
287 "type": "t"
288 },
289 "current_user": {
290 "href": "h",
291 "type": "t"
292 },
293 "current_user_actor": {
294 "href": "h",
295 "type": "t"
296 },
297 "current_user_organization": {
298 "href": "h",
299 "type": "t"
300 },
301 "current_user_organizations": [
302 {
303 "href": "h",
304 "type": "t"
305 }
306 ]
307 }`
308
309 testJSONMarshal(t, u, want)
310 }
311
View as plain text