-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathgenerics_test.go
More file actions
176 lines (138 loc) · 5.87 KB
/
Copy pathgenerics_test.go
File metadata and controls
176 lines (138 loc) · 5.87 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package fuego_test
import (
"net/http/httptest"
"strings"
"testing"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
"github.com/go-fuego/fuego"
)
type GenericInput[T any] struct {
Thing string `json:"thing"`
Data T `json:"data"`
}
type GenericResponse[Res any] struct {
StatusCode int `json:"statusCode"`
Result Res `json:"result"`
Message string `json:"message"`
}
type User struct {
ID int `json:"id"`
Name string `json:"name" validate:"required,min=1,max=100" example:"Napoleon"`
}
func TestGenericReturnType(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", func(c fuego.ContextWithBody[GenericInput[User]]) (*GenericResponse[User], error) {
body, err := c.Body()
if err != nil {
return nil, err
}
return &GenericResponse[User]{
StatusCode: 200,
Result: User{ID: 1, Name: body.Data.Name},
Message: "success",
}, nil
})
// Request OpenAPI
t.Log(route.Operation.RequestBody)
require.NotNil(t, route.Operation.RequestBody.Value.Content["*/*"])
requestType := route.Operation.RequestBody.Value.Content["*/*"].Schema.Value
require.Equal(t, &openapi3.Types{"object"}, requestType.Type)
require.Equal(t, &openapi3.Types{"string"}, requestType.Properties["thing"].Value.Type)
// "data" is a User struct, now a $ref to a component schema
require.Equal(t, "#/components/schemas/User", requestType.Properties["data"].Ref)
userSchema := s.OpenAPI.Description().Components.Schemas["User"].Value
require.Equal(t, &openapi3.Types{"object"}, userSchema.Type)
require.Equal(t, &openapi3.Types{"integer"}, userSchema.Properties["id"].Value.Type)
// Response OpenAPI
responseType := route.Operation.Responses.Value("200").Value.Content["application/json"].Schema.Value
require.Equal(t, &openapi3.Types{"integer"}, responseType.Properties["statusCode"].Value.Type)
// "result" is a User struct, now a $ref to a component schema
require.Equal(t, "#/components/schemas/User", responseType.Properties["result"].Ref)
require.Equal(t, &openapi3.Types{"object"}, userSchema.Type)
require.Equal(t, &openapi3.Types{"integer"}, userSchema.Properties["id"].Value.Type)
require.Equal(t, &openapi3.Types{"string"}, userSchema.Properties["name"].Value.Type)
// Behavior at runtime
t.Run("Happy path", func(t *testing.T) {
req := httptest.NewRequest("GET", "/test", strings.NewReader(`{"thing":"user","data":{"id":1,"name":"Napoleon"}}`))
req.Header.Set("Content-Type", "application/json")
res := httptest.NewRecorder()
s.Mux.ServeHTTP(res, req)
require.Equal(t, 200, res.Code)
response := res.Body.String()
require.JSONEq(t, `{"statusCode":200,"result":{"id":1,"name":"Napoleon"},"message":"success"}`, response)
})
t.Run("Generic request still support nested validation tags", func(t *testing.T) {
req := httptest.NewRequest("GET", "/test", nil)
req.Header.Set("Content-Type", "application/json")
res := httptest.NewRecorder()
s.Mux.ServeHTTP(res, req)
require.Equal(t, 400, res.Code)
response := res.Body.String()
require.JSONEq(t, `{"title":"Validation Error","detail":"Name is required","errors":[{"more":{"field":"Name","nsField":"GenericInput[github.com/go-fuego/fuego_test.User].Data.Name","param":"","tag":"required","value":""},"name":"GenericInput[github.com/go-fuego/fuego_test.User].Data.Name","reason":"Key: 'GenericInput[github.com/go-fuego/fuego_test.User].Data.Name' Error:Field validation for 'Name' failed on the 'required' tag"}],"status":400}`, response)
})
}
func TestSlices(t *testing.T) {
type myListItem struct {
ID int `json:"id"`
Name string `json:"name"`
}
s := fuego.NewServer()
fuego.Get(s, "/ints", func(c fuego.ContextWithBody[[]int]) ([]int, error) { return c.Body() })
fuego.Get(s, "/strings", func(c fuego.ContextWithBody[[]string]) ([]string, error) { return c.Body() })
fuego.Get(s, "/structs", func(c fuego.ContextWithBody[[]myListItem]) ([]myListItem, error) { return c.Body() })
t.Run("nil body", func(t *testing.T) {
r := httptest.NewRequest("GET", "/ints", nil)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 200, w.Code)
response := w.Body.String()
require.JSONEq(t, "null", response)
})
t.Run("empty body", func(t *testing.T) {
r := httptest.NewRequest("GET", "/ints", strings.NewReader(``))
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 200, w.Code)
response := w.Body.String()
require.JSONEq(t, "null", response)
})
t.Run("empty slice", func(t *testing.T) {
r := httptest.NewRequest("GET", "/ints", strings.NewReader(`[]`))
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 200, w.Code)
response := w.Body.String()
require.JSONEq(t, `[]`, response)
})
t.Run("slice of ints", func(t *testing.T) {
r := httptest.NewRequest("GET", "/ints", strings.NewReader(`[1,2,3]`))
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 200, w.Code)
response := w.Body.String()
require.JSONEq(t, `[1,2,3]`, response)
})
t.Run("slice of strings", func(t *testing.T) {
r := httptest.NewRequest("GET", "/strings", strings.NewReader(`["hello","world"]`))
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 200, w.Code)
response := w.Body.String()
require.JSONEq(t, `["hello","world"]`, response)
})
t.Run("slice of structs", func(t *testing.T) {
r := httptest.NewRequest("GET", "/structs", strings.NewReader(`[{"id":1,"name":"Napoleon"}]`))
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 200, w.Code)
response := w.Body.String()
require.JSONEq(t, `[{"id":1,"name":"Napoleon"}]`, response)
})
}