| 1 |
// Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package errors |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
) |
| 11 |
|
| 12 |
func TestUndeclaredPropertyError(t *testing.T) { |
| 13 |
err := UndeclaredPropertyError( |
| 14 |
"$.body.user.extra", |
| 15 |
"extra", |
| 16 |
"some value", |
| 17 |
[]string{"name", "email"}, |
| 18 |
"request", |
| 19 |
"/users", |
| 20 |
"POST", |
| 21 |
42, |
| 22 |
10, |
| 23 |
) |
| 24 |
|
| 25 |
assert.NotNil(t, err) |
| 26 |
assert.Equal(t, StrictValidationType, err.ValidationType) |
| 27 |
assert.Equal(t, StrictSubTypeProperty, err.ValidationSubType) |
| 28 |
assert.Contains(t, err.Message, "request property 'extra' at '$.body.user.extra'") |
| 29 |
assert.Contains(t, err.Reason, "name, email") |
| 30 |
assert.Contains(t, err.HowToFix, "extra") |
| 31 |
assert.Contains(t, err.HowToFix, "$.body.user.extra") |
| 32 |
assert.Equal(t, "/users", err.RequestPath) |
| 33 |
assert.Equal(t, "POST", err.RequestMethod) |
| 34 |
assert.Equal(t, "extra", err.ParameterName) |
| 35 |
assert.Equal(t, 42, err.SpecLine) |
| 36 |
assert.Equal(t, 10, err.SpecCol) |
| 37 |
} |
| 38 |
|
| 39 |
func TestUndeclaredPropertyError_Response(t *testing.T) { |
| 40 |
err := UndeclaredPropertyError( |
| 41 |
"$.body.data.undeclared", |
| 42 |
"undeclared", |
| 43 |
map[string]any{"nested": "value"}, |
| 44 |
[]string{"id", "name"}, |
| 45 |
"response", |
| 46 |
"/items/123", |
| 47 |
"GET", |
| 48 |
100, |
| 49 |
5, |
| 50 |
) |
| 51 |
|
| 52 |
assert.NotNil(t, err) |
| 53 |
assert.Contains(t, err.Message, "response property 'undeclared'") |
| 54 |
assert.Contains(t, err.Reason, "id, name") |
| 55 |
assert.Equal(t, "{...}", err.Context) // Map truncated |
| 56 |
assert.Equal(t, 100, err.SpecLine) |
| 57 |
assert.Equal(t, 5, err.SpecCol) |
| 58 |
} |
| 59 |
|
| 60 |
func TestUndeclaredPropertyError_EmptyDirection(t *testing.T) { |
| 61 |
err := UndeclaredPropertyError( |
| 62 |
"$.body.prop", |
| 63 |
"prop", |
| 64 |
"value", |
| 65 |
nil, |
| 66 |
"", // Empty direction defaults to "request" |
| 67 |
"/test", |
| 68 |
"POST", |
| 69 |
0, // Zero values for unknown location |
| 70 |
0, |
| 71 |
) |
| 72 |
|
| 73 |
assert.Contains(t, err.Message, "request property") |
| 74 |
assert.Equal(t, 0, err.SpecLine) |
| 75 |
assert.Equal(t, 0, err.SpecCol) |
| 76 |
} |
| 77 |
|
| 78 |
func TestUndeclaredHeaderError(t *testing.T) { |
| 79 |
err := UndeclaredHeaderError( |
| 80 |
"X-Custom-Header", |
| 81 |
"header-value", |
| 82 |
[]string{"Content-Type", "Authorization"}, |
| 83 |
"request", |
| 84 |
"/api/endpoint", |
| 85 |
"GET", |
| 86 |
) |
| 87 |
|
| 88 |
assert.NotNil(t, err) |
| 89 |
assert.Equal(t, StrictValidationType, err.ValidationType) |
| 90 |
assert.Equal(t, StrictSubTypeHeader, err.ValidationSubType) |
| 91 |
assert.Contains(t, err.Message, "request header 'X-Custom-Header'") |
| 92 |
assert.Contains(t, err.Reason, "Content-Type, Authorization") |
| 93 |
assert.Contains(t, err.HowToFix, "X-Custom-Header") |
| 94 |
assert.Equal(t, "/api/endpoint", err.RequestPath) |
| 95 |
assert.Equal(t, "GET", err.RequestMethod) |
| 96 |
assert.Equal(t, "X-Custom-Header", err.ParameterName) |
| 97 |
assert.Equal(t, "header-value", err.Context) |
| 98 |
} |
| 99 |
|
| 100 |
func TestUndeclaredHeaderError_Response(t *testing.T) { |
| 101 |
err := UndeclaredHeaderError( |
| 102 |
"X-Response-Header", |
| 103 |
"value", |
| 104 |
nil, |
| 105 |
"response", |
| 106 |
"/test", |
| 107 |
"POST", |
| 108 |
) |
| 109 |
|
| 110 |
assert.Contains(t, err.Message, "response header") |
| 111 |
} |
| 112 |
|
| 113 |
func TestUndeclaredHeaderError_EmptyDirection(t *testing.T) { |
| 114 |
err := UndeclaredHeaderError( |
| 115 |
"X-Header", |
| 116 |
"value", |
| 117 |
nil, |
| 118 |
"", |
| 119 |
"/test", |
| 120 |
"GET", |
| 121 |
) |
| 122 |
|
| 123 |
assert.Contains(t, err.Message, "request header") |
| 124 |
} |
| 125 |
|
| 126 |
func TestUndeclaredQueryParamError(t *testing.T) { |
| 127 |
err := UndeclaredQueryParamError( |
| 128 |
"$.query.debug", |
| 129 |
"debug", |
| 130 |
"true", |
| 131 |
[]string{"page", "limit"}, |
| 132 |
"/items", |
| 133 |
"GET", |
| 134 |
) |
| 135 |
|
| 136 |
assert.NotNil(t, err) |
| 137 |
assert.Equal(t, StrictValidationType, err.ValidationType) |
| 138 |
assert.Equal(t, StrictSubTypeQuery, err.ValidationSubType) |
| 139 |
assert.Contains(t, err.Message, "query parameter 'debug' at '$.query.debug'") |
| 140 |
assert.Contains(t, err.Reason, "page, limit") |
| 141 |
assert.Contains(t, err.HowToFix, "debug") |
| 142 |
assert.Contains(t, err.HowToFix, "$.query.debug") |
| 143 |
assert.Equal(t, "/items", err.RequestPath) |
| 144 |
assert.Equal(t, "GET", err.RequestMethod) |
| 145 |
assert.Equal(t, "debug", err.ParameterName) |
| 146 |
} |
| 147 |
|
| 148 |
func TestUndeclaredCookieError(t *testing.T) { |
| 149 |
err := UndeclaredCookieError( |
| 150 |
"$.cookies.tracking", |
| 151 |
"tracking", |
| 152 |
"abc123", |
| 153 |
[]string{"session", "csrf"}, |
| 154 |
"/dashboard", |
| 155 |
"GET", |
| 156 |
) |
| 157 |
|
| 158 |
assert.NotNil(t, err) |
| 159 |
assert.Equal(t, StrictValidationType, err.ValidationType) |
| 160 |
assert.Equal(t, StrictSubTypeCookie, err.ValidationSubType) |
| 161 |
assert.Contains(t, err.Message, "cookie 'tracking' at '$.cookies.tracking'") |
| 162 |
assert.Contains(t, err.Reason, "session, csrf") |
| 163 |
assert.Contains(t, err.HowToFix, "tracking") |
| 164 |
assert.Contains(t, err.HowToFix, "$.cookies.tracking") |
| 165 |
assert.Equal(t, "/dashboard", err.RequestPath) |
| 166 |
assert.Equal(t, "GET", err.RequestMethod) |
| 167 |
assert.Equal(t, "tracking", err.ParameterName) |
| 168 |
} |
| 169 |
|
| 170 |
func TestReadOnlyPropertyError(t *testing.T) { |
| 171 |
err := ReadOnlyPropertyError( |
| 172 |
"$.body.id", |
| 173 |
"id", |
| 174 |
"user-123", |
| 175 |
"/users", |
| 176 |
"POST", |
| 177 |
10, |
| 178 |
5, |
| 179 |
) |
| 180 |
|
| 181 |
assert.NotNil(t, err) |
| 182 |
assert.Equal(t, StrictValidationType, err.ValidationType) |
| 183 |
assert.Equal(t, StrictSubTypeReadOnlyProperty, err.ValidationSubType) |
| 184 |
assert.Contains(t, err.Message, "readOnly") |
| 185 |
assert.Contains(t, err.Message, "'id'") |
| 186 |
assert.Contains(t, err.Message, "'$.body.id'") |
| 187 |
assert.Contains(t, err.Reason, "readOnly") |
| 188 |
assert.Contains(t, err.HowToFix, "id") |
| 189 |
assert.Equal(t, "/users", err.RequestPath) |
| 190 |
assert.Equal(t, "POST", err.RequestMethod) |
| 191 |
assert.Equal(t, "id", err.ParameterName) |
| 192 |
assert.Equal(t, 10, err.SpecLine) |
| 193 |
assert.Equal(t, 5, err.SpecCol) |
| 194 |
} |
| 195 |
|
| 196 |
func TestWriteOnlyPropertyError(t *testing.T) { |
| 197 |
err := WriteOnlyPropertyError( |
| 198 |
"$.body.password", |
| 199 |
"password", |
| 200 |
"secret", |
| 201 |
"/users/123", |
| 202 |
"GET", |
| 203 |
20, |
| 204 |
3, |
| 205 |
) |
| 206 |
|
| 207 |
assert.NotNil(t, err) |
| 208 |
assert.Equal(t, StrictValidationType, err.ValidationType) |
| 209 |
assert.Equal(t, StrictSubTypeWriteOnlyProperty, err.ValidationSubType) |
| 210 |
assert.Contains(t, err.Message, "writeOnly") |
| 211 |
assert.Contains(t, err.Message, "'password'") |
| 212 |
assert.Contains(t, err.Message, "'$.body.password'") |
| 213 |
assert.Contains(t, err.Reason, "writeOnly") |
| 214 |
assert.Contains(t, err.HowToFix, "password") |
| 215 |
assert.Equal(t, "/users/123", err.RequestPath) |
| 216 |
assert.Equal(t, "GET", err.RequestMethod) |
| 217 |
assert.Equal(t, "password", err.ParameterName) |
| 218 |
assert.Equal(t, 20, err.SpecLine) |
| 219 |
assert.Equal(t, 3, err.SpecCol) |
| 220 |
} |
| 221 |
|
| 222 |
func TestTruncateForContext_String(t *testing.T) { |
| 223 |
// Short string should not be truncated |
| 224 |
short := truncateForContext("short") |
| 225 |
assert.Equal(t, "short", short) |
| 226 |
|
| 227 |
// Long string should be truncated |
| 228 |
long := truncateForContext("this is a very long string that exceeds fifty characters and should be truncated") |
| 229 |
assert.Len(t, long, 50) |
| 230 |
assert.True(t, len(long) <= 50) |
| 231 |
assert.Contains(t, long, "...") |
| 232 |
} |
| 233 |
|
| 234 |
func TestTruncateForContext_Map(t *testing.T) { |
| 235 |
m := map[string]any{"key": "value"} |
| 236 |
result := truncateForContext(m) |
| 237 |
assert.Equal(t, "{...}", result) |
| 238 |
} |
| 239 |
|
| 240 |
func TestTruncateForContext_Slice(t *testing.T) { |
| 241 |
s := []any{1, 2, 3} |
| 242 |
result := truncateForContext(s) |
| 243 |
assert.Equal(t, "[...]", result) |
| 244 |
} |
| 245 |
|
| 246 |
func TestTruncateForContext_Other(t *testing.T) { |
| 247 |
// Integer |
| 248 |
i := truncateForContext(12345) |
| 249 |
assert.Equal(t, "12345", i) |
| 250 |
|
| 251 |
// Boolean |
| 252 |
b := truncateForContext(true) |
| 253 |
assert.Equal(t, "true", b) |
| 254 |
|
| 255 |
// Long formatted value |
| 256 |
type customType struct { |
| 257 |
Field1 string |
| 258 |
Field2 string |
| 259 |
Field3 string |
| 260 |
} |
| 261 |
longValue := customType{ |
| 262 |
Field1: "this is a long value", |
| 263 |
Field2: "that will exceed fifty", |
| 264 |
Field3: "characters when formatted", |
| 265 |
} |
| 266 |
result := truncateForContext(longValue) |
| 267 |
assert.True(t, len(result) <= 50) |
| 268 |
assert.Contains(t, result, "...") |
| 269 |
} |