From 72d09e65ca2fee3d281200d92ec63f15e8c19257 Mon Sep 17 00:00:00 2001 From: Damien Mathieu Date: Fri, 26 Mar 2010 10:48:43 +0100 Subject: [PATCH] when the url is a regex, we get it's parameters in the content --- moqueur.js | 32 +++++++++++++++++++++++++------- spec/fixtures/default_mocks.js | 2 +- spec/lib/moqueur_spec.rb | 6 ++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/moqueur.js b/moqueur.js index af024b9..5353176 100644 --- a/moqueur.js +++ b/moqueur.js @@ -6,7 +6,7 @@ $.mockAjax = function(params) { $._mocked.push(params); - } + }, $.ajax = function() { var args = arguments[0]; @@ -16,13 +16,31 @@ if ( ($._mocked[i].url.constructor == String && args.url == $._mocked[i].url) || ($._mocked[i].url.constructor == RegExp && args.url.search($._mocked[i].url) > -1) - ) - if (args.success != undefined) - return args.success($._mocked[i].content); - else - return $._mocked[i].content; + ) { + var content = $._mockAjax.parse_content($._mocked[i].url, args.url, $._mocked[i].content); + + if (args.success != undefined) + return args.success(content); + else + return content; + } }; return $._dup_ajax.apply($, arguments); - }; + }, + + + $._mockAjax = { + + parse_content: function(regex, url, content) { + if (regex.constructor == RegExp) { + var occurencies = regex.exec(url); + for(i=1; i <= occurencies.length; i++) { + content = content.replace('$' + i, occurencies[i]); + } + } + + return content; + } + } })(jQuery); \ No newline at end of file diff --git a/spec/fixtures/default_mocks.js b/spec/fixtures/default_mocks.js index d7e5460..f394c43 100644 --- a/spec/fixtures/default_mocks.js +++ b/spec/fixtures/default_mocks.js @@ -5,5 +5,5 @@ jQuery.mockAjax({ jQuery.mockAjax({ url: new RegExp('^/regex/([0-9]+)$'), - content: "Request with regex." + content: "Request with regex. Param: $1" }); \ No newline at end of file diff --git a/spec/lib/moqueur_spec.rb b/spec/lib/moqueur_spec.rb index 73fdff1..7c8dbd1 100644 --- a/spec/lib/moqueur_spec.rb +++ b/spec/lib/moqueur_spec.rb @@ -23,4 +23,10 @@ success: function() { return 'Executed Success method with regex'; } })").should eql('Executed Success method with regex') end + + it 'should give the param in the content when regex' do + @page.execute_js("jQuery.ajax({ + url: '/regex/1234' + })").should eql('Request with regex. Param: 1234') + end end \ No newline at end of file