Skip to content

Commit

Permalink
Support specification of content type in attaches_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
pd committed Jun 5, 2008
1 parent 3b72459 commit 17f4dba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/webrat/core/field.rb
Expand Up @@ -240,11 +240,19 @@ def default_value

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

Expand Down
9 changes: 5 additions & 4 deletions lib/webrat/core/page.rb
Expand Up @@ -91,13 +91,14 @@ def selects(option_text, options = {})

# 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 <tt>content_type</tt> 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
Expand Down
13 changes: 13 additions & 0 deletions spec/api/attaches_file_spec.rb
Expand Up @@ -56,4 +56,17 @@
@session.attaches_file "Spreadsheet", @filename
@session.clicks_button
end

it "should allow the content type to be specified" do
@session.response_body = <<-EOS
<form method="post" action="/widgets">
<label for="person_picture">Picture</label>
<input type="file" id="person_picture" name="person[picture]" />
<input type="submit" />
</form>
EOS
ActionController::TestUploadedFile.expects(:new).with(@filename, "image/png").returns(@uploaded_file)
@session.attaches_file "Picture", @filename, "image/png"
@session.clicks_button
end
end

0 comments on commit 17f4dba

Please sign in to comment.