-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing.go
140 lines (120 loc) · 3.86 KB
/
testing.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
//go:build !wasm
package main
import (
"fmt"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"log"
"strconv"
"time"
)
func testing() {
l := launcher.New().
Headless(false).
Devtools(true)
defer l.Cleanup() // remove launcher.FlagUserDataDir
url := l.MustLaunch()
browser := rod.New().
ControlURL(url).
//Trace(false).
//SlowMotion(2000 * time.Millisecond).
MustConnect()
defer browser.MustClose()
page := browser.MustPage()
logConsole := true
// Listen for all events of console output.
go page.EachEvent(
func(e *proto.RuntimeConsoleAPICalled) {
if logConsole {
prefix := log.Prefix()
log.SetPrefix("[con] ")
log.Printf("%s", page.MustObjectsToJSON(e.Args))
log.SetPrefix(prefix)
}
},
func(e *proto.RuntimeExceptionThrown) {
prefix := log.Prefix()
log.SetPrefix("[exc] ")
log.Printf("%#v", e.ExceptionDetails)
log.SetPrefix(prefix)
},
)()
router := browser.HijackRequests()
defer router.MustStop()
// without some route defined it hangs here
router.MustAdd("not-to-happen", func(*rod.Hijack) {})
go router.Run()
wait := page.WaitEvent(&proto.PageLoadEventFired{})
page.MustNavigate("http://" + siteURL)
wait()
// Wait till the loader has vanished
page.MustWait(`() => document.getElementById("app-wasm-loader") == null`)
// Click without modifying the result
fmt.Println("\n")
log.Println("Clicking the button without hijacking active")
wait = page.MustWaitRequestIdle()
page.MustElement("#reqbut").MustClick()
wait()
fmt.Println("\n")
log.Println("Clicking the button with hijacking 'Data' active")
router.MustAdd("*/post", injectData)
wait = page.MustWaitRequestIdle()
page.MustElement("#reqbut").MustClick()
wait()
// remove that hijacker
router.MustRemove("*/post")
// switching the endpoint url
fmt.Println("\n")
log.Println("Clicking the button to change the endpoint url")
page.MustElement("#urlbut").MustClick()
log.Println("Clicking the button, button replies with an empty body")
router.MustAdd("*/post", injectData)
wait = page.MustWaitRequestIdle()
page.MustElement("#reqbut").MustClick()
wait()
log.Println("Notice: The frontend got the empty body response just as expected.")
// remove that hijacker
router.MustRemove("*/post")
// This is the Error case which actually should work
fmt.Println("\n")
log.Println("Clicking the button with hijacking 'EmptyBody' active")
router.MustAdd("*/empty", injectEmpty)
wait = page.MustWaitRequestIdle()
page.MustElement("#reqbut").MustClick()
wait()
log.Println("Notice: The javascript console ([con]) says the backend request started, but now it hangs. The reasons seems to be that there is no (empty) body.")
time.Sleep(5 * time.Second)
//utils.Pause()
}
func injectData(ctx *rod.Hijack) {
log.Println("Start: Intercept and modifying POST with data")
//ctx.MustLoadResponse()
body := "Injected response"
ctx.Response.SetBody(body).SetHeader(
"Access-Control-Allow-Origin", siteURL,
"Access-Control-Expose-Headers", "Link",
"Cache-Control", "public, max-age=60",
"Content-Length", strconv.Itoa(len(body)),
"Content-Type", "application/text",
//"Date", "Mon, 16 May 2022 20:53:07 GMT",
"Vary", "Origin",
)
log.Println("End: Intercept and modifying POST with data")
//ctx.Response.Fail(proto.NetworkErrorReasonAddressUnreachable)
}
func injectEmpty(ctx *rod.Hijack) {
log.Println("Start: Intercept and modifying POST with empty BODY")
//ctx.MustLoadResponse()
ctx.Response.SetBody([]byte{}).SetHeader(
"Access-Control-Allow-Origin", siteURL,
"Access-Control-Expose-Headers", "Link",
"Cache-Control", "public, max-age=60",
"Content-Length", "0",
"Content-Type", "application/text",
//"Date", "Mon, 16 May 2022 20:53:07 GMT",
"Vary", "Origin",
)
log.Println("End: Intercept and modifying POST with empty BODY")
//ctx.Response.Fail(proto.NetworkErrorReasonAddressUnreachable)
}