@@ -15,17 +15,18 @@ import (
1515 "xurl/config"
1616 xurlErrors "xurl/errors"
1717 "xurl/store"
18+
19+ "github.com/stretchr/testify/assert"
20+ "github.com/stretchr/testify/require"
1821)
1922
2023// Helper function to create a temporary token store for testing
2124func createTempTokenStore (t * testing.T ) (* store.TokenStore , string ) {
22- // Create a temporary directory for testing
2325 tempDir , err := os .MkdirTemp ("" , "xurl_test" )
2426 if err != nil {
2527 t .Fatalf ("Failed to create temp directory: %v" , err )
2628 }
2729
28- // Create a token store with a file in the temp directory
2930 tempFile := filepath .Join (tempDir , "tokens.json" )
3031 tokenStore := & store.TokenStore {
3132 OAuth2Tokens : make (map [string ]store.Token ),
@@ -50,7 +51,6 @@ func createMockAuth(t *testing.T) (*auth.Auth, string) {
5051 mockAuth := auth .NewAuth (cfg )
5152 tokenStore , tempDir := createTempTokenStore (t )
5253
53- // Add a test bearer token
5454 err := tokenStore .SaveBearerToken ("test-bearer-token" )
5555 if err != nil {
5656 t .Fatalf ("Failed to save bearer token: %v" , err )
@@ -69,17 +69,9 @@ func TestNewApiClient(t *testing.T) {
6969
7070 client := NewApiClient (cfg , auth )
7171
72- if client .url != cfg .APIBaseURL {
73- t .Errorf ("Expected URL to be %s, got %s" , cfg .APIBaseURL , client .url )
74- }
75-
76- if client .auth != auth {
77- t .Errorf ("Expected auth to be set correctly" )
78- }
79-
80- if client .client == nil {
81- t .Errorf ("HTTP client should not be nil" )
82- }
72+ assert .Equal (t , cfg .APIBaseURL , client .url , "URL should match config" )
73+ assert .Equal (t , auth , client .auth , "Auth should be set correctly" )
74+ assert .NotNil (t , client .client , "HTTP client should not be nil" )
8375}
8476
8577func TestBuildRequest (t * testing.T ) {
@@ -159,43 +151,29 @@ func TestBuildRequest(t *testing.T) {
159151
160152 req , err := client .BuildRequest (tt .method , tt .endpoint , tt .headers , body , contentType , tt .authType , tt .username )
161153
162- if ( err != nil ) != tt .wantErr {
163- t . Errorf ( "BuildRequest() error = %v, wantErr %v" , err , tt . wantErr )
154+ if tt .wantErr {
155+ assert . Error ( t , err )
164156 return
165157 }
166158
167- if err != nil {
168- return
169- }
170-
171- if req .Method != tt .wantMethod {
172- t .Errorf ("BuildRequest() method = %v, want %v" , req .Method , tt .wantMethod )
173- }
174-
175- if req .URL .String () != tt .wantURL {
176- t .Errorf ("BuildRequest() URL = %v, want %v" , req .URL .String (), tt .wantURL )
177- }
159+ require .NoError (t , err )
160+ assert .Equal (t , tt .wantMethod , req .Method )
161+ assert .Equal (t , tt .wantURL , req .URL .String ())
178162
179163 for _ , header := range tt .headers {
180164 parts := strings .Split (header , ": " )
181- if len (parts ) != 2 {
182- t .Errorf ("Invalid header format: %s" , header )
183- continue
184- }
165+ require .Len (t , parts , 2 , "Invalid header format: %s" , header )
185166
186167 key := strings .TrimSpace (parts [0 ])
187168 value := strings .TrimSpace (parts [1 ])
188169
189- if req .Header .Get (key ) != value {
190- t . Errorf ( "BuildRequest() header %s = %s, want %s" , key , req . Header . Get ( key ), value )
191- }
192- }
170+ assert . Equal ( t , value , req .Header .Get (key ))
171+ }
172+
173+ assert . Equal ( t , "xurl/dev" , req . Header . Get ( "User-Agent" ))
193174
194175 if tt .method == "POST" && tt .data != "" {
195- contentType := req .Header .Get ("Content-Type" )
196- if contentType != "application/json" {
197- t .Errorf ("Expected Content-Type header to be application/json, got %s" , contentType )
198- }
176+ assert .Equal (t , "application/json" , req .Header .Get ("Content-Type" ))
199177 }
200178 })
201179 }
@@ -228,7 +206,6 @@ func TestSendRequest(t *testing.T) {
228206 }))
229207 defer server .Close ()
230208
231- // Setup client
232209 cfg := & config.Config {
233210 APIBaseURL : server .URL ,
234211 }
@@ -240,70 +217,40 @@ func TestSendRequest(t *testing.T) {
240217 t .Run ("Get user profile" , func (t * testing.T ) {
241218 resp , err := client .SendRequest ("GET" , "/2/users/me" , []string {"Authorization: Bearer test-token" }, "" , "" , "" , false )
242219
243- if err != nil {
244- t .Errorf ("SendRequest() error = %v" , err )
245- return
246- }
220+ require .NoError (t , err )
247221
248222 var result map [string ]interface {}
249- if e := json .Unmarshal (resp , & result ); e != nil {
250- t .Errorf ("Failed to parse response: %v" , e )
251- return
252- }
223+ err = json .Unmarshal (resp , & result )
224+ require .NoError (t , err , "Failed to parse response" )
253225
254226 data , ok := result ["data" ].(map [string ]interface {})
255- if ! ok {
256- t .Errorf ("Expected data object in response" )
257- return
258- }
227+ require .True (t , ok , "Expected data object in response" )
259228
260- if username , ok := data ["username" ]; ! ok || username != "testuser" {
261- t .Errorf ("Expected username 'testuser', got %v" , username )
262- }
229+ assert .Equal (t , "testuser" , data ["username" ], "Username should match" )
263230 })
264231
265232 // Test successful POST request
266233 t .Run ("Post tweet" , func (t * testing.T ) {
267234 resp , err := client .SendRequest ("POST" , "/2/tweets" , []string {"Authorization: Bearer test-token" }, `{"text":"Hello world!"}` , "" , "" , false )
268235
269- if err != nil {
270- t .Errorf ("SendRequest() error = %v" , err )
271- return
272- }
236+ require .NoError (t , err )
273237
274238 var result map [string ]interface {}
275- if e := json .Unmarshal (resp , & result ); e != nil {
276- t .Errorf ("Failed to parse response: %v" , e )
277- return
278- }
239+ err = json .Unmarshal (resp , & result )
240+ require .NoError (t , err , "Failed to parse response" )
279241
280242 data , ok := result ["data" ].(map [string ]interface {})
281- if ! ok {
282- t .Errorf ("Expected data object in response" )
283- return
284- }
243+ require .True (t , ok , "Expected data object in response" )
285244
286- if text , ok := data ["text" ]; ! ok || text != "Hello world!" {
287- t .Errorf ("Expected text 'Hello world!', got %v" , text )
288- }
245+ assert .Equal (t , "Hello world!" , data ["text" ], "Tweet text should match" )
289246 })
290247
291- // Test error response
292248 t .Run ("Error response" , func (t * testing.T ) {
293249 resp , err := client .SendRequest ("GET" , "/2/tweets/search/recent" , []string {"Authorization: Bearer test-token" }, "" , "" , "" , false )
294250
295- if err == nil {
296- t .Errorf ("SendRequest() expected error, got nil" )
297- return
298- }
299-
300- if resp != nil {
301- t .Errorf ("SendRequest() expected nil response, got %v" , resp )
302- }
303-
304- if ! xurlErrors .IsAPIError (err ) {
305- t .Errorf ("Expected API error, got %v" , err )
306- }
251+ assert .Error (t , err , "Expected an error" )
252+ assert .Nil (t , resp , "Response should be nil" )
253+ assert .True (t , xurlErrors .IsAPIError (err ), "Expected API error" )
307254 })
308255}
309256
@@ -317,13 +264,8 @@ func TestGetAuthHeader(t *testing.T) {
317264
318265 _ , err := client .GetAuthHeader ("GET" , "https://api.x.com/2/users/me" , "" , "" )
319266
320- if err == nil {
321- t .Errorf ("GetAuthHeader() expected error, got nil" )
322- }
323-
324- if ! xurlErrors .IsAuthError (err ) {
325- t .Errorf ("Expected auth error, got %v" , err )
326- }
267+ assert .Error (t , err , "Expected an error" )
268+ assert .True (t , xurlErrors .IsAuthError (err ), "Expected auth error" )
327269 })
328270
329271 t .Run ("Invalid auth type" , func (t * testing.T ) {
@@ -333,13 +275,8 @@ func TestGetAuthHeader(t *testing.T) {
333275
334276 _ , err := client .GetAuthHeader ("GET" , "https://api.x.com/2/users/me" , "invalid" , "" )
335277
336- if err == nil {
337- t .Errorf ("GetAuthHeader() expected error, got nil" )
338- }
339-
340- if ! xurlErrors .IsAuthError (err ) {
341- t .Errorf ("Expected auth error, got %v" , err )
342- }
278+ assert .Error (t , err , "Expected an error" )
279+ assert .True (t , xurlErrors .IsAuthError (err ), "Expected auth error" )
343280 })
344281}
345282
@@ -377,14 +314,7 @@ func TestStreamRequest(t *testing.T) {
377314 t .Run ("Stream error response" , func (t * testing.T ) {
378315 err := client .StreamRequest ("GET" , "/2/tweets/search/stream/error" , []string {"Authorization: Bearer test-token" }, "" , "" , "" , false )
379316
380- if err == nil {
381- t .Errorf ("StreamRequest() expected error, got nil" )
382- return
383- }
384-
385- // Check if it's an API error
386- if ! xurlErrors .IsAPIError (err ) {
387- t .Errorf ("Expected API error, got %v" , err )
388- }
317+ assert .Error (t , err , "Expected an error" )
318+ assert .True (t , xurlErrors .IsAPIError (err ), "Expected API error" )
389319 })
390320}
0 commit comments