-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
executable file
·245 lines (221 loc) · 6.24 KB
/
example_test.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package fpf_test
import (
"html/template"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"time"
"github.com/saracen/fpf"
)
func ExampleFormPopulationFilter_ExecuteTemplate() {
const tpl = `
<html>
<head>
<title>Your Information</title>
</head>
<body>
<form action="/" method="post">
<div class="form-group">
<label for="name">Name</label>
<input name="name" type="text" placeholder="John Smith" />
</div>
<div class="form-group">
<label>Do you like food? <input type="checkbox" name="food" /></label>
</div>
<div class="form-group">
<label for="month">Favourite Month</label>
<select id="month" name="month">
{{- range $index, $month := .Months }}
<option value="{{ $index }}">{{ $month }}</option>
{{- end }}
</select>
</div>
</form>
</body>
</html>`
// Parse template
t, err := template.New("info").Parse(tpl)
if err != nil {
log.Fatal(err)
}
// Create data we'll use with the template
var data struct {
Months []time.Month
}
for i := time.January; i <= time.December; i++ {
data.Months = append(data.Months, i)
}
// Set the values we want populated
values := url.Values{}
values.Set("name", "Arran Walker")
values.Set("food", "1")
values.Set("month", "3")
// Execute template with fpf
fp := fpf.New()
err = fp.ExecuteTemplate([]fpf.Form{{Values: values}}, os.Stdout, t, data)
if err != nil {
log.Fatal(err)
}
// Output:
//
// <html><head>
// <title>Your Information</title>
// </head>
// <body>
// <form action="/" method="post">
// <div class="form-group">
// <label for="name">Name</label>
// <input name="name" type="text" placeholder="John Smith" value="Arran Walker"/>
// </div>
//
// <div class="form-group">
// <label>Do you like food? <input type="checkbox" name="food" checked="checked"/></label>
// </div>
//
// <div class="form-group">
// <label for="month">Favourite Month</label>
// <select id="month" name="month">
// <option value="0">January</option>
// <option value="1">February</option>
// <option value="2">March</option>
// <option value="3" selected="selected">April</option>
// <option value="4">May</option>
// <option value="5">June</option>
// <option value="6">July</option>
// <option value="7">August</option>
// <option value="8">September</option>
// <option value="9">October</option>
// <option value="10">November</option>
// <option value="11">December</option>
// </select>
// </div>
// </form>
//
// </body></html>
}
func ExampleFormPopulationFilter_ExecuteTemplate_errorInsertion() {
const tpl = `
<html>
<head>
<title>{{ .Title }}</title>
</head>
<body>
<form action="/" method="post">
<div class="form-group">
<label for="username">Username</label>
<input name="username" type="text" />
</div>
<div class="form-group">
<div class="form-group-left">
<label for="password">Password</label>
<input name="password" type="password" />
</div>
<div class="form-group-right">
<label for="password-confirm">Confirm Password</label>
<input name="password-confirm" type="password" />
</div>
</div>
<div class="form-group">
<label>Opt In Newsletter <input type="checkbox" name="newsletter" /></label>
</div>
<div class="form-group">
<label>Opt In Spam <input type="checkbox" name="spam" /></label>
</div>
</form>
</body>
</html>`
// Parse template
t, err := template.New("register").Parse(tpl)
if err != nil {
log.Fatal(err)
}
// Create validation function
validate := func(register url.Values) (incidents []fpf.Incident) {
// validate username
if len(register.Get("username")) < 5 {
incidents = append(incidents, fpf.Incident{
[]string{"username"},
[]string{"Username needs to be 5 or more characters long."},
})
}
// validate password
if len(register.Get("password")) < 6 {
incidents = append(incidents, fpf.Incident{
[]string{"password", "password-confirm"},
[]string{"Password needs to be 6 or more characters long."},
})
} else if register.Get("password") != register.Get("password-confirm") {
incidents = append(incidents, fpf.Incident{
[]string{"password", "password-confirm"},
[]string{"Passwords do not match."},
})
}
return incidents
}
// Using httptest server for example purposes
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var incidents []fpf.Incident
if r.Method == "POST" {
err := r.ParseForm()
if err != nil {
return
}
// Validate posted form
if incidents = validate(r.PostForm); len(incidents) == 0 {
// Validated successful
// Add saving / additional logic
return
}
}
// Execute template with fpf
fp := fpf.New()
fp.ExecuteTemplate([]fpf.Form{{Values: r.PostForm, Incidents: incidents}}, os.Stdout, t, struct{ Title string }{"Registration Page"})
}))
defer ts.Close()
// Emulate browser client post
resp, err := http.PostForm(ts.URL, url.Values{
"username": {"sara"},
"password": {"password"},
"password-comfirm": {"password123"},
"spam": {"on"},
})
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
io.Copy(os.Stdout, resp.Body)
// Output:
// <html><head>
// <title>Registration Page</title>
// </head>
// <body>
// <form action="/" method="post">
// <div class="form-group">
// <label for="username">Username</label>
// <input name="username" type="text" value="sara" class="error"/><ul class="errors"><li>Username needs to be 5 or more characters long.</li></ul>
// </div>
//
// <div class="form-group">
// <div class="form-group-left">
// <label for="password">Password</label>
// <input name="password" type="password" class="error"/>
// </div>
// <div class="form-group-right">
// <label for="password-confirm">Confirm Password</label>
// <input name="password-confirm" type="password" class="error"/>
// </div>
// <ul class="errors"><li>Passwords do not match.</li></ul></div>
//
// <div class="form-group">
// <label>Opt In Newsletter <input type="checkbox" name="newsletter"/></label>
// </div>
// <div class="form-group">
// <label>Opt In Spam <input type="checkbox" name="spam" checked="checked"/></label>
// </div>
// </form>
//
// </body></html>
}