项目作者: jeremija

项目描述 :
Go library for parsing and submitting HTML forms
高级语言: Go
项目地址: git://github.com/jeremija/gosubmit.git
创建时间: 2020-02-16T14:09:41Z
项目社区:https://github.com/jeremija/gosubmit

开源协议:

下载


gosubmit

Build Status

Description

Docs are available here: https://godoc.org/github.com/jeremija/gosubmit

Helps filling out plain html forms during testing. Will automatically take the
existing values from the form so there is no need to manually set things like
csrf tokens. Alerts about missing required fields, or when pattern validation
does not match. See example_test.go for a full example.

  1. package gosubmit_test
  2. import (
  3. // TODO import app
  4. . "github.com/jeremija/gosubmit"
  5. "net/http"
  6. "net/http/httptest"
  7. )
  8. func TestLogin(t *testing.T) {
  9. w := httptest.NewRecorder()
  10. r := httptest.NewRequest("GET", "/auth/login", nil)
  11. app.ServeHTTP(w, r)
  12. r, err := ParseResponse(w.Result(), r.URL).FirstForm().NewTestRequest(
  13. Set("username", "user"),
  14. Set("password", "password"),
  15. )
  16. if err != nil {
  17. t.Fatalf("Error filling form: %s", err)
  18. }
  19. w := httptest.NewRecorder()
  20. app.ServeHTTP(w, r)
  21. if code := w.Result().StatusCode; code != http.StatusOK {
  22. t.Errorf("Expected status ok but got %d", code)
  23. }
  24. }

Autofilling of all required input fields is supported:

  1. r, err := ParseResponse(w.Result(), r.URL).FirstForm().NewTestRequest(
  2. Autofill(),
  3. )

Elements that include a pattern attribute for validation will not be autofilled
and have to be filled in manually. For example:

  1. r, err := ParseResponse(w.Result(), r.URL).FirstForm().NewTestRequest(
  2. Autofill(),
  3. Set("validatedURL", "https://www.example.com"),
  4. )

Testing Helpers

To avoid checking for error in tests manually when creating a new test request
, the value of t *testing.T can be provided:

  1. r := ParseResponse(w.Result(), r.URL).FirstForm().Testing(t).NewTestRequest(
  2. Autofill(),
  3. Set("validatedURL", "https://www.example.com"),
  4. )

In case of any errors, the t.Fatalf() function will be called. t.Helper()
is used appropriately to ensure line numbers reported by go test are correct.

Supported Elements

  • input[type=checkbox]
  • input[type=date]
  • input[type=email]
  • input[type=hidden]
  • input[type=number]
  • input[type=radio]
  • input[type=text]
  • input[type=url]
  • textarea
  • select
  • select[multiple]
  • button[type=submit] with name and value
  • input[type=submit] with name and value

If an input element is not on this list, it will default to text input.

Who Is Using gosubmit?

License

MIT