|
4 | 4 | package parameters |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
7 | 10 | "github.com/pb33f/libopenapi" |
8 | 11 | "github.com/pb33f/libopenapi-validator/paths" |
9 | 12 | "github.com/stretchr/testify/assert" |
10 | | - "net/http" |
11 | | - "testing" |
| 13 | + "github.com/stretchr/testify/require" |
12 | 14 | ) |
13 | 15 |
|
14 | 16 | func TestNewValidator_SimpleArrayEncodedPath(t *testing.T) { |
@@ -280,7 +282,64 @@ paths: |
280 | 282 |
|
281 | 283 | assert.False(t, valid) |
282 | 284 | assert.Len(t, errors, 1) |
283 | | - assert.Equal(t, "GET Path '/burgers/hello/locate' not found", errors[0].Message) |
| 285 | + assert.Equal(t, "Path parameter 'burgerId' is not a valid number", errors[0].Message) |
| 286 | +} |
| 287 | + |
| 288 | +func TestNewValidator_SimpleEncodedPath_IntegerViolation(t *testing.T) { |
| 289 | + |
| 290 | + spec := `openapi: 3.1.0 |
| 291 | +paths: |
| 292 | + /burgers/{burgerId}/locate: |
| 293 | + parameters: |
| 294 | + - name: burgerId |
| 295 | + in: path |
| 296 | + schema: |
| 297 | + type: integer |
| 298 | + minimum: 10 |
| 299 | + get: |
| 300 | + operationId: locateBurgers` |
| 301 | + |
| 302 | + doc, err := libopenapi.NewDocument([]byte(spec)) |
| 303 | + require.NoError(t, err) |
| 304 | + m, _ := doc.BuildV3Model() |
| 305 | + |
| 306 | + v := NewParameterValidator(&m.Model) |
| 307 | + |
| 308 | + request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/1/locate", nil) |
| 309 | + valid, errors := v.ValidatePathParams(request) |
| 310 | + |
| 311 | + assert.False(t, valid) |
| 312 | + assert.Len(t, errors, 1) |
| 313 | + assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message) |
| 314 | + assert.Len(t, errors[0].SchemaValidationErrors, 1) |
| 315 | + assert.Equal(t, "Reason: must be >= 10 but found 1, Location: /minimum", errors[0].SchemaValidationErrors[0].Error()) |
| 316 | +} |
| 317 | + |
| 318 | +func TestNewValidator_SimpleEncodedPath_Integer(t *testing.T) { |
| 319 | + |
| 320 | + spec := `openapi: 3.1.0 |
| 321 | +paths: |
| 322 | + /burgers/{burgerId}/locate: |
| 323 | + parameters: |
| 324 | + - name: burgerId |
| 325 | + in: path |
| 326 | + schema: |
| 327 | + type: integer |
| 328 | + minimum: 10 |
| 329 | + get: |
| 330 | + operationId: locateBurgers` |
| 331 | + |
| 332 | + doc, err := libopenapi.NewDocument([]byte(spec)) |
| 333 | + require.NoError(t, err) |
| 334 | + m, _ := doc.BuildV3Model() |
| 335 | + |
| 336 | + v := NewParameterValidator(&m.Model) |
| 337 | + |
| 338 | + request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/14/locate", nil) |
| 339 | + valid, errors := v.ValidatePathParams(request) |
| 340 | + |
| 341 | + assert.True(t, valid) |
| 342 | + assert.Nil(t, errors) |
284 | 343 | } |
285 | 344 |
|
286 | 345 | func TestNewValidator_SimpleEncodedPath_InvalidBoolean(t *testing.T) { |
@@ -338,6 +397,37 @@ paths: |
338 | 397 | assert.Equal(t, "Path parameter 'burgerId' is not a valid number", errors[0].Message) |
339 | 398 | } |
340 | 399 |
|
| 400 | +func TestNewValidator_LabelEncodedPath_IntegerViolation(t *testing.T) { |
| 401 | + |
| 402 | + spec := `openapi: 3.1.0 |
| 403 | +paths: |
| 404 | + /burgers/{.burgerId}/locate: |
| 405 | + parameters: |
| 406 | + - name: burgerId |
| 407 | + in: path |
| 408 | + style: label |
| 409 | + schema: |
| 410 | + type: integer |
| 411 | + minimum: 10 |
| 412 | + get: |
| 413 | + operationId: locateBurgers` |
| 414 | + |
| 415 | + doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 416 | + |
| 417 | + m, _ := doc.BuildV3Model() |
| 418 | + |
| 419 | + v := NewParameterValidator(&m.Model) |
| 420 | + |
| 421 | + request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.3/locate", nil) |
| 422 | + valid, errors := v.ValidatePathParams(request) |
| 423 | + |
| 424 | + assert.False(t, valid) |
| 425 | + assert.Len(t, errors, 1) |
| 426 | + assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message) |
| 427 | + assert.Len(t, errors[0].SchemaValidationErrors, 1) |
| 428 | + assert.Equal(t, "Reason: must be >= 10 but found 3, Location: /minimum", errors[0].SchemaValidationErrors[0].Error()) |
| 429 | +} |
| 430 | + |
341 | 431 | func TestNewValidator_LabelEncodedPath_InvalidBoolean(t *testing.T) { |
342 | 432 |
|
343 | 433 | spec := `openapi: 3.1.0 |
@@ -684,6 +774,37 @@ paths: |
684 | 774 | assert.Equal(t, "Path parameter 'burgerId' is not a valid number", errors[0].Message) |
685 | 775 | } |
686 | 776 |
|
| 777 | +func TestNewValidator_MatrixEncodedPath_PrimitiveNumberViolation(t *testing.T) { |
| 778 | + |
| 779 | + spec := `openapi: 3.1.0 |
| 780 | +paths: |
| 781 | + /burgers/{;burgerId}/locate: |
| 782 | + parameters: |
| 783 | + - name: burgerId |
| 784 | + in: path |
| 785 | + style: matrix |
| 786 | + schema: |
| 787 | + type: integer |
| 788 | + minimum: 5 |
| 789 | + get: |
| 790 | + operationId: locateBurgers` |
| 791 | + |
| 792 | + doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 793 | + |
| 794 | + m, _ := doc.BuildV3Model() |
| 795 | + |
| 796 | + v := NewParameterValidator(&m.Model) |
| 797 | + |
| 798 | + request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=3/locate", nil) |
| 799 | + valid, errors := v.ValidatePathParams(request) |
| 800 | + |
| 801 | + assert.False(t, valid) |
| 802 | + assert.Len(t, errors, 1) |
| 803 | + assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message) |
| 804 | + assert.Len(t, errors[0].SchemaValidationErrors, 1) |
| 805 | + assert.Equal(t, "Reason: must be >= 5 but found 3, Location: /minimum", errors[0].SchemaValidationErrors[0].Error()) |
| 806 | +} |
| 807 | + |
687 | 808 | func TestNewValidator_MatrixEncodedPath_ValidPrimitiveBoolean(t *testing.T) { |
688 | 809 |
|
689 | 810 | spec := `openapi: 3.1.0 |
@@ -1075,6 +1196,36 @@ paths: |
1075 | 1196 |
|
1076 | 1197 | } |
1077 | 1198 |
|
| 1199 | +func TestNewValidator_PathParamStringViolation(t *testing.T) { |
| 1200 | + |
| 1201 | + spec := `openapi: 3.1.0 |
| 1202 | +paths: |
| 1203 | + /burgers/{burgerId}/locate: |
| 1204 | + parameters: |
| 1205 | + - name: burgerId |
| 1206 | + in: path |
| 1207 | + schema: |
| 1208 | + type: string |
| 1209 | + minLength: 4 |
| 1210 | + get: |
| 1211 | + operationId: locateBurgers` |
| 1212 | + |
| 1213 | + doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1214 | + |
| 1215 | + m, _ := doc.BuildV3Model() |
| 1216 | + |
| 1217 | + v := NewParameterValidator(&m.Model) |
| 1218 | + |
| 1219 | + request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/big/locate", nil) |
| 1220 | + valid, errors := v.ValidatePathParams(request) |
| 1221 | + |
| 1222 | + assert.False(t, valid) |
| 1223 | + assert.Len(t, errors, 1) |
| 1224 | + assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message) |
| 1225 | + assert.Len(t, errors[0].SchemaValidationErrors, 1) |
| 1226 | + assert.Equal(t, "Reason: length must be >= 4, but got 3, Location: /minLength", errors[0].SchemaValidationErrors[0].Error()) |
| 1227 | +} |
| 1228 | + |
1078 | 1229 | func TestNewValidator_PathParamIntegerEnumValid(t *testing.T) { |
1079 | 1230 |
|
1080 | 1231 | spec := `openapi: 3.1.0 |
|
0 commit comments