forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
private_test.go
181 lines (141 loc) · 3.8 KB
/
private_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package launcher
import (
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"sync"
"testing"
"time"
"github.com/assetnote/rod/lib/cdp"
"github.com/assetnote/rod/lib/defaults"
"github.com/assetnote/rod/lib/launcher/flags"
"github.com/assetnote/rod/lib/utils"
"github.com/ysmood/got"
)
func HostTest(host string) Host {
return func(revision int) string {
return fmt.Sprintf(
"%s/chromium-browser-snapshots/%s/%d/%s",
host,
hostConf.urlPrefix,
revision,
hostConf.zipName,
)
}
}
var setup = got.Setup(nil)
func TestToHTTP(t *testing.T) {
g := setup(t)
u, _ := url.Parse("wss://a.com")
g.Eq("https", toHTTP(*u).Scheme)
u, _ = url.Parse("ws://a.com")
g.Eq("http", toHTTP(*u).Scheme)
}
func TestToWS(t *testing.T) {
g := setup(t)
u, _ := url.Parse("https://a.com")
g.Eq("wss", toWS(*u).Scheme)
u, _ = url.Parse("http://a.com")
g.Eq("ws", toWS(*u).Scheme)
}
func TestLaunchOptions(t *testing.T) {
g := setup(t)
defaults.Show = true
defaults.Devtools = true
inContainer = true
// restore
defer func() {
defaults.ResetWith("")
inContainer = utils.InContainer
}()
l := New()
g.False(l.Has(flags.Headless))
g.True(l.Has(flags.NoSandbox))
g.True(l.Has("auto-open-devtools-for-tabs"))
}
func TestGetURLErr(t *testing.T) {
g := setup(t)
l := New()
l.ctxCancel()
_, err := l.getURL()
g.Err(err)
l = New()
l.parser.lock.Lock()
l.parser.Buffer = "err"
l.parser.lock.Unlock()
close(l.exit)
_, err = l.getURL()
g.Eq("[launcher] Failed to get the debug url: err", err.Error())
}
func TestManaged(t *testing.T) {
g := setup(t)
ctx := g.Timeout(5 * time.Second)
s := got.New(g).Serve()
rl := NewManager()
s.Mux.Handle("/", rl)
l := MustNewManaged(s.URL()).KeepUserDataDir().Delete(flags.KeepUserDataDir)
c := l.MustClient()
g.E(c.Call(ctx, "", "Browser.getVersion", nil))
utils.Sleep(1)
_, _ = c.Call(ctx, "", "Browser.crash", nil)
dir := l.Get(flags.UserDataDir)
for ctx.Err() == nil {
utils.Sleep(0.1)
_, err := os.Stat(dir)
if err != nil {
break
}
}
g.Err(os.Stat(dir))
u, h := MustNewManaged(s.URL()).Bin("go").ClientHeader()
_, err := cdp.StartWithURL(ctx, u, h)
g.Eq(err.(*cdp.BadHandshakeError).Body, "[rod-manager] not allowed rod-bin path: go (use --allow-all to disable the protection)")
}
func TestLaunchErrs(t *testing.T) {
g := setup(t)
l := New().Bin("echo")
_, err := l.Launch()
g.Err(err)
s := g.Serve()
s.Route("/", "", nil)
l = New().Bin("")
l.browser.Logger = utils.LoggerQuiet
l.browser.RootDir = filepath.Join("tmp", "browser-from-mirror", g.RandStr(16))
l.browser.Hosts = []Host{HostTest(s.URL())}
_, err = l.Launch()
g.Err(err)
}
func TestURLParserErr(t *testing.T) {
g := setup(t)
u := &URLParser{
Buffer: "error",
lock: &sync.Mutex{},
}
g.Eq(u.Err().Error(), "[launcher] Failed to get the debug url: error")
u.Buffer = "/tmp/rod/chromium-818858/chrome: error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory"
g.Eq(u.Err().Error(), "[launcher] Failed to launch the browser, the doc might help https://go-rod.github.io/#/compatibility?id=os: /tmp/rod/chromium-818858/chrome: error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory")
}
func TestTestOpen(_ *testing.T) {
openExec = func(_ string, _ ...string) *exec.Cmd {
cmd := exec.Command("not-exists")
cmd.Process = &os.Process{}
return cmd
}
defer func() { openExec = exec.Command }()
Open("about:blank")
}
func TestLaunchClient(t *testing.T) {
g := setup(t)
ctx := g.Timeout(5 * time.Second)
s := got.New(g).Serve()
rl := NewManager()
s.Mux.Handle("/", rl)
l := MustNewManaged(s.URL()).KeepUserDataDir().Delete(flags.KeepUserDataDir)
c, err := l.Client()
if err != nil {
g.Err(err)
}
g.E(c.Call(ctx, "", "Browser.getVersion", nil))
}