Debian Codegraph @

common_test.go

raw symbols SCIP
1 // Copyright 2014 Martini Authors
2 // Copyright 2014 The Macaron Authors
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License"): you may
5 // not use this file except in compliance with the License. You may obtain
6 // a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 // License for the specific language governing permissions and limitations
14 // under the License.
15
16 package binding
17
18 import (
19 "mime/multipart"
20 "net/http"
21 )
22
23 // These types are mostly contrived examples, but they're used
24 // across many test cases. The idea is to cover all the scenarios
25 // that this binding package might encounter in actual use.
26 type (
27 // For basic test cases with a required field
28 Post struct {
29 Title string `form:"title" json:"title" binding:"Required"`
30 Content string `form:"content" json:"content"`
31 }
32
33 // To be used as a nested struct (with a required field)
34 Person struct {
35 Name string `form:"name" json:"name" binding:"Required"`
36 Email string `form:"email" json:"email"`
37 }
38
39 // For advanced test cases: multiple values, embedded
40 // and nested structs, an ignored field, and single
41 // and multiple file uploads
42 //
43 //nolint:all
44 BlogPost struct {
45 Post
46 Id int `binding:"Required"` // JSON not specified here for test coverage
47 Ignored string `form:"-" json:"-"`
48 Ratings []int `form:"rating" json:"ratings"`
49 Author Person `json:"author"`
50 Coauthor *Person `json:"coauthor"`
51 HeaderImage *multipart.FileHeader
52 Pictures []*multipart.FileHeader `form:"picture"`
53 unexported string `form:"unexported"` //nolint:all
54 }
55
56 EmbedPerson struct {
57 *Person
58 }
59
60 SadForm struct {
61 AlphaDash string `form:"AlphaDash" binding:"AlphaDash"`
62 AlphaDashDot string `form:"AlphaDashDot" binding:"AlphaDashDot"`
63 Size string `form:"Size" binding:"Size(1)"`
64 SizeSlice []string `form:"SizeSlice" binding:"Size(1)"`
65 MinSize string `form:"MinSize" binding:"MinSize(5)"`
66 MinSizeSlice []string `form:"MinSizeSlice" binding:"MinSize(5)"`
67 MaxSize string `form:"MaxSize" binding:"MaxSize(1)"`
68 MaxSizeSlice []string `form:"MaxSizeSlice" binding:"MaxSize(1)"`
69 Range int `form:"Range" binding:"Range(1,2)"`
70 RangeInvalid int `form:"RangeInvalid" binding:"Range(1)"`
71 Email string `binding:"Email"`
72 URL string `form:"Url" binding:"Url"`
73 URLEmpty string `form:"UrlEmpty" binding:"Url"`
74 In string `form:"In" binding:"Default(0);In(1,2,3)"`
75 InInvalid string `form:"InInvalid" binding:"In(1,2,3)"`
76 NotIn string `form:"NotIn" binding:"NotIn(1,2,3)"`
77 Include string `form:"Include" binding:"Include(a)"`
78 Exclude string `form:"Exclude" binding:"Exclude(a)"`
79 Empty string
80 }
81
82 Group struct {
83 Name string `json:"name" binding:"Required"`
84 People []Person `json:"people" binding:"MinSize(1)"`
85 }
86
87 PointerForm struct {
88 ID int `form:"Id" binding:"Id"`
89 IDPointer *int `form:"IdPointer" binding:"IdPointer"`
90 URL string `form:"Url" binding:"Url"`
91 URLPointer *string `form:"UrlPointer" binding:"Url"`
92 AlphaDash string `form:"AlphaDash" binding:"AlphaDash"`
93 AlphaDashPointer *string `form:"AlphaDashPointer" binding:"AlphaDash"`
94 }
95
96 CustomErrorHandle struct {
97 Rule `binding:"CustomRule"`
98 }
99
100 TrimSpaceForm struct {
101 Title string `form:"title" preprocess:"TrimSpace"`
102 Description string `form:"description"`
103 Slug string `form:"slug" preprocess:"TrimSpace" binding:"Required;AlphaDash"`
104 }
105
106 // The common function signature of the handlers going under test.
107 handlerFunc func(req *http.Request, obj any) Errors
108 )
109
110 func (p Post) Validate(_ *http.Request, errs Errors) Errors {
111 if len(p.Title) < 10 {
112 errs = append(errs, Error{
113 FieldNames: []string{"title"},
114 Classification: "LengthError",
115 Message: "Life is too short",
116 })
117 }
118 return errs
119 }
120
121 const (
122 testRoute = "/test"
123 formContentType = "application/x-www-form-urlencoded"
124 )