@@ -3,15 +3,115 @@ package api
33import (
44 "bytes"
55 "fmt"
6+ "io"
67 "io/ioutil"
78 "net/http"
8- "reflect "
9+ "os "
910 "testing"
1011
1112 "github.com/cli/cli/pkg/cmdutil"
1213 "github.com/cli/cli/pkg/iostreams"
14+ "github.com/google/shlex"
15+ "github.com/stretchr/testify/assert"
1316)
1417
18+ func Test_NewCmdApi (t * testing.T ) {
19+ f := & cmdutil.Factory {}
20+
21+ tests := []struct {
22+ name string
23+ cli string
24+ wants ApiOptions
25+ wantsErr bool
26+ }{
27+ {
28+ name : "no flags" ,
29+ cli : "graphql" ,
30+ wants : ApiOptions {
31+ RequestMethod : "GET" ,
32+ RequestMethodPassed : false ,
33+ RequestPath : "graphql" ,
34+ RawFields : []string (nil ),
35+ MagicFields : []string (nil ),
36+ RequestHeaders : []string (nil ),
37+ ShowResponseHeaders : false ,
38+ },
39+ wantsErr : false ,
40+ },
41+ {
42+ name : "override method" ,
43+ cli : "repos/octocat/Spoon-Knife -XDELETE" ,
44+ wants : ApiOptions {
45+ RequestMethod : "DELETE" ,
46+ RequestMethodPassed : true ,
47+ RequestPath : "repos/octocat/Spoon-Knife" ,
48+ RawFields : []string (nil ),
49+ MagicFields : []string (nil ),
50+ RequestHeaders : []string (nil ),
51+ ShowResponseHeaders : false ,
52+ },
53+ wantsErr : false ,
54+ },
55+ {
56+ name : "with fields" ,
57+ cli : "graphql -f query=QUERY -F body=@file.txt" ,
58+ wants : ApiOptions {
59+ RequestMethod : "GET" ,
60+ RequestMethodPassed : false ,
61+ RequestPath : "graphql" ,
62+ RawFields : []string {"query=QUERY" },
63+ MagicFields : []string {"body=@file.txt" },
64+ RequestHeaders : []string (nil ),
65+ ShowResponseHeaders : false ,
66+ },
67+ wantsErr : false ,
68+ },
69+ {
70+ name : "with headers" ,
71+ cli : "user -H 'accept: text/plain' -i" ,
72+ wants : ApiOptions {
73+ RequestMethod : "GET" ,
74+ RequestMethodPassed : false ,
75+ RequestPath : "user" ,
76+ RawFields : []string (nil ),
77+ MagicFields : []string (nil ),
78+ RequestHeaders : []string {"accept: text/plain" },
79+ ShowResponseHeaders : true ,
80+ },
81+ wantsErr : false ,
82+ },
83+ {
84+ name : "no arguments" ,
85+ cli : "" ,
86+ wantsErr : true ,
87+ },
88+ }
89+ for _ , tt := range tests {
90+ t .Run (tt .name , func (t * testing.T ) {
91+ cmd := NewCmdApi (f , func (o * ApiOptions ) error {
92+ assert .Equal (t , tt .wants .RequestMethod , o .RequestMethod )
93+ assert .Equal (t , tt .wants .RequestMethodPassed , o .RequestMethodPassed )
94+ assert .Equal (t , tt .wants .RequestPath , o .RequestPath )
95+ assert .Equal (t , tt .wants .RawFields , o .RawFields )
96+ assert .Equal (t , tt .wants .MagicFields , o .MagicFields )
97+ assert .Equal (t , tt .wants .RequestHeaders , o .RequestHeaders )
98+ assert .Equal (t , tt .wants .ShowResponseHeaders , o .ShowResponseHeaders )
99+ return nil
100+ })
101+
102+ argv , err := shlex .Split (tt .cli )
103+ assert .NoError (t , err )
104+ cmd .SetArgs (argv )
105+ _ , err = cmd .ExecuteC ()
106+ if tt .wantsErr {
107+ assert .Error (t , err )
108+ return
109+ }
110+ assert .NoError (t , err )
111+ })
112+ }
113+ }
114+
15115func Test_apiRun (t * testing.T ) {
16116 tests := []struct {
17117 name string
@@ -30,6 +130,16 @@ func Test_apiRun(t *testing.T) {
30130 stdout : `bam!` ,
31131 stderr : `` ,
32132 },
133+ {
134+ name : "success 204" ,
135+ httpResponse : & http.Response {
136+ StatusCode : 204 ,
137+ Body : nil ,
138+ },
139+ err : nil ,
140+ stdout : `` ,
141+ stderr : `` ,
142+ },
33143 {
34144 name : "failure" ,
35145 httpResponse : & http.Response {
@@ -109,7 +219,76 @@ func Test_parseFields(t *testing.T) {
109219 "enabled" : true ,
110220 "victories" : 123 ,
111221 }
112- if ! reflect .DeepEqual (params , expect ) {
113- t .Errorf ("expected %v, got %v" , expect , params )
222+ assert .Equal (t , expect , params )
223+ }
224+
225+ func Test_magicFieldValue (t * testing.T ) {
226+ f , err := ioutil .TempFile ("" , "gh-test" )
227+ if err != nil {
228+ t .Fatal (err )
229+ }
230+ fmt .Fprint (f , "file contents" )
231+ f .Close ()
232+ t .Cleanup (func () { os .Remove (f .Name ()) })
233+
234+ type args struct {
235+ v string
236+ stdin io.ReadCloser
237+ }
238+ tests := []struct {
239+ name string
240+ args args
241+ want interface {}
242+ wantErr bool
243+ }{
244+ {
245+ name : "string" ,
246+ args : args {v : "hello" },
247+ want : "hello" ,
248+ wantErr : false ,
249+ },
250+ {
251+ name : "bool true" ,
252+ args : args {v : "true" },
253+ want : true ,
254+ wantErr : false ,
255+ },
256+ {
257+ name : "bool false" ,
258+ args : args {v : "false" },
259+ want : false ,
260+ wantErr : false ,
261+ },
262+ {
263+ name : "null" ,
264+ args : args {v : "null" },
265+ want : nil ,
266+ wantErr : false ,
267+ },
268+ {
269+ name : "file" ,
270+ args : args {v : "@" + f .Name ()},
271+ want : []byte ("file contents" ),
272+ wantErr : false ,
273+ },
274+ {
275+ name : "file error" ,
276+ args : args {v : "@" },
277+ want : nil ,
278+ wantErr : true ,
279+ },
280+ }
281+ for _ , tt := range tests {
282+ t .Run (tt .name , func (t * testing.T ) {
283+ got , err := magicFieldValue (tt .args .v , tt .args .stdin )
284+ if (err != nil ) != tt .wantErr {
285+ t .Errorf ("magicFieldValue() error = %v, wantErr %v" , err , tt .wantErr )
286+ return
287+ }
288+ if tt .wantErr {
289+ return
290+ }
291+ assert .Equal (t , tt .want , got )
292+ })
114293 }
115294}
0 commit comments