Repository navigation
Expand file tree
/
Copy pathrequest_precognition_test.go
More file actions
212 lines (196 loc) · 6.19 KB
/
Copy pathrequest_precognition_test.go
File metadata and controls
212 lines (196 loc) · 6.19 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package httpsuite
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestParseRequestPrecognition(t *testing.T) {
ClearValidator()
t.Cleanup(ClearValidator)
invalid := stubValidator{problem: NewProblemDetails(http.StatusBadRequest, "", "Validation Error", "name is required")}
tests := []struct {
name string
body string
path string
header bool
opts *ParseOptions
wantErr error
wantStatus int
wantResponse bool
wantVary bool
}{
{
name: "valid precognitive request",
body: `{"name":"Test"}`,
path: "/test/1",
header: true,
opts: &ParseOptions{Precognition: true},
wantErr: ErrPrecognitionHandled,
wantStatus: http.StatusNoContent,
wantVary: true,
},
{
name: "validation failure returns 422",
body: `{"name":""}`,
path: "/test/1",
header: true,
opts: &ParseOptions{Precognition: true, Validator: invalid},
wantErr: errValidationFailed,
wantStatus: http.StatusUnprocessableEntity,
wantVary: true,
},
{
name: "malformed json stays a 400 parse error",
body: `{bad`,
path: "/test/1",
header: true,
opts: &ParseOptions{Precognition: true},
wantStatus: http.StatusBadRequest,
wantVary: true,
},
{
name: "oversized body stays 413",
body: `{"name":"` + strings.Repeat("a", 64) + `"}`,
path: "/test/1",
header: true,
opts: &ParseOptions{Precognition: true, MaxBodyBytes: 8},
wantStatus: http.StatusRequestEntityTooLarge,
wantVary: true,
},
{
name: "invalid path param stays 400",
body: `{"name":"Test"}`,
path: "/test/nope",
header: true,
opts: &ParseOptions{Precognition: true},
wantStatus: http.StatusBadRequest,
wantVary: true,
},
{
name: "ordinary request on opted-in endpoint",
body: `{"name":"Test"}`,
path: "/test/1",
opts: &ParseOptions{Precognition: true},
wantResponse: true,
wantVary: true,
},
{
name: "header ignored when not opted in",
body: `{"name":"Test"}`,
path: "/test/1",
header: true,
opts: nil,
wantResponse: true,
},
{
name: "validation status untouched when not opted in",
body: `{"name":""}`,
path: "/test/1",
header: true,
opts: &ParseOptions{Validator: invalid},
wantErr: errValidationFailed,
wantStatus: http.StatusBadRequest,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, tt.path, strings.NewReader(tt.body))
if tt.header {
r.Header.Set(PrecognitionHeader, "true")
}
w := httptest.NewRecorder()
got, err := ParseRequest[*testRequest](w, r, testParamExtractor, tt.opts, "id")
if tt.wantResponse {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got == nil || got.ID != 1 || got.Name != "Test" {
t.Fatalf("unexpected request: %#v", got)
}
if w.Body.Len() != 0 {
t.Fatalf("expected nothing written, got %q", w.Body.String())
}
} else {
if err == nil {
t.Fatal("expected error")
}
if got != nil {
t.Fatalf("expected zero request, got %#v", got)
}
if tt.wantErr != nil && !errors.Is(err, tt.wantErr) {
t.Fatalf("expected %v, got %v", tt.wantErr, err)
}
if w.Code != tt.wantStatus {
t.Fatalf("expected status %d, got %d", tt.wantStatus, w.Code)
}
}
if IsPrecognitionHandled(err) != errors.Is(tt.wantErr, ErrPrecognitionHandled) {
t.Fatalf("IsPrecognitionHandled mismatch for %v", err)
}
if vary := w.Header().Get("Vary") == PrecognitionHeader; vary != tt.wantVary {
t.Fatalf("Vary header = %q, want precognition vary %v", w.Header().Get("Vary"), tt.wantVary)
}
})
}
}
func TestParseRequestPrecognitionResponses(t *testing.T) {
ClearValidator()
t.Cleanup(ClearValidator)
t.Run("success has empty body and success header", func(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/test/1", strings.NewReader(`{"name":"Test"}`))
r.Header.Set(PrecognitionHeader, "true")
w := httptest.NewRecorder()
_, err := ParseRequest[*testRequest](w, r, testParamExtractor, &ParseOptions{Precognition: true}, "id")
if !IsPrecognitionHandled(err) {
t.Fatalf("expected ErrPrecognitionHandled, got %v", err)
}
if w.Body.Len() != 0 {
t.Fatalf("expected empty body, got %q", w.Body.String())
}
if w.Header().Get(PrecognitionSuccessHeader) != "true" {
t.Fatalf("expected %s header", PrecognitionSuccessHeader)
}
})
t.Run("failure keeps problem body and extensions", func(t *testing.T) {
problem := NewProblemDetails(http.StatusBadRequest, "", "Validation Error", "invalid")
problem.Extensions = map[string]interface{}{
"errors": []ValidationErrorDetail{{Field: "name", Message: "required"}},
}
r := httptest.NewRequest(http.MethodPost, "/test/1", strings.NewReader(`{"name":""}`))
r.Header.Set(PrecognitionHeader, "true")
w := httptest.NewRecorder()
_, _ = ParseRequest[*testRequest](w, r, testParamExtractor, &ParseOptions{
Precognition: true,
Validator: stubValidator{problem: problem},
}, "id")
var body map[string]any
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("decode: %v", err)
}
if body["status"] != float64(http.StatusUnprocessableEntity) {
t.Fatalf("expected status 422 in body, got %v", body["status"])
}
if _, ok := body["errors"]; !ok {
t.Fatalf("expected errors extension, got %v", body)
}
if problem.Status != http.StatusBadRequest {
t.Fatal("validator problem must not be mutated")
}
})
}
func TestIsPrecognitionRequest(t *testing.T) {
t.Parallel()
for value, want := range map[string]bool{"true": true, "TRUE": true, " true ": true, "false": false, "": false, "1": false} {
r := httptest.NewRequest(http.MethodPost, "/", nil)
r.Header.Set(PrecognitionHeader, value)
if got := IsPrecognitionRequest(r); got != want {
t.Errorf("IsPrecognitionRequest(%q) = %v, want %v", value, got, want)
}
}
if IsPrecognitionRequest(nil) {
t.Error("nil request must not be precognitive")
}
}