Skip to content

Commit 5517786

Browse files
committed
Add more submit test cases
Allow submit cases to be run standalone (using ws/main.go)
1 parent 64487b6 commit 5517786

File tree

4 files changed

+104
-39
lines changed

4 files changed

+104
-39
lines changed

public/form/input_button.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<body>
3+
<form id=f method=post action=submit enctype="application/x-www-form-urlencoded">
4+
<input type=hidden name=h1 value=v1>
5+
<input type=text name=h2 value=v2 disabled>
6+
<input name=h3 value=v3>
7+
<select name="favorite drink"><option>tea</option></select>
8+
<input type=submit name=s1 value=go>
9+
<input type=submit name=s2 value=no>
10+
</form>
11+
12+
<button name="b1" value="b1v" type=submit form=f></button>
13+
<button name="b2" value="b2" type=submit form=f disabled></button>
14+
<button name="b3" value="b3" form=f></button>

public/form/submit_button.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<body>
3+
<form id=f method=post action=submit enctype="application/x-www-form-urlencoded">
4+
<input type=hidden name=h1 value=v1>
5+
<input type=text name=h2 value=v2 disabled>
6+
<input name=h3 value=v3>
7+
<select name="favorite drink"><option>tea</option></select>
8+
<input type=submit name=s1 value=go>
9+
<input type=submit name=s2 value=no>
10+
</form>

puppeteer/form.js

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,65 @@ const context = await browser.createBrowserContext();
2828
const page = await context.newPage();
2929

3030
await testForm(page, '/form/get.html', {
31-
method: 'GET',
32-
body: '',
33-
query: 'h1=v1&h3=v3&favorite+drink=tea',
31+
method: 'GET',
32+
body: '',
33+
query: 'h1=v1&h3=v3&favorite+drink=tea',
3434
});
3535

3636
await testForm(page, '/form/post.html', {
37-
method: 'POST',
38-
body: 'h1=v1&h3=v3&favorite+drink=tea',
39-
query: '',
37+
method: 'POST',
38+
body: 'h1=v1&h3=v3&favorite+drink=tea',
39+
query: '',
4040
});
4141

42+
await testForm(page, '/form/submit_button.html', {
43+
method: 'POST',
44+
body: 'h1=v1&h3=v3&favorite+drink=tea&s1=go',
45+
query: '',
46+
}, async () => {
47+
await page.click("[name=s1]");
48+
});
49+
50+
await testForm(page, '/form/input_button.html', {
51+
method: 'POST',
52+
body: 'h1=v1&h3=v3&favorite+drink=tea&b1=b1v',
53+
query: '',
54+
}, async () => {
55+
await page.click("[name=b2]"); // disabled, should do nothing
56+
await page.click("[name=b3]"); // not submit
57+
await page.click("[name=b1]");
58+
});
4259

4360
await context.close();
4461
await browser.disconnect();
4562

63+
async function testForm(page, url, expected, onLoad) {
64+
await page.goto(baseURL + url);
4665

47-
async function testForm(page, url, expected) {
48-
await page.goto(baseURL + url);;
66+
if (onLoad) {
67+
await onLoad();
68+
}
4969

50-
await page.waitForFunction(() => {
51-
const p = document.querySelector('#method');
52-
return p.textContent != '';
53-
}, {timeout: 4000});
70+
await page.waitForFunction(() => {
71+
const p = document.querySelector('#method');
72+
return p.textContent != '';
73+
}, {timeout: 4000});
5474

55-
const method = await page.evaluate(() => { return document.querySelector('#method').textContent; });
56-
if (method !== expected.method) {
57-
console.log(method);
58-
throw new Error("invalid method");
59-
}
75+
const method = await page.evaluate(() => { return document.querySelector('#method').textContent; });
76+
if (method !== expected.method) {
77+
console.log(method);
78+
throw new Error("invalid method");
79+
}
6080

61-
const body = await page.evaluate(() => { return document.querySelector('#body').textContent; });
62-
if (body !== expected.body) {
63-
console.log(body);
64-
throw new Error("invalid body");
65-
}
81+
const body = await page.evaluate(() => { return document.querySelector('#body').textContent; });
82+
if (body !== expected.body) {
83+
console.log(body);
84+
throw new Error("invalid body");
85+
}
6686

67-
const query = await page.evaluate(() => { return document.querySelector('#query').textContent; });
68-
if (query !== expected.query) {
69-
console.log(query);
70-
throw new Error("invalid query");
71-
}
87+
const query = await page.evaluate(() => { return document.querySelector('#query').textContent; });
88+
if (query !== expected.query) {
89+
console.log(query);
90+
throw new Error("invalid query");
91+
}
7292
}

ws/main.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616

1717
import (
1818
"fmt"
19+
"io"
1920
"log"
2021
"net/http"
2122
"os"
@@ -34,18 +35,17 @@ func main() {
3435
dir = "public"
3536
}
3637

37-
handler := http.FileServer(http.Dir(dir))
38-
wait := os.Getenv("WS_WAIT")
39-
if wait != "" {
38+
handler := Handler{
39+
next: http.FileServer(http.Dir(dir)),
40+
wait: time.Duration(0),
41+
}
42+
43+
if wait := os.Getenv("WS_WAIT"); wait != "" {
4044
v, err := strconv.Atoi(wait)
4145
if err != nil {
4246
fmt.Fprintf(os.Stderr, "invalid wait value: %v", err)
4347
}
44-
45-
handler = Slower{
46-
next: handler,
47-
wait: time.Duration(v) * time.Millisecond,
48-
}
48+
handler.wait = time.Duration(v) * time.Millisecond
4949
}
5050

5151
fmt.Fprintf(os.Stderr, "expose dir: %q\nlisten: %q\n", dir, address)
@@ -54,12 +54,33 @@ func main() {
5454
log.Fatal(http.ListenAndServe(address, handler))
5555
}
5656

57-
type Slower struct {
57+
type Handler struct {
5858
next http.Handler
5959
wait time.Duration
6060
}
6161

62-
func (s Slower) ServeHTTP(w http.ResponseWriter, r *http.Request) {
63-
time.Sleep(s.wait)
64-
s.next.ServeHTTP(w, r)
62+
func (s Handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
63+
if s.wait > 0 {
64+
time.Sleep(s.wait)
65+
}
66+
67+
switch req.URL.Path {
68+
case "/form/submit":
69+
defer req.Body.Close()
70+
body, err := io.ReadAll(req.Body)
71+
if err != nil {
72+
panic(err)
73+
}
74+
75+
res.Header().Add("Content-Type", "text/html")
76+
res.Write([]byte("<html><ul><li id=method>"))
77+
res.Write([]byte(req.Method))
78+
res.Write([]byte("<li id=body>"))
79+
res.Write(body)
80+
res.Write([]byte("<li id=query>"))
81+
res.Write([]byte(req.URL.RawQuery))
82+
res.Write([]byte("</ul>"))
83+
default:
84+
s.next.ServeHTTP(res, req)
85+
}
6586
}

0 commit comments

Comments
 (0)