forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.feature
More file actions
530 lines (496 loc) · 14.7 KB
/
Copy pathapi.feature
File metadata and controls
530 lines (496 loc) · 14.7 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
@cache_clear
Feature: hub api
Background:
Given I am "octokitten" on github.com with OAuth token "OTOKEN"
Scenario: GET resource
Given the GitHub API server:
"""
get('/hello/world') {
halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN'
halt 401 unless request.env['HTTP_ACCEPT'] == 'application/vnd.github.v3+json;charset=utf-8'
json :name => "Ed"
}
"""
When I successfully run `hub api hello/world`
Then the output should contain exactly:
"""
{"name":"Ed"}
"""
Scenario: GET Enterprise resource
Given I am "octokitten" on git.my.org with OAuth token "FITOKEN"
Given the GitHub API server:
"""
get('/api/v3/hello/world', :host_name => 'git.my.org') {
halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token FITOKEN'
json :name => "Ed"
}
"""
And $GITHUB_HOST is "git.my.org"
When I successfully run `hub api hello/world`
Then the output should contain exactly:
"""
{"name":"Ed"}
"""
Scenario: Non-success response
Given the GitHub API server:
"""
get('/hello/world') {
status 400
json :name => "Ed"
}
"""
When I run `hub api hello/world`
Then the exit status should be 22
And the stdout should contain exactly:
"""
{"name":"Ed"}
"""
And the stderr should contain exactly ""
Scenario: Non-success response flat output
Given the GitHub API server:
"""
get('/hello/world') {
status 400
json :name => "Ed"
}
"""
When I run `hub api -t hello/world`
Then the exit status should be 22
And the stdout should contain exactly:
"""
.name Ed\n
"""
And the stderr should contain exactly ""
Scenario: Non-success response doesn't choke on non-JSON
Given the GitHub API server:
"""
get('/hello/world') {
status 400
content_type :text
'Something went wrong'
}
"""
When I run `hub api -t hello/world`
Then the exit status should be 22
And the stdout should contain exactly:
"""
Something went wrong
"""
And the stderr should contain exactly ""
Scenario: GET query string
Given the GitHub API server:
"""
get('/hello/world') {
json Hash[*params.sort.flatten]
}
"""
When I successfully run `hub api -XGET -Fname=Ed -Fnum=12 -Fbool=false -Fvoid=null hello/world`
Then the output should contain exactly:
"""
{"bool":"false","name":"Ed","num":"12","void":""}
"""
Scenario: GET full URL
Given the GitHub API server:
"""
get('/hello/world', :host_name => 'api.github.com') {
halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN'
json :name => "Faye"
}
"""
When I successfully run `hub api https://api.github.com/hello/world`
Then the output should contain exactly:
"""
{"name":"Faye"}
"""
Scenario: Paginate REST
Given the GitHub API server:
"""
get('/comments') {
assert :per_page => "6"
page = (params[:page] || 1).to_i
response.headers["Link"] = %(<#{request.url}&page=#{page+1}>; rel="next") if page < 3
json [{:page => page}]
}
"""
When I successfully run `hub api --paginate comments?per_page=6`
Then the output should contain exactly:
"""
[{"page":1}]
[{"page":2}]
[{"page":3}]
"""
Scenario: Paginate GraphQL
Given the GitHub API server:
"""
post('/graphql') {
variables = params[:variables] || {}
page = (variables["endCursor"] || 1).to_i
json :data => {
:pageInfo => {
:hasNextPage => page < 3,
:endCursor => (page+1).to_s
}
}
}
"""
When I successfully run `hub api --paginate graphql -f query=QUERY`
Then the output should contain exactly:
"""
{"data":{"pageInfo":{"hasNextPage":true,"endCursor":"2"}}}
{"data":{"pageInfo":{"hasNextPage":true,"endCursor":"3"}}}
{"data":{"pageInfo":{"hasNextPage":false,"endCursor":"4"}}}
"""
Scenario: Avoid leaking token to a 3rd party
Given the GitHub API server:
"""
get('/hello/world', :host_name => 'example.com') {
halt 401 unless request.env['HTTP_AUTHORIZATION'].nil?
json :name => "Jet"
}
"""
When I successfully run `hub api http://example.com/hello/world`
Then the output should contain exactly:
"""
{"name":"Jet"}
"""
Scenario: Request headers
Given the GitHub API server:
"""
get('/hello/world') {
json :accept => request.env['HTTP_ACCEPT'],
:foo => request.env['HTTP_X_FOO']
}
"""
When I successfully run `hub api hello/world -H 'x-foo:bar' -H 'Accept: text/json'`
Then the output should contain exactly:
"""
{"accept":"text/json","foo":"bar"}
"""
Scenario: Response headers
Given the GitHub API server:
"""
get('/hello/world') {
json({})
}
"""
When I successfully run `hub api hello/world -i`
Then the output should contain "HTTP/1.1 200 OK"
And the output should contain "Content-Length: 2"
Scenario: POST fields
Given the GitHub API server:
"""
post('/hello/world') {
json Hash[*params.sort.flatten]
}
"""
When I successfully run `hub api -f name=@hubot -Fnum=12 -Fbool=false -Fvoid=null hello/world`
Then the output should contain exactly:
"""
{"bool":false,"name":"@hubot","num":12,"void":null}
"""
Scenario: POST raw fields
Given the GitHub API server:
"""
post('/hello/world') {
json Hash[*params.sort.flatten]
}
"""
When I successfully run `hub api -fnum=12 -fbool=false hello/world`
Then the output should contain exactly:
"""
{"bool":"false","num":"12"}
"""
Scenario: POST from stdin
Given the GitHub API server:
"""
post('/graphql') {
json :query => params[:query]
}
"""
When I run `hub api -t -F query=@- graphql` interactively
And I pass in:
"""
query {
repository
}
"""
Then the output should contain exactly:
"""
.query query {\n repository\n}\n
"""
Scenario: POST body from file
Given the GitHub API server:
"""
post('/create') {
params[:obj].inspect
}
"""
Given a file named "payload.json" with:
"""
{"obj": ["one", 2, null]}
"""
When I successfully run `hub api create --input payload.json`
Then the output should contain exactly:
"""
["one", 2, nil]
"""
Scenario: POST body from stdin
Given the GitHub API server:
"""
post('/create') {
params[:obj].inspect
}
"""
When I run `hub api create --input -` interactively
And I pass in:
"""
{"obj": {"name": "Ein", "datadog": true}}
"""
Then the output should contain exactly:
"""
{"name"=>"Ein", "datadog"=>true}
"""
Scenario: Pass extra GraphQL variables
Given the GitHub API server:
"""
post('/graphql') {
json(params[:variables])
}
"""
When I successfully run `hub api -F query='query {}' -Fname=Jet -Fsize=2 graphql`
Then the output should contain exactly:
"""
{"name":"Jet","size":2}
"""
Scenario: Enterprise GraphQL
Given I am "octokitten" on git.my.org with OAuth token "FITOKEN"
Given the GitHub API server:
"""
post('/api/graphql', :host_name => 'git.my.org') {
halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token FITOKEN'
json :name => "Ed"
}
"""
And $GITHUB_HOST is "git.my.org"
When I successfully run `hub api graphql -f query=QUERY`
Then the output should contain exactly:
"""
{"name":"Ed"}
"""
Scenario: Repo context
Given I am in "git://github.com/octocat/Hello-World.git" git repo
Given the GitHub API server:
"""
get('/repos/octocat/Hello-World/commits') {
json :commits => 12
}
"""
When I successfully run `hub api repos/{owner}/{repo}/commits`
Then the output should contain exactly:
"""
{"commits":12}
"""
Scenario: Multiple string interpolation
Given I am in "git://github.com/octocat/Hello-World.git" git repo
Given the GitHub API server:
"""
get('/repos/octocat/Hello-World/pulls') {
json(params)
}
"""
When I successfully run `hub api repos/{owner}/{repo}/pulls?head={owner}:{repo}`
Then the output should contain exactly:
"""
{"head":"octocat:Hello-World"}
"""
Scenario: Repo context in graphql
Given I am in "git://github.com/octocat/Hello-World.git" git repo
Given the GitHub API server:
"""
post('/graphql') {
json :query => params[:query]
}
"""
When I run `hub api -t -F query=@- graphql` interactively
And I pass in:
"""
repository(owner: "{owner}", name: "{repo}", nameWithOwner: "{owner}/{repo}")
"""
Then the output should contain exactly:
"""
.query repository(owner: "octocat", name: "Hello-World", nameWithOwner: "octocat/Hello-World")\n
"""
Scenario: Cache response
Given the GitHub API server:
"""
count = 0
get('/count') {
count += 1
json :count => count
}
"""
When I run `hub api -t 'count?a=1&b=2' --cache 5`
Then it should pass with ".count 1"
When I run `hub api -t 'count?b=2&a=1' --cache 5`
Then it should pass with ".count 1"
Scenario: Cache graphql response
Given the GitHub API server:
"""
count = 0
post('/graphql') {
halt 400 unless params[:query] =~ /^Q\d$/
count += 1
json :count => count
}
"""
When I run `hub api -t graphql -F query=Q1 --cache 5`
Then it should pass with ".count 1"
When I run `hub api -t graphql -F query=Q1 --cache 5`
Then it should pass with ".count 1"
When I run `hub api -t graphql -F query=Q2 --cache 5`
Then it should pass with ".count 2"
Scenario: Cache client error response
Given the GitHub API server:
"""
count = 0
get('/count') {
count += 1
status 404 if count == 1
json :count => count
}
"""
When I run `hub api -t count --cache 5`
Then it should fail with ".count 1"
When I run `hub api -t count --cache 5`
Then it should fail with ".count 1"
And the exit status should be 22
Scenario: Avoid caching server error response
Given the GitHub API server:
"""
count = 0
get('/count') {
count += 1
status 500 if count == 1
json :count => count
}
"""
When I run `hub api -t count --cache 5`
Then it should fail with ".count 1"
When I run `hub api -t count --cache 5`
Then it should pass with ".count 2"
When I run `hub api -t count --cache 5`
Then it should pass with ".count 2"
Scenario: Avoid caching response if the OAuth token changes
Given the GitHub API server:
"""
count = 0
get('/count') {
count += 1
json :count => count
}
"""
When I run `hub api -t count --cache 5`
Then it should pass with ".count 1"
Given I am "octocat" on github.com with OAuth token "TOKEN2"
When I run `hub api -t count --cache 5`
Then it should pass with ".count 2"
Scenario: Honor rate limit with pagination
Given the GitHub API server:
"""
get('/hello') {
page = (params[:page] || 1).to_i
if page < 2
response.headers['X-Ratelimit-Remaining'] = '0'
response.headers['X-Ratelimit-Reset'] = Time.now.utc.to_i.to_s
response.headers['Link'] = %(</hello?page=#{page+1}>; rel="next")
end
json [{}]
}
"""
When I successfully run `hub api --obey-ratelimit --paginate hello`
Then the stderr should contain "API rate limit exceeded; pausing until "
Scenario: Succumb to rate limit with pagination
Given the GitHub API server:
"""
get('/hello') {
page = (params[:page] || 1).to_i
response.headers['X-Ratelimit-Remaining'] = '0'
response.headers['X-Ratelimit-Reset'] = Time.now.utc.to_i.to_s
if page == 2
status 403
json :message => "API rate limit exceeded"
else
response.headers['Link'] = %(</hello?page=#{page+1}>; rel="next")
json [{page:page}]
end
}
"""
When I run `hub api --paginate -t hello`
Then the exit status should be 22
And the stderr should not contain "API rate limit exceeded"
And the stdout should contain exactly:
"""
.[0].page 1
.message API rate limit exceeded\n
"""
Scenario: Honor rate limit for 403s
Given the GitHub API server:
"""
count = 0
get('/hello') {
count += 1
if count == 1
response.headers['X-Ratelimit-Remaining'] = '0'
response.headers['X-Ratelimit-Reset'] = Time.now.utc.to_i.to_s
halt 403
end
json [{}]
}
"""
When I successfully run `hub api --obey-ratelimit hello`
Then the stderr should contain "API rate limit exceeded; pausing until "
Scenario: 403 unrelated to rate limit
Given the GitHub API server:
"""
get('/hello') {
response.headers['X-Ratelimit-Remaining'] = '1'
status 403
}
"""
When I run `hub api --obey-ratelimit hello`
Then the exit status should be 22
Then the stderr should not contain "API rate limit exceeded"
Scenario: Warn about insufficient OAuth scopes
Given the GitHub API server:
"""
get('/hello') {
response.headers['X-Accepted-Oauth-Scopes'] = 'repo, admin'
response.headers['X-Oauth-Scopes'] = 'public_repo'
status 403
json({})
}
"""
When I run `hub api hello`
Then the exit status should be 22
And the output should contain exactly:
"""
{}
Your access token may have insufficient scopes. Visit http://github.com/settings/tokens
to edit the 'hub' token and enable one of the following scopes: admin, repo
"""
Scenario: Print the SSO challenge to stderr
Given the GitHub API server:
"""
get('/orgs/acme') {
response.headers['X-GitHub-SSO'] = 'required; url=http://example.com?auth=HASH'
status 403
json({})
}
"""
When I run `hub api orgs/acme`
Then the exit status should be 22
And the stderr should contain exactly:
"""
You must authorize your token to access this organization:
http://example.com?auth=HASH
"""