-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
ctx_params_test.go
57 lines (47 loc) · 1.68 KB
/
ctx_params_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
package fuego_test
import (
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/stretchr/testify/require"
"github.com/go-fuego/fuego"
"github.com/go-fuego/fuego/option"
"github.com/go-fuego/fuego/param"
)
func TestParam(t *testing.T) {
t.Run("Query params default values", func(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/test", func(c fuego.ContextNoBody) (string, error) {
name := c.QueryParam("name")
age := c.QueryParamInt("age")
isok := c.QueryParamBool("is_ok")
return name + strconv.Itoa(age) + strconv.FormatBool(isok), nil
},
option.Query("name", "Name", param.Required(), param.Default("hey"), param.Example("example1", "you")),
option.QueryInt("age", "Age", param.Nullable(), param.Default(18), param.Example("example1", 1)),
option.QueryBool("is_ok", "Is OK?", param.Default(true), param.Example("example1", true)),
)
t.Run("Default should correctly set parameter in controller", func(t *testing.T) {
r := httptest.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, "hey18true", w.Body.String())
})
})
t.Run("Should enforce Required", func(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/test", func(c fuego.ContextNoBody) (string, error) {
name := c.QueryParam("name")
return name, nil
},
option.Query("name", "Name", param.Required(), param.Example("example1", "you")),
)
r := httptest.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, http.StatusBadRequest, w.Code)
require.Contains(t, w.Body.String(), "name is a required query param")
})
}