| 1 |
// Copyright 2023-2026 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// https://pb33f.io |
| 3 |
|
| 4 |
package errors |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/pb33f/libopenapi/datamodel/low" |
| 11 |
"github.com/pb33f/libopenapi/orderedmap" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
"go.yaml.in/yaml/v4" |
| 14 |
|
| 15 |
v3 "github.com/pb33f/libopenapi/datamodel/high/v3" |
| 16 |
lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3" |
| 17 |
|
| 18 |
"github.com/pb33f/libopenapi-validator/helpers" |
| 19 |
) |
| 20 |
|
| 21 |
// Helper to create a mock v3.Operation object with a RequestBody |
| 22 |
func createMockOperationWithRequestBody() *v3.Operation { |
| 23 |
content := orderedmap.New[low.KeyReference[string], low.ValueReference[*lowv3.MediaType]]() |
| 24 |
content.Set(low.KeyReference[string]{ |
| 25 |
Value: "application/json", |
| 26 |
}, low.ValueReference[*lowv3.MediaType]{ |
| 27 |
Value: &lowv3.MediaType{}, |
| 28 |
}) |
| 29 |
|
| 30 |
reqBody := &lowv3.RequestBody{ |
| 31 |
Content: low.NodeReference[*orderedmap.Map[low.KeyReference[string], low.ValueReference[*lowv3.MediaType]]]{ |
| 32 |
Value: content, |
| 33 |
KeyNode: &yaml.Node{Line: 10, Column: 20}, |
| 34 |
ValueNode: &yaml.Node{}, |
| 35 |
}, |
| 36 |
} |
| 37 |
|
| 38 |
// Create a lowv3.Operation object |
| 39 |
op := &lowv3.Operation{ |
| 40 |
RequestBody: low.NodeReference[*lowv3.RequestBody]{ |
| 41 |
Value: reqBody, |
| 42 |
KeyNode: &yaml.Node{}, |
| 43 |
ValueNode: &yaml.Node{}, |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
// Create a new v3.Operation object from the low |
| 48 |
return v3.NewOperation(op) |
| 49 |
} |
| 50 |
|
| 51 |
// Helper to create a mock v3.PathItem object |
| 52 |
func createMockPathItem() *v3.PathItem { |
| 53 |
pathItem := &lowv3.PathItem{ |
| 54 |
KeyNode: &yaml.Node{Line: 15, Column: 25}, |
| 55 |
} |
| 56 |
return v3.NewPathItem(pathItem) |
| 57 |
} |
| 58 |
|
| 59 |
func TestRequestContentTypeNotFound(t *testing.T) { |
| 60 |
// Create a mock operation with request body content types |
| 61 |
op := createMockOperationWithRequestBody() |
| 62 |
|
| 63 |
// Create a mock request with an invalid content type |
| 64 |
request, _ := http.NewRequest(http.MethodPost, "/test", nil) |
| 65 |
request.Header.Set(helpers.ContentTypeHeader, "application/xml") |
| 66 |
|
| 67 |
// Call the function |
| 68 |
err := RequestContentTypeNotFound(op, request, "/test") |
| 69 |
|
| 70 |
// Validate the error |
| 71 |
require.NotNil(t, err) |
| 72 |
require.Equal(t, helpers.RequestBodyValidation, err.ValidationType) |
| 73 |
require.Equal(t, helpers.RequestBodyContentType, err.ValidationSubType) |
| 74 |
require.Contains(t, err.Message, "'application/xml' does not exist") |
| 75 |
require.Contains(t, err.Reason, "The content type 'application/xml' of the POST request submitted has not been defined") |
| 76 |
require.Equal(t, 10, err.SpecLine) |
| 77 |
require.Equal(t, 20, err.SpecCol) |
| 78 |
require.Contains(t, err.HowToFix, "application/json") |
| 79 |
} |
| 80 |
|
| 81 |
func TestOperationNotFound(t *testing.T) { |
| 82 |
// Create a mock path item |
| 83 |
pathItem := createMockPathItem() |
| 84 |
|
| 85 |
// Create a mock request |
| 86 |
request, _ := http.NewRequest(http.MethodPatch, "/test", nil) |
| 87 |
|
| 88 |
// Call the function |
| 89 |
err := OperationNotFound(pathItem, request, http.MethodPatch, "/test") |
| 90 |
|
| 91 |
// Validate the error |
| 92 |
require.NotNil(t, err) |
| 93 |
require.Equal(t, helpers.RequestValidation, err.ValidationType) |
| 94 |
require.Equal(t, helpers.ValidationMissingOperation, err.ValidationSubType) |
| 95 |
require.Contains(t, err.Message, "'PATCH' does not exist") |
| 96 |
require.Contains(t, err.Reason, "there was no 'PATCH' method found in the spec") |
| 97 |
require.Equal(t, 15, err.SpecLine) |
| 98 |
require.Equal(t, 25, err.SpecCol) |
| 99 |
require.Equal(t, HowToFixPathMethod, err.HowToFix) |
| 100 |
} |
| 101 |
|
| 102 |
func TestRequestContentTypeNotFound_NilContentKeyNode(t *testing.T) { |
| 103 |
// RequestBody exists but has no content KeyNode — should not panic |
| 104 |
reqBody := &lowv3.RequestBody{ |
| 105 |
Content: low.NodeReference[*orderedmap.Map[low.KeyReference[string], low.ValueReference[*lowv3.MediaType]]]{ |
| 106 |
Value: orderedmap.New[low.KeyReference[string], low.ValueReference[*lowv3.MediaType]](), |
| 107 |
// KeyNode intentionally nil |
| 108 |
}, |
| 109 |
} |
| 110 |
op := &lowv3.Operation{ |
| 111 |
RequestBody: low.NodeReference[*lowv3.RequestBody]{ |
| 112 |
Value: reqBody, |
| 113 |
KeyNode: &yaml.Node{}, |
| 114 |
ValueNode: &yaml.Node{}, |
| 115 |
}, |
| 116 |
} |
| 117 |
highOp := v3.NewOperation(op) |
| 118 |
|
| 119 |
request, _ := http.NewRequest(http.MethodPost, "/test", nil) |
| 120 |
request.Header.Set(helpers.ContentTypeHeader, "application/xml") |
| 121 |
|
| 122 |
// Should not panic |
| 123 |
err := RequestContentTypeNotFound(highOp, request, "/test") |
| 124 |
require.NotNil(t, err) |
| 125 |
require.Equal(t, 1, err.SpecLine) |
| 126 |
require.Equal(t, 0, err.SpecCol) |
| 127 |
} |