diff --git a/main_test.go b/main_test.go index 6878f53..5105b4e 100644 --- a/main_test.go +++ b/main_test.go @@ -49,8 +49,6 @@ func TestHandleSignIn(t *testing.T) { if err != nil { t.Fatalf("Error creating request: %v", err) } - - // Set the content type to application/x-www-form-urlencoded req.Header.Set("Content-Type", "application/x-www-form-urlencoded") resp, err := httpClient.Do(req) @@ -62,13 +60,7 @@ func TestHandleSignIn(t *testing.T) { t.Fatalf("Expected status code 303, got %d", resp.StatusCode) } - var cookie *http.Cookie - for _, c := range resp.Cookies() { - if c.Name == "id" { - cookie = c - break - } - } + cookie := findCookie(resp, "id") if cookie == nil { t.Fatalf("No session cookie found") } else if cookie.SameSite != http.SameSiteStrictMode || cookie.HttpOnly != true || cookie.Secure != true { @@ -77,6 +69,16 @@ func TestHandleSignIn(t *testing.T) { }) } +func findCookie(resp *http.Response, name string) *http.Cookie { + for _, cookie := range resp.Cookies() { + if cookie.Name == name { + return cookie + } + } + + return nil +} + func setupIntegrationTest(t *testing.T, port string) (*sql.DB, context.Context) { ctx, done := context.WithCancel(context.Background()) t.Cleanup(done)