From 3b724592980cdd1e7d36fbdebbcfa0a81a706c6c Mon Sep 17 00:00:00 2001 From: Kyle Hargraves Date: Fri, 16 May 2008 19:22:41 -0500 Subject: [PATCH 1/2] Support '&' in submitted values --- lib/webrat/core/field.rb | 3 ++- spec/api/selects_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 471d0a4..f3e9307 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -43,7 +43,8 @@ module Webrat end def to_param - param_parser.parse_query_parameters("#{name}=#{@value}") + value = @value.to_s.gsub('&', '%26') + param_parser.parse_query_parameters("#{name}=#{value}") end def set(value) diff --git a/spec/api/selects_spec.rb b/spec/api/selects_spec.rb index 68503ad..1ffdc16 100644 --- a/spec/api/selects_spec.rb +++ b/spec/api/selects_spec.rb @@ -48,6 +48,18 @@ describe "selects" do @session.clicks_button end + it "should send values with HTML encoded ampersands" do + @session.response_body = <<-EOS +
+ + +
+ EOS + @session.expects(:post).with("/login", "encoded" => "A & B") + @session.selects "Encoded", :from => "encoded" + @session.clicks_button + end + it "should work with empty select lists" do @session.response_body = <<-EOS
From 17f4dba96588672dd65fa3428a5816ef8d0a0410 Mon Sep 17 00:00:00 2001 From: Kyle Hargraves Date: Wed, 21 May 2008 14:27:47 -0500 Subject: [PATCH 2/2] Support specification of content type in attaches_file() --- lib/webrat/core/field.rb | 10 +++++++++- lib/webrat/core/page.rb | 9 +++++---- spec/api/attaches_file_spec.rb | 13 +++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index f3e9307..cd732e4 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -240,11 +240,19 @@ module Webrat class FileField < Field + attr_accessor :content_type + + def set(value, content_type = nil) + super(value) + @content_type = content_type + end + def to_param if @value.nil? super else - replace_param_value(super, @value, ActionController::TestUploadedFile.new(@value)) + file = content_type ? ActionController::TestUploadedFile.new(@value, content_type) : ActionController::TestUploadedFile.new(@value) + replace_param_value(super, @value, file) end end diff --git a/lib/webrat/core/page.rb b/lib/webrat/core/page.rb index ce964b1..c562cd0 100644 --- a/lib/webrat/core/page.rb +++ b/lib/webrat/core/page.rb @@ -91,13 +91,14 @@ module Webrat # Verifies that an input file field exists on the current page and sets # its value to the given +file+, so that the file will be uploaded - # along with the form. + # along with the form. An optional content_type may be given. # # Example: - # attaches_file "Photo", "/path/to/the/photo.jpg" - def attaches_file(id_or_name_or_label, path) + # attaches_file "Resume", "/path/to/the/resume.txt" + # attaches_file "Photo", "/path/to/the/image.png", "image/png" + def attaches_file(id_or_name_or_label, path, content_type = nil) field = find_field(id_or_name_or_label, FileField) - field.set(path) + field.set(path, content_type) end # Saves the page out to RAILS_ROOT/tmp/ and opens it in the default diff --git a/spec/api/attaches_file_spec.rb b/spec/api/attaches_file_spec.rb index f807f6e..a6792c3 100644 --- a/spec/api/attaches_file_spec.rb +++ b/spec/api/attaches_file_spec.rb @@ -56,4 +56,17 @@ describe "attaches_file" do @session.attaches_file "Spreadsheet", @filename @session.clicks_button end + + it "should allow the content type to be specified" do + @session.response_body = <<-EOS + + + + + + EOS + ActionController::TestUploadedFile.expects(:new).with(@filename, "image/png").returns(@uploaded_file) + @session.attaches_file "Picture", @filename, "image/png" + @session.clicks_button + end end