@@ -46,6 +46,7 @@ func Test_CheckJWT(t *testing.T) {
46
46
wantToken interface {}
47
47
wantStatusCode int
48
48
wantBody string
49
+ path string
49
50
}{
50
51
{
51
52
name : "it can successfully validate a token" ,
@@ -133,6 +134,50 @@ func Test_CheckJWT(t *testing.T) {
133
134
wantStatusCode : http .StatusBadRequest ,
134
135
wantBody : `{"message":"JWT is missing."}` ,
135
136
},
137
+ {
138
+ name : "JWT not required for /public" ,
139
+ options : []Option {
140
+ WithExclusionUrls ([]string {"/public" , "/health" , "/special" }),
141
+ },
142
+ method : http .MethodGet ,
143
+ path : "/public" ,
144
+ token : "" ,
145
+ wantStatusCode : http .StatusOK ,
146
+ wantBody : `{"message":"Authenticated."}` ,
147
+ },
148
+ {
149
+ name : "JWT not required for /health" ,
150
+ options : []Option {
151
+ WithExclusionUrls ([]string {"/public" , "/health" , "/special" }),
152
+ },
153
+ method : http .MethodGet ,
154
+ path : "/health" ,
155
+ token : "" ,
156
+ wantStatusCode : http .StatusOK ,
157
+ wantBody : `{"message":"Authenticated."}` ,
158
+ },
159
+ {
160
+ name : "JWT not required for /special" ,
161
+ options : []Option {
162
+ WithExclusionUrls ([]string {"/public" , "/health" , "/special" }),
163
+ },
164
+ method : http .MethodGet ,
165
+ path : "/special" ,
166
+ token : "" ,
167
+ wantStatusCode : http .StatusOK ,
168
+ wantBody : `{"message":"Authenticated."}` ,
169
+ },
170
+ {
171
+ name : "JWT required for /secure (not in exclusion list)" ,
172
+ options : []Option {
173
+ WithExclusionUrls ([]string {"/public" , "/health" , "/special" }),
174
+ },
175
+ method : http .MethodGet ,
176
+ path : "/secure" ,
177
+ token : "" ,
178
+ wantStatusCode : http .StatusBadRequest ,
179
+ wantBody : `{"message":"JWT is missing."}` ,
180
+ },
136
181
}
137
182
138
183
for _ , testCase := range testCases {
@@ -154,7 +199,8 @@ func Test_CheckJWT(t *testing.T) {
154
199
testServer := httptest .NewServer (middleware .CheckJWT (handler ))
155
200
defer testServer .Close ()
156
201
157
- request , err := http .NewRequest (testCase .method , testServer .URL , nil )
202
+ url := testServer .URL + testCase .path
203
+ request , err := http .NewRequest (testCase .method , url , nil )
158
204
require .NoError (t , err )
159
205
160
206
if testCase .token != "" {
0 commit comments