-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifier_test.go
192 lines (150 loc) · 5.32 KB
/
verifier_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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package copper
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInvalidSpec(t *testing.T) {
f, err := os.ReadFile("testdata/invalid-spec.yaml")
require.NoError(t, err)
_, err = NewVerifier(f)
require.Error(t, err)
}
func TestCurrentErrors(t *testing.T) {
f, err := os.ReadFile("testdata/thing-spec.yaml")
require.NoError(t, err)
v, err := NewVerifier(f)
require.NoError(t, err)
errs := v.CurrentErrors()
assert.NotEmpty(t, errs)
}
func TestCurrentError(t *testing.T) {
f, err := os.ReadFile("testdata/delete-spec.yaml")
require.NoError(t, err)
v, err := NewVerifier(f)
require.NoError(t, err)
t.Run("errors can be joined", func(t *testing.T) {
assert.ErrorIs(t, v.CurrentError(), ErrNotChecked)
})
t.Run("errors can be nil", func(t *testing.T) {
req := httptest.NewRequest(http.MethodDelete, "/thing/19", nil)
v.Record(&http.Response{StatusCode: 204, Request: req})
assert.NoError(t, v.CurrentError())
})
}
func TestWithInternalServerErrors(t *testing.T) {
f, err := os.ReadFile("testdata/server-error-spec.yaml")
require.NoError(t, err)
t.Run("unchecked 500 returns error when including server errors", func(t *testing.T) {
t.Parallel()
v, err := NewVerifier(f, WithInternalServerErrors())
require.NoError(t, err)
assert.ErrorIs(t, v.CurrentError(), ErrNotChecked)
})
t.Run("unchecked 500 is fine", func(t *testing.T) {
t.Parallel()
v, err := NewVerifier(f)
require.NoError(t, err)
assert.NoError(t, v.CurrentError())
})
t.Run("checked 500 is fine", func(t *testing.T) {
t.Parallel()
v, err := NewVerifier(f, WithInternalServerErrors())
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, "/fault", nil)
v.Record(&http.Response{StatusCode: 500, Request: req})
assert.NoError(t, v.CurrentError())
})
}
func TestReset(t *testing.T) {
f, err := os.ReadFile("testdata/delete-spec.yaml")
require.NoError(t, err)
v, err := NewVerifier(f)
require.NoError(t, err)
req := httptest.NewRequest(http.MethodDelete, "/thing/10", nil)
v.Record(&http.Response{
StatusCode: 404,
Request: req,
})
require.Error(t, v.CurrentError())
// Now reset and see that it is possible to re-use the Verifier.
v.Reset()
v.Record(&http.Response{
StatusCode: 204,
Request: req,
})
v.Verify(t)
}
func TestBinaryBodies(t *testing.T) {
videoSpec, err := os.ReadFile("testdata/video-spec.yaml")
require.NoError(t, err)
t.Run("binary body formats can be processed", func(t *testing.T) {
v, err := NewVerifier(videoSpec)
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, "/video", nil)
v.Record(&http.Response{
StatusCode: 200,
Request: req,
Header: http.Header{"Content-Type": []string{"video/mp4"}},
Body: io.NopCloser(bytes.NewReader([]byte{0x01, 0x88})),
})
assert.NoError(t, v.CurrentError())
})
}
func TestWithRequestValidation(t *testing.T) {
bodySpec, err := os.ReadFile("testdata/request-body-spec.yaml")
require.NoError(t, err)
queryParamSpec, err := os.ReadFile("testdata/query-param-spec.yaml")
require.NoError(t, err)
t.Run("invalid body fails validation when checked", func(t *testing.T) {
v, err := NewVerifier(bodySpec, WithRequestValidation())
require.NoError(t, err)
req := httptest.NewRequest(http.MethodPost, "/req", strings.NewReader(`{"borken": "yes"}`))
req.Header.Set("Content-Type", "application/json")
v.Record(&http.Response{StatusCode: 204, Request: req})
assert.ErrorIs(t, v.CurrentError(), ErrRequestInvalid)
})
t.Run("invalid body succeeds when not checked", func(t *testing.T) {
v, err := NewVerifier(bodySpec)
require.NoError(t, err)
req := httptest.NewRequest(http.MethodPost, "/req", strings.NewReader(`{"borken": "yes"}`))
req.Header.Set("Content-Type", "application/json")
v.Record(&http.Response{StatusCode: 204, Request: req})
assert.NoError(t, v.CurrentError())
})
t.Run("invalid query param succeeds when not checked", func(t *testing.T) {
v, err := NewVerifier(queryParamSpec)
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, "/req?id=1", nil)
req.Header.Set("Content-Type", "application/json")
v.Record(&http.Response{StatusCode: 204, Request: req})
assert.NoError(t, v.CurrentError())
})
t.Run("invalid query param fails validation when checked", func(t *testing.T) {
v, err := NewVerifier(queryParamSpec, WithRequestValidation())
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, "/req?id=1", nil)
req.Header.Set("Content-Type", "application/json")
v.Record(&http.Response{StatusCode: 204, Request: req})
assert.ErrorIs(t, v.CurrentError(), ErrRequestInvalid)
})
t.Run("disabling full coverage validation allows untested endpoints", func(t *testing.T) {
v, err := NewVerifier(queryParamSpec, WithoutFullCoverage())
require.NoError(t, err)
assert.NoError(t, v.CurrentError())
})
t.Run("disabling full coverage validation does not allow undocumented requests", func(t *testing.T) {
v, err := NewVerifier(queryParamSpec, WithoutFullCoverage())
req := httptest.NewRequest(http.MethodGet, "/some-other-path", nil)
req.Header.Set("Content-Type", "application/json")
v.Record(&http.Response{StatusCode: 204, Request: req})
require.NoError(t, err)
assert.ErrorIs(t, v.CurrentError(), ErrNotPartOfSpec)
})
}