|
8 | 8 | "net/http" |
9 | 9 | "net/http/httptest" |
10 | 10 | "reflect" |
| 11 | + "strings" |
11 | 12 | "testing" |
12 | 13 |
|
13 | 14 | "github.com/stretchr/testify/assert" |
@@ -155,3 +156,123 @@ func TestGet_NotFound(t *testing.T) { |
155 | 156 | assert.Error(t, err) |
156 | 157 | assert.Contains(t, err.Error(), "404") |
157 | 158 | } |
| 159 | + |
| 160 | +func TestKeyValidation(t *testing.T) { |
| 161 | + t.Parallel() |
| 162 | + |
| 163 | + tests := []struct { |
| 164 | + name string |
| 165 | + call func(Service) error |
| 166 | + }{ |
| 167 | + { |
| 168 | + name: "get rejects slash", |
| 169 | + call: func(svc Service) error { |
| 170 | + _, err := svc.Get(context.Background(), "test-uuid", "a/b") |
| 171 | + return err |
| 172 | + }, |
| 173 | + }, |
| 174 | + { |
| 175 | + name: "set rejects empty", |
| 176 | + call: func(svc Service) error { |
| 177 | + _, err := svc.Set(context.Background(), "test-uuid", "", "value") |
| 178 | + return err |
| 179 | + }, |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "delete rejects too long", |
| 183 | + call: func(svc Service) error { |
| 184 | + err := svc.Delete(context.Background(), "test-uuid", strings.Repeat("a", 65)) |
| 185 | + return err |
| 186 | + }, |
| 187 | + }, |
| 188 | + } |
| 189 | + |
| 190 | + for _, tt := range tests { |
| 191 | + t.Run(tt.name, func(t *testing.T) { |
| 192 | + t.Parallel() |
| 193 | + |
| 194 | + svc, srv := newTestService(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 195 | + t.Fatalf("unexpected request: %s %s", r.Method, r.RequestURI) |
| 196 | + })) |
| 197 | + defer srv.Close() |
| 198 | + |
| 199 | + err := tt.call(svc) |
| 200 | + assert.ErrorIs(t, err, ErrInvalidKey) |
| 201 | + }) |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +func TestDotSegmentKeysAreEscaped(t *testing.T) { |
| 206 | + t.Parallel() |
| 207 | + |
| 208 | + tests := []struct { |
| 209 | + name string |
| 210 | + method string |
| 211 | + key string |
| 212 | + wantURI string |
| 213 | + wantBody string |
| 214 | + call func(Service, string) error |
| 215 | + statusCode int |
| 216 | + }{ |
| 217 | + { |
| 218 | + name: "get dot key", |
| 219 | + method: http.MethodGet, |
| 220 | + key: ".", |
| 221 | + wantURI: "/files/test-uuid/metadata/%2E/", |
| 222 | + statusCode: http.StatusOK, |
| 223 | + call: func(svc Service, key string) error { |
| 224 | + _, err := svc.Get(context.Background(), "test-uuid", key) |
| 225 | + return err |
| 226 | + }, |
| 227 | + }, |
| 228 | + { |
| 229 | + name: "set dotdot key", |
| 230 | + method: http.MethodPut, |
| 231 | + key: "..", |
| 232 | + wantURI: "/files/test-uuid/metadata/%2E%2E/", |
| 233 | + wantBody: `"value"`, |
| 234 | + statusCode: http.StatusOK, |
| 235 | + call: func(svc Service, key string) error { |
| 236 | + _, err := svc.Set(context.Background(), "test-uuid", key, "value") |
| 237 | + return err |
| 238 | + }, |
| 239 | + }, |
| 240 | + { |
| 241 | + name: "delete dot key", |
| 242 | + method: http.MethodDelete, |
| 243 | + key: ".", |
| 244 | + wantURI: "/files/test-uuid/metadata/%2E/", |
| 245 | + statusCode: http.StatusNoContent, |
| 246 | + call: func(svc Service, key string) error { |
| 247 | + return svc.Delete(context.Background(), "test-uuid", key) |
| 248 | + }, |
| 249 | + }, |
| 250 | + } |
| 251 | + |
| 252 | + for _, tt := range tests { |
| 253 | + t.Run(tt.name, func(t *testing.T) { |
| 254 | + t.Parallel() |
| 255 | + |
| 256 | + svc, srv := newTestService(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 257 | + assert.Equal(t, tt.method, r.Method) |
| 258 | + assert.Equal(t, tt.wantURI, r.RequestURI) |
| 259 | + |
| 260 | + if tt.wantBody != "" { |
| 261 | + body, err := io.ReadAll(r.Body) |
| 262 | + assert.NoError(t, err) |
| 263 | + assert.Equal(t, tt.wantBody, string(body)) |
| 264 | + } |
| 265 | + |
| 266 | + w.Header().Set("Content-Type", "application/json") |
| 267 | + w.WriteHeader(tt.statusCode) |
| 268 | + if tt.statusCode != http.StatusNoContent { |
| 269 | + json.NewEncoder(w).Encode("ok") |
| 270 | + } |
| 271 | + })) |
| 272 | + defer srv.Close() |
| 273 | + |
| 274 | + err := tt.call(svc, tt.key) |
| 275 | + assert.NoError(t, err) |
| 276 | + }) |
| 277 | + } |
| 278 | +} |
0 commit comments