| 1 |
package gosubmit |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/http" |
| 6 |
|
| 7 |
"golang.org/x/net/html" |
| 8 |
) |
| 9 |
|
| 10 |
type errorContainer struct { |
| 11 |
err error |
| 12 |
} |
| 13 |
|
| 14 |
type Forms []Form |
| 15 |
|
| 16 |
func (e *errorContainer) setError(err error) { |
| 17 |
if e.err == nil { |
| 18 |
e.err = err |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
func (e *errorContainer) Err() error { |
| 23 |
return e.err |
| 24 |
} |
| 25 |
|
| 26 |
type Document struct { |
| 27 |
errorContainer |
| 28 |
forms Forms |
| 29 |
} |
| 30 |
|
| 31 |
type Inputs map[string]Input |
| 32 |
|
| 33 |
func (d Document) Forms() Forms { |
| 34 |
return d.forms |
| 35 |
} |
| 36 |
|
| 37 |
func (forms Forms) First() (form Form) { |
| 38 |
if len(forms) == 0 { |
| 39 |
form.Inputs = make(Inputs) |
| 40 |
form.setError(fmt.Errorf("No forms found")) |
| 41 |
return |
| 42 |
} |
| 43 |
return forms[0] |
| 44 |
} |
| 45 |
|
| 46 |
func (forms Forms) Last() (form Form) { |
| 47 |
size := len(forms) |
| 48 |
if size == 0 { |
| 49 |
form.Inputs = make(Inputs) |
| 50 |
form.setError(fmt.Errorf("No forms found")) |
| 51 |
return |
| 52 |
} |
| 53 |
return forms[size-1] |
| 54 |
} |
| 55 |
|
| 56 |
func (d Document) FirstForm() (form Form) { |
| 57 |
return d.forms.First() |
| 58 |
} |
| 59 |
|
| 60 |
func (d Document) FindForm(attrKey string, attrValue string) (form Form) { |
| 61 |
for _, f := range d.forms { |
| 62 |
for _, attr := range f.Attr { |
| 63 |
if attr.Key == attrKey && attr.Val == attrValue { |
| 64 |
return f |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
form.Inputs = make(Inputs) |
| 70 |
form.setError(fmt.Errorf("No form with attributes %s='%s' found", attrKey, attrValue)) |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
func (d Document) FindFormsByClass(className string) (forms Forms) { |
| 75 |
for _, f := range d.forms { |
| 76 |
for _, class := range f.ClassList { |
| 77 |
if class == className { |
| 78 |
forms = append(forms, f) |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
return |
| 83 |
} |
| 84 |
|
| 85 |
type Form struct { |
| 86 |
errorContainer |
| 87 |
|
| 88 |
Attr []html.Attribute |
| 89 |
ClassList []string |
| 90 |
|
| 91 |
|
| 92 |
ContentType string |
| 93 |
|
| 94 |
Inputs Inputs |
| 95 |
|
| 96 |
Method string |
| 97 |
|
| 98 |
URL string |
| 99 |
|
| 100 |
Buttons []Button |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
func (f Form) IsRequired(name string) bool { |
| 105 |
input, ok := f.Inputs[name] |
| 106 |
return ok && input.Required() |
| 107 |
} |
| 108 |
|
| 109 |
func (f Form) newFiller(opts []Option) (filler *filler, err error) { |
| 110 |
if f.err != nil { |
| 111 |
err = f.err |
| 112 |
return |
| 113 |
} |
| 114 |
filler, err = newFiller(f, opts) |
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
func (f Form) Validate(opts ...Option) error { |
| 121 |
_, err := f.newFiller(opts) |
| 122 |
return err |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
func (f Form) NewRequest(opts ...Option) (*http.Request, error) { |
| 128 |
filler, err := f.newFiller(opts) |
| 129 |
if err != nil { |
| 130 |
return nil, err |
| 131 |
} |
| 132 |
return filler.NewRequest() |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
func (f Form) NewTestRequest(opts ...Option) (*http.Request, error) { |
| 138 |
filler, err := f.newFiller(opts) |
| 139 |
if err != nil { |
| 140 |
return nil, err |
| 141 |
} |
| 142 |
return filler.NewTestRequest() |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
func (f Form) MultipartParams(opts ...Option) (boundary string, data []byte, err error) { |
| 149 |
filler, err := f.newFiller(opts) |
| 150 |
if err != nil { |
| 151 |
return "", nil, err |
| 152 |
} |
| 153 |
return filler.BuildMultipart() |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
func (f Form) GetParams(opts ...Option) (string, error) { |
| 158 |
filler, err := f.newFiller(opts) |
| 159 |
if err != nil { |
| 160 |
return "", err |
| 161 |
} |
| 162 |
return filler.BuildGet() |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
func (f Form) PostParams(opts ...Option) ([]byte, error) { |
| 167 |
filler, err := f.newFiller(opts) |
| 168 |
if err != nil { |
| 169 |
return nil, err |
| 170 |
} |
| 171 |
return filler.BuildPost() |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
func (f Form) GetOptionsFor(name string) (options []string) { |
| 177 |
input, ok := f.Inputs[name] |
| 178 |
if !ok { |
| 179 |
return |
| 180 |
} |
| 181 |
return input.Options() |
| 182 |
} |
| 183 |
|
| 184 |
func (f Form) Testing(t test) TestingForm { |
| 185 |
return TestingForm{form: f, t: t} |
| 186 |
} |