forked from bluele/gforms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtextfield_test.go
More file actions
42 lines (38 loc) · 814 Bytes
/
textfield_test.go
File metadata and controls
42 lines (38 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package gforms
import (
"net/http"
"net/url"
"strings"
"testing"
)
type testTextObject struct {
Name string `gforms:"name"`
}
func TestTextField(t *testing.T) {
Form := DefineForm(NewFields(
NewTextField("name", nil),
))
data := url.Values{"name": {"bluele"}}
req, _ := http.NewRequest("POST", "/", strings.NewReader(data.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
form := Form(req)
if form.IsValid() {
v, ok := form.CleanedData["name"]
if !ok {
t.Error(`"name" is required.`)
return
}
_, ok = v.(string)
if !ok {
t.Error(`"name" should be string type.`)
return
}
obj := new(testTextObject)
form.MapTo(obj)
if obj.Name == "" {
t.Error(`"obj.Name" should not be empty string.`)
}
} else {
t.Error("validation error.")
}
}