@@ -5,20 +5,33 @@ import (
55 "encoding/base64"
66 "io"
77 "net/http"
8- "net/http/httptest"
98 "strings"
109 "testing"
11- "github.com/google/uuid"
10+ uuid "github.com/google/uuid"
1211)
1312
1413func TestTokenGenerationWithJTI (t * testing.T ) {
1514 resp , err := http .Post ("http://localhost:8082/auth/login" , "application/json" , strings .NewReader (`{"username":"test", "password":"testpass"}` ))
15+ if err != nil {
16+ t .Fatalf ("Failed to make login request: %v" , err )
17+ }
18+ defer resp .Body .Close ()
1619
17- accessToken , hasAccess := response ["access_token" ]
18- refreshToken , hasRefresh := response ["refresh_token" ]
20+ if resp .StatusCode != http .StatusOK {
21+ body , _ := io .ReadAll (resp .Body )
22+ t .Fatalf ("Login request failed with status %d: %s" , resp .StatusCode , string (body ))
23+ }
24+
25+ var responseMap map [string ]string
26+ if err := json .NewDecoder (resp .Body ).Decode (& responseMap ); err != nil {
27+ t .Fatalf ("Failed to decode response: %v" , err )
28+ }
29+
30+ accessToken , hasAccess := responseMap ["access_token" ]
31+ refreshToken , hasRefresh := responseMap ["refresh_token" ]
1932
2033 if ! hasAccess || ! hasRefresh {
21- t .Fatalf ("Expected both tokens in response, got: %v" , response )
34+ t .Fatalf ("Expected both tokens in response, got: %v" , responseMap )
2235 }
2336
2437 verifyJTI (t , accessToken , "access" )
@@ -41,8 +54,13 @@ func verifyJTI(t *testing.T, token string, tokenType string) {
4154 return
4255 }
4356
44- _ , err := uuid .Parse (jti .(string ))
57+ parsedUUID , err := uuid .Parse (jti .(string ))
4558 if err != nil {
4659 t .Errorf ("%s token JTI is not a valid UUID: %v" , tokenType , jti )
60+ return
61+ }
62+
63+ if parsedUUID .Version () != 4 {
64+ t .Errorf ("%s token JTI must be UUID version 4, got version %d" , tokenType , parsedUUID .Version ())
4765 }
4866}
0 commit comments