| 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 |
| 22 |
func createMockOperation() *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 |
r := &lowv3.Response{ |
| 31 |
Content: low.NodeReference[*orderedmap.Map[low.KeyReference[string], low.ValueReference[*lowv3.MediaType]]]{ |
| 32 |
Value: content, |
| 33 |
KeyNode: &yaml.Node{}, |
| 34 |
ValueNode: &yaml.Node{}, |
| 35 |
}, |
| 36 |
} |
| 37 |
// resp := v3.NewResponse(r) |
| 38 |
|
| 39 |
// create a lowv3.Responses object |
| 40 |
responses := &lowv3.Responses{ |
| 41 |
Default: low.NodeReference[*lowv3.Response]{ |
| 42 |
Value: r, |
| 43 |
KeyNode: &yaml.Node{}, |
| 44 |
ValueNode: &yaml.Node{}, |
| 45 |
}, |
| 46 |
Codes: orderedmap.New[low.KeyReference[string], low.ValueReference[*lowv3.Response]](), |
| 47 |
KeyNode: &yaml.Node{}, |
| 48 |
} |
| 49 |
|
| 50 |
// create a lowv3.Operation object |
| 51 |
op := &lowv3.Operation{ |
| 52 |
Responses: low.NodeReference[*lowv3.Responses]{ |
| 53 |
Value: responses, |
| 54 |
KeyNode: &yaml.Node{}, |
| 55 |
ValueNode: &yaml.Node{}, |
| 56 |
}, |
| 57 |
} |
| 58 |
|
| 59 |
// create a new v3.Operation object from the low |
| 60 |
highOp := v3.NewOperation(op) |
| 61 |
return highOp |
| 62 |
} |
| 63 |
|
| 64 |
func TestResponseContentTypeNotFound_Default(t *testing.T) { |
| 65 |
// Create a mock operation with a default response and content type |
| 66 |
op := createMockOperation() |
| 67 |
op.Responses.Default.Content.Set("application/json", &v3.MediaType{}) |
| 68 |
op.Responses.Default.GoLow().Content.KeyNode.Line = 12 |
| 69 |
op.Responses.Default.GoLow().Content.KeyNode.Column = 34 |
| 70 |
|
| 71 |
// Create a mock request and response |
| 72 |
request, _ := http.NewRequest(http.MethodGet, "/test", nil) |
| 73 |
response := &http.Response{ |
| 74 |
Header: http.Header{ |
| 75 |
helpers.ContentTypeHeader: {"application/xml"}, |
| 76 |
}, |
| 77 |
} |
| 78 |
|
| 79 |
// Call the function |
| 80 |
err := ResponseContentTypeNotFound(op, request, response, "200", true) |
| 81 |
|
| 82 |
// Validate the error |
| 83 |
require.NotNil(t, err) |
| 84 |
require.Equal(t, helpers.ResponseBodyValidation, err.ValidationType) |
| 85 |
require.Equal(t, helpers.RequestBodyContentType, err.ValidationSubType) |
| 86 |
require.Contains(t, err.Message, "'application/xml' does not exist") |
| 87 |
require.Contains(t, err.Reason, "The content type 'application/xml' of the GET response received has not been defined") |
| 88 |
require.Equal(t, 12, err.SpecLine) |
| 89 |
require.Equal(t, 34, err.SpecCol) |
| 90 |
require.Contains(t, err.HowToFix, "application/json") |
| 91 |
} |
| 92 |
|
| 93 |
func TestResponseContentTypeNotFound_SpecificCode(t *testing.T) { |
| 94 |
// Create a mock operation with a response code and content type |
| 95 |
op := createMockOperation() |
| 96 |
responseContent := orderedmap.New[string, *v3.MediaType]() |
| 97 |
responseContent.Set("application/json", &v3.MediaType{}) |
| 98 |
|
| 99 |
content := orderedmap.New[low.KeyReference[string], low.ValueReference[*lowv3.MediaType]]() |
| 100 |
content.Set(low.KeyReference[string]{ |
| 101 |
Value: "application/json", |
| 102 |
}, low.ValueReference[*lowv3.MediaType]{ |
| 103 |
Value: &lowv3.MediaType{}, |
| 104 |
}) |
| 105 |
r := &lowv3.Response{ |
| 106 |
Content: low.NodeReference[*orderedmap.Map[low.KeyReference[string], low.ValueReference[*lowv3.MediaType]]]{ |
| 107 |
Value: content, |
| 108 |
KeyNode: &yaml.Node{}, |
| 109 |
ValueNode: &yaml.Node{}, |
| 110 |
}, |
| 111 |
} |
| 112 |
resp := v3.NewResponse(r) |
| 113 |
|
| 114 |
op.Responses.Codes.Set("200", resp) |
| 115 |
op.Responses.Codes.GetOrZero("200").GoLow().Content.KeyNode.Line = 15 |
| 116 |
op.Responses.Codes.GetOrZero("200").GoLow().Content.KeyNode.Column = 42 |
| 117 |
|
| 118 |
// Create a mock request and response |
| 119 |
request, _ := http.NewRequest(http.MethodPost, "/test", nil) |
| 120 |
response := &http.Response{ |
| 121 |
Header: http.Header{ |
| 122 |
helpers.ContentTypeHeader: {"application/xml"}, |
| 123 |
}, |
| 124 |
} |
| 125 |
|
| 126 |
// Call the function |
| 127 |
err := ResponseContentTypeNotFound(op, request, response, "200", false) |
| 128 |
|
| 129 |
// Validate the error |
| 130 |
require.NotNil(t, err) |
| 131 |
require.Equal(t, helpers.ResponseBodyValidation, err.ValidationType) |
| 132 |
require.Equal(t, helpers.RequestBodyContentType, err.ValidationSubType) |
| 133 |
require.Contains(t, err.Message, "'application/xml' does not exist") |
| 134 |
require.Contains(t, err.Reason, "The content type 'application/xml' of the POST response received has not been defined") |
| 135 |
require.Equal(t, 15, err.SpecLine) |
| 136 |
require.Equal(t, 42, err.SpecCol) |
| 137 |
require.Contains(t, err.HowToFix, "application/json") |
| 138 |
} |
| 139 |
|
| 140 |
func TestResponseCodeNotFound(t *testing.T) { |
| 141 |
// Create a mock operation with responses |
| 142 |
op := createMockOperation() |
| 143 |
op.GoLow().Responses.KeyNode.Line = 22 |
| 144 |
op.GoLow().Responses.KeyNode.Column = 56 |
| 145 |
|
| 146 |
// Create a mock request |
| 147 |
request, _ := http.NewRequest(http.MethodDelete, "/test", nil) |
| 148 |
|
| 149 |
// Call the function with a response code that doesn't exist |
| 150 |
err := ResponseCodeNotFound(op, request, 404) |
| 151 |
|
| 152 |
// Validate the error |
| 153 |
require.NotNil(t, err) |
| 154 |
require.Equal(t, helpers.ResponseBodyValidation, err.ValidationType) |
| 155 |
require.Equal(t, helpers.ResponseBodyResponseCode, err.ValidationSubType) |
| 156 |
require.Contains(t, err.Message, "response code '404' does not exist") |
| 157 |
require.Contains(t, err.Reason, "The response code '404' of the DELETE request submitted has not been defined") |
| 158 |
require.Equal(t, 22, err.SpecLine) |
| 159 |
require.Equal(t, 56, err.SpecCol) |
| 160 |
require.Equal(t, HowToFixInvalidResponseCode, err.HowToFix) |
| 161 |
} |
| 162 |
|
| 163 |
func TestResponseContentTypeNotFound_NilContentKeyNode(t *testing.T) { |
| 164 |
// Response code exists but has no content KeyNode — should not panic |
| 165 |
r := &lowv3.Response{ |
| 166 |
Content: low.NodeReference[*orderedmap.Map[low.KeyReference[string], low.ValueReference[*lowv3.MediaType]]]{ |
| 167 |
Value: orderedmap.New[low.KeyReference[string], low.ValueReference[*lowv3.MediaType]](), |
| 168 |
// KeyNode intentionally nil |
| 169 |
}, |
| 170 |
} |
| 171 |
resp := v3.NewResponse(r) |
| 172 |
|
| 173 |
responses := &lowv3.Responses{ |
| 174 |
Codes: orderedmap.New[low.KeyReference[string], low.ValueReference[*lowv3.Response]](), |
| 175 |
KeyNode: &yaml.Node{}, |
| 176 |
} |
| 177 |
op := &lowv3.Operation{ |
| 178 |
Responses: low.NodeReference[*lowv3.Responses]{ |
| 179 |
Value: responses, |
| 180 |
KeyNode: &yaml.Node{}, |
| 181 |
ValueNode: &yaml.Node{}, |
| 182 |
}, |
| 183 |
} |
| 184 |
highOp := v3.NewOperation(op) |
| 185 |
highOp.Responses.Codes.Set("200", resp) |
| 186 |
|
| 187 |
request, _ := http.NewRequest(http.MethodGet, "/test", nil) |
| 188 |
response := &http.Response{ |
| 189 |
Header: http.Header{ |
| 190 |
helpers.ContentTypeHeader: {"application/xml"}, |
| 191 |
}, |
| 192 |
} |
| 193 |
|
| 194 |
// Should not panic |
| 195 |
err := ResponseContentTypeNotFound(highOp, request, response, "200", false) |
| 196 |
require.NotNil(t, err) |
| 197 |
require.Equal(t, 1, err.SpecLine) |
| 198 |
require.Equal(t, 0, err.SpecCol) |
| 199 |
} |
| 200 |
|
| 201 |
func TestResponseContentTypeNotFound_NilDefaultContentKeyNode(t *testing.T) { |
| 202 |
// Default response exists but has no content KeyNode — should not panic |
| 203 |
r := &lowv3.Response{ |
| 204 |
Content: low.NodeReference[*orderedmap.Map[low.KeyReference[string], low.ValueReference[*lowv3.MediaType]]]{ |
| 205 |
Value: orderedmap.New[low.KeyReference[string], low.ValueReference[*lowv3.MediaType]](), |
| 206 |
// KeyNode intentionally nil |
| 207 |
}, |
| 208 |
} |
| 209 |
|
| 210 |
responses := &lowv3.Responses{ |
| 211 |
Default: low.NodeReference[*lowv3.Response]{ |
| 212 |
Value: r, |
| 213 |
KeyNode: &yaml.Node{}, |
| 214 |
ValueNode: &yaml.Node{}, |
| 215 |
}, |
| 216 |
Codes: orderedmap.New[low.KeyReference[string], low.ValueReference[*lowv3.Response]](), |
| 217 |
KeyNode: &yaml.Node{}, |
| 218 |
} |
| 219 |
op := &lowv3.Operation{ |
| 220 |
Responses: low.NodeReference[*lowv3.Responses]{ |
| 221 |
Value: responses, |
| 222 |
KeyNode: &yaml.Node{}, |
| 223 |
ValueNode: &yaml.Node{}, |
| 224 |
}, |
| 225 |
} |
| 226 |
highOp := v3.NewOperation(op) |
| 227 |
|
| 228 |
request, _ := http.NewRequest(http.MethodGet, "/test", nil) |
| 229 |
response := &http.Response{ |
| 230 |
Header: http.Header{ |
| 231 |
helpers.ContentTypeHeader: {"application/xml"}, |
| 232 |
}, |
| 233 |
} |
| 234 |
|
| 235 |
// Should not panic |
| 236 |
err := ResponseContentTypeNotFound(highOp, request, response, "200", true) |
| 237 |
require.NotNil(t, err) |
| 238 |
require.Equal(t, 1, err.SpecLine) |
| 239 |
require.Equal(t, 0, err.SpecCol) |
| 240 |
} |
| 241 |
|
| 242 |
func TestResponseCodeNotFound_NilResponsesKeyNode(t *testing.T) { |
| 243 |
// Operation with nil Responses KeyNode — should not panic |
| 244 |
responses := &lowv3.Responses{ |
| 245 |
Codes: orderedmap.New[low.KeyReference[string], low.ValueReference[*lowv3.Response]](), |
| 246 |
// KeyNode intentionally nil |
| 247 |
} |
| 248 |
op := &lowv3.Operation{ |
| 249 |
Responses: low.NodeReference[*lowv3.Responses]{ |
| 250 |
Value: responses, |
| 251 |
ValueNode: &yaml.Node{}, |
| 252 |
}, |
| 253 |
} |
| 254 |
highOp := v3.NewOperation(op) |
| 255 |
|
| 256 |
request, _ := http.NewRequest(http.MethodGet, "/test", nil) |
| 257 |
|
| 258 |
// Should not panic |
| 259 |
err := ResponseCodeNotFound(highOp, request, 404) |
| 260 |
require.NotNil(t, err) |
| 261 |
require.Equal(t, 1, err.SpecLine) |
| 262 |
require.Equal(t, 0, err.SpecCol) |
| 263 |
} |