-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbrowser_session_httpclient_test.go
More file actions
103 lines (91 loc) · 2.5 KB
/
browser_session_httpclient_test.go
File metadata and controls
103 lines (91 loc) · 2.5 KB
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
package kernel
import (
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/kernel/kernel-go-sdk/internal/requestconfig"
"github.com/kernel/kernel-go-sdk/lib/browserrouting"
"github.com/kernel/kernel-go-sdk/option"
)
func TestBrowserSessionHTTPClientRawCurl(t *testing.T) {
var sawRaw string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/browser/kernel/curl/raw" {
http.NotFound(w, r)
return
}
sawRaw = r.URL.RawQuery
w.Header().Set("Content-Type", "text/plain")
_, _ = w.Write([]byte("proxied"))
}))
defer srv.Close()
c := NewClient(
option.WithBaseURL("https://api.example/"),
option.WithAPIKey("sk"),
option.WithHTTPClient(srv.Client()),
)
storeBrowserRouteCache(c.Options, browserrouting.Ref{
SessionID: "sid",
BaseURL: srv.URL + "/browser/kernel",
CdpWsURL: "wss://x/browser/cdp?jwt=j1",
})
hc, err := c.Browsers.HTTPClient("sid")
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest(http.MethodGet, "https://httpbin.org/get", nil)
if err != nil {
t.Fatal(err)
}
res, err := hc.Do(req)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
if string(body) != "proxied" {
t.Fatalf("body %q", body)
}
if sawRaw == "" {
t.Fatal("expected raw query on curl/raw")
}
}
func TestBrowserSessionHTTPClientRequiresCachedRoute(t *testing.T) {
c := NewClient(
option.WithBaseURL("https://api.example/"),
option.WithAPIKey("sk"),
)
storeBrowserRouteCache(c.Options, browserrouting.Ref{
SessionID: "sid",
BaseURL: "https://browser-session.test/browser/kernel",
CdpWsURL: "wss://x/browser/cdp?jwt=j1",
})
c.BrowserRouteCache.Delete("sid")
_, err := c.Browsers.HTTPClient("sid")
if err == nil {
t.Fatal("expected cached route lookup failure")
}
}
func TestBrowserHTTPClientPropagatesRequestConfigError(t *testing.T) {
c := NewClient(
option.WithBaseURL("https://api.example/"),
option.WithAPIKey("sk"),
)
storeBrowserRouteCache(c.Options, browserrouting.Ref{
SessionID: "sid",
BaseURL: "https://browser-session.test/browser/kernel",
CdpWsURL: "wss://x/browser/cdp?jwt=j1",
})
wantErr := errors.New("request config failed")
hc, err := c.Browsers.HTTPClient("sid", requestconfig.RequestOptionFunc(func(*requestconfig.RequestConfig) error {
return wantErr
}))
if !errors.Is(err, wantErr) {
t.Fatalf("expected error %q, got %v", wantErr, err)
}
if hc != nil {
t.Fatal("expected nil client when request config fails")
}
}