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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
require 'net/http'
require 'test/unit'
class HTTPHeaderTest < Test::Unit::TestCase
class C
include Net::HTTPHeader
def initialize
initialize_http_header({})
end
attr_accessor :body
end
def setup
@c = C.new
end
def test_size
assert_equal 0, @c.size
@c['a'] = 'a'
assert_equal 1, @c.size
@c['b'] = 'b'
assert_equal 2, @c.size
@c['b'] = 'b'
assert_equal 2, @c.size
@c['c'] = 'c'
assert_equal 3, @c.size
end
def test_ASET
@c['My-Header'] = 'test string'
@c['my-Header'] = 'test string'
@c['My-header'] = 'test string'
@c['my-header'] = 'test string'
@c['MY-HEADER'] = 'test string'
assert_equal 1, @c.size
@c['AaA'] = 'aaa'
@c['aaA'] = 'aaa'
@c['AAa'] = 'aaa'
assert_equal 2, @c.length
end
def test_AREF
@c['My-Header'] = 'test string'
assert_equal 'test string', @c['my-header']
assert_equal 'test string', @c['MY-header']
assert_equal 'test string', @c['my-HEADER']
@c['Next-Header'] = 'next string'
assert_equal 'next string', @c['next-header']
end
def test_add_field
@c.add_field 'My-Header', 'a'
assert_equal 'a', @c['My-Header']
assert_equal ['a'], @c.get_fields('My-Header')
@c.add_field 'My-Header', 'b'
assert_equal 'a, b', @c['My-Header']
assert_equal ['a', 'b'], @c.get_fields('My-Header')
@c.add_field 'My-Header', 'c'
assert_equal 'a, b, c', @c['My-Header']
assert_equal ['a', 'b', 'c'], @c.get_fields('My-Header')
@c.add_field 'My-Header', 'd, d'
assert_equal 'a, b, c, d, d', @c['My-Header']
assert_equal ['a', 'b', 'c', 'd, d'], @c.get_fields('My-Header')
end
def test_get_fields
@c['My-Header'] = 'test string'
assert_equal ['test string'], @c.get_fields('my-header')
assert_equal ['test string'], @c.get_fields('My-header')
assert_equal ['test string'], @c.get_fields('my-Header')
assert_nil @c.get_fields('not-found')
assert_nil @c.get_fields('Not-Found')
@c.get_fields('my-header').push 'junk'
assert_equal ['test string'], @c.get_fields('my-header')
@c.get_fields('my-header').clear
assert_equal ['test string'], @c.get_fields('my-header')
end
def test_delete
@c['My-Header'] = 'test'
assert_equal 'test', @c['My-Header']
assert_nil @c['not-found']
@c.delete 'My-Header'
assert_nil @c['My-Header']
assert_nil @c['not-found']
@c.delete 'My-Header'
@c.delete 'My-Header'
assert_nil @c['My-Header']
assert_nil @c['not-found']
end
def test_each
@c['My-Header'] = 'test'
@c.each do |k, v|
assert_equal 'my-header', k
assert_equal 'test', v
end
@c.each do |k, v|
assert_equal 'my-header', k
assert_equal 'test', v
end
end
def test_each_key
@c['My-Header'] = 'test'
@c.each_key do |k|
assert_equal 'my-header', k
end
@c.each_key do |k|
assert_equal 'my-header', k
end
end
def test_each_value
@c['My-Header'] = 'test'
@c.each_value do |v|
assert_equal 'test', v
end
@c.each_value do |v|
assert_equal 'test', v
end
end
def test_canonical_each
@c['my-header'] = ['a', 'b']
@c.canonical_each do |k,v|
assert_equal 'My-Header', k
assert_equal 'a, b', v
end
end
def test_each_capitalized
@c['my-header'] = ['a', 'b']
@c.each_capitalized do |k,v|
assert_equal 'My-Header', k
assert_equal 'a, b', v
end
end
def test_key?
@c['My-Header'] = 'test'
assert_equal true, @c.key?('My-Header')
assert_equal true, @c.key?('my-header')
assert_equal false, @c.key?('Not-Found')
assert_equal false, @c.key?('not-found')
assert_equal false, @c.key?('')
assert_equal false, @c.key?('x' * 1024)
end
def test_to_hash
end
def test_range
try_range(1..5, '1-5')
try_range(234..567, '234-567')
try_range(-5..-1, '-5')
try_range(1..-1, '1-')
end
def try_range(r, s)
@c['range'] = "bytes=#{s}"
assert_equal r, Array(@c.range)[0]
end
def test_range=
@c.range = 0..499
assert_equal 'bytes=0-499', @c['range']
@c.range = 0...500
assert_equal 'bytes=0-499', @c['range']
@c.range = 300
assert_equal 'bytes=0-299', @c['range']
@c.range = -400
assert_equal 'bytes=-400', @c['range']
@c.set_range 0, 500
assert_equal 'bytes=0-499', @c['range']
end
def test_content_range
end
def test_range_length
@c['Content-Range'] = "bytes 0-499/1000"
assert_equal 500, @c.range_length
@c['Content-Range'] = "bytes 1-500/1000"
assert_equal 500, @c.range_length
@c['Content-Range'] = "bytes 1-1/1000"
assert_equal 1, @c.range_length
end
def test_chunked?
try_chunked true, 'chunked'
try_chunked true, ' chunked '
try_chunked true, '(OK)chunked'
try_chunked false, 'not-chunked'
try_chunked false, 'chunked-but-not-chunked'
end
def try_chunked(bool, str)
@c['transfer-encoding'] = str
assert_equal bool, @c.chunked?
end
def test_content_length
@c.delete('content-length')
assert_nil @c['content-length']
try_content_length 500, '500'
try_content_length 10000_0000_0000, '1000000000000'
try_content_length 123, ' 123'
try_content_length 1, '1 23'
try_content_length 500, '(OK)500'
assert_raises(Net::HTTPHeaderSyntaxError, 'here is no digit, but') {
@c['content-length'] = 'no digit'
@c.content_length
}
end
def try_content_length(len, str)
@c['content-length'] = str
assert_equal len, @c.content_length
end
def test_content_length=
@c.content_length = 0
assert_equal 0, @c.content_length
@c.content_length = 1
assert_equal 1, @c.content_length
@c.content_length = 999
assert_equal 999, @c.content_length
@c.content_length = 10000000000000
assert_equal 10000000000000, @c.content_length
end
def test_content_type
assert_nil @c.content_type
@c.content_type = 'text/html'
assert_equal 'text/html', @c.content_type
@c.content_type = 'application/pdf'
assert_equal 'application/pdf', @c.content_type
@c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'}
assert_equal 'text/html', @c.content_type
@c.content_type = 'text'
assert_equal 'text', @c.content_type
end
def test_main_type
assert_nil @c.main_type
@c.content_type = 'text/html'
assert_equal 'text', @c.main_type
@c.content_type = 'application/pdf'
assert_equal 'application', @c.main_type
@c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'}
assert_equal 'text', @c.main_type
@c.content_type = 'text'
assert_equal 'text', @c.main_type
end
def test_sub_type
assert_nil @c.sub_type
@c.content_type = 'text/html'
assert_equal 'html', @c.sub_type
@c.content_type = 'application/pdf'
assert_equal 'pdf', @c.sub_type
@c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'}
assert_equal 'html', @c.sub_type
@c.content_type = 'text'
assert_nil @c.sub_type
end
def test_type_params
assert_equal({}, @c.type_params)
@c.content_type = 'text/html'
assert_equal({}, @c.type_params)
@c.content_type = 'application/pdf'
assert_equal({}, @c.type_params)
@c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'}
assert_equal({'charset' => 'iso-2022-jp'}, @c.type_params)
@c.content_type = 'text'
assert_equal({}, @c.type_params)
end
def test_set_content_type
end
def test_form_data=
@c.form_data = {"cmd"=>"search", "q"=>"ruby", "max"=>"50"}
assert_equal 'application/x-www-form-urlencoded', @c.content_type
assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort
end
def test_set_form_data
@c.set_form_data "cmd"=>"search", "q"=>"ruby", "max"=>"50"
assert_equal 'application/x-www-form-urlencoded', @c.content_type
assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort
@c.set_form_data "cmd"=>"search", "q"=>"ruby", "max"=>50
assert_equal 'application/x-www-form-urlencoded', @c.content_type
assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort
@c.set_form_data({"cmd"=>"search", "q"=>"ruby", "max"=>"50"}, ';')
assert_equal 'application/x-www-form-urlencoded', @c.content_type
assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split(';').sort
end
def test_basic_auth
end
def test_proxy_basic_auth
end
end
|