Skip to content

Commit d65f3bc

Browse files
committed
test: Verify some standard HTML form post behaviour
1 parent bbaee26 commit d65f3bc

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

html/html_button_element.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package html
22

3-
import "strings"
3+
import (
4+
"strings"
5+
6+
"github.com/gost-dom/browser/internal/log"
7+
)
48

59
type HTMLButtonElement interface {
610
HTMLElement
@@ -39,7 +43,9 @@ func (e *htmlButtonElement) trySubmitForm() {
3943
parent = parent.ParentNode()
4044
}
4145
if form != nil {
42-
form.RequestSubmit(e)
46+
if err := form.RequestSubmit(e); err != nil {
47+
e.logger().Error("Button click, error submitting form", log.ErrAttr(err))
48+
}
4349
}
4450
}
4551

html/html_form_element.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/gost-dom/browser/dom"
1010
"github.com/gost-dom/browser/dom/event"
11+
"github.com/gost-dom/browser/internal/log"
1112
"github.com/gost-dom/browser/url"
1213
)
1314

@@ -127,6 +128,8 @@ func (e *htmlFormElement) submitFormData(formData *FormData) error {
127128
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
128129
}
129130
if err != nil {
131+
l := e.logger()
132+
l.Error("Error creating request for form", log.ErrAttr(err))
130133
return err
131134
}
132135
return e.htmlDocument.window().fetchRequest(req)

html/html_form_element_submit_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package html_test
22

33
import (
4+
"log/slog"
45
"net/http"
56
"net/url"
67
"reflect"
@@ -9,13 +10,15 @@ import (
910
"github.com/gost-dom/browser/dom/event"
1011
"github.com/gost-dom/browser/html"
1112
"github.com/gost-dom/browser/internal/gosthttp"
13+
"github.com/gost-dom/browser/internal/testing/browsertest"
1214
"github.com/gost-dom/browser/internal/testing/eventtest"
1315
"github.com/gost-dom/browser/internal/testing/fixtures"
1416
. "github.com/gost-dom/browser/internal/testing/gomega-matchers"
1517
"github.com/gost-dom/browser/internal/testing/gosttest"
1618
"github.com/gost-dom/browser/internal/testing/htmltest"
1719
. "github.com/gost-dom/browser/testing/gomega-matchers"
1820
"github.com/gost-dom/fixture"
21+
"github.com/stretchr/testify/assert"
1922
)
2023

2124
type HTTPHandlerFixture struct{ *http.ServeMux }
@@ -296,6 +299,7 @@ func TestHTMLFormElementSubmitInputWithClickResetButton(t *testing.T) {
296299
}
297300

298301
func TestResubmitFormOn307Redirects(t *testing.T) {
302+
assert := assert.New(t)
299303
w, setup := fixture.Init(t, &HTMLFormSubmitInputFixture{})
300304
w.BaseLocation = "http://example.com/forms"
301305
setup.Setup()
@@ -309,6 +313,25 @@ func TestResubmitFormOn307Redirects(t *testing.T) {
309313
form.SetAction("/form-destination")
310314
form.Submit()
311315

312-
w.Assert().Equal(1, len(rec.Requests), "Request sent to the redirected location")
313-
w.Assert().Equal([]string{"bar"}, rec.Single().PostForm["foo"])
316+
assert.Equal(1, len(rec.Requests), "Request sent to the redirected location")
317+
assert.Equal([]string{"bar"}, rec.Single().PostForm["foo"])
318+
}
319+
320+
func TestFormSubmitDisplaysPageOnNewLocation(t *testing.T) {
321+
h := gosttest.HttpHandlerMap{
322+
"/": gosttest.StaticHTML(`<body>
323+
<form method="post" action="/post/action">
324+
<input type="submit" id="btn">Submit</input>
325+
</form>
326+
</body>`),
327+
"/post/action": gosttest.StaticHTML(`<body><h1>Target page</h1></body>`),
328+
}
329+
b := browsertest.InitBrowser(t, h, nil, browsertest.WithMinLogLevel(slog.LevelDebug))
330+
win := b.OpenWindow("http://example.com/")
331+
t.Log(win.HTMLDocument().DocumentElement().OuterHTML())
332+
btn := win.HTMLDocument().GetHTMLElementById("btn")
333+
btn.Click()
334+
t.Log(win.HTMLDocument().Body().OuterHTML())
335+
h1 := win.HTMLDocument().MustQuerySelectorHTML("h1")
336+
assert.Equal(t, "Target page", h1.TextContent())
314337
}

html/window.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,12 @@ func (w *window) get(href string) error {
367367
func (w *window) fetchRequest(req *http.Request) error {
368368
// Create a copy of the client, and set the CheckRedirect to a function that
369369
// updates the window location to reflect the new URL.
370+
l := w.Logger()
371+
l.Info("window.fetchRequest", "method", req.Method, "url", req.URL.String())
370372
client := w.httpClient
371373
client.CheckRedirect = w.checkRedirect
372374
resp, err := client.Do(req)
375+
l.Debug("window.fetchRequest done", log.ErrAttr(err), slog.Int("status", resp.StatusCode))
373376
if err != nil {
374377
return err
375378
}

internal/testing/htmltest/helpers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ func NewHTMLParentNodeHelper(t testing.TB, n dom.ElementParent) HTMLParentNodeHe
146146
return HTMLParentNodeHelper{t, n}
147147
}
148148

149+
func (h HTMLParentNodeHelper) MustQuerySelectorHTML(pattern string) HTMLElementHelper {
150+
res := h.QuerySelectorHTML(pattern)
151+
if res.HTMLElement == nil {
152+
h.t.Fatalf("QuerySelector returned nil: %s", pattern)
153+
}
154+
return res
155+
}
156+
149157
func (h HTMLParentNodeHelper) QuerySelectorHTML(pattern string) (res HTMLElementHelper) {
150158
h.t.Helper()
151159
e, err := h.ElementParent.QuerySelector(pattern)

0 commit comments

Comments
 (0)