| 1 | # gosubmit |
| 2 | |
| 3 | [](https://travis-ci.com/jeremija/gosubmit) |
| 4 | |
| 5 | # Description |
| 6 | |
| 7 | Docs are available here: https://godoc.org/github.com/jeremija/gosubmit |
| 8 | |
| 9 | Helps filling out plain html forms during testing. Will automatically take the |
| 10 | existing values from the form so there is no need to manually set things like |
| 11 | csrf tokens. Alerts about missing required fields, or when pattern validation |
| 12 | does not match. See [example_test.go](example_test.go) for a full example. |
| 13 | |
| 14 | ```golang |
| 15 | package gosubmit_test |
| 16 | |
| 17 | import ( |
| 18 | // TODO import app |
| 19 | . "github.com/jeremija/gosubmit" |
| 20 | |
| 21 | "net/http" |
| 22 | "net/http/httptest" |
| 23 | ) |
| 24 | |
| 25 | func TestLogin(t *testing.T) { |
| 26 | w := httptest.NewRecorder() |
| 27 | r := httptest.NewRequest("GET", "/auth/login", nil) |
| 28 | |
| 29 | app.ServeHTTP(w, r) |
| 30 | |
| 31 | r, err := ParseResponse(w.Result(), r.URL).FirstForm().NewTestRequest( |
| 32 | Set("username", "user"), |
| 33 | Set("password", "password"), |
| 34 | ) |
| 35 | |
| 36 | if err != nil { |
| 37 | t.Fatalf("Error filling form: %s", err) |
| 38 | } |
| 39 | |
| 40 | w := httptest.NewRecorder() |
| 41 | app.ServeHTTP(w, r) |
| 42 | |
| 43 | if code := w.Result().StatusCode; code != http.StatusOK { |
| 44 | t.Errorf("Expected status ok but got %d", code) |
| 45 | } |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | Autofilling of all required input fields is supported: |
| 50 | |
| 51 | ```golang |
| 52 | r, err := ParseResponse(w.Result(), r.URL).FirstForm().NewTestRequest( |
| 53 | Autofill(), |
| 54 | ) |
| 55 | ``` |
| 56 | |
| 57 | Elements that include a pattern attribute for validation will not be autofilled |
| 58 | and have to be filled in manually. For example: |
| 59 | |
| 60 | ```golang |
| 61 | r, err := ParseResponse(w.Result(), r.URL).FirstForm().NewTestRequest( |
| 62 | Autofill(), |
| 63 | Set("validatedURL", "https://www.example.com"), |
| 64 | ) |
| 65 | ``` |
| 66 | |
| 67 | # Testing Helpers |
| 68 | |
| 69 | To avoid checking for error in tests manually when creating a new test request |
| 70 | , the value of `t *testing.T` can be provided: |
| 71 | |
| 72 | ```golang |
| 73 | r := ParseResponse(w.Result(), r.URL).FirstForm().Testing(t).NewTestRequest( |
| 74 | Autofill(), |
| 75 | Set("validatedURL", "https://www.example.com"), |
| 76 | ) |
| 77 | ``` |
| 78 | |
| 79 | In case of any errors, the `t.Fatalf()` function will be called. `t.Helper()` |
| 80 | is used appropriately to ensure line numbers reported by `go test` are correct. |
| 81 | |
| 82 | # Supported Elements |
| 83 | |
| 84 | - `input[type=checkbox]` |
| 85 | - `input[type=date]` |
| 86 | - `input[type=email]` |
| 87 | - `input[type=hidden]` |
| 88 | - `input[type=number]` |
| 89 | - `input[type=radio]` |
| 90 | - `input[type=text]` |
| 91 | - `input[type=url]` |
| 92 | - `textarea` |
| 93 | - `select` |
| 94 | - `select[multiple]` |
| 95 | - `button[type=submit]` with name and value |
| 96 | - `input[type=submit]` with name and value |
| 97 | |
| 98 | If an input element is not on this list, it will default to text input. |
| 99 | |
| 100 | # Who Is Using `gosubmit`? |
| 101 | |
| 102 | - [rondomoon](https://rondomoon.com) |
| 103 | - [rondoBB](https://bb.rondo.dev) |
| 104 | - _Your app here - send a PR!_ |
| 105 | |
| 106 | # License |
| 107 | |
| 108 | MIT |