Sha256: 2f0c7e51363c59f33c7b139db3f0b08236b788eddd8951e3f52e1c4f5fae196a

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 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
  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

1 entries across 1 versions & 1 rubygems

Version Path
mechanize-0.5.3 test/tc_upload.rb