|
| 1 | +package html_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gost-dom/browser/html" |
| 8 | + "github.com/gost-dom/browser/internal/testing/htmltest" |
| 9 | + "github.com/stretchr/testify/suite" |
| 10 | +) |
| 11 | + |
| 12 | +type HTMLFormElementWithCheckboxTestSuite struct { |
| 13 | + suite.Suite |
| 14 | +} |
| 15 | + |
| 16 | +func TestHTMLFormElementWithCheckbox(t *testing.T) { |
| 17 | + suite.Run(t, new(HTMLFormElementWithCheckboxTestSuite)) |
| 18 | +} |
| 19 | + |
| 20 | +// httpRequestRecorder is a very simple http.Handler that just records the |
| 21 | +// incoming requests, and returns a 200 status |
| 22 | +type httpRequestRecorder struct { |
| 23 | + t testing.TB |
| 24 | + requests []*http.Request |
| 25 | +} |
| 26 | + |
| 27 | +func (rec *httpRequestRecorder) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 28 | + r.ParseForm() |
| 29 | + rec.requests = append(rec.requests, r) |
| 30 | +} |
| 31 | + |
| 32 | +// Single asserts that a single request was made |
| 33 | +func (r httpRequestRecorder) Single() *http.Request { |
| 34 | + r.t.Helper() |
| 35 | + if len(r.requests) != 1 { |
| 36 | + r.t.Errorf("Expected single recorded request. Got: %d", len(r.requests)) |
| 37 | + } |
| 38 | + return r.requests[0] |
| 39 | +} |
| 40 | + |
| 41 | +func (s *HTMLFormElementWithCheckboxTestSuite) TestSubmitWithCheckboxes() { |
| 42 | + rec := &httpRequestRecorder{t: s.T()} |
| 43 | + win := htmltest.NewWindowHelper(s.T(), NewWindowFromHandler(rec)) |
| 44 | + win.LoadHTML(`<body> |
| 45 | + <form method="post"> |
| 46 | + <input id="check-1" name="check-me-1" type="checkbox" /> |
| 47 | + <label id="lbl-1" for="check">Check me 1</label> |
| 48 | + <input id="check-2" name="check-me-2" type="checkbox" /> |
| 49 | + <label id="lbl-2" for="check">Check me 2</label> |
| 50 | + </form> |
| 51 | + </body>`) |
| 52 | + form := win.HTMLDocument().QuerySelectorHTML("form").(html.HTMLFormElement) |
| 53 | + check := win.HTMLDocument().GetHTMLElementById("check-1").(html.HTMLInputElement) |
| 54 | + check.SetChecked(true) |
| 55 | + form.Submit() |
| 56 | + req := rec.Single() |
| 57 | + s.Assert().Equal([]string{"on"}, req.PostForm["check-me-1"]) |
| 58 | + s.Assert().Empty(req.PostForm["check-me-2"]) |
| 59 | +} |
0 commit comments