Skip to content

Commit ce67e4d

Browse files
committed
Rewrite simple tests to table-driven tests
1 parent f399db5 commit ce67e4d

File tree

1 file changed

+29
-41
lines changed

1 file changed

+29
-41
lines changed

jwtauth_test.go

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,47 +66,35 @@ func TestSimple(t *testing.T) {
6666
ts := httptest.NewServer(r)
6767
defer ts.Close()
6868

69-
// sending unauthorized requests
70-
if status, resp := testRequest(t, ts, "GET", "/", nil, nil); status != 401 || resp != "no token found\n" {
71-
t.Fatalf(resp)
72-
}
73-
74-
h := http.Header{}
75-
h.Set("Authorization", "BEARER "+newJwtToken([]byte("wrong"), map[string]interface{}{}))
76-
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 401 || resp != "token is unauthorized\n" {
77-
t.Fatalf(resp)
78-
}
79-
h.Set("Authorization", "BEARER asdf")
80-
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 401 || resp != "token is unauthorized\n" {
81-
t.Fatalf(resp)
82-
}
83-
// wrong token secret and wrong alg
84-
h.Set("Authorization", "BEARER "+newJwt512Token([]byte("wrong"), map[string]interface{}{}))
85-
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 401 || resp != "token is unauthorized\n" {
86-
t.Fatalf(resp)
87-
}
88-
// correct token secret but wrong alg
89-
h.Set("Authorization", "BEARER "+newJwt512Token(TokenSecret, map[string]interface{}{}))
90-
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 401 || resp != "token is unauthorized\n" {
91-
t.Fatalf(resp)
92-
}
93-
94-
// correct token, but has expired within the skew time
95-
h.Set("Authorization", "BEARER "+newJwtToken(TokenSecret, map[string]interface{}{"exp": time.Now().Unix() - 29}))
96-
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 200 || resp != "welcome" {
97-
fmt.Println("status", status, "resp", resp)
98-
t.Fatalf(resp)
99-
}
100-
101-
// correct token, but has expired outside of the skew time
102-
h.Set("Authorization", "BEARER "+newJwtToken(TokenSecret, map[string]interface{}{"exp": time.Now().Unix() - 31}))
103-
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 401 || resp != "token is expired\n" {
104-
t.Fatalf(resp)
105-
}
106-
107-
// sending authorized requests
108-
if status, resp := testRequest(t, ts, "GET", "/", newAuthHeader(), nil); status != 200 || resp != "welcome" {
109-
t.Fatalf(resp)
69+
tt := []struct {
70+
Name string
71+
Authorization string
72+
Status int
73+
Resp string
74+
}{
75+
{Name: "empty token", Authorization: "", Status: 401, Resp: "no token found\n"},
76+
{Name: "wrong token", Authorization: "Bearer asdf", Status: 401, Resp: "token is unauthorized\n"},
77+
{Name: "wrong secret", Authorization: "Bearer " + newJwtToken([]byte("wrong")), Status: 401, Resp: "token is unauthorized\n"},
78+
{Name: "wrong secret/alg", Authorization: "Bearer " + newJwt512Token([]byte("wrong")), Status: 401, Resp: "token is unauthorized\n"},
79+
{Name: "wrong alg", Authorization: "Bearer " + newJwt512Token(TokenSecret, map[string]interface{}{}), Status: 401, Resp: "token is unauthorized\n"},
80+
{Name: "expired within skew", Authorization: "Bearer " + newJwtToken(TokenSecret, map[string]interface{}{"exp": time.Now().Unix() - 29}), Status: 200, Resp: "welcome"},
81+
{Name: "expired outside skew", Authorization: "Bearer " + newJwtToken(TokenSecret, map[string]interface{}{"exp": time.Now().Unix() - 31}), Status: 401, Resp: "token is expired\n"},
82+
{Name: "valid token", Authorization: "Bearer " + newJwtToken(TokenSecret), Status: 200, Resp: "welcome"},
83+
{Name: "valid Bearer", Authorization: "Bearer " + newJwtToken(TokenSecret, map[string]interface{}{"service": "test"}), Status: 200, Resp: "welcome"},
84+
{Name: "valid BEARER", Authorization: "BEARER " + newJwtToken(TokenSecret), Status: 200, Resp: "welcome"},
85+
{Name: "valid bearer", Authorization: "bearer " + newJwtToken(TokenSecret), Status: 200, Resp: "welcome"},
86+
{Name: "valid claim", Authorization: "Bearer " + newJwtToken(TokenSecret, map[string]interface{}{"service": "test"}), Status: 200, Resp: "welcome"},
87+
}
88+
89+
for _, tc := range tt {
90+
h := http.Header{}
91+
if tc.Authorization != "" {
92+
h.Set("Authorization", tc.Authorization)
93+
}
94+
status, resp := testRequest(t, ts, "GET", "/", h, nil)
95+
if status != tc.Status || resp != tc.Resp {
96+
t.Fatalf("test '%s' failed: expected status %d, got %d", tc.Name, tc.Status, status)
97+
}
11098
}
11199
}
112100

0 commit comments

Comments
 (0)