-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgopi_test.go
More file actions
70 lines (57 loc) · 1.36 KB
/
gopi_test.go
File metadata and controls
70 lines (57 loc) · 1.36 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
60
61
62
63
64
65
66
67
68
69
70
package gopi_test
import (
"net/http"
"os"
"testing"
"time"
"github.com/lallenfrancisl/gopi"
)
func TestGopi(t *testing.T) {
api := gopi.New()
route := api.Route("/users")
route.
Description("This is the description").
Summary("This is the summary")
type Address struct {
State string
Country string
}
type CreateUser struct {
Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
}
type Time struct {
Hour int
Minutes int
Seconds int
}
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
CreatedAt Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Address Address `json:"address"`
}
type envelope map[string]any
route.Get().
Summary("List users").
Tags([]string{"api"}).
Response(http.StatusOK, envelope{"user": &User{}})
route.Post().
Summary("Create user").
Tags([]string{"api"}).
Body(&CreateUser{}).
Response(http.StatusOK, envelope{"user": &CreateUser{}}).
Response(http.StatusBadRequest, envelope{"error": ""}).
Response(http.StatusInternalServerError, map[string]string{})
js, err := api.MarshalJSONIndent("", " ")
if err != nil {
t.Fatal(err)
}
err = os.WriteFile("./build/schema.json", js, os.FileMode(os.O_TRUNC))
if err != nil {
t.Fatal(err)
}
}