-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathadapter_test.go
More file actions
106 lines (96 loc) · 2.72 KB
/
Copy pathadapter_test.go
File metadata and controls
106 lines (96 loc) · 2.72 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
package crud
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestNewServeMuxAdapter(t *testing.T) {
t.Run("validates path parameters", func(t *testing.T) {
adapter := NewServeMuxAdapter()
router := NewRouter("title", "1.0", adapter)
err := router.Add(Spec{
Method: "GET",
Path: "/widgets/{id}",
Handler: func(w http.ResponseWriter, r *http.Request) {},
Validate: Validate{
Path: Object(map[string]Field{
"id": Number().Required().Max(10),
}),
},
})
if err != nil {
t.Fatal(err)
}
r := httptest.NewRequest("GET", "/widgets/11", nil)
w := httptest.NewRecorder()
adapter.Engine.ServeHTTP(w, r)
if w.Code != http.StatusBadRequest {
t.Errorf("expected status code %d, got %d", http.StatusBadRequest, w.Code)
}
if !strings.Contains(w.Body.String(), `"path validation failed for field id: maximum exceeded"`) {
t.Errorf("unexpected body %q", w.Body.String())
}
})
t.Run("strips values from the body", func(t *testing.T) {
adapter := NewServeMuxAdapter()
router := NewRouter("title", "1.0", adapter)
err := router.Add(Spec{
Method: "POST",
Path: "/widgets",
Handler: func(w http.ResponseWriter, r *http.Request) {
// reflect the body back for ease of testing
io.Copy(w, r.Body)
},
Validate: Validate{
Body: Object(map[string]Field{
"value": String().Required(),
}),
},
})
if err != nil {
t.Fatal(err)
}
r := httptest.NewRequest("POST", "/widgets", strings.NewReader(`{"value": "hello", "unexpected": 1}`))
w := httptest.NewRecorder()
adapter.Engine.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("expected status code %d, got %d", http.StatusOK, w.Code)
}
// unexpected property should be stripped
if w.Body.String() != `{"value":"hello"}` {
t.Errorf("unexpected body %q", w.Body.String())
}
})
t.Run("unexpected URL parameters are stripped", func(t *testing.T) {
adapter := NewServeMuxAdapter()
router := NewRouter("title", "1.0", adapter)
err := router.Add(Spec{
Method: "GET",
Path: "/widgets",
Handler: func(w http.ResponseWriter, r *http.Request) {
// reflect the parameters back for ease of testing
io.WriteString(w, r.URL.RawQuery)
},
Validate: Validate{
Query: Object(map[string]Field{
"limit": Number().Required(),
}),
},
})
if err != nil {
t.Fatal(err)
}
r := httptest.NewRequest("GET", "/widgets?limit=1&hello=world", nil)
w := httptest.NewRecorder()
adapter.Engine.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("expected status code %d, got %d", http.StatusOK, w.Code)
}
// unexpected property should be stripped
if w.Body.String() != `limit=1` {
t.Errorf("unexpected body %q", w.Body.String())
}
})
}