@@ -142,47 +142,37 @@ func TestSimple(t *testing.T) {
142142 ts := httptest .NewServer (r )
143143 defer ts .Close ()
144144
145- // sending unauthorized requests
146- if status , resp := testRequest (t , ts , "GET" , "/" , nil , nil ); status != 401 || resp != "no token found\n " {
147- t .Fatalf (resp )
148- }
149-
150- h := http.Header {}
151- h .Set ("Authorization" , "BEARER " + newJwtToken ([]byte ("wrong" ), map [string ]interface {}{}))
152- if status , resp := testRequest (t , ts , "GET" , "/" , h , nil ); status != 401 || resp != "token is unauthorized\n " {
153- t .Fatalf (resp )
154- }
155- h .Set ("Authorization" , "BEARER asdf" )
156- if status , resp := testRequest (t , ts , "GET" , "/" , h , nil ); status != 401 || resp != "token is unauthorized\n " {
157- t .Fatalf (resp )
158- }
159- // wrong token secret and wrong alg
160- h .Set ("Authorization" , "BEARER " + newJwt512Token ([]byte ("wrong" ), map [string ]interface {}{}))
161- if status , resp := testRequest (t , ts , "GET" , "/" , h , nil ); status != 401 || resp != "token is unauthorized\n " {
162- t .Fatalf (resp )
163- }
164- // correct token secret but wrong alg
165- h .Set ("Authorization" , "BEARER " + newJwt512Token (TokenSecret , map [string ]interface {}{}))
166- if status , resp := testRequest (t , ts , "GET" , "/" , h , nil ); status != 401 || resp != "token is unauthorized\n " {
167- t .Fatalf (resp )
168- }
169-
170- // correct token, but has expired within the skew time
171- h .Set ("Authorization" , "BEARER " + newJwtToken (TokenSecret , map [string ]interface {}{"exp" : time .Now ().Unix () - 29 }))
172- if status , resp := testRequest (t , ts , "GET" , "/" , h , nil ); status != 200 || resp != "welcome" {
173- fmt .Println ("status" , status , "resp" , resp )
174- t .Fatalf (resp )
175- }
176-
177- // correct token, but has expired outside of the skew time
178- h .Set ("Authorization" , "BEARER " + newJwtToken (TokenSecret , map [string ]interface {}{"exp" : time .Now ().Unix () - 31 }))
179- if status , resp := testRequest (t , ts , "GET" , "/" , h , nil ); status != 401 || resp != "token is expired\n " {
180- t .Fatalf (resp )
181- }
182-
183- // sending authorized requests
184- if status , resp := testRequest (t , ts , "GET" , "/" , newAuthHeader (), nil ); status != 200 || resp != "welcome" {
185- t .Fatalf (resp )
145+ tt := []struct {
146+ Name string
147+ Authorization string
148+ Status int
149+ Resp string
150+ }{
151+ {Name : "empty token" , Authorization : "" , Status : 401 , Resp : "no token found\n " },
152+ {Name : "wrong token" , Authorization : "Bearer asdf" , Status : 401 , Resp : "token is unauthorized\n " },
153+ {Name : "wrong secret" , Authorization : "Bearer " + newJwtToken ([]byte ("wrong" )), Status : 401 , Resp : "token is unauthorized\n " },
154+ {Name : "wrong secret/alg" , Authorization : "Bearer " + newJwt512Token ([]byte ("wrong" )), Status : 401 , Resp : "token is unauthorized\n " },
155+ {Name : "wrong alg" , Authorization : "Bearer " + newJwt512Token (TokenSecret , map [string ]interface {}{}), Status : 401 , Resp : "token is unauthorized\n " },
156+ {Name : "expired within skew" , Authorization : "Bearer " + newJwtToken (TokenSecret , map [string ]interface {}{"exp" : time .Now ().Unix () - 29 }), Status : 200 , Resp : "welcome" },
157+ {Name : "expired outside skew" , Authorization : "Bearer " + newJwtToken (TokenSecret , map [string ]interface {}{"exp" : time .Now ().Unix () - 31 }), Status : 401 , Resp : "token is expired\n " },
158+ {Name : "valid token" , Authorization : "Bearer " + newJwtToken (TokenSecret ), Status : 200 , Resp : "welcome" },
159+ {Name : "valid Bearer" , Authorization : "Bearer " + newJwtToken (TokenSecret , map [string ]interface {}{"service" : "test" }), Status : 200 , Resp : "welcome" },
160+ {Name : "valid BEARER" , Authorization : "BEARER " + newJwtToken (TokenSecret ), Status : 200 , Resp : "welcome" },
161+ {Name : "valid bearer" , Authorization : "bearer " + newJwtToken (TokenSecret ), Status : 200 , Resp : "welcome" },
162+ {Name : "valid claim" , Authorization : "Bearer " + newJwtToken (TokenSecret , map [string ]interface {}{"service" : "test" }), Status : 200 , Resp : "welcome" },
163+ {Name : "invalid bearer_" , Authorization : "BEARER_" + newJwtToken (TokenSecret ), Status : 401 , Resp : "no token found\n " },
164+ {Name : "invalid bearerx" , Authorization : "BEARERx" + newJwtToken (TokenSecret ), Status : 401 , Resp : "no token found\n " },
165+ }
166+
167+ for _ , tc := range tt {
168+ h := http.Header {}
169+ if tc .Authorization != "" {
170+ h .Set ("Authorization" , tc .Authorization )
171+ }
172+ status , resp := testRequest (t , ts , "GET" , "/" , h , nil )
173+ if status != tc .Status || resp != tc .Resp {
174+ t .Errorf ("test '%s' failed: expected Status: %d %q, got %d %q" , tc .Name , tc .Status , tc .Resp , status , resp )
175+ }
186176 }
187177}
188178
0 commit comments