@@ -56,7 +56,7 @@ func TestAPIKey(t *testing.T) {
5656 rw = httptest .NewRecorder ()
5757 )
5858 r .AddCookie (& http.Cookie {
59- Name : httpmw .AuthCookie ,
59+ Name : httpmw .SessionTokenKey ,
6060 Value : "test-wow-hello" ,
6161 })
6262
@@ -74,7 +74,7 @@ func TestAPIKey(t *testing.T) {
7474 rw = httptest .NewRecorder ()
7575 )
7676 r .AddCookie (& http.Cookie {
77- Name : httpmw .AuthCookie ,
77+ Name : httpmw .SessionTokenKey ,
7878 Value : "test-wow" ,
7979 })
8080
@@ -92,7 +92,7 @@ func TestAPIKey(t *testing.T) {
9292 rw = httptest .NewRecorder ()
9393 )
9494 r .AddCookie (& http.Cookie {
95- Name : httpmw .AuthCookie ,
95+ Name : httpmw .SessionTokenKey ,
9696 Value : "testtestid-wow" ,
9797 })
9898
@@ -111,7 +111,7 @@ func TestAPIKey(t *testing.T) {
111111 rw = httptest .NewRecorder ()
112112 )
113113 r .AddCookie (& http.Cookie {
114- Name : httpmw .AuthCookie ,
114+ Name : httpmw .SessionTokenKey ,
115115 Value : fmt .Sprintf ("%s-%s" , id , secret ),
116116 })
117117
@@ -130,7 +130,7 @@ func TestAPIKey(t *testing.T) {
130130 rw = httptest .NewRecorder ()
131131 )
132132 r .AddCookie (& http.Cookie {
133- Name : httpmw .AuthCookie ,
133+ Name : httpmw .SessionTokenKey ,
134134 Value : fmt .Sprintf ("%s-%s" , id , secret ),
135135 })
136136
@@ -157,7 +157,7 @@ func TestAPIKey(t *testing.T) {
157157 rw = httptest .NewRecorder ()
158158 )
159159 r .AddCookie (& http.Cookie {
160- Name : httpmw .AuthCookie ,
160+ Name : httpmw .SessionTokenKey ,
161161 Value : fmt .Sprintf ("%s-%s" , id , secret ),
162162 })
163163
@@ -182,7 +182,7 @@ func TestAPIKey(t *testing.T) {
182182 rw = httptest .NewRecorder ()
183183 )
184184 r .AddCookie (& http.Cookie {
185- Name : httpmw .AuthCookie ,
185+ Name : httpmw .SessionTokenKey ,
186186 Value : fmt .Sprintf ("%s-%s" , id , secret ),
187187 })
188188
@@ -209,6 +209,37 @@ func TestAPIKey(t *testing.T) {
209209 require .Equal (t , sentAPIKey .ExpiresAt , gotAPIKey .ExpiresAt )
210210 })
211211
212+ t .Run ("QueryParameter" , func (t * testing.T ) {
213+ t .Parallel ()
214+ var (
215+ db = databasefake .New ()
216+ id , secret = randomAPIKeyParts ()
217+ hashed = sha256 .Sum256 ([]byte (secret ))
218+ r = httptest .NewRequest ("GET" , "/" , nil )
219+ rw = httptest .NewRecorder ()
220+ )
221+ q := r .URL .Query ()
222+ q .Add (httpmw .SessionTokenKey , fmt .Sprintf ("%s-%s" , id , secret ))
223+ r .URL .RawQuery = q .Encode ()
224+
225+ _ , err := db .InsertAPIKey (r .Context (), database.InsertAPIKeyParams {
226+ ID : id ,
227+ HashedSecret : hashed [:],
228+ ExpiresAt : database .Now ().AddDate (0 , 0 , 1 ),
229+ })
230+ require .NoError (t , err )
231+ httpmw .ExtractAPIKey (db , nil )(http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
232+ // Checks that it exists on the context!
233+ _ = httpmw .APIKey (r )
234+ httpapi .Write (rw , http .StatusOK , httpapi.Response {
235+ Message : "it worked!" ,
236+ })
237+ })).ServeHTTP (rw , r )
238+ res := rw .Result ()
239+ defer res .Body .Close ()
240+ require .Equal (t , http .StatusOK , res .StatusCode )
241+ })
242+
212243 t .Run ("ValidUpdateLastUsed" , func (t * testing.T ) {
213244 t .Parallel ()
214245 var (
@@ -219,7 +250,7 @@ func TestAPIKey(t *testing.T) {
219250 rw = httptest .NewRecorder ()
220251 )
221252 r .AddCookie (& http.Cookie {
222- Name : httpmw .AuthCookie ,
253+ Name : httpmw .SessionTokenKey ,
223254 Value : fmt .Sprintf ("%s-%s" , id , secret ),
224255 })
225256
@@ -252,7 +283,7 @@ func TestAPIKey(t *testing.T) {
252283 rw = httptest .NewRecorder ()
253284 )
254285 r .AddCookie (& http.Cookie {
255- Name : httpmw .AuthCookie ,
286+ Name : httpmw .SessionTokenKey ,
256287 Value : fmt .Sprintf ("%s-%s" , id , secret ),
257288 })
258289
@@ -285,7 +316,7 @@ func TestAPIKey(t *testing.T) {
285316 rw = httptest .NewRecorder ()
286317 )
287318 r .AddCookie (& http.Cookie {
288- Name : httpmw .AuthCookie ,
319+ Name : httpmw .SessionTokenKey ,
289320 Value : fmt .Sprintf ("%s-%s" , id , secret ),
290321 })
291322
@@ -319,7 +350,7 @@ func TestAPIKey(t *testing.T) {
319350 rw = httptest .NewRecorder ()
320351 )
321352 r .AddCookie (& http.Cookie {
322- Name : httpmw .AuthCookie ,
353+ Name : httpmw .SessionTokenKey ,
323354 Value : fmt .Sprintf ("%s-%s" , id , secret ),
324355 })
325356
0 commit comments