forked from ggicci/httpin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequired_test.go
More file actions
59 lines (54 loc) · 1.4 KB
/
required_test.go
File metadata and controls
59 lines (54 loc) · 1.4 KB
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
package httpin
import (
"errors"
"net/http"
"net/url"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestDirectiveRequired(t *testing.T) {
Convey("Required field is missing", t, func() {
r, _ := http.NewRequest("GET", "/", nil)
r.Form = url.Values{
"color": {"red"},
"is_soldout": {"true"},
"sort_by": {"id", "quantity"},
"sort_desc": {"0", "true"},
"page": {"1"},
"per_page": {"20"},
}
core, err := New(&ProductQuery{}) // struct pointer also works
So(err, ShouldBeNil)
got, err := core.Decode(r)
So(got, ShouldBeNil)
So(errors.Is(err, ErrMissingField), ShouldBeTrue)
var invalidField *InvalidFieldError
So(errors.As(err, &invalidField), ShouldBeTrue)
So(invalidField.Source, ShouldEqual, "required")
So(invalidField.Value, ShouldBeNil)
})
Convey("Non-required fields can be absent", t, func() {
r, _ := http.NewRequest("GET", "/", nil)
r.Form = url.Values{
"created_at": {"1991-11-10T08:00:00+08:00"},
"is_soldout": {"true"},
"page": {"1"},
"per_page": {"20"},
}
expected := &ProductQuery{
CreatedAt: time.Date(1991, 11, 10, 0, 0, 0, 0, time.UTC),
Color: "",
IsSoldout: true,
Pagination: Pagination{
Page: 1,
PerPage: 20,
},
}
core, err := New(ProductQuery{})
So(err, ShouldBeNil)
got, err := core.Decode(r)
So(err, ShouldBeNil)
So(got, ShouldResemble, expected)
})
}