github.com/google/go-github/v70@v70.0.0/github/activity_test.go (about) 1 // Copyright 2016 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 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 t.Parallel() 18 client, mux, _ := setup(t) 19 20 mux.HandleFunc("/feeds", func(w http.ResponseWriter, r *http.Request) { 21 testMethod(t, r, "GET") 22 23 w.WriteHeader(http.StatusOK) 24 assertWrite(t, w, 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: Ptr("https://github.com/timeline"), 92 UserURL: Ptr("https://github.com/{user}"), 93 CurrentUserPublicURL: Ptr("https://github.com/defunkt"), 94 CurrentUserURL: Ptr("https://github.com/defunkt.private?token=abc123"), 95 CurrentUserActorURL: Ptr("https://github.com/defunkt.private.actor?token=abc123"), 96 CurrentUserOrganizationURL: Ptr(""), 97 CurrentUserOrganizationURLs: []string{ 98 "https://github.com/organizations/github/defunkt.private.atom?token=abc123", 99 }, 100 Links: &FeedLinks{ 101 Timeline: &FeedLink{ 102 HRef: Ptr("https://github.com/timeline"), 103 Type: Ptr("application/atom+xml"), 104 }, 105 User: &FeedLink{ 106 HRef: Ptr("https://github.com/{user}"), 107 Type: Ptr("application/atom+xml"), 108 }, 109 CurrentUserPublic: &FeedLink{ 110 HRef: Ptr("https://github.com/defunkt"), 111 Type: Ptr("application/atom+xml"), 112 }, 113 CurrentUser: &FeedLink{ 114 HRef: Ptr("https://github.com/defunkt.private?token=abc123"), 115 Type: Ptr("application/atom+xml"), 116 }, 117 CurrentUserActor: &FeedLink{ 118 HRef: Ptr("https://github.com/defunkt.private.actor?token=abc123"), 119 Type: Ptr("application/atom+xml"), 120 }, 121 CurrentUserOrganization: &FeedLink{ 122 HRef: Ptr(""), 123 Type: Ptr(""), 124 }, 125 CurrentUserOrganizations: []*FeedLink{ 126 { 127 HRef: Ptr("https://github.com/organizations/github/defunkt.private.atom?token=abc123"), 128 Type: Ptr("application/atom+xml"), 129 }, 130 }, 131 }, 132 } 133 134 func TestFeedLink_Marshal(t *testing.T) { 135 t.Parallel() 136 testJSONMarshal(t, &FeedLink{}, "{}") 137 138 u := &FeedLink{ 139 HRef: Ptr("h"), 140 Type: Ptr("t"), 141 } 142 143 want := `{ 144 "href": "h", 145 "type": "t" 146 }` 147 148 testJSONMarshal(t, u, want) 149 } 150 151 func TestFeeds_Marshal(t *testing.T) { 152 t.Parallel() 153 testJSONMarshal(t, &Feeds{}, "{}") 154 155 u := &Feeds{ 156 TimelineURL: Ptr("t"), 157 UserURL: Ptr("u"), 158 CurrentUserPublicURL: Ptr("cupu"), 159 CurrentUserURL: Ptr("cuu"), 160 CurrentUserActorURL: Ptr("cuau"), 161 CurrentUserOrganizationURL: Ptr("cuou"), 162 CurrentUserOrganizationURLs: []string{"a"}, 163 Links: &FeedLinks{ 164 Timeline: &FeedLink{ 165 HRef: Ptr("h"), 166 Type: Ptr("t"), 167 }, 168 User: &FeedLink{ 169 HRef: Ptr("h"), 170 Type: Ptr("t"), 171 }, 172 CurrentUserPublic: &FeedLink{ 173 HRef: Ptr("h"), 174 Type: Ptr("t"), 175 }, 176 CurrentUser: &FeedLink{ 177 HRef: Ptr("h"), 178 Type: Ptr("t"), 179 }, 180 CurrentUserActor: &FeedLink{ 181 HRef: Ptr("h"), 182 Type: Ptr("t"), 183 }, 184 CurrentUserOrganization: &FeedLink{ 185 HRef: Ptr("h"), 186 Type: Ptr("t"), 187 }, 188 CurrentUserOrganizations: []*FeedLink{ 189 { 190 HRef: Ptr("h"), 191 Type: Ptr("t"), 192 }, 193 }, 194 }, 195 } 196 197 want := `{ 198 "timeline_url": "t", 199 "user_url": "u", 200 "current_user_public_url": "cupu", 201 "current_user_url": "cuu", 202 "current_user_actor_url": "cuau", 203 "current_user_organization_url": "cuou", 204 "current_user_organization_urls": ["a"], 205 "_links": { 206 "timeline": { 207 "href": "h", 208 "type": "t" 209 }, 210 "user": { 211 "href": "h", 212 "type": "t" 213 }, 214 "current_user_public": { 215 "href": "h", 216 "type": "t" 217 }, 218 "current_user": { 219 "href": "h", 220 "type": "t" 221 }, 222 "current_user_actor": { 223 "href": "h", 224 "type": "t" 225 }, 226 "current_user_organization": { 227 "href": "h", 228 "type": "t" 229 }, 230 "current_user_organizations": [ 231 { 232 "href": "h", 233 "type": "t" 234 } 235 ] 236 } 237 }` 238 239 testJSONMarshal(t, u, want) 240 } 241 242 func TestFeedLinks_Marshal(t *testing.T) { 243 t.Parallel() 244 testJSONMarshal(t, &FeedLinks{}, "{}") 245 246 u := &FeedLinks{ 247 Timeline: &FeedLink{ 248 HRef: Ptr("h"), 249 Type: Ptr("t"), 250 }, 251 User: &FeedLink{ 252 HRef: Ptr("h"), 253 Type: Ptr("t"), 254 }, 255 CurrentUserPublic: &FeedLink{ 256 HRef: Ptr("h"), 257 Type: Ptr("t"), 258 }, 259 CurrentUser: &FeedLink{ 260 HRef: Ptr("h"), 261 Type: Ptr("t"), 262 }, 263 CurrentUserActor: &FeedLink{ 264 HRef: Ptr("h"), 265 Type: Ptr("t"), 266 }, 267 CurrentUserOrganization: &FeedLink{ 268 HRef: Ptr("h"), 269 Type: Ptr("t"), 270 }, 271 CurrentUserOrganizations: []*FeedLink{ 272 { 273 HRef: Ptr("h"), 274 Type: Ptr("t"), 275 }, 276 }, 277 } 278 279 want := `{ 280 "timeline": { 281 "href": "h", 282 "type": "t" 283 }, 284 "user": { 285 "href": "h", 286 "type": "t" 287 }, 288 "current_user_public": { 289 "href": "h", 290 "type": "t" 291 }, 292 "current_user": { 293 "href": "h", 294 "type": "t" 295 }, 296 "current_user_actor": { 297 "href": "h", 298 "type": "t" 299 }, 300 "current_user_organization": { 301 "href": "h", 302 "type": "t" 303 }, 304 "current_user_organizations": [ 305 { 306 "href": "h", 307 "type": "t" 308 } 309 ] 310 }` 311 312 testJSONMarshal(t, u, want) 313 }