diff --git a/run.go b/run.go index a7ab4f92..ecd3fd7b 100644 --- a/run.go +++ b/run.go @@ -17,7 +17,7 @@ import ( const ( playwrightCliVersion = "1.39.0" - baseURL = "https://playwright.azureedge.net/builds/driver" + playwrightCDN = "https://playwright.azureedge.net" ) type PlaywrightDriver struct { @@ -329,6 +329,11 @@ func (d *PlaywrightDriver) getDriverURL() string { } } + baseURL := fmt.Sprintf("%s/builds/driver", playwrightCDN) + if hostEnv := os.Getenv("PLAYWRIGHT_DOWNLOAD_HOST"); hostEnv != "" { + baseURL = fmt.Sprintf("%s/builds/driver", hostEnv) + } + if d.isReleaseVersion() { return fmt.Sprintf("%s/playwright-%s-%s.zip", baseURL, d.Version, platform) } diff --git a/run_test.go b/run_test.go index cdae1e38..0582e742 100644 --- a/run_test.go +++ b/run_test.go @@ -1,7 +1,10 @@ package playwright import ( + "net/http" + "net/http/httptest" "os" + "strings" "testing" ) @@ -29,3 +32,30 @@ func TestDriverInstall(t *testing.T) { t.Fatalf("could not uninstall driver: %v", err) } } + +func TestDriverDownloadHostEnv(t *testing.T) { + driverPath := t.TempDir() + driver, err := NewDriver(&RunOptions{ + DriverDirectory: driverPath, + SkipInstallBrowsers: true, + }) + if err != nil { + t.Fatalf("could not start driver: %v", err) + } + uri := "" + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + uri = r.URL.String() + w.WriteHeader(404) + })) + defer ts.Close() + + err = os.Setenv("PLAYWRIGHT_DOWNLOAD_HOST", ts.URL) + if err != nil { + t.Fatalf("could not set PLAYWRIGHT_DOWNLOAD_HOST: %v", err) + } + defer os.Unsetenv("PLAYWRIGHT_DOWNLOAD_HOST") + err = driver.Install() + if err == nil || !strings.Contains(err.Error(), "404 Not Found") || !strings.Contains(uri, "/builds/driver") { + t.Fatalf("PLAYWRIGHT_DOWNLOAD_HOST do not work: %v", err) + } +}