Debian Codegraph @

helpers/json_pointer_test.go

raw
1 // Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package helpers
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 )
11
12 func TestEscapeJSONPointerSegment(t *testing.T) {
13 tests := []struct {
14 name string
15 input string
16 expected string
17 }{
18 {
19 name: "no special characters",
20 input: "simple",
21 expected: "simple",
22 },
23 {
24 name: "tilde only",
25 input: "some~thing",
26 expected: "some~0thing",
27 },
28 {
29 name: "slash only",
30 input: "path/to/something",
31 expected: "path~1to~1something",
32 },
33 {
34 name: "both tilde and slash",
35 input: "path/with~special/chars~",
36 expected: "path~1with~0special~1chars~0",
37 },
38 {
39 name: "path template",
40 input: "/users/{id}",
41 expected: "~1users~1{id}",
42 },
43 }
44
45 for _, tt := range tests {
46 t.Run(tt.name, func(t *testing.T) {
47 result := EscapeJSONPointerSegment(tt.input)
48 assert.Equal(t, tt.expected, result)
49 })
50 }
51 }
52
53 func TestConstructParameterJSONPointer(t *testing.T) {
54 tests := []struct {
55 name string
56 pathTemplate string
57 method string
58 paramName string
59 keyword string
60 expected string
61 }{
62 {
63 name: "simple path with query parameter type",
64 pathTemplate: "/users",
65 method: "GET",
66 paramName: "limit",
67 keyword: "type",
68 expected: "/paths/users/get/parameters/limit/schema/type",
69 },
70 {
71 name: "path with parameter and enum keyword",
72 pathTemplate: "/users/{id}",
73 method: "POST",
74 paramName: "status",
75 keyword: "enum",
76 expected: "/paths/users~1{id}/post/parameters/status/schema/enum",
77 },
78 {
79 name: "path with tilde character",
80 pathTemplate: "/some~path",
81 method: "PUT",
82 paramName: "value",
83 keyword: "format",
84 expected: "/paths/some~0path/put/parameters/value/schema/format",
85 },
86 {
87 name: "path with multiple slashes",
88 pathTemplate: "/api/v1/users/{userId}/posts/{postId}",
89 method: "DELETE",
90 paramName: "filter",
91 keyword: "required",
92 expected: "/paths/api~1v1~1users~1{userId}~1posts~1{postId}/delete/parameters/filter/schema/required",
93 },
94 }
95
96 for _, tt := range tests {
97 t.Run(tt.name, func(t *testing.T) {
98 result := ConstructParameterJSONPointer(tt.pathTemplate, tt.method, tt.paramName, tt.keyword)
99 assert.Equal(t, tt.expected, result)
100 })
101 }
102 }
103
104 func TestConstructResponseHeaderJSONPointer(t *testing.T) {
105 tests := []struct {
106 name string
107 pathTemplate string
108 method string
109 statusCode string
110 headerName string
111 keyword string
112 expected string
113 }{
114 {
115 name: "simple response header",
116 pathTemplate: "/health",
117 method: "GET",
118 statusCode: "200",
119 headerName: "X-Request-ID",
120 keyword: "required",
121 expected: "/paths/health/get/responses/200/headers/X-Request-ID/required",
122 },
123 {
124 name: "path with parameter",
125 pathTemplate: "/users/{id}",
126 method: "POST",
127 statusCode: "201",
128 headerName: "Location",
129 keyword: "schema",
130 expected: "/paths/users~1{id}/post/responses/201/headers/Location/schema",
131 },
132 {
133 name: "path with tilde and slash",
134 pathTemplate: "/some~path/to/resource",
135 method: "PUT",
136 statusCode: "204",
137 headerName: "ETag",
138 keyword: "type",
139 expected: "/paths/some~0path~1to~1resource/put/responses/204/headers/ETag/type",
140 },
141 }
142
143 for _, tt := range tests {
144 t.Run(tt.name, func(t *testing.T) {
145 result := ConstructResponseHeaderJSONPointer(tt.pathTemplate, tt.method, tt.statusCode, tt.headerName, tt.keyword)
146 assert.Equal(t, tt.expected, result)
147 })
148 }
149 }