File: tc_curl_postfield.rb

package info (click to toggle)
ruby-curb 0.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 604 kB
  • ctags: 880
  • sloc: ansic: 4,242; ruby: 2,768; makefile: 3
file content (143 lines) | stat: -rw-r--r-- 3,961 bytes parent folder | download | duplicates (3)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))

class TestCurbCurlPostfield < Test::Unit::TestCase
  def test_private_new
    assert_raise(NoMethodError) { Curl::PostField.new }  
  end
  
  def test_new_content_01
    pf = Curl::PostField.content('foo', 'bar')
    
    assert_equal 'foo', pf.name
    assert_equal 'bar', pf.content
    assert_nil pf.content_type
    assert_nil pf.local_file
    assert_nil pf.remote_file    
    assert_nil pf.set_content_proc
  end
    
  def test_new_content_02
    pf = Curl::PostField.content('foo', 'bar', 'text/html')
    
    assert_equal 'foo', pf.name
    assert_equal 'bar', pf.content
    assert_equal 'text/html', pf.content_type
    assert_nil pf.local_file
    assert_nil pf.remote_file
    assert_nil pf.set_content_proc    
  end    
  
  def test_new_content_03
    l = lambda { |field| "never gets run" }
    pf = Curl::PostField.content('foo', &l)
    
    assert_equal 'foo', pf.name
    assert_nil pf.content
    assert_nil pf.content_type
    assert_nil pf.local_file
    assert_nil pf.remote_file
    
    # N.B. This doesn't just get the proc, but also removes it.
    assert_equal l, pf.set_content_proc
  end    

  def test_new_content_04
    l = lambda { |field| "never gets run" }
    pf = Curl::PostField.content('foo', 'text/html', &l)
    
    assert_equal 'foo', pf.name
    assert_nil pf.content
    assert_equal 'text/html', pf.content_type
    assert_nil pf.local_file
    assert_nil pf.remote_file
    
    # N.B. This doesn't just get the proc, but also removes it.
    assert_equal l, pf.set_content_proc
  end    


  def test_new_file_01
    pf = Curl::PostField.file('foo', 'localname')
    
    assert_equal 'foo', pf.name
    assert_equal 'localname', pf.local_file
    assert_equal 'localname', pf.remote_file
    assert_nothing_raised { pf.to_s }
    assert_nil pf.content_type
    assert_nil pf.content
    assert_nil pf.set_content_proc
  end
    
  def test_new_file_02
    pf = Curl::PostField.file('foo', 'localname', 'remotename')
    
    assert_equal 'foo', pf.name
    assert_equal 'localname', pf.local_file
    assert_equal 'remotename', pf.remote_file
    assert_nil pf.content_type
    assert_nil pf.content
    assert_nil pf.set_content_proc
  end    
  
  def test_new_file_03
    l = lambda { |field| "never gets run" }
    pf = Curl::PostField.file('foo', 'remotename', &l)
    
    assert_equal 'foo', pf.name
    assert_equal 'remotename', pf.remote_file
    assert_nil pf.local_file
    assert_nil pf.content_type
    assert_nil pf.content
    
    # N.B. This doesn't just get the proc, but also removes it.
    assert_equal l, pf.set_content_proc
  end    

  def test_new_file_04
    assert_raise(ArgumentError) do
      # no local name, no block
      Curl::PostField.file('foo')
    end
    
    assert_raise(ArgumentError) do
      # no remote name with block
      Curl::PostField.file('foo') { |field| "never runs" }
    end
  end
  
  def test_new_file_05
    # local gets ignored when supplying a block, but remote
    # is still set up properly.
    l = lambda { |field| "never runs" }
    pf = Curl::PostField.file('foo', 'local', 'remote', &l)

    assert_equal 'foo', pf.name
    assert_equal 'remote', pf.remote_file
    assert_nil pf.local_file
    assert_nil pf.content_type
    assert_nil pf.content

    assert_equal l, pf.set_content_proc
  end    
  
  def test_to_s_01
    pf = Curl::PostField.content('foo', 'bar')    
    assert_equal "foo=bar", pf.to_s
  end

  def test_to_s_02
    pf = Curl::PostField.content('foo', 'bar ton')    
    assert_equal "foo=bar%20ton", pf.to_s
  end

  def test_to_s_03
    pf = Curl::PostField.content('foo') { |field| field.name.upcase + "BAR" }
    assert_equal "foo=FOOBAR", pf.to_s
  end

  def test_to_s_04
    pf = Curl::PostField.file('foo.file', 'bar.file')
    assert_nothing_raised { pf.to_s }
    #assert_raise(Curl::Err::InvalidPostFieldError) { pf.to_s }
  end
end