2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2012-07-19 18:43:38 -04:00
|
|
|
require 'net/http'
|
|
|
|
require 'test/unit'
|
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
class HTTPRequestTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def test_initialize_GET
|
|
|
|
req = Net::HTTP::Get.new '/'
|
|
|
|
|
|
|
|
assert_equal 'GET', req.method
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_predicate req, :request_body_permitted?
|
|
|
|
assert_predicate req, :response_body_permitted?
|
2012-07-19 18:43:38 -04:00
|
|
|
|
|
|
|
expected = {
|
|
|
|
'accept' => %w[*/*],
|
|
|
|
'user-agent' => %w[Ruby],
|
|
|
|
}
|
|
|
|
|
|
|
|
expected['accept-encoding'] = %w[gzip;q=1.0,deflate;q=0.6,identity;q=0.3] if
|
|
|
|
Net::HTTP::HAVE_ZLIB
|
|
|
|
|
|
|
|
assert_equal expected, req.to_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_GET_range
|
|
|
|
req = Net::HTTP::Get.new '/', 'Range' => 'bytes=0-9'
|
|
|
|
|
|
|
|
assert_equal 'GET', req.method
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_predicate req, :request_body_permitted?
|
|
|
|
assert_predicate req, :response_body_permitted?
|
2012-07-19 18:43:38 -04:00
|
|
|
|
|
|
|
expected = {
|
|
|
|
'accept' => %w[*/*],
|
|
|
|
'user-agent' => %w[Ruby],
|
|
|
|
'range' => %w[bytes=0-9],
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal expected, req.to_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_HEAD
|
|
|
|
req = Net::HTTP::Head.new '/'
|
|
|
|
|
|
|
|
assert_equal 'HEAD', req.method
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_predicate req, :request_body_permitted?
|
|
|
|
assert_not_predicate req, :response_body_permitted?
|
2012-07-19 18:43:38 -04:00
|
|
|
|
|
|
|
expected = {
|
|
|
|
'accept' => %w[*/*],
|
|
|
|
'user-agent' => %w[Ruby],
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal expected, req.to_hash
|
|
|
|
end
|
|
|
|
|
2013-02-13 20:08:19 -05:00
|
|
|
def test_initialize_accept_encoding
|
|
|
|
req1 = Net::HTTP::Get.new '/'
|
|
|
|
|
|
|
|
assert req1.decode_content, 'Bug #7831 - automatically decode content'
|
|
|
|
|
|
|
|
req2 = Net::HTTP::Get.new '/', 'accept-encoding' => 'identity'
|
|
|
|
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_predicate req2, :decode_content,
|
|
|
|
'Bug #7381 - do not decode content if the user overrides'
|
2013-02-14 00:32:58 -05:00
|
|
|
end if Net::HTTP::HAVE_ZLIB
|
2013-02-13 20:08:19 -05:00
|
|
|
|
2018-03-08 11:07:54 -05:00
|
|
|
def test_initialize_GET_uri
|
|
|
|
req = Net::HTTP::Get.new(URI("http://example.com/foo"))
|
|
|
|
assert_equal "/foo", req.path
|
|
|
|
assert_equal "example.com", req['Host']
|
|
|
|
|
|
|
|
req = Net::HTTP::Get.new(URI("https://example.com/foo"))
|
|
|
|
assert_equal "/foo", req.path
|
|
|
|
assert_equal "example.com", req['Host']
|
|
|
|
|
|
|
|
assert_raise(ArgumentError){ Net::HTTP::Get.new(URI("urn:ietf:rfc:7231")) }
|
|
|
|
end
|
|
|
|
|
2013-02-13 20:08:19 -05:00
|
|
|
def test_header_set
|
|
|
|
req = Net::HTTP::Get.new '/'
|
|
|
|
|
|
|
|
assert req.decode_content, 'Bug #7831 - automatically decode content'
|
|
|
|
|
|
|
|
req['accept-encoding'] = 'identity'
|
|
|
|
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_predicate req, :decode_content,
|
|
|
|
'Bug #7831 - do not decode content if the user overrides'
|
2013-02-14 00:32:58 -05:00
|
|
|
end if Net::HTTP::HAVE_ZLIB
|
2013-02-13 20:08:19 -05:00
|
|
|
|
2012-07-19 18:43:38 -04:00
|
|
|
end
|
|
|
|
|