Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mechanize #post does not retain order of fields defined #307

Closed
jasonwbarnett opened this issue May 5, 2013 · 5 comments · Fixed by #315
Closed

Mechanize #post does not retain order of fields defined #307

jasonwbarnett opened this issue May 5, 2013 · 5 comments · Fixed by #315

Comments

@jasonwbarnett
Copy link

I'm working on a project where the order of which fields are passed to the query is crucial. I was trying to build something and realized that the order was not retained. I believe it only happens when one of the fields contains bracket in it, i.e.

field name: rsargs[]
field value: anything

Code to reproduce the problem: https://gist.github.com/jasonwbarnett/249aa44a0e2b3ac078fe

I know that a lot of the code probably makes no sense (nor does the server response), but it conveys the problem quite well.

Version Info:
Mechanize/2.6.0
Ruby/1.9.3p392

@ryansch
Copy link
Contributor

ryansch commented May 9, 2013

I just hit my head on this in phillbaker/capybara-mechanize#52.

@ryansch
Copy link
Contributor

ryansch commented May 9, 2013

My analysis points to https://github.com/sparklemotion/mechanize/blob/master/lib/mechanize/form.rb#L259 as the source of the problem. Mechanize seems to go out of its way to define a custom sorting for fields (https://github.com/sparklemotion/mechanize/blob/master/lib/mechanize/form/field.rb#L39). Why is this?

@drbrain
Copy link
Member

drbrain commented May 10, 2013

The sort in mechanize/form.rb is designed to keep the fields in the order they appear in the document and the case of a generic POST as in your example was not considered.

I think this can be fixed by altering Mechanize#post to store entries with a :order field in addition to the 'name' field that Mechanize::Form::Field#<=> uses to keep custom entries in the proper position.

@ryansch
Copy link
Contributor

ryansch commented May 10, 2013

I dove deeper into the '#sort' stuff and it works as intended as long as each node is backed by nokogiri. If you build up a form from hashes, the sort order is incorrect.

Edit: Looks like @jasonwbarnett is doing a very similar thing with arrays.

@drbrain I can change the way we're using mechanize in capybara-mechanize to rely more on nokogiri. Is this an issue you want fixed in mechanize itself?

@ryansch
Copy link
Contributor

ryansch commented May 10, 2013

Another clarification. Jason and I are having the same problem from 2 different entry points. I'm building a form with code similar to @m_form.fields << Mechanize::Form::Field.new({'name' => key.to_s}, value) and then calling @agent.submit on that form. Jason is calling @agent.post which builds a form consisting of fields built from hashes as well. The end result is the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants