chore: extract find cookie #181
All checks were successful
Build and Push Docker Image / Explore-Gitea-Actions (push) Successful in 53s
Build Docker Image / Explore-Gitea-Actions (push) Successful in 47s

This commit was merged in pull request #197.
This commit is contained in:
2024-10-03 22:55:00 +02:00
parent 8d90874d04
commit cc3747b226

View File

@@ -49,8 +49,6 @@ func TestHandleSignIn(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error creating request: %v", err) 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") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)
@@ -62,13 +60,7 @@ func TestHandleSignIn(t *testing.T) {
t.Fatalf("Expected status code 303, got %d", resp.StatusCode) t.Fatalf("Expected status code 303, got %d", resp.StatusCode)
} }
var cookie *http.Cookie cookie := findCookie(resp, "id")
for _, c := range resp.Cookies() {
if c.Name == "id" {
cookie = c
break
}
}
if cookie == nil { if cookie == nil {
t.Fatalf("No session cookie found") t.Fatalf("No session cookie found")
} else if cookie.SameSite != http.SameSiteStrictMode || cookie.HttpOnly != true || cookie.Secure != true { } 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) { func setupIntegrationTest(t *testing.T, port string) (*sql.DB, context.Context) {
ctx, done := context.WithCancel(context.Background()) ctx, done := context.WithCancel(context.Background())
t.Cleanup(done) t.Cleanup(done)