|
| 1 | +component extends="org.lucee.cfml.test.LuceeTestCase" labels="ajax,form,url" { |
| 2 | + |
| 3 | + function beforeAll() { |
| 4 | + variables.uri = createURI( "LDEV6070" ); |
| 5 | + } |
| 6 | + |
| 7 | + function run( testResults, testBox ) { |
| 8 | + describe( "LDEV-6070: Support bracket notation in form/URL parameters", function() { |
| 9 | + |
| 10 | + it( "should parse simple bracket notation into nested struct", function() { |
| 11 | + var result = _internalRequest( |
| 12 | + template: "#uri#/test.cfm", |
| 13 | + forms: { |
| 14 | + "user[name]": "John", |
| 15 | + "user[age]": "30" |
| 16 | + } |
| 17 | + ); |
| 18 | + expect( result.filecontent ).toInclude( "user.name=John" ); |
| 19 | + expect( result.filecontent ).toInclude( "user.age=30" ); |
| 20 | + }); |
| 21 | + |
| 22 | + it( "should parse deep bracket notation", function() { |
| 23 | + var result = _internalRequest( |
| 24 | + template: "#uri#/test.cfm", |
| 25 | + forms: { |
| 26 | + "user[address][city]": "Sydney", |
| 27 | + "user[address][country]": "Australia" |
| 28 | + } |
| 29 | + ); |
| 30 | + expect( result.filecontent ).toInclude( "user.address.city=Sydney" ); |
| 31 | + expect( result.filecontent ).toInclude( "user.address.country=Australia" ); |
| 32 | + }); |
| 33 | + |
| 34 | + it( "should parse mixed dot and bracket notation", function() { |
| 35 | + var result = _internalRequest( |
| 36 | + template: "#uri#/test.cfm", |
| 37 | + forms: { |
| 38 | + "user.address[city]": "Sydney", |
| 39 | + "user.address[zip]": "2000" |
| 40 | + } |
| 41 | + ); |
| 42 | + expect( result.filecontent ).toInclude( "user.address.city=Sydney" ); |
| 43 | + expect( result.filecontent ).toInclude( "user.address.zip=2000" ); |
| 44 | + }); |
| 45 | + |
| 46 | + it( "should handle array notation with brackets", function() { |
| 47 | + var result = _internalRequest( |
| 48 | + template: "#uri#/test.cfm", |
| 49 | + forms: { |
| 50 | + "tags[]": [ "java", "cfml", "lucee" ] |
| 51 | + } |
| 52 | + ); |
| 53 | + expect( result.filecontent ).toInclude( "tags=java,cfml,lucee" ); |
| 54 | + }); |
| 55 | + |
| 56 | + it( "should handle complex nested structures", function() { |
| 57 | + var result = _internalRequest( |
| 58 | + template: "#uri#/test.cfm", |
| 59 | + forms: { |
| 60 | + "order[customer][name]": "Jane", |
| 61 | + "order[customer][email]": "jane@example.com", |
| 62 | + "order[items][0][product]": "Widget", |
| 63 | + "order[items][0][qty]": "2", |
| 64 | + "order[items][1][product]": "Gadget", |
| 65 | + "order[items][1][qty]": "5" |
| 66 | + } |
| 67 | + ); |
| 68 | + expect( result.filecontent ).toInclude( "order.customer.name=Jane" ); |
| 69 | + // Numeric indices create struct keys, not array indices |
| 70 | + expect( result.filecontent ).toInclude( "order.items.0.product=Widget" ); |
| 71 | + expect( result.filecontent ).toInclude( "order.items.1.qty=5" ); |
| 72 | + }); |
| 73 | + |
| 74 | + // Remote CFC calls need different test approach - internalRequest doesn't work same way |
| 75 | + xit( "should work with remote CFC calls", function() { |
| 76 | + var result = _internalRequest( |
| 77 | + template: "#uri#/TestService.cfc?method=testMethod", |
| 78 | + forms: { |
| 79 | + "userData[name]": "John", |
| 80 | + "userData[sellerID]": "12345" |
| 81 | + } |
| 82 | + ); |
| 83 | + var data = deserializeJSON( result.filecontent ); |
| 84 | + expect( data ).toHaveKey( "userData" ); |
| 85 | + expect( data.userData.name ).toBe( "John" ); |
| 86 | + expect( data.userData.sellerID ).toBe( "12345" ); |
| 87 | + }); |
| 88 | + |
| 89 | + it( "should respect formUrlAsStruct=false setting", function() { |
| 90 | + var result = _internalRequest( |
| 91 | + template: "#uri#/test-disabled/test-disabled.cfm", |
| 92 | + forms: { |
| 93 | + "user[name]": "John" |
| 94 | + } |
| 95 | + ); |
| 96 | + // Should have literal key, not nested |
| 97 | + expect( result.filecontent ).toInclude( "user[name]=John" ); |
| 98 | + }); |
| 99 | + |
| 100 | + it( "should handle malformed brackets - missing closing bracket", function() { |
| 101 | + var result = _internalRequest( |
| 102 | + template: "#uri#/test.cfm", |
| 103 | + forms: { |
| 104 | + "user[name": "John" |
| 105 | + } |
| 106 | + ); |
| 107 | + // Should treat as literal key when malformed |
| 108 | + expect( result.filecontent ).toInclude( "user[name=John" ); |
| 109 | + }); |
| 110 | + |
| 111 | + it( "should handle malformed brackets - missing opening bracket", function() { |
| 112 | + var result = _internalRequest( |
| 113 | + template: "#uri#/test.cfm", |
| 114 | + forms: { |
| 115 | + "username]": "John" |
| 116 | + } |
| 117 | + ); |
| 118 | + // Should treat as literal key when malformed |
| 119 | + expect( result.filecontent ).toInclude( "username]=John" ); |
| 120 | + }); |
| 121 | + |
| 122 | + it( "should skip empty bracket segments", function() { |
| 123 | + var result = _internalRequest( |
| 124 | + template: "#uri#/test.cfm", |
| 125 | + forms: { |
| 126 | + "user[]name": "John" |
| 127 | + } |
| 128 | + ); |
| 129 | + // Empty brackets should be skipped: user[]name -> user.name |
| 130 | + expect( result.filecontent ).toInclude( "user.name=John" ); |
| 131 | + }); |
| 132 | + |
| 133 | + it( "should skip empty dot segments", function() { |
| 134 | + var result = _internalRequest( |
| 135 | + template: "#uri#/test.cfm", |
| 136 | + forms: { |
| 137 | + "user..name": "John" |
| 138 | + } |
| 139 | + ); |
| 140 | + // Empty dot segments should be skipped: user..name -> user.name |
| 141 | + expect( result.filecontent ).toInclude( "user.name=John" ); |
| 142 | + }); |
| 143 | + |
| 144 | + it( "should skip trailing dots and brackets", function() { |
| 145 | + var result = _internalRequest( |
| 146 | + template: "#uri#/test.cfm", |
| 147 | + forms: { |
| 148 | + "user.": "John" |
| 149 | + } |
| 150 | + ); |
| 151 | + // Trailing dot should be skipped: user. -> user |
| 152 | + expect( result.filecontent ).toInclude( "user=John" ); |
| 153 | + }); |
| 154 | + |
| 155 | + it( "should skip leading dots", function() { |
| 156 | + var result = _internalRequest( |
| 157 | + template: "#uri#/test.cfm", |
| 158 | + forms: { |
| 159 | + ".name": "John" |
| 160 | + } |
| 161 | + ); |
| 162 | + // Leading dot should be skipped: .name -> name |
| 163 | + expect( result.filecontent ).toInclude( "name=John" ); |
| 164 | + }); |
| 165 | + |
| 166 | + it( "should handle special characters in bracket keys", function() { |
| 167 | + var result = _internalRequest( |
| 168 | + template: "#uri#/test.cfm", |
| 169 | + forms: { |
| 170 | + "user[first-name]": "John", |
| 171 | + "user[last_name]": "Doe" |
| 172 | + } |
| 173 | + ); |
| 174 | + expect( result.filecontent ).toInclude( "user.first-name=John" ); |
| 175 | + expect( result.filecontent ).toInclude( "user.last_name=Doe" ); |
| 176 | + }); |
| 177 | + |
| 178 | + it( "should handle numeric string keys vs array indices", function() { |
| 179 | + var result = _internalRequest( |
| 180 | + template: "#uri#/test.cfm", |
| 181 | + forms: { |
| 182 | + "items[0]": "first", |
| 183 | + "items[1]": "second", |
| 184 | + "items[foo]": "bar" |
| 185 | + } |
| 186 | + ); |
| 187 | + // Numeric indices create struct keys, not array |
| 188 | + expect( result.filecontent ).toInclude( "items.0=first" ); |
| 189 | + expect( result.filecontent ).toInclude( "items.1=second" ); |
| 190 | + expect( result.filecontent ).toInclude( "items.foo=bar" ); |
| 191 | + }); |
| 192 | + |
| 193 | + it( "should handle deeply nested brackets", function() { |
| 194 | + var result = _internalRequest( |
| 195 | + template: "#uri#/test.cfm", |
| 196 | + forms: { |
| 197 | + "a[b][c][d][e][f]": "deep" |
| 198 | + } |
| 199 | + ); |
| 200 | + expect( result.filecontent ).toInclude( "a.b.c.d.e.f=deep" ); |
| 201 | + }); |
| 202 | + |
| 203 | + }); |
| 204 | + } |
| 205 | + |
| 206 | + private string function createURI( string calledName ) { |
| 207 | + var baseURI = "/test/#listLast( getDirectoryFromPath( getCurrenttemplatepath() ), "\/" )#/"; |
| 208 | + return baseURI & "" & calledName; |
| 209 | + } |
| 210 | + |
| 211 | +} |
0 commit comments