-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_example_test.go
127 lines (97 loc) · 2.93 KB
/
api_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
package web_test
import "github.com/ecnepsnai/web"
func ExampleAPI_GET() {
server := web.New("127.0.0.1:8080")
handle := func(request web.Request) (interface{}, *web.APIResponse, *web.Error) {
username := request.Parameters["username"]
return map[string]string{
"username": username,
}, nil, nil
}
server.API.GET("/users/user/:username", handle, web.HandleOptions{})
server.Start()
}
func ExampleAPI_HEAD() {
server := web.New("127.0.0.1:8080")
handle := func(request web.Request) (interface{}, *web.APIResponse, *web.Error) {
return nil, nil, nil
}
server.API.HEAD("/users/user/", handle, web.HandleOptions{})
server.Start()
}
func ExampleAPI_OPTIONS() {
server := web.New("127.0.0.1:8080")
handle := func(request web.Request) (interface{}, *web.APIResponse, *web.Error) {
return nil, nil, nil
}
server.API.OPTIONS("/users/user/", handle, web.HandleOptions{})
server.Start()
}
func ExampleAPI_POST() {
server := web.New("127.0.0.1:8080")
type userRequestType struct {
FirstName string `json:"first_name"`
}
handle := func(request web.Request) (interface{}, *web.APIResponse, *web.Error) {
username := request.Parameters["username"]
params := userRequestType{}
if err := request.DecodeJSON(¶ms); err != nil {
return nil, nil, err
}
return map[string]string{
"first_name": params.FirstName,
"username": username,
}, nil, nil
}
server.API.POST("/users/user/:username", handle, web.HandleOptions{})
server.Start()
}
func ExampleAPI_PUT() {
server := web.New("127.0.0.1:8080")
type userRequestType struct {
FirstName string `json:"first_name"`
}
handle := func(request web.Request) (interface{}, *web.APIResponse, *web.Error) {
username := request.Parameters["username"]
params := userRequestType{}
if err := request.DecodeJSON(¶ms); err != nil {
return nil, nil, err
}
return map[string]string{
"first_name": params.FirstName,
"username": username,
}, nil, nil
}
server.API.PUT("/users/user/:username", handle, web.HandleOptions{})
server.Start()
}
func ExampleAPI_PATCH() {
server := web.New("127.0.0.1:8080")
type userRequestType struct {
FirstName string `json:"first_name"`
}
handle := func(request web.Request) (interface{}, *web.APIResponse, *web.Error) {
username := request.Parameters["username"]
params := userRequestType{}
if err := request.DecodeJSON(¶ms); err != nil {
return nil, nil, err
}
return map[string]string{
"first_name": params.FirstName,
"username": username,
}, nil, nil
}
server.API.PATCH("/users/user/:username", handle, web.HandleOptions{})
server.Start()
}
func ExampleAPI_DELETE() {
server := web.New("127.0.0.1:8080")
handle := func(request web.Request) (interface{}, *web.APIResponse, *web.Error) {
username := request.Parameters["username"]
return map[string]string{
"username": username,
}, nil, nil
}
server.API.DELETE("/users/user/:username", handle, web.HandleOptions{})
server.Start()
}