File: tc_upload.rb

package info (click to toggle)
libwww-mechanize-ruby 0.7.6-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 752 kB
  • ctags: 607
  • sloc: ruby: 4,883; makefile: 4
file content (109 lines) | stat: -rw-r--r-- 3,209 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require File.dirname(__FILE__) + "/helper"

class UploadMechTest < Test::Unit::TestCase
  def setup
    @agent = WWW::Mechanize.new
    @page = @agent.get("http://localhost/file_upload.html")
  end

  def test_upload_with_post_param
    page = @agent.post('http://localhost/file_upload', {
      :name       => 'Some file',
      :userfile1  => File.open(__FILE__)
    })
    assert_match(
      "Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{File.basename(__FILE__)}\"",
      page.body
    )
    assert page.body.length > File.read(__FILE__).length
  end

  def test_form_enctype
    assert_equal('multipart/form-data', @page.forms[0].enctype)

    form = @page.forms.first
    form.file_uploads.first.file_name = "#{BASE_DIR}/test_all.rb"
    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=\"test_all.rb\"",
      @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
    assert_equal('multipart/form-data', @page.forms[1].enctype)

    form = @page.forms[1]
    form.file_uploads.first.file_name = "#{BASE_DIR}/test_all.rb"
    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=\"test_all.rb\"",
      @page.body
    )
  end

  def test_form_read_file
    assert_equal('multipart/form-data', @page.forms[1].enctype)

    form = @page.forms[1]
    form.file_uploads.first.file_name = "#{BASE_DIR}/test_all.rb"

    @page = @agent.submit(form)

    contents = File.open("#{BASE_DIR}/test_all.rb", 'rb') { |f| f.read }
    assert_match(
      "Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"test_all.rb\"",
      @page.body
    )
    assert_match(contents, @page.body)
  end

  def test_form_io_obj
    assert_equal('multipart/form-data', @page.forms[1].enctype)

    form = @page.forms[1]
    form.file_uploads.first.file_name = "#{BASE_DIR}/test_all.rb"
    form.file_uploads.first.file_data = File.open("#{BASE_DIR}/test_all.rb", 'rb')

    @page = @agent.submit(form)

    contents = File.open("#{BASE_DIR}/test_all.rb", 'rb') { |f| f.read }
    assert_match(
      "Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"test_all.rb\"",
      @page.body
    )
    assert_match(contents, @page.body)
  end

  def test_submit_no_file
    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

  def test_no_value
    form = @page.form('value_test')
    assert_nil(form.file_uploads.first.value)
    assert_nil(form.file_uploads.first.file_name)
  end
end