Debian Codegraph @

paths/specificity_test.go

raw
1 // Copyright 2023-2025 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package paths
5
6 import (
7 "testing"
8
9 v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
10 "github.com/stretchr/testify/assert"
11 )
12
13 func TestComputeSpecificityScore(t *testing.T) {
14 tests := []struct {
15 name string
16 path string
17 expected int
18 }{
19 {
20 name: "single literal segment",
21 path: "/pets",
22 expected: 1000,
23 },
24 {
25 name: "single parameter segment",
26 path: "/{id}",
27 expected: 1,
28 },
29 {
30 name: "literal then parameter",
31 path: "/pets/{id}",
32 expected: 1001,
33 },
34 {
35 name: "two literal segments",
36 path: "/pets/mine",
37 expected: 2000,
38 },
39 {
40 name: "two parameter segments",
41 path: "/{tenant}/{id}",
42 expected: 2,
43 },
44 {
45 name: "mixed - param literal param",
46 path: "/{tenant}/users/{id}",
47 expected: 1002,
48 },
49 {
50 name: "three literal segments",
51 path: "/api/v1/users",
52 expected: 3000,
53 },
54 {
55 name: "two literals one param",
56 path: "/api/v1/{resource}",
57 expected: 2001,
58 },
59 {
60 name: "four literals",
61 path: "/api/v1/users/profile",
62 expected: 4000,
63 },
64 {
65 name: "label parameter format",
66 path: "/burgers/{.burgerId}/locate",
67 expected: 2001,
68 },
69 {
70 name: "exploded parameter format",
71 path: "/burgers/{burgerId*}/locate",
72 expected: 2001,
73 },
74 {
75 name: "empty path",
76 path: "/",
77 expected: 0,
78 },
79 {
80 name: "OData style path",
81 path: "/entities('{Entity}')",
82 expected: 1,
83 },
84 {
85 name: "complex OData path",
86 path: "/orders(RelationshipNumber='{RelationshipNumber}',ValidityEndDate=datetime'{ValidityEndDate}')",
87 expected: 1,
88 },
89 }
90
91 for _, tt := range tests {
92 t.Run(tt.name, func(t *testing.T) {
93 score := computeSpecificityScore(tt.path)
94 assert.Equal(t, tt.expected, score, "path: %s", tt.path)
95 })
96 }
97 }
98
99 func TestIsParameterSegment(t *testing.T) {
100 tests := []struct {
101 segment string
102 expected bool
103 }{
104 {"users", false},
105 {"{id}", true},
106 {"{.id}", true},
107 {"{id*}", true},
108 {"mine", false},
109 {"", false},
110 {"v1", false},
111 {"{petId}", true},
112 {"{message_id}", true},
113 {"Operations", false},
114 {"entities('{Entity}')", true},
115 {"literal", false},
116 }
117
118 for _, tt := range tests {
119 t.Run(tt.segment, func(t *testing.T) {
120 result := isParameterSegment(tt.segment)
121 assert.Equal(t, tt.expected, result, "segment: %s", tt.segment)
122 })
123 }
124 }
125
126 func TestPathHasMethod(t *testing.T) {
127 tests := []struct {
128 name string
129 pathItem *v3.PathItem
130 method string
131 expected bool
132 }{
133 {
134 name: "GET exists",
135 pathItem: &v3.PathItem{Get: &v3.Operation{}},
136 method: "GET",
137 expected: true,
138 },
139 {
140 name: "GET missing",
141 pathItem: &v3.PathItem{Post: &v3.Operation{}},
142 method: "GET",
143 expected: false,
144 },
145 {
146 name: "POST exists",
147 pathItem: &v3.PathItem{Post: &v3.Operation{}},
148 method: "POST",
149 expected: true,
150 },
151 {
152 name: "PUT exists",
153 pathItem: &v3.PathItem{Put: &v3.Operation{}},
154 method: "PUT",
155 expected: true,
156 },
157 {
158 name: "DELETE exists",
159 pathItem: &v3.PathItem{Delete: &v3.Operation{}},
160 method: "DELETE",
161 expected: true,
162 },
163 {
164 name: "OPTIONS exists",
165 pathItem: &v3.PathItem{Options: &v3.Operation{}},
166 method: "OPTIONS",
167 expected: true,
168 },
169 {
170 name: "HEAD exists",
171 pathItem: &v3.PathItem{Head: &v3.Operation{}},
172 method: "HEAD",
173 expected: true,
174 },
175 {
176 name: "HEAD if GET exists",
177 pathItem: &v3.PathItem{Get: &v3.Operation{}},
178 method: "HEAD",
179 expected: true,
180 },
181 {
182 name: "PATCH exists",
183 pathItem: &v3.PathItem{Patch: &v3.Operation{}},
184 method: "PATCH",
185 expected: true,
186 },
187 {
188 name: "TRACE exists",
189 pathItem: &v3.PathItem{Trace: &v3.Operation{}},
190 method: "TRACE",
191 expected: true,
192 },
193 {
194 name: "unknown method",
195 pathItem: &v3.PathItem{Get: &v3.Operation{}},
196 method: "UNKNOWN",
197 expected: false,
198 },
199 {
200 name: "empty pathItem",
201 pathItem: &v3.PathItem{},
202 method: "GET",
203 expected: false,
204 },
205 }
206
207 for _, tt := range tests {
208 t.Run(tt.name, func(t *testing.T) {
209 result := pathHasMethod(tt.pathItem, tt.method)
210 assert.Equal(t, tt.expected, result)
211 })
212 }
213 }
214
215 func TestSelectMatches(t *testing.T) {
216 tests := []struct {
217 name string
218 candidates []pathCandidate
219 expectedWithMethod string // expected path for withMethod, or empty if nil
220 expectedHighest string // expected path for highest, or empty if nil
221 }{
222 {
223 name: "single candidate with method",
224 candidates: []pathCandidate{
225 {path: "/pets/{id}", score: 1001, hasMethod: true},
226 },
227 expectedWithMethod: "/pets/{id}",
228 expectedHighest: "/pets/{id}",
229 },
230 {
231 name: "single candidate without method",
232 candidates: []pathCandidate{
233 {path: "/pets/{id}", score: 1001, hasMethod: false},
234 },
235 expectedWithMethod: "",
236 expectedHighest: "/pets/{id}",
237 },
238 {
239 name: "higher score wins",
240 candidates: []pathCandidate{
241 {path: "/pets/{id}", score: 1001, hasMethod: true},
242 {path: "/pets/mine", score: 2000, hasMethod: true},
243 },
244 expectedWithMethod: "/pets/mine",
245 expectedHighest: "/pets/mine",
246 },
247 {
248 name: "higher score wins - reverse order",
249 candidates: []pathCandidate{
250 {path: "/pets/mine", score: 2000, hasMethod: true},
251 {path: "/pets/{id}", score: 1001, hasMethod: true},
252 },
253 expectedWithMethod: "/pets/mine",
254 expectedHighest: "/pets/mine",
255 },
256 {
257 name: "higher score without method is skipped for withMethod",
258 candidates: []pathCandidate{
259 {path: "/pets/{id}", score: 1001, hasMethod: true},
260 {path: "/pets/mine", score: 2000, hasMethod: false},
261 },
262 expectedWithMethod: "/pets/{id}",
263 expectedHighest: "/pets/mine",
264 },
265 {
266 name: "equal scores - first wins",
267 candidates: []pathCandidate{
268 {path: "/pets/{petId}", score: 1001, hasMethod: true},
269 {path: "/pets/{petName}", score: 1001, hasMethod: true},
270 },
271 expectedWithMethod: "/pets/{petId}",
272 expectedHighest: "/pets/{petId}",
273 },
274 {
275 name: "empty candidates",
276 candidates: []pathCandidate{},
277 expectedWithMethod: "",
278 expectedHighest: "",
279 },
280 {
281 name: "all candidates without method",
282 candidates: []pathCandidate{
283 {path: "/pets/{id}", score: 1001, hasMethod: false},
284 {path: "/pets/mine", score: 2000, hasMethod: false},
285 },
286 expectedWithMethod: "",
287 expectedHighest: "/pets/mine",
288 },
289 {
290 name: "three candidates mixed",
291 candidates: []pathCandidate{
292 {path: "/{tenant}/users/{id}", score: 1002, hasMethod: true},
293 {path: "/api/users/{id}", score: 2001, hasMethod: true},
294 {path: "/api/users/me", score: 3000, hasMethod: true},
295 },
296 expectedWithMethod: "/api/users/me",
297 expectedHighest: "/api/users/me",
298 },
299 }
300
301 for _, tt := range tests {
302 t.Run(tt.name, func(t *testing.T) {
303 withMethod, highest := selectMatches(tt.candidates)
304
305 if tt.expectedWithMethod == "" {
306 assert.Nil(t, withMethod)
307 } else {
308 assert.NotNil(t, withMethod)
309 assert.Equal(t, tt.expectedWithMethod, withMethod.path)
310 }
311
312 if tt.expectedHighest == "" {
313 assert.Nil(t, highest)
314 } else {
315 assert.NotNil(t, highest)
316 assert.Equal(t, tt.expectedHighest, highest.path)
317 }
318 })
319 }
320 }