|
149 | 149 | end |
150 | 150 |
|
151 | 151 | context 'with path encoding' do |
152 | | - it 'should handle properly encoded URIs (with double encoding)' do |
| 152 | + it 'should handle already encoded URIs without double encoding' do |
153 | 153 | relative_uri = '/1.0/kb/accounts/my%20account%20with%20spaces' |
154 | 154 | options = {} |
155 | 155 | uri = http_adapter.send(:build_uri, relative_uri, options) |
156 | | - expect(uri.to_s).to eq('http://example.com:8080/%2F1.0%2Fkb%2Faccounts%2Fmy%2520account%2520with%2520spaces') |
| 156 | + expect(uri.to_s).to eq('http://example.com:8080/1.0/kb/accounts/my%20account%20with%20spaces') |
157 | 157 | end |
158 | 158 |
|
159 | 159 | it 'should not encode safe characters in path' do |
|
163 | 163 | expect(uri.to_s).to eq('http://example.com:8080/1.0/kb/accounts/abc-1234') |
164 | 164 | end |
165 | 165 |
|
166 | | - it 'should handle properly encoded URI with query string (with double encoding)' do |
| 166 | + it 'should handle already encoded URI with query string without double encoding' do |
167 | 167 | relative_uri = '/1.0/kb/accounts/my%20account?search=test%20value' |
168 | 168 | options = {} |
169 | 169 | uri = http_adapter.send(:build_uri, relative_uri, options) |
170 | | - expect(uri.to_s).to eq('http://example.com:8080/%2F1.0%2Fkb%2Faccounts%2Fmy%2520account?search=test%20value') |
| 170 | + expect(uri.to_s).to eq('http://example.com:8080/1.0/kb/accounts/my%20account?search=test%20value') |
171 | 171 | end |
172 | 172 | end |
173 | 173 |
|
|
238 | 238 | expect(uri.query).to include('controlPluginName=plugin2') |
239 | 239 | end |
240 | 240 | end |
| 241 | + |
| 242 | + context 'with spaces in path segments' do |
| 243 | + it 'should encode spaces in relative URI path segments' do |
| 244 | + relative_uri = '/1.0/kb/accounts/search/Kill Bill Client' |
| 245 | + options = {} |
| 246 | + uri = http_adapter.send(:build_uri, relative_uri, options) |
| 247 | + expect(uri.to_s).to eq('http://example.com:8080/1.0/kb/accounts/search/Kill%20Bill%20Client') |
| 248 | + end |
| 249 | + |
| 250 | + it 'should handle absolute URI with spaces in path' do |
| 251 | + relative_uri = 'http://127.0.0.1:8080/1.0/kb/accounts/search/Kill Bill Client' |
| 252 | + options = {} |
| 253 | + uri = http_adapter.send(:build_uri, relative_uri, options) |
| 254 | + expect(uri.to_s).to eq('http://127.0.0.1:8080/1.0/kb/accounts/search/Kill%20Bill%20Client') |
| 255 | + end |
| 256 | + |
| 257 | + it 'should preserve scheme and host when encoding spaces in absolute URI' do |
| 258 | + relative_uri = 'https://api.example.com:9090/search/My Test Account' |
| 259 | + options = {} |
| 260 | + uri = http_adapter.send(:build_uri, relative_uri, options) |
| 261 | + expect(uri.to_s).to eq('https://api.example.com:9090/search/My%20Test%20Account') |
| 262 | + expect(uri.scheme).to eq('https') |
| 263 | + expect(uri.host).to eq('api.example.com') |
| 264 | + expect(uri.port).to eq(9090) |
| 265 | + end |
| 266 | + end |
| 267 | + |
| 268 | + context 'with nil values in query parameters' do |
| 269 | + it 'should skip nil values in params' do |
| 270 | + relative_uri = '/1.0/kb/accounts' |
| 271 | + options = { |
| 272 | + params: { |
| 273 | + account: nil, |
| 274 | + withStackTrace: true, |
| 275 | + limit: 10 |
| 276 | + } |
| 277 | + } |
| 278 | + uri = http_adapter.send(:build_uri, relative_uri, options) |
| 279 | + expect(uri.query).not_to include('account=') |
| 280 | + expect(uri.query).to include('withStackTrace=true') |
| 281 | + expect(uri.query).to include('limit=10') |
| 282 | + end |
| 283 | + |
| 284 | + it 'should handle all nil values in params' do |
| 285 | + relative_uri = '/1.0/kb/accounts' |
| 286 | + options = { |
| 287 | + params: { |
| 288 | + account: nil, |
| 289 | + tenant: nil |
| 290 | + } |
| 291 | + } |
| 292 | + uri = http_adapter.send(:build_uri, relative_uri, options) |
| 293 | + expect(uri.query).to be_nil |
| 294 | + end |
| 295 | + |
| 296 | + it 'should handle mixed nil and valid values' do |
| 297 | + relative_uri = '/1.0/kb/accounts' |
| 298 | + options = { |
| 299 | + params: { |
| 300 | + account: nil, |
| 301 | + withStackTrace: true, |
| 302 | + offset: 0, |
| 303 | + search: nil, |
| 304 | + limit: 50 |
| 305 | + } |
| 306 | + } |
| 307 | + uri = http_adapter.send(:build_uri, relative_uri, options) |
| 308 | + expect(uri.query).not_to include('account=') |
| 309 | + expect(uri.query).not_to include('search=') |
| 310 | + expect(uri.query).to include('withStackTrace=true') |
| 311 | + expect(uri.query).to include('offset=0') |
| 312 | + expect(uri.query).to include('limit=50') |
| 313 | + end |
| 314 | + end |
| 315 | + end |
| 316 | + |
| 317 | + describe '#encode_params' do |
| 318 | + let(:http_adapter) { DummyForHTTPAdapter.new } |
| 319 | + |
| 320 | + it 'should filter out nil values from params' do |
| 321 | + options = { |
| 322 | + params: { |
| 323 | + account: nil, |
| 324 | + withStackTrace: true, |
| 325 | + limit: 10, |
| 326 | + search: nil |
| 327 | + } |
| 328 | + } |
| 329 | + query_string = http_adapter.send(:encode_params, options) |
| 330 | + expect(query_string).to include('withStackTrace=true') |
| 331 | + expect(query_string).to include('limit=10') |
| 332 | + expect(query_string).not_to include('account=') |
| 333 | + expect(query_string).not_to include('search=') |
| 334 | + end |
| 335 | + |
| 336 | + it 'should return nil when all params are nil' do |
| 337 | + options = { |
| 338 | + params: { |
| 339 | + account: nil, |
| 340 | + tenant: nil |
| 341 | + } |
| 342 | + } |
| 343 | + query_string = http_adapter.send(:encode_params, options) |
| 344 | + expect(query_string).to be_nil |
| 345 | + end |
| 346 | + |
| 347 | + it 'should return nil when params hash is empty' do |
| 348 | + options = { params: {} } |
| 349 | + query_string = http_adapter.send(:encode_params, options) |
| 350 | + expect(query_string).to be_nil |
| 351 | + end |
| 352 | + |
| 353 | + it 'should handle options without params key' do |
| 354 | + options = { account: nil, withStackTrace: true } |
| 355 | + query_string = http_adapter.send(:encode_params, options) |
| 356 | + expect(query_string).to be_nil |
| 357 | + end |
241 | 358 | end |
242 | 359 | end |
243 | 360 |
|
|
0 commit comments