@@ -36,6 +36,7 @@ func Test_NewCmdApi(t *testing.T) {
3636 MagicFields : []string (nil ),
3737 RequestHeaders : []string (nil ),
3838 ShowResponseHeaders : false ,
39+ Paginate : false ,
3940 },
4041 wantsErr : false ,
4142 },
@@ -51,6 +52,7 @@ func Test_NewCmdApi(t *testing.T) {
5152 MagicFields : []string (nil ),
5253 RequestHeaders : []string (nil ),
5354 ShowResponseHeaders : false ,
55+ Paginate : false ,
5456 },
5557 wantsErr : false ,
5658 },
@@ -66,6 +68,7 @@ func Test_NewCmdApi(t *testing.T) {
6668 MagicFields : []string {"body=@file.txt" },
6769 RequestHeaders : []string (nil ),
6870 ShowResponseHeaders : false ,
71+ Paginate : false ,
6972 },
7073 wantsErr : false ,
7174 },
@@ -81,9 +84,52 @@ func Test_NewCmdApi(t *testing.T) {
8184 MagicFields : []string (nil ),
8285 RequestHeaders : []string {"accept: text/plain" },
8386 ShowResponseHeaders : true ,
87+ Paginate : false ,
8488 },
8589 wantsErr : false ,
8690 },
91+ {
92+ name : "with pagination" ,
93+ cli : "repos/OWNER/REPO/issues --paginate" ,
94+ wants : ApiOptions {
95+ RequestMethod : "GET" ,
96+ RequestMethodPassed : false ,
97+ RequestPath : "repos/OWNER/REPO/issues" ,
98+ RequestInputFile : "" ,
99+ RawFields : []string (nil ),
100+ MagicFields : []string (nil ),
101+ RequestHeaders : []string (nil ),
102+ ShowResponseHeaders : false ,
103+ Paginate : true ,
104+ },
105+ wantsErr : false ,
106+ },
107+ {
108+ name : "POST pagination" ,
109+ cli : "-XPOST repos/OWNER/REPO/issues --paginate" ,
110+ wantsErr : true ,
111+ },
112+ {
113+ name : "GraphQL pagination" ,
114+ cli : "-XPOST graphql --paginate" ,
115+ wants : ApiOptions {
116+ RequestMethod : "POST" ,
117+ RequestMethodPassed : true ,
118+ RequestPath : "graphql" ,
119+ RequestInputFile : "" ,
120+ RawFields : []string (nil ),
121+ MagicFields : []string (nil ),
122+ RequestHeaders : []string (nil ),
123+ ShowResponseHeaders : false ,
124+ Paginate : true ,
125+ },
126+ wantsErr : false ,
127+ },
128+ {
129+ name : "input pagination" ,
130+ cli : "--input repos/OWNER/REPO/issues --paginate" ,
131+ wantsErr : true ,
132+ },
87133 {
88134 name : "with request body from file" ,
89135 cli : "user --input myfile" ,
@@ -96,6 +142,7 @@ func Test_NewCmdApi(t *testing.T) {
96142 MagicFields : []string (nil ),
97143 RequestHeaders : []string (nil ),
98144 ShowResponseHeaders : false ,
145+ Paginate : false ,
99146 },
100147 wantsErr : false ,
101148 },
@@ -246,6 +293,57 @@ func Test_apiRun(t *testing.T) {
246293 }
247294}
248295
296+ func Test_apiRun_pagination (t * testing.T ) {
297+ io , _ , stdout , stderr := iostreams .Test ()
298+
299+ requestCount := 0
300+ responses := []* http.Response {
301+ {
302+ StatusCode : 200 ,
303+ Body : ioutil .NopCloser (bytes .NewBufferString (`{"page":1}` )),
304+ Header : http.Header {
305+ "Link" : []string {`<https://api.github.com/repositories/1227/issues?page=2>; rel="next", <https://api.github.com/repositories/1227/issues?page=3>; rel="last"` },
306+ },
307+ },
308+ {
309+ StatusCode : 200 ,
310+ Body : ioutil .NopCloser (bytes .NewBufferString (`{"page":2}` )),
311+ Header : http.Header {
312+ "Link" : []string {`<https://api.github.com/repositories/1227/issues?page=3>; rel="next", <https://api.github.com/repositories/1227/issues?page=3>; rel="last"` },
313+ },
314+ },
315+ {
316+ StatusCode : 200 ,
317+ Body : ioutil .NopCloser (bytes .NewBufferString (`{"page":3}` )),
318+ Header : http.Header {},
319+ },
320+ }
321+
322+ options := ApiOptions {
323+ IO : io ,
324+ HttpClient : func () (* http.Client , error ) {
325+ var tr roundTripper = func (req * http.Request ) (* http.Response , error ) {
326+ resp := responses [requestCount ]
327+ resp .Request = req
328+ requestCount ++
329+ return resp , nil
330+ }
331+ return & http.Client {Transport : tr }, nil
332+ },
333+
334+ Paginate : true ,
335+ }
336+
337+ err := apiRun (& options )
338+ assert .NoError (t , err )
339+
340+ assert .Equal (t , `{"page":1}{"page":2}{"page":3}` , stdout .String (), "stdout" )
341+ assert .Equal (t , "" , stderr .String (), "stderr" )
342+
343+ assert .Equal (t , "https://api.github.com/repositories/1227/issues?page=2" , responses [1 ].Request .URL .String ())
344+ assert .Equal (t , "https://api.github.com/repositories/1227/issues?page=3" , responses [2 ].Request .URL .String ())
345+ }
346+
249347func Test_apiRun_inputFile (t * testing.T ) {
250348 tests := []struct {
251349 name string
0 commit comments