-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (62 loc) · 1.25 KB
/
main.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
//nolint:all
package main
import (
"fmt"
"io"
"net/http"
"strings"
"github.com/logica0419/isugata"
)
type user struct {
ID int `json:"id"`
Name string `json:"name"`
}
func main() {
body := `
[
{
"id": 1,
"name": "test1"
},
{
"id": 2,
"name": "test2"
}
]
`
res := &http.Response{
Status: "200 OK",
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(strings.NewReader(body)),
}
err := isugata.Validate(res,
// status code validation
isugata.WithStatusCode(http.StatusOK),
// content type validation
isugata.WithContentType("application/json"),
// JSON array body validation
isugata.WithJSONArrayValidation(
// length validation
isugata.JSONArrayLengthEquals[user](2),
// order validation
isugata.JSONArrayValidateOrder(
func(u user) int { return u.ID },
isugata.Asc,
),
// element validation
isugata.JSONArrayValidateEach(
// validation with original ValidateOpt
func(body user) error {
if body.Name != fmt.Sprintf("test%d", body.ID) {
return fmt.Errorf("expected: test%d, actual: %s", body.ID, body.Name)
}
return nil
},
),
),
)
if err != nil {
panic(err)
}
}