forked from go-macaron/binding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
executable file
·127 lines (110 loc) · 4.1 KB
/
common_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
// Copyright 2014 martini-contrib/binding Authors
// Copyright 2014 Unknwon
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package binding
import (
"mime/multipart"
"github.com/Unknwon/macaron"
)
// These types are mostly contrived examples, but they're used
// across many test cases. The idea is to cover all the scenarios
// that this binding package might encounter in actual use.
type (
// For basic test cases with a required field
Post struct {
Title string `form:"title" json:"title" binding:"Required"`
Content string `form:"content" json:"content"`
}
// To be used as a nested struct (with a required field)
Person struct {
Name string `form:"name" json:"name" binding:"Required"`
Email string `form:"email" json:"email"`
}
// For advanced test cases: multiple values, embedded
// and nested structs, an ignored field, and single
// and multiple file uploads
BlogPost struct {
Post
Id int `binding:"Required"` // JSON not specified here for test coverage
Ignored string `form:"-" json:"-"`
Ratings []int `form:"rating" json:"ratings"`
Author Person `json:"author"`
Coauthor *Person `json:"coauthor"`
HeaderImage *multipart.FileHeader
Pictures []*multipart.FileHeader `form:"picture"`
unexported string `form:"unexported"`
}
EmbedPerson struct {
*Person
}
SadForm struct {
AlphaDash string `form:"AlphaDash" binding:"AlphaDash"`
AlphaDashDot string `form:"AlphaDashDot" binding:"AlphaDashDot"`
Size string `form:"Size" binding:"Size(1)"`
SizeSlice []string `form:"SizeSlice" binding:"Size(1)"`
MinSize string `form:"MinSize" binding:"MinSize(5)"`
MinSizeSlice []string `form:"MinSizeSlice" binding:"MinSize(5)"`
MaxSize string `form:"MaxSize" binding:"MaxSize(1)"`
MaxSizeSlice []string `form:"MaxSizeSlice" binding:"MaxSize(1)"`
Range int `form:"Range" binding:"Range(1,2)"`
RangeInvalid int `form:"RangeInvalid" binding:"Range(1)"`
Email string `binding:"Email"`
Url string `form:"Url" binding:"Url"`
UrlEmpty string `form:"UrlEmpty" binding:"Url"`
In string `form:"In" binding:"Default(0);In(1,2,3)"`
InInvalid string `form:"InInvalid" binding:"In(1,2,3)"`
NotIn string `form:"NotIn" binding:"NotIn(1,2,3)"`
Include string `form:"Include" binding:"Include(a)"`
Exclude string `form:"Exclude" binding:"Exclude(a)"`
Empty string `binding:"OmitEmpty"`
}
Group struct {
Name string `json:"name" binding:"Required"`
People []Person `json:"people" binding:"MinSize(1)"`
}
CustomErrorHandle struct {
Rule `binding:"CustomRule"`
}
// The common function signature of the handlers going under test.
handlerFunc func(interface{}, ...interface{}) macaron.Handler
// Used for testing mapping an interface to the context
// If used (withInterface = true in the testCases), a modeler
// should be mapped to the context as well as BlogPost, meaning
// you can receive a modeler in your application instead of a
// concrete BlogPost.
modeler interface {
Model() string
}
)
func (p Post) Validate(ctx *macaron.Context, errs Errors) Errors {
if len(p.Title) < 10 {
errs = append(errs, Error{
FieldNames: []string{"title"},
Classification: "LengthError",
Message: "Life is too short",
})
}
return errs
}
func (p Post) Model() string {
return p.Title
}
func (g Group) Model() string {
return g.Name
}
func (_ CustomErrorHandle) Error(_ *macaron.Context, _ Errors) {}
const (
testRoute = "/test"
formContentType = "application/x-www-form-urlencoded"
)