Files
TradarrV2/backend/internal/e2estack/network.go
Blomios 4459a81206 test(e2e): add isolated browser journey and cleanup supervision
Add a fake-only end-to-end stack, Firefox journey, and bounded process cleanup for normal, failed, and interrupted runs.

Refs #12

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-20 23:39:02 +01:00

50 lines
1.4 KiB
Go

// Package e2estack contains test-only loopback adapters. Production does not import it.
package e2estack
import (
"context"
"errors"
"net"
"net/http"
"net/url"
)
func ValidateURL(raw string, database bool) error {
u, err := url.Parse(raw)
if err != nil {
return errors.New("invalid E2E URL")
}
scheme := u.Scheme == "http" || u.Scheme == "https"
if database {
scheme = u.Scheme == "postgres" || u.Scheme == "postgresql"
}
ip := net.ParseIP(u.Hostname())
if !scheme || ip == nil || !ip.IsLoopback() || u.Port() == "" {
return errors.New("E2E URL must use a literal loopback IP and explicit port")
}
return nil
}
type loopbackTransport struct{ base *http.Transport }
func (t loopbackTransport) RoundTrip(r *http.Request) (*http.Response, error) {
if err := ValidateURL(r.URL.String(), false); err != nil {
return nil, err
}
return t.base.RoundTrip(r)
}
func LoopbackClient() *http.Client {
transport := &http.Transport{Proxy: nil, DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
host, _, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
ip := net.ParseIP(host)
if ip == nil || !ip.IsLoopback() {
return nil, errors.New("E2E external egress forbidden")
}
return (&net.Dialer{}).DialContext(ctx, network, address)
}}
return &http.Client{Transport: loopbackTransport{transport}, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
}