-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathui_test.go
More file actions
245 lines (202 loc) · 7.03 KB
/
Copy pathui_test.go
File metadata and controls
245 lines (202 loc) · 7.03 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/gorilla/websocket"
)
// fakeBrowser mimics a browser exposing the DevTools protocol: it answers
// /json/version and echoes a result for every command received on the websocket.
func fakeBrowser(t *testing.T) *httptest.Server {
t.Helper()
mux := http.NewServeMux()
mux.HandleFunc("/json/version", func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json")
json.NewEncoder(res).Encode(map[string]string{
"Protocol-Version": "1.3",
"Browser": "FakeChrome/1.0",
})
})
mux.HandleFunc("/devtools/page/", func(res http.ResponseWriter, req *http.Request) {
c, err := wsUpgrader.Upgrade(res, req, nil)
if err != nil {
t.Errorf("fake browser could not upgrade: %v", err)
return
}
defer c.Close()
for {
var msg protocolMessage
if err := c.ReadJSON(&msg); err != nil {
return
}
reply := map[string]interface{}{
"id": msg.ID,
"result": map[string]interface{}{"method": msg.Method},
}
if err := c.WriteJSON(reply); err != nil {
return
}
}
})
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
return server
}
func wsURL(server *httptest.Server, path string) string {
return "ws" + strings.TrimPrefix(server.URL, "http") + path
}
func dialWS(t *testing.T, url string) *websocket.Conn {
t.Helper()
c, res, err := wsDialer.Dial(url, nil)
if err != nil {
t.Fatalf("could not dial %s: %v", url, err)
}
if res != nil {
defer res.Body.Close()
}
t.Cleanup(func() { c.Close() })
return c
}
// readUIMessage reads messages from the UI websocket until predicate matches.
func readUIMessage(t *testing.T, c *websocket.Conn, what string, predicate func(map[string]interface{}) bool) map[string]interface{} {
t.Helper()
c.SetReadDeadline(time.Now().Add(5 * time.Second))
for {
var msg map[string]interface{}
if err := c.ReadJSON(&msg); err != nil {
t.Fatalf("did not receive %s: %v", what, err)
}
if predicate(msg) {
return msg
}
}
}
func TestInteractiveUI(t *testing.T) {
browser := fakeBrowser(t)
*flagRemote = strings.TrimPrefix(browser.URL, "http://")
*flagQuiet = true
*flagDirLogs = t.TempDir()
*flagUI = true
proxy := httptest.NewServer(createMux())
t.Cleanup(proxy.Close)
// serves the embedded UI page
res, err := http.Get(proxy.URL + "/ui")
if err != nil {
t.Fatalf("could not fetch /ui: %v", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
t.Fatalf("unexpected /ui status: %d", res.StatusCode)
}
ui := dialWS(t, wsURL(proxy, "/ui/ws"))
client := dialWS(t, wsURL(proxy, "/devtools/page/test-page"))
// UI is told about the proxied connection
var connID string
readUIMessage(t, ui, "connections message", func(msg map[string]interface{}) bool {
if msg["type"] != "connections" {
return false
}
conns, _ := msg["connections"].([]interface{})
if len(conns) == 0 {
return false
}
connID = conns[0].(map[string]interface{})["id"].(string)
return true
})
// a frame sent by the client is streamed to the UI and forwarded to the browser
if err := client.WriteJSON(map[string]interface{}{"id": 1, "method": "Page.enable"}); err != nil {
t.Fatalf("client could not send: %v", err)
}
var requestBoot string
var requestSeq float64
requestFrame := readUIMessage(t, ui, "client request frame", func(msg map[string]interface{}) bool {
if msg["type"] != "frame" || msg["direction"] != directionSend {
return false
}
message := msg["message"].(map[string]interface{})
return message["method"] == "Page.enable"
})
requestBoot, _ = requestFrame["boot"].(string)
requestSeq, _ = requestFrame["seq"].(float64)
if requestBoot == "" || requestSeq == 0 {
t.Fatalf("frame is missing boot/seq stamps: %+v", requestFrame)
}
// the browser response reaches both the client and the UI
client.SetReadDeadline(time.Now().Add(5 * time.Second))
var clientReply protocolMessage
if err := client.ReadJSON(&clientReply); err != nil {
t.Fatalf("client did not receive response: %v", err)
}
if clientReply.ID != 1 {
t.Fatalf("client received unexpected response: %+v", clientReply)
}
responseFrame := readUIMessage(t, ui, "browser response frame", func(msg map[string]interface{}) bool {
return msg["type"] == "frame" && msg["direction"] == directionRecv
})
if seq, _ := responseFrame["seq"].(float64); seq <= requestSeq {
t.Fatalf("frame seq is not monotonic: request=%v response=%v", requestSeq, seq)
}
// a command injected from the UI reaches the browser, its response comes back
// to the UI marked as injected
command := map[string]interface{}{
"type": "send",
"connId": connID,
"method": "Runtime.evaluate",
"params": map[string]interface{}{"expression": "1+1"},
}
if err := ui.WriteJSON(command); err != nil {
t.Fatalf("ui could not send command: %v", err)
}
var injectedID float64
readUIMessage(t, ui, "injected request frame", func(msg map[string]interface{}) bool {
if msg["type"] != "frame" || msg["injected"] != true || msg["direction"] != directionSend {
return false
}
message := msg["message"].(map[string]interface{})
injectedID = message["id"].(float64)
return message["method"] == "Runtime.evaluate"
})
if uint64(injectedID) < injectedIDBase {
t.Fatalf("injected command id %d is below the reserved range", uint64(injectedID))
}
readUIMessage(t, ui, "injected response frame", func(msg map[string]interface{}) bool {
if msg["type"] != "frame" || msg["injected"] != true || msg["direction"] != directionRecv {
return false
}
message := msg["message"].(map[string]interface{})
return message["id"] == injectedID
})
// the proxied client must never see the injected response
client.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
var unexpected protocolMessage
if err := client.ReadJSON(&unexpected); err == nil {
t.Fatalf("client received a frame it should not have: %+v", unexpected)
}
// errors are reported back to the UI
if err := ui.WriteJSON(map[string]interface{}{"type": "send", "connId": "nope", "method": "Page.enable"}); err != nil {
t.Fatalf("ui could not send command: %v", err)
}
readUIMessage(t, ui, "error message", func(msg map[string]interface{}) bool {
return msg["type"] == "error" && strings.Contains(fmt.Sprint(msg["message"]), "nope")
})
// a reconnecting UI client receives the recent buffer with the same boot and
// seq stamps, so it can skip frames it already has
reconnected := dialWS(t, wsURL(proxy, "/ui/ws"))
replayed := readUIMessage(t, reconnected, "replayed request frame", func(msg map[string]interface{}) bool {
if msg["type"] != "frame" {
return false
}
message := msg["message"].(map[string]interface{})
return message["method"] == "Page.enable" && msg["direction"] == directionSend
})
if boot, _ := replayed["boot"].(string); boot != requestBoot {
t.Fatalf("replayed frame boot mismatch: %q != %q", boot, requestBoot)
}
if seq, _ := replayed["seq"].(float64); seq != requestSeq {
t.Fatalf("replayed frame seq mismatch: %v != %v", seq, requestSeq)
}
}