@@ -58,6 +58,70 @@ describe("core/openapi-utils", () => {
5858 ] )
5959 } )
6060
61- // todo: expand tests for special characters / escaping
61+ it ( "handles special characters in placeholder names" , ( ) => {
62+ // template-expression-param-name = 1*( %x00-7A / %x7C / %x7E-10FFFF )
63+ // essentially everything except { and }
64+
65+ const specialNames = [
66+ "id:with-colon" ,
67+ "id@with-at" ,
68+ "id.with-dot" ,
69+ "id_with-underscore" ,
70+ "id~with-tilde" ,
71+ "id!with-bang" ,
72+ "id$with-dollar" ,
73+ "id&with-ampersand" ,
74+ "id'with-quote" ,
75+ "id(with-parens)" ,
76+ "id*with-asterisk" ,
77+ "id+with-plus" ,
78+ "id,with-comma" ,
79+ "id;with-semicolon" ,
80+ "id=with-equals" ,
81+ "id%20with-percent" ,
82+ "id中文" , // Unicode
83+ ]
84+
85+ for ( const name of specialNames ) {
86+ expect ( extractPlaceholders ( `/{${ name } }` ) ) . toStrictEqual ( [
87+ {
88+ placeholder : name ,
89+ wholeString : `{${ name } }` ,
90+ } ,
91+ ] )
92+ }
93+ } )
94+
95+ it ( "handles /, ?, and # in placeholder names as per ABNF" , ( ) => {
96+ // although these are forbidden in parameter values, the ABNF for the name allows them
97+ const names = [ "name/with/slash" , "name?with?question" , "name#with#hash" ]
98+
99+ for ( const name of names ) {
100+ expect ( extractPlaceholders ( `/{${ name } }` ) ) . toStrictEqual ( [
101+ {
102+ placeholder : name ,
103+ wholeString : `{${ name } }` ,
104+ } ,
105+ ] )
106+ }
107+ } )
108+
109+ it ( "does not extract empty placeholders" , ( ) => {
110+ // template-expression-param-name = 1*...
111+ expect ( extractPlaceholders ( "/{}" ) ) . toStrictEqual ( [ ] )
112+ } )
113+
114+ it ( "handles multiple placeholders in a single path segment" , ( ) => {
115+ expect ( extractPlaceholders ( "/{foo}{bar}" ) ) . toStrictEqual ( [
116+ { placeholder : "foo" , wholeString : "{foo}" } ,
117+ { placeholder : "bar" , wholeString : "{bar}" } ,
118+ ] )
119+ } )
120+
121+ it ( "handles placeholders mixed with literals in a single path segment" , ( ) => {
122+ expect ( extractPlaceholders ( "/prefix{foo}suffix" ) ) . toStrictEqual ( [
123+ { placeholder : "foo" , wholeString : "{foo}" } ,
124+ ] )
125+ } )
62126 } )
63127} )
0 commit comments