-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
validate_params_test.go
54 lines (45 loc) · 1.52 KB
/
validate_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
package fuego_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
"github.com/go-fuego/fuego"
"github.com/go-fuego/fuego/option"
"github.com/go-fuego/fuego/param"
)
func TestParamsValidation(t *testing.T) {
t.Run("Should enforce Required query parameter", func(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/test", dummyController,
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")
})
t.Run("Should enforce Required header", func(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/test", dummyController,
option.Header("foo", "header that is foo", param.Required()),
)
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(), "foo is a required header")
})
t.Run("Should enforce Required cookie", func(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/test", dummyController,
option.Cookie("bar", "cookie that is bar", param.Required()),
)
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(), "bar is a required cookie")
})
}