Permalink
Browse files

parse query strings

  • Loading branch information...
Kellen Presley
Kellen Presley committed Jun 24, 2010
1 parent a983910 commit 8c38176d3416bd3f501559bee715bbecc6209068
Showing with 28 additions and 5 deletions.
  1. +2 −0 picard/lib/picard.js
  2. +17 −5 picard/lib/picard/request_extensions.js
  3. +4 −0 sample_app/app.js
  4. +5 −0 sample_app/spec/app_spec.rb
View
@@ -12,6 +12,8 @@ Picard.merge({
Picard.merge(request, Picard.request_extensions)
request.response = response
+ locals._extract_path_params.call(request)
+
request.addListener('data', function(chunk){
locals._extract_form_params.call(request, chunk)
})
@@ -147,15 +147,27 @@ var locals = {
_extract_form_params: function(chunk){
try {
if( chunk == undefined ) { return }
- var chunks = chunk.toString().replace(/\+/g, '%20').split('&')
- for(var i in chunks){
- var k_v = chunks[i].split('=')
- this[k_v[0]] = decodeURIComponent(k_v[1])
- }
+ locals._extract_params.call(this, chunk)
} catch(ex) {
this.handle_exception(ex)
}
},
+ _extract_path_params: function(){
+ try {
+ var query = this.parsed_url().query
+ if ( query == undefined ){ return }
+ locals._extract_params.call(this, query)
+ } catch(ex){
+ this.handle_exception(ex)
+ }
+ },
+ _extract_params: function(string){
+ var chunks = string.toString().replace(/\+/g, '%20').split('&')
+ for(var i in chunks){
+ var k_v = chunks[i].split('=')
+ this[k_v[0]] = decodeURIComponent(k_v[1])
+ }
+ },
_extract_route_params: function(route, match_data){
var i, l
View
@@ -92,6 +92,10 @@ get(/\/selective\/(\d+)/, function(params){ // must be a number
return { text: params.captures[0] }
})
+get('/get_with_params', function(env){ // called with ?foo=bar&baz=bat
+ return { text: env.foo + " " + env.baz }
+})
+
get('/this_will_fail', function(){
foo.bar // foo is undefined
})
@@ -70,6 +70,11 @@
res.header_str.should include('500')
res.body_str.should include("<h3>foo is not defined</h3>")
end
+
+ it 'should extract params from a url with a query string' do
+ res = Curl::Easy.perform(base_url + '/get_with_params?foo=bar&baz=bat')
+ res.body_str.should include("bar bat")
+ end
end

0 comments on commit 8c38176

Please sign in to comment.