Skip to content

Commit

Permalink
feat: env PLAYWRIGHT_DOWNLOAD_HOST also used to download driver (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
canstand committed Oct 31, 2023
1 parent 53597c2 commit 43f81b3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
7 changes: 6 additions & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
30 changes: 30 additions & 0 deletions run_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package playwright

import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -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)
}
}

0 comments on commit 43f81b3

Please sign in to comment.