Sha256: 0977e2fc219d7c9362282ab03d62977d3df6dfdece3ba164703296bf98e9fc90

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

$:.unshift File.join(File.dirname(__FILE__), "..", "lib")

require 'test/unit'
require 'rubygems'
require 'mechanize'
require 'test_includes'

class UploadMechTest < Test::Unit::TestCase
  include TestMethods

  def setup
    @agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
  end

  def test_form_enctype
    page = @agent.get("http://localhost:#{PORT}/file_upload.html")
    assert_equal('multipart/form-data', page.forms[0].enctype)

    form = page.forms.first
    form.file_uploads.first.file_name = "README"
    form.file_uploads.first.mime_type = "text/plain"
    form.file_uploads.first.file_data = "Hello World\n\n"

    page = @agent.submit(form)

    assert_match(
      "Content-Disposition: form-data; name=\"userfile1\"; filename=\"README\"",
      page.body
    )
    assert_match(
      "Content-Disposition: form-data; name=\"name\"",
      page.body
    )
    assert_match('Content-Type: text/plain', page.body)
    assert_match('Hello World', page.body)
    assert_match('foo[aaron]', page.body)
  end

  def test_form_multipart
    page = @agent.get("http://localhost:#{PORT}/file_upload.html")
    assert_equal('multipart/form-data', page.forms[1].enctype)

    form = page.forms[1]
    form.file_uploads.first.file_name = "README"
    form.file_uploads.first.mime_type = "text/plain"
    form.file_uploads.first.file_data = "Hello World\n\n"

    page = @agent.submit(form)

    assert_match(
      "Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"README\"",
      page.body
    )
  end

  def test_submit_no_file
    page = @agent.get("http://localhost:#{PORT}/file_upload.html")
    form = page.forms.first
    form.fields.name('name').value = 'Aaron'
    page = @agent.submit(form)
    assert_match('Aaron', page.body)
    assert_match(
      "Content-Disposition: form-data; name=\"userfile1\"; filename=\"\"",
      page.body
    )
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mechanize-0.5.1 test/tc_upload.rb
mechanize-0.5.2 test/tc_upload.rb