@@ -2,6 +2,7 @@ package todoist
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "io"
78 "net/http"
@@ -100,31 +101,27 @@ func TestTodoist_Pattern(t *testing.T) {
100101}
101102
102103func TestTodoist_VerificationEndpoint (t * testing.T ) {
103- d := Scanner {}
104104 input := fmt .Sprintf ("%s token = '%s'" , keyword , validPattern )
105105
106- prevClient := client
107- t .Cleanup (func () {
108- client = prevClient
109- })
110-
111106 called := false
112- client = & http.Client {
113- Transport : roundTripFunc (func (req * http.Request ) (* http.Response , error ) {
114- called = true
115- if req .URL .String () != "https://api.todoist.com/api/v1/projects" {
116- t .Fatalf ("unexpected verification URL: %s" , req .URL .String ())
117- }
118- if req .Header .Get ("Authorization" ) == "" {
119- t .Fatal ("missing Authorization header" )
120- }
107+ d := Scanner {
108+ client : & http.Client {
109+ Transport : roundTripFunc (func (req * http.Request ) (* http.Response , error ) {
110+ called = true
111+ if req .URL .String () != "https://api.todoist.com/api/v1/projects" {
112+ t .Fatalf ("unexpected verification URL: %s" , req .URL .String ())
113+ }
114+ if req .Header .Get ("Authorization" ) == "" {
115+ t .Fatal ("missing Authorization header" )
116+ }
121117
122- return & http.Response {
123- StatusCode : http .StatusOK ,
124- Body : io .NopCloser (strings .NewReader ("{}" )),
125- Header : make (http.Header ),
126- }, nil
127- }),
118+ return & http.Response {
119+ StatusCode : http .StatusOK ,
120+ Body : io .NopCloser (strings .NewReader ("{}" )),
121+ Header : make (http.Header ),
122+ }, nil
123+ }),
124+ },
128125 }
129126
130127 results , err := d .FromData (context .Background (), true , []byte (input ))
@@ -141,3 +138,89 @@ func TestTodoist_VerificationEndpoint(t *testing.T) {
141138 t .Fatal ("expected result to be verified" )
142139 }
143140}
141+
142+ func TestTodoist_VerificationResponses (t * testing.T ) {
143+ input := fmt .Sprintf ("%s token = '%s'" , keyword , validPattern )
144+
145+ tests := []struct {
146+ name string
147+ response * http.Response
148+ responseErr error
149+ wantVerified bool
150+ wantVerificationErr bool
151+ }{
152+ {
153+ name : "valid token" ,
154+ response : & http.Response {
155+ StatusCode : http .StatusOK ,
156+ Body : io .NopCloser (strings .NewReader ("{}" )),
157+ Header : make (http.Header ),
158+ },
159+ wantVerified : true ,
160+ },
161+ {
162+ name : "valid token with nil body" ,
163+ response : & http.Response {
164+ StatusCode : http .StatusOK ,
165+ Header : make (http.Header ),
166+ },
167+ wantVerified : true ,
168+ },
169+ {
170+ name : "unauthorized token" ,
171+ response : & http.Response {
172+ StatusCode : http .StatusUnauthorized ,
173+ Body : io .NopCloser (strings .NewReader ("{}" )),
174+ Header : make (http.Header ),
175+ },
176+ },
177+ {
178+ name : "forbidden token" ,
179+ response : & http.Response {
180+ StatusCode : http .StatusForbidden ,
181+ Body : io .NopCloser (strings .NewReader ("{}" )),
182+ Header : make (http.Header ),
183+ },
184+ },
185+ {
186+ name : "client error" ,
187+ responseErr : errors .New ("network failure" ),
188+ wantVerificationErr : true ,
189+ },
190+ {
191+ name : "unexpected status" ,
192+ response : & http.Response {
193+ StatusCode : http .StatusInternalServerError ,
194+ Body : io .NopCloser (strings .NewReader ("{}" )),
195+ Header : make (http.Header ),
196+ },
197+ wantVerificationErr : true ,
198+ },
199+ }
200+
201+ for _ , tt := range tests {
202+ t .Run (tt .name , func (t * testing.T ) {
203+ d := Scanner {
204+ client : & http.Client {
205+ Transport : roundTripFunc (func (req * http.Request ) (* http.Response , error ) {
206+ return tt .response , tt .responseErr
207+ }),
208+ },
209+ }
210+
211+ results , err := d .FromData (context .Background (), true , []byte (input ))
212+ if err != nil {
213+ t .Fatalf ("FromData returned error: %v" , err )
214+ }
215+ if len (results ) != 1 {
216+ t .Fatalf ("expected 1 result, got %d" , len (results ))
217+ }
218+ if results [0 ].Verified != tt .wantVerified {
219+ t .Fatalf ("Verified = %v, want %v" , results [0 ].Verified , tt .wantVerified )
220+ }
221+ if (results [0 ].VerificationError () != nil ) != tt .wantVerificationErr {
222+ t .Fatalf ("wantVerificationError = %v, verification error = %v" , tt .wantVerificationErr , results [0 ].VerificationError ())
223+ }
224+ })
225+ }
226+ }
0 commit comments