forked from danielgtaylor/huma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_test.go
229 lines (199 loc) · 7.06 KB
/
patch_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package huma
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type SaleModel struct {
Location string `json:"location"`
Count int `json:"count"`
}
func (m SaleModel) String() string {
return fmt.Sprintf("%s%d", m.Location, m.Count)
}
type ThingModel struct {
ID string `json:"id"`
Price float32 `json:"price,omitempty"`
Sales []SaleModel `json:"sales,omitempty"`
Tags []string `json:"tags,omitempty"`
}
func (m ThingModel) ETag() string {
return fmt.Sprintf("%s%v%v%v", m.ID, m.Price, m.Sales, m.Tags)
}
type ThingIDParam struct {
ThingID string `path:"thing-id"`
}
func TestPatch(t *testing.T) {
db := map[string]*ThingModel{
"test": {
ID: "test",
Price: 1.00,
Sales: []SaleModel{
{Location: "US", Count: 123},
{Location: "EU", Count: 456},
},
},
}
app := newTestRouter()
things := app.Resource("/things/{thing-id}")
// Create the necessary GET/PUT
things.Get("get-thing", "docs",
NewResponse(http.StatusOK, "OK").Headers("ETag").Model(&ThingModel{}),
NewResponse(http.StatusNotFound, "Not Found"),
NewResponse(http.StatusPreconditionFailed, "Failed"),
).Run(func(ctx Context, input struct {
ThingIDParam
}) {
t := db[input.ThingID]
if t == nil {
ctx.WriteError(http.StatusNotFound, "Not found")
return
}
ctx.Header().Set("ETag", t.ETag())
ctx.WriteModel(http.StatusOK, t)
})
things.Put("put-thing", "docs",
NewResponse(http.StatusOK, "OK").Headers("ETag").Model(&ThingModel{}),
NewResponse(http.StatusPreconditionFailed, "Precondition failed").Model(&ErrorModel{}),
).Run(func(ctx Context, input struct {
ThingIDParam
Body ThingModel
IfMatch []string `header:"If-Match" doc:"Succeeds if the server's resource matches one of the passed values."`
}) {
if len(input.IfMatch) > 0 {
found := false
if existing := db[input.ThingID]; existing != nil {
for _, possible := range input.IfMatch {
if possible == existing.ETag() {
found = true
break
}
}
}
if !found {
ctx.WriteError(http.StatusPreconditionFailed, "ETag does not match")
return
}
} else {
// Since the GET returns an ETag, and the auto-patch feature should always
// use it when available, we can fail the test if we ever get here.
t.Fatal("No If-Match header set during PUT")
}
db[input.ThingID] = &input.Body
ctx.Header().Set("ETag", db[input.ThingID].ETag())
ctx.WriteModel(http.StatusOK, db[input.ThingID])
})
// Merge Patch Test
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPatch, "/things/test", strings.NewReader(`{"price": 1.23}`))
req.Header.Set("Content-Type", "application/merge-patch+json")
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
assert.Equal(t, "test1.23[US123 EU456][]", w.Result().Header.Get("ETag"))
// Same change results in a 304 (patches are idempotent)
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPatch, "/things/test", strings.NewReader(`{"price": 1.23}`))
req.Header.Set("Content-Type", "application/merge-patch+json")
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotModified, w.Code, w.Body.String())
// New change but with wrong manual ETag, should fail!
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPatch, "/things/test", strings.NewReader(`{"price": 4.56}`))
req.Header.Set("Content-Type", "application/merge-patch+json")
req.Header.Set("If-Match", "abc123")
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusPreconditionFailed, w.Code, w.Body.String())
// Correct manual ETag should pass!
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPatch, "/things/test", strings.NewReader(`{"price": 4.56}`))
req.Header.Set("Content-Type", "application/merge-patch+json")
req.Header.Set("If-Match", "test1.23[US123 EU456][]")
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
assert.Equal(t, "test4.56[US123 EU456][]", w.Result().Header.Get("ETag"))
// Merge Patch: invalid
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPatch, "/things/test", strings.NewReader(`{`))
req.Header.Set("Content-Type", "application/merge-patch+json")
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code, w.Body.String())
// JSON Patch Test
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPatch, "/things/test", strings.NewReader(`[
{"op": "add", "path": "/tags", "value": ["b"]},
{"op": "add", "path": "/tags/0", "value": "a"}
]`))
req.Header.Set("Content-Type", "application/json-patch+json")
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
assert.Equal(t, "test4.56[US123 EU456][a b]", w.Result().Header.Get("ETag"))
// JSON Patch: bad JSON
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPatch, "/things/test", strings.NewReader(`[`))
req.Header.Set("Content-Type", "application/json-patch+json")
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code, w.Body.String())
// JSON Patch: invalid patch
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPatch, "/things/test", strings.NewReader(`[{"op": "unsupported"}]`))
req.Header.Set("Content-Type", "application/json-patch+json")
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code, w.Body.String())
// Bad content type
w = httptest.NewRecorder()
req, _ = http.NewRequest(http.MethodPatch, "/things/test", strings.NewReader(`{}`))
req.Header.Set("Content-Type", "application/unsupported-content-type")
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusUnsupportedMediaType, w.Code, w.Body.String())
}
func TestPatchPutNoBody(t *testing.T) {
app := newTestRouter()
things := app.Resource("/things/{thing-id}")
things.Get("get-thing", "docs",
NewResponse(http.StatusOK, "OK").Model(&ThingModel{}),
).Run(func(ctx Context, input struct {
ThingIDParam
}) {
ctx.WriteModel(http.StatusOK, &ThingModel{})
})
things.Put("put-thing", "docs",
NewResponse(http.StatusNoContent, "No Content"),
).Run(func(ctx Context, input struct {
ThingIDParam
// Note: no body!
}) {
ctx.WriteHeader(http.StatusNoContent)
})
// There should be no generated PATCH since there is nothing to
// write in the PUT!
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPatch, "/things/test", nil)
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusMethodNotAllowed, w.Code, w.Body.String())
}
func TestDeprecatedPatch(t *testing.T) {
app := newTestRouter()
things := app.Resource("/things/{thing-id}")
things.Get("get-thing", "docs",
NewResponse(http.StatusOK, "OK").Model(&ThingModel{}),
).Run(func(ctx Context, input struct {
ThingIDParam
}) {
ctx.WriteModel(http.StatusOK, &ThingModel{})
})
put := things.Put("put-thing", "docs",
NewResponse(http.StatusOK, "OK").Model(&ThingModel{}),
)
put.Deprecated()
put.Run(func(ctx Context, input struct {
ThingIDParam
Body ThingModel
}) {
ctx.WriteModel(http.StatusOK, &ThingModel{})
})
app.setupDocs()
assert.Contains(t, app.OpenAPI().Search("paths", "/things/{thing-id}", "patch").String(), "deprecated")
}