#380 new

Fix for query string building when using Mechanize adapter

Reported by johnbintz | June 2nd, 2010 @ 09:23 AM

Webrat is incorrectly passing in an Array of Hashes to Mechanize for query string building, causing incorrect query strings to be constructed by Mechanize. This patch fixes the issue by correctly constructing a Hash for Mechanize rather than an Array of Hashes:

diff --git a/lib/webrat/core/elements/form.rb b/lib/webrat/core/elements/form.rb
index 5ab345c..77d9699 100644
--- a/lib/webrat/core/elements/form.rb
+++ b/lib/webrat/core/elements/form.rb
@@ -110,7 +110,7 @@ module Webrat
       when :rack, :sinatra
-        query_string.split('&').map {|query| { query.split('=').first => query.split('=').last }}
+        Hash[query_string.split('&').map {|query| [ query.split('=').first, query.split('=').last ]}]
diff --git a/spec/private/core/form_spec.rb b/spec/private/core/form_spec.rb
index aa28fa1..6c91eed 100644
--- a/spec/private/core/form_spec.rb
+++ b/spec/private/core/form_spec.rb
@@ -48,4 +48,22 @@ describe "Multiple nested params" do
     webrat_session.should_receive(:post).with("/family", params)
+  it "should correctly construct a query string" do
+    Webrat.configuration.mode = :mechanize
+    with_html <<-HTML
+      <html>
+        <form method="get" action="/search">
+          <input type="text" name="query" value="my-query" />
+          <input type="submit" />
+        </form>
+      </html>
+    HTML
+    params = { "query" => "my-query" }
+    webrat_session.should_receive(:get).with("/search", params)
+    click_button
+  end

Comments and changes to this ticket

  • Paolo "Nusco" Perrotta

    Paolo "Nusco" Perrotta June 29th, 2010 @ 08:56 PM

    • Milestone order changed from “0” to “0”

    I also fixed the same problem and asked for a pull. I fixed it straight in the Mechanize adapter, which you might like more or not. However, johnbintz provided a test, which I was unable to write - so you might want to use his patch, or merge the two patches.

  • johnbintz

    johnbintz June 30th, 2010 @ 08:21 AM

    Actually, a fix to the Mechanize adapter may be better, as my change and test don't take into account having more than one parameter with the same name, as successive ones will get overwritten.

  • Ryan Carver

    Ryan Carver August 26th, 2010 @ 11:27 AM

    This branch contains a couple fixes for Mechanize.

    • made sure mechanize is tested in spec/private/core/form_spec.rb this exposed the above error
    • made sure that the parsed Form.params is correctly built into nested params expected by Mechanize#post

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Ruby Acceptance Testing for Web applications.

Shared Ticket Bins