|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +// Package e2e exercises the complete browser challenge flow against a running |
| 4 | +// Berghain and HAProxy stack. |
| 5 | +package e2e |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/chromedp/chromedp" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + defaultBaseURL = "http://localhost:18080" |
| 21 | + backendBody = "Berghain E2E backend reached" |
| 22 | +) |
| 23 | + |
| 24 | +func baseURL() string { |
| 25 | + if value := os.Getenv("BERGHAIN_E2E_BASE_URL"); value != "" { |
| 26 | + return strings.TrimRight(value, "/") |
| 27 | + } |
| 28 | + return defaultBaseURL |
| 29 | +} |
| 30 | + |
| 31 | +func requireChallengePage(t *testing.T, url string) { |
| 32 | + t.Helper() |
| 33 | + |
| 34 | + client := &http.Client{Timeout: 5 * time.Second} |
| 35 | + response, err := client.Get(url + "/") |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("request challenge page: %v", err) |
| 38 | + } |
| 39 | + defer response.Body.Close() |
| 40 | + |
| 41 | + body, err := io.ReadAll(io.LimitReader(response.Body, 1<<20)) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("read challenge page: %v", err) |
| 44 | + } |
| 45 | + if response.StatusCode != http.StatusForbidden { |
| 46 | + t.Fatalf("expected challenge status 403, got %d: %q", response.StatusCode, body) |
| 47 | + } |
| 48 | + if !strings.Contains(string(body), "Request on Hold") { |
| 49 | + t.Fatalf("expected challenge page, got: %q", body) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestBrowserSolvesChallenge(t *testing.T) { |
| 54 | + url := baseURL() |
| 55 | + requireChallengePage(t, url) |
| 56 | + |
| 57 | + options := append([]chromedp.ExecAllocatorOption{}, chromedp.DefaultExecAllocatorOptions[:]...) |
| 58 | + options = append(options, chromedp.NoSandbox) |
| 59 | + if binary := os.Getenv("CHROME_BIN"); binary != "" { |
| 60 | + options = append(options, chromedp.ExecPath(binary)) |
| 61 | + } |
| 62 | + |
| 63 | + allocatorContext, cancelAllocator := chromedp.NewExecAllocator(context.Background(), options...) |
| 64 | + defer cancelAllocator() |
| 65 | + browserContext, cancelBrowser := chromedp.NewContext(allocatorContext) |
| 66 | + defer cancelBrowser() |
| 67 | + browserContext, cancelTimeout := context.WithTimeout(browserContext, 90*time.Second) |
| 68 | + defer cancelTimeout() |
| 69 | + |
| 70 | + var title string |
| 71 | + if err := chromedp.Run(browserContext, |
| 72 | + chromedp.Navigate(url+"/"), |
| 73 | + chromedp.Title(&title), |
| 74 | + ); err != nil { |
| 75 | + t.Fatalf("navigate to challenge page: %v", err) |
| 76 | + } |
| 77 | + if title != "Request on Hold" { |
| 78 | + t.Fatalf("expected browser challenge page, got title %q", title) |
| 79 | + } |
| 80 | + |
| 81 | + deadline := time.Now().Add(75 * time.Second) |
| 82 | + var body string |
| 83 | + for time.Now().Before(deadline) { |
| 84 | + err := chromedp.Run(browserContext, |
| 85 | + chromedp.Evaluate(`document.body ? document.body.textContent : ""`, &body), |
| 86 | + ) |
| 87 | + if err == nil && strings.Contains(body, backendBody) { |
| 88 | + return |
| 89 | + } |
| 90 | + time.Sleep(250 * time.Millisecond) |
| 91 | + } |
| 92 | + |
| 93 | + t.Fatalf("browser did not reach the backend after solving the challenge; last body: %q", body) |
| 94 | +} |
0 commit comments