@@ -3,6 +3,7 @@ package httputil_test
33import (
44 "context"
55 "errors"
6+ "fmt"
67 "net/http"
78 "net/http/httptest"
89 "slices"
@@ -32,7 +33,7 @@ func authenticate(r *http.Request) (string, error) {
3233 return "" , errors .New ("missing" )
3334}
3435
35- func authorize (ctx context.Context , user string , scopes []string ) error {
36+ func authorize (_ context.Context , user string , scopes []string ) error {
3637 if slices .Contains (scopes , user ) {
3738 return nil
3839 }
@@ -140,3 +141,111 @@ func TestSecurityGroups_Auth(t *testing.T) {
140141 })
141142 }
142143}
144+
145+ func strAuth (_ context.Context , token string ) (string , error ) {
146+ if token == "" {
147+ return "" , errors .New ("unreachable" )
148+ }
149+
150+ if token == "good" {
151+ return "good" , nil
152+ }
153+
154+ if token == "bad" {
155+ return "" , errors .New ("bad" )
156+ }
157+
158+ return "" , errors .New ("badder" )
159+ }
160+
161+ func TestHeaderAuth (t * testing.T ) {
162+ type testCase [T any ] struct {
163+ name string
164+ key string
165+ val string
166+ fn httputil.TokenAuthenticatorFunc [T ]
167+ want string
168+ err error
169+ }
170+ tests := []testCase [string ]{
171+ {"Empty" , "Missing" , "" , strAuth , "" , httputil .ErrUnauthorized },
172+ {"Good" , "Authorization" , "good" , strAuth , "good" , nil },
173+ {"Bad" , "Authorization" , "bad" , strAuth , "" , errors .New ("bad" )},
174+ {"other" , "Authorization" , "other" , strAuth , "" , errors .New ("badder" )},
175+ }
176+ for _ , tt := range tests {
177+ t .Run (tt .name , func (t * testing.T ) {
178+ r := httptest .NewRequest ("FOO" , "/asdf" , nil )
179+ r .Header .Set (tt .key , tt .val )
180+
181+ got , err := httputil .HeaderAuth (tt .key , tt .fn )(r )
182+ if tt .err != nil {
183+ assert .Equal (t , tt .err , err )
184+ }
185+
186+ assert .Equalf (t , tt .want , got , "HeaderAuth(%v, %v)" , tt .key , tt .fn )
187+ })
188+ }
189+ }
190+
191+ func TestQueryAuth (t * testing.T ) {
192+ type testCase [T any ] struct {
193+ name string
194+ key string
195+ val string
196+ fn httputil.TokenAuthenticatorFunc [T ]
197+ want string
198+ err error
199+ }
200+ tests := []testCase [string ]{
201+ {"Empty" , "Missing" , "" , strAuth , "" , httputil .ErrUnauthorized },
202+ {"Good" , "Authorization" , "good" , strAuth , "good" , nil },
203+ {"Bad" , "Authorization" , "bad" , strAuth , "" , errors .New ("bad" )},
204+ {"other" , "Authorization" , "other" , strAuth , "" , errors .New ("badder" )},
205+ }
206+ for _ , tt := range tests {
207+ t .Run (tt .name , func (t * testing.T ) {
208+ r := httptest .NewRequest ("FOO" , fmt .Sprintf ("/asdf?%s=%s" , tt .key , tt .val ), nil )
209+
210+ got , err := httputil .QueryAuth (tt .key , tt .fn )(r )
211+ if tt .err != nil {
212+ assert .Equal (t , tt .err , err )
213+ }
214+
215+ assert .Equal (t , tt .want , got )
216+ })
217+ }
218+ }
219+
220+ func TestCookieAuth (t * testing.T ) {
221+ type testCase [T any ] struct {
222+ name string
223+ key string
224+ val string
225+ fn httputil.TokenAuthenticatorFunc [T ]
226+ want string
227+ err error
228+ }
229+ tests := []testCase [string ]{
230+ {"Empty" , "Missing" , "" , strAuth , "" , httputil .ErrUnauthorized },
231+ {"Good" , "Authorization" , "good" , strAuth , "good" , nil },
232+ {"Bad" , "Authorization" , "bad" , strAuth , "" , errors .New ("bad" )},
233+ {"other" , "Authorization" , "other" , strAuth , "" , errors .New ("badder" )},
234+ }
235+ for _ , tt := range tests {
236+ t .Run (tt .name , func (t * testing.T ) {
237+ r := httptest .NewRequest ("FOO" , "/asdf" , nil )
238+ r .AddCookie (& http.Cookie {
239+ Name : tt .key ,
240+ Value : tt .val ,
241+ })
242+
243+ got , err := httputil .CookieAuth (tt .key , tt .fn )(r )
244+ if tt .err != nil {
245+ assert .Equal (t , tt .err , err )
246+ }
247+
248+ assert .Equal (t , tt .want , got )
249+ })
250+ }
251+ }
0 commit comments