GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
Preserve order parse query, respect query string override in test request

* added option 'preserve_order' to query_parse. retaining order of args is

  necessary to generate cache urls that nginx/apache can match.
* allow params_to_query_string to accept Dictionary and respect order of 
args
* Test::RequestHelper.dispatch_to method creates QUERY_STRING env variable

  from params and overwrites passed-in value. this behaviour does not 
  match
  live environment. it also makes it impossible to generate consistent 
  cache
  keys from query. this tests if QUERY_STRING exists before overwriting 
  it.

Signed-off-by: Michael S. Klishin <michael@novemberain.com>
bchiu (author)
Fri May 23 14:12:54 -0700 2008
michaelklishin (committer)
Sun Jun 01 23:21:43 -0700 2008
commit  2d452bf4731f398c54d98b0a364f2a64c98fc7f8
tree    c6b925aee2481c0d4d74069d12525c21585ea676
parent  a5326c7f47aba67b1aa6b9a73c3670e40fcf26df
...
396
397
398
399
 
400
401
402
...
420
421
422
423
 
424
425
426
...
454
455
456
 
457
458
459
 
460
461
462
463
464
465
 
 
 
466
467
468
 
 
469
470
471
...
396
397
398
 
399
400
401
402
...
420
421
422
 
423
424
425
426
...
454
455
456
457
458
459
 
460
461
462
463
464
 
 
465
466
467
468
469
 
470
471
472
473
474
0
@@ -396,7 +396,7 @@ module Merb
0
     class << self
0
       
0
       # ==== Parameters
0
- # value<Array, Hash, ~to_s>:: The value for the query string.
0
+ # value<Array, Hash, Dictionary ~to_s>:: The value for the query string.
0
       # prefix<~to_s>:: The prefix to add to the query string keys.
0
       #
0
       # ==== Returns
0
@@ -420,7 +420,7 @@ module Merb
0
           value.map { |v|
0
             params_to_query_string(v, "#{prefix}[]")
0
           } * "&"
0
- when Hash
0
+ when Hash, Dictionary
0
           value.map { |k, v|
0
             params_to_query_string(v, prefix ? "#{prefix}[#{Merb::Request.escape(k)}]" : Merb::Request.escape(k))
0
           } * "&"
0
@@ -454,18 +454,21 @@ module Merb
0
       # ==== Parameters
0
       # qs<String>:: The query string.
0
       # d<String>:: The query string divider. Defaults to "&".
0
+ # preserve_order<Boolean>:: Preserve order of args. Defaults to false.
0
       #
0
       # ==== Returns
0
- # Mash:: The parsed query string.
0
+ # Mash:: The parsed query string (Dictionary if preserve_order is set).
0
       #
0
       # ==== Examples
0
       # query_parse("bar=nik&post[body]=heya")
0
       # # => { :bar => "nik", :post => { :body => "heya" } }
0
- def query_parse(qs, d = '&;')
0
- (qs||'').split(/[#{d}] */n).inject({}) { |h,p|
0
+ def query_parse(qs, d = '&;', preserve_order = false)
0
+ qh = preserve_order ? Dictionary.new : {}
0
+ (qs||'').split(/[#{d}] */n).inject(qh) { |h,p|
0
           key, value = unescape(p).split('=',2)
0
           normalize_params(h, key, value)
0
- }.to_mash
0
+ }
0
+ preserve_order ? qh : qh.to_mash
0
       end
0
     
0
       NAME_REGEX = /Content-Disposition:.* name="?([^\";]*)"?/ni.freeze
...
99
100
101
102
103
104
 
 
105
106
107
...
99
100
101
 
 
 
102
103
104
105
106
0
@@ -99,9 +99,8 @@ module Merb
0
       def dispatch_to(controller_klass, action, params = {}, env = {}, &blk)
0
         action = action.to_s
0
         request_body = { :post_body => env[:post_body], :req => env[:req] }
0
- request = fake_request(env.merge(
0
- :query_string => Merb::Request.params_to_query_string(params)), request_body)
0
-
0
+ env = env.merge(:query_string => Merb::Request.params_to_query_string(params)) unless env.key?('QUERY_STRING')
0
+ request = fake_request(env, request_body)
0
         dispatch_request(request, controller_klass, action, &blk)
0
       end
0
 

Comments

    No one has commented yet.