diff options
Diffstat (limited to 'spec/ruby/library/net/http')
148 files changed, 3904 insertions, 0 deletions
diff --git a/spec/ruby/library/net/http/HTTPBadResponse_spec.rb b/spec/ruby/library/net/http/HTTPBadResponse_spec.rb new file mode 100644 index 0000000000..78bb0c8420 --- /dev/null +++ b/spec/ruby/library/net/http/HTTPBadResponse_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPBadResponse" do + it "is a subclass of StandardError" do + Net::HTTPBadResponse.should < StandardError + end +end diff --git a/spec/ruby/library/net/http/HTTPError_spec.rb b/spec/ruby/library/net/http/HTTPError_spec.rb new file mode 100644 index 0000000000..5fa87f140b --- /dev/null +++ b/spec/ruby/library/net/http/HTTPError_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPError" do + it "is a subclass of Net::ProtocolError" do + Net::HTTPError.should < Net::ProtocolError + end + + it "includes the Net::HTTPExceptions module" do + Net::HTTPError.should < Net::HTTPExceptions + end +end diff --git a/spec/ruby/library/net/http/HTTPFatalError_spec.rb b/spec/ruby/library/net/http/HTTPFatalError_spec.rb new file mode 100644 index 0000000000..72468faf8d --- /dev/null +++ b/spec/ruby/library/net/http/HTTPFatalError_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPFatalError" do + it "is a subclass of Net::ProtoFatalError" do + Net::HTTPFatalError.should < Net::ProtoFatalError + end + + it "includes the Net::HTTPExceptions module" do + Net::HTTPFatalError.should < Net::HTTPExceptions + end +end diff --git a/spec/ruby/library/net/http/HTTPHeaderSyntaxError_spec.rb b/spec/ruby/library/net/http/HTTPHeaderSyntaxError_spec.rb new file mode 100644 index 0000000000..3e7ee24365 --- /dev/null +++ b/spec/ruby/library/net/http/HTTPHeaderSyntaxError_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPHeaderSyntaxError" do + it "is a subclass of StandardError" do + Net::HTTPHeaderSyntaxError.should < StandardError + end +end diff --git a/spec/ruby/library/net/http/HTTPRetriableError_spec.rb b/spec/ruby/library/net/http/HTTPRetriableError_spec.rb new file mode 100644 index 0000000000..71f6bdb196 --- /dev/null +++ b/spec/ruby/library/net/http/HTTPRetriableError_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPRetriableError" do + it "is a subclass of Net::ProtoRetriableError" do + Net::HTTPRetriableError.should < Net::ProtoRetriableError + end + + it "includes the Net::HTTPExceptions module" do + Net::HTTPRetriableError.should < Net::HTTPExceptions + end +end diff --git a/spec/ruby/library/net/http/HTTPServerException_spec.rb b/spec/ruby/library/net/http/HTTPServerException_spec.rb new file mode 100644 index 0000000000..35566ab0c5 --- /dev/null +++ b/spec/ruby/library/net/http/HTTPServerException_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPServerException" do + it "is a subclass of Net::ProtoServerError" do + Net::HTTPServerException.should < Net::ProtoServerError + end + + it "includes the Net::HTTPExceptions module" do + Net::HTTPServerException.should < Net::HTTPExceptions + end +end diff --git a/spec/ruby/library/net/http/http/Proxy_spec.rb b/spec/ruby/library/net/http/http/Proxy_spec.rb new file mode 100644 index 0000000000..2de3fb2d75 --- /dev/null +++ b/spec/ruby/library/net/http/http/Proxy_spec.rb @@ -0,0 +1,35 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.Proxy" do + it "returns a new subclass of Net::HTTP" do + Net::HTTP.Proxy("localhost").should < Net::HTTP + end + + it "returns Net::HTTP when the passed address is nil" do + Net::HTTP.Proxy(nil).should == Net::HTTP + end + + it "sets the returned subclasses' proxy options based on the passed arguments" do + http_with_proxy = Net::HTTP.Proxy("localhost", 1234, "rspec", "rocks") + http_with_proxy.proxy_address.should == "localhost" + http_with_proxy.proxy_port.should eql(1234) + http_with_proxy.proxy_user.should == "rspec" + http_with_proxy.proxy_pass.should == "rocks" + end +end + +describe "Net::HTTP#proxy?" do + describe "when self is no proxy class instance" do + it "returns false" do + Net::HTTP.new("localhost", 3333).proxy?.should be_false + end + end + + describe "when self is a proxy class instance" do + it "returns false" do + http_with_proxy = Net::HTTP.Proxy("localhost", 1234, "rspec", "rocks") + http_with_proxy.new("localhost", 3333).proxy?.should be_true + end + end +end diff --git a/spec/ruby/library/net/http/http/active_spec.rb b/spec/ruby/library/net/http/http/active_spec.rb new file mode 100644 index 0000000000..28f1872fa9 --- /dev/null +++ b/spec/ruby/library/net/http/http/active_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) +require File.expand_path('../shared/started', __FILE__) + +describe "Net::HTTP#active?" do + it_behaves_like :net_http_started_p, :active? +end diff --git a/spec/ruby/library/net/http/http/address_spec.rb b/spec/ruby/library/net/http/http/address_spec.rb new file mode 100644 index 0000000000..a0fe28fb9e --- /dev/null +++ b/spec/ruby/library/net/http/http/address_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP#address" do + it "returns the current host name" do + net = Net::HTTP.new("localhost") + net.address.should == "localhost" + end +end diff --git a/spec/ruby/library/net/http/http/close_on_empty_response_spec.rb b/spec/ruby/library/net/http/http/close_on_empty_response_spec.rb new file mode 100644 index 0000000000..52bede524e --- /dev/null +++ b/spec/ruby/library/net/http/http/close_on_empty_response_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP#close_on_empty_response" do + it "needs to be reviewed for spec completeness" +end + +describe "Net::HTTP#close_on_empty_response=" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/net/http/http/copy_spec.rb b/spec/ruby/library/net/http/http/copy_spec.rb new file mode 100644 index 0000000000..c3c5e784b1 --- /dev/null +++ b/spec/ruby/library/net/http/http/copy_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#copy" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends a COPY request to the passed path and returns the response" do + response = @http.copy("/request") + response.should be_kind_of(Net::HTTPResponse) + response.body.should == "Request type: COPY" + end +end diff --git a/spec/ruby/library/net/http/http/default_port_spec.rb b/spec/ruby/library/net/http/http/default_port_spec.rb new file mode 100644 index 0000000000..8d9b3ac64a --- /dev/null +++ b/spec/ruby/library/net/http/http/default_port_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.default_port" do + it "returns 80" do + Net::HTTP.http_default_port.should eql(80) + end +end diff --git a/spec/ruby/library/net/http/http/delete_spec.rb b/spec/ruby/library/net/http/http/delete_spec.rb new file mode 100644 index 0000000000..856d3b3af3 --- /dev/null +++ b/spec/ruby/library/net/http/http/delete_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#delete" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends a DELETE request to the passed path and returns the response" do + response = @http.delete("/request") + response.should be_kind_of(Net::HTTPResponse) + response.body.should == "Request type: DELETE" + end +end diff --git a/spec/ruby/library/net/http/http/finish_spec.rb b/spec/ruby/library/net/http/http/finish_spec.rb new file mode 100644 index 0000000000..ccc6878961 --- /dev/null +++ b/spec/ruby/library/net/http/http/finish_spec.rb @@ -0,0 +1,29 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#finish" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.new("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + describe "when self has been started" do + it "closes the tcp connection" do + @http.start + @http.finish + @http.started?.should be_false + end + end + + describe "when self has not been started yet" do + it "raises an IOError" do + lambda { @http.finish }.should raise_error(IOError) + end + end +end diff --git a/spec/ruby/library/net/http/http/fixtures/http_server.rb b/spec/ruby/library/net/http/http/fixtures/http_server.rb new file mode 100644 index 0000000000..c2ae2360d3 --- /dev/null +++ b/spec/ruby/library/net/http/http/fixtures/http_server.rb @@ -0,0 +1,93 @@ +require 'webrick' +require 'webrick/httpservlet/abstract' + +module NetHTTPSpecs + class NullWriter + def <<(s) end + def puts(*args) end + def print(*args) end + def printf(*args) end + end + + class SpecServlet < WEBrick::HTTPServlet::AbstractServlet + def handle(req, res) + reply(req, res) + end + + %w{ do_GET do_HEAD do_POST do_PUT do_PROPPATCH do_LOCK do_UNLOCK + do_OPTIONS do_PROPFIND do_DELETE do_MOVE do_COPY + do_MKCOL do_TRACE }.each do |method| + alias_method method.to_sym, :handle + end + end + + class RequestServlet < SpecServlet + def reply(req, res) + res.content_type = "text/plain" + res.body = "Request type: #{req.request_method}" + end + end + + class RequestBodyServlet < SpecServlet + def reply(req, res) + res.content_type = "text/plain" + res.body = req.body + end + end + + class RequestHeaderServlet < SpecServlet + def reply(req, res) + res.content_type = "text/plain" + res.body = req.header.inspect + end + end + + class << self + @server = nil + @server_thread = nil + + def port + raise "server not started" unless @server + @server.config[:Port] + end + + def start_server + server_config = { + BindAddress: "localhost", + Port: 0, + Logger: WEBrick::Log.new(NullWriter.new), + AccessLog: [], + ServerType: Thread + } + + @server = WEBrick::HTTPServer.new(server_config) + + @server.mount_proc('/') do |req, res| + res.content_type = "text/plain" + res.body = "This is the index page." + end + @server.mount('/request', RequestServlet) + @server.mount("/request/body", RequestBodyServlet) + @server.mount("/request/header", RequestHeaderServlet) + + @server_thread = @server.start + end + + def stop_server + if @server + begin + @server.shutdown + rescue Errno::EPIPE + # Because WEBrick is not thread-safe and only catches IOError + end + @server = nil + end + if @server_thread + @server_thread.join + @server_thread = nil + end + timeout = WEBrick::Utils::TimeoutHandler + timeout.terminate if timeout.respond_to?(:terminate) + end + end +end diff --git a/spec/ruby/library/net/http/http/get2_spec.rb b/spec/ruby/library/net/http/http/get2_spec.rb new file mode 100644 index 0000000000..2173156adf --- /dev/null +++ b/spec/ruby/library/net/http/http/get2_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) +require File.expand_path('../shared/request_get', __FILE__) + +describe "Net::HTTP#get2" do + it_behaves_like :net_ftp_request_get, :get2 +end diff --git a/spec/ruby/library/net/http/http/get_print_spec.rb b/spec/ruby/library/net/http/http/get_print_spec.rb new file mode 100644 index 0000000000..d91a584aaa --- /dev/null +++ b/spec/ruby/library/net/http/http/get_print_spec.rb @@ -0,0 +1,30 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP.get_print" do + before :each do + NetHTTPSpecs.start_server + @port = NetHTTPSpecs.port + end + + after :each do + NetHTTPSpecs.stop_server + end + + describe "when passed URI" do + it "it prints the body of the specified uri to $stdout" do + lambda do + Net::HTTP.get_print URI.parse("http://localhost:#{@port}/") + end.should output(/This is the index page\./) + end + end + + describe "when passed host, path, port" do + it "it prints the body of the specified uri to $stdout" do + lambda do + Net::HTTP.get_print 'localhost', "/", @port + end.should output(/This is the index page\./) + end + end +end diff --git a/spec/ruby/library/net/http/http/get_response_spec.rb b/spec/ruby/library/net/http/http/get_response_spec.rb new file mode 100644 index 0000000000..fdc1ae1f66 --- /dev/null +++ b/spec/ruby/library/net/http/http/get_response_spec.rb @@ -0,0 +1,30 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP.get_response" do + before :each do + NetHTTPSpecs.start_server + @port = NetHTTPSpecs.port + end + + after :each do + NetHTTPSpecs.stop_server + end + + describe "when passed URI" do + it "returns the response for the specified uri" do + res = Net::HTTP.get_response(URI.parse("http://localhost:#{@port}/")) + res.content_type.should == "text/plain" + res.body.should == "This is the index page." + end + end + + describe "when passed host, path, port" do + it "returns the response for the specified host-path-combination" do + res = Net::HTTP.get_response('localhost', "/", @port) + res.content_type.should == "text/plain" + res.body.should == "This is the index page." + end + end +end diff --git a/spec/ruby/library/net/http/http/get_spec.rb b/spec/ruby/library/net/http/http/get_spec.rb new file mode 100644 index 0000000000..d434191d2d --- /dev/null +++ b/spec/ruby/library/net/http/http/get_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP.get when passed URI" do + before :each do + NetHTTPSpecs.start_server + @port = NetHTTPSpecs.port + end + + after :each do + NetHTTPSpecs.stop_server + end + + describe "when passed URI" do + it "returns the body of the specified uri" do + Net::HTTP.get(URI.parse("http://localhost:#{@port}/")).should == "This is the index page." + end + end + + describe "when passed host, path, port" do + it "returns the body of the specified host-path-combination" do + Net::HTTP.get('localhost', "/", @port).should == "This is the index page." + end + end +end diff --git a/spec/ruby/library/net/http/http/head2_spec.rb b/spec/ruby/library/net/http/http/head2_spec.rb new file mode 100644 index 0000000000..66d533c84d --- /dev/null +++ b/spec/ruby/library/net/http/http/head2_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) +require File.expand_path('../shared/request_head', __FILE__) + +describe "Net::HTTP#head2" do + it_behaves_like :net_ftp_request_head, :head2 +end + diff --git a/spec/ruby/library/net/http/http/head_spec.rb b/spec/ruby/library/net/http/http/head_spec.rb new file mode 100644 index 0000000000..d0d13a6451 --- /dev/null +++ b/spec/ruby/library/net/http/http/head_spec.rb @@ -0,0 +1,25 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#head" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends a HEAD request to the passed path and returns the response" do + response = @http.head("/request") + # HEAD requests have no responses + response.body.should be_nil + end + + it "returns a Net::HTTPResponse" do + @http.head("/request").should be_kind_of(Net::HTTPResponse) + end +end diff --git a/spec/ruby/library/net/http/http/http_default_port_spec.rb b/spec/ruby/library/net/http/http/http_default_port_spec.rb new file mode 100644 index 0000000000..6b840bbf30 --- /dev/null +++ b/spec/ruby/library/net/http/http/http_default_port_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.http_default_port" do + it "returns 80" do + Net::HTTP.http_default_port.should eql(80) + end +end diff --git a/spec/ruby/library/net/http/http/https_default_port_spec.rb b/spec/ruby/library/net/http/http/https_default_port_spec.rb new file mode 100644 index 0000000000..bd213f2325 --- /dev/null +++ b/spec/ruby/library/net/http/http/https_default_port_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.https_default_port" do + it "returns 443" do + Net::HTTP.https_default_port.should eql(443) + end +end diff --git a/spec/ruby/library/net/http/http/initialize_spec.rb b/spec/ruby/library/net/http/http/initialize_spec.rb new file mode 100644 index 0000000000..0cd59f493d --- /dev/null +++ b/spec/ruby/library/net/http/http/initialize_spec.rb @@ -0,0 +1,46 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP#initialize" do + it "is private" do + Net::HTTP.should have_private_instance_method(:initialize) + end + + describe "when passed address" do + before :each do + @net = Net::HTTP.allocate + @net.send(:initialize, "localhost") + end + + it "sets the new Net::HTTP instance's address to the passed address" do + @net.address.should == "localhost" + end + + it "sets the new Net::HTTP instance's port to the default HTTP port" do + @net.port.should eql(Net::HTTP.default_port) + end + + it "does not start the new Net::HTTP instance" do + @net.started?.should be_false + end + end + + describe "when passed address, port" do + before :each do + @net = Net::HTTP.allocate + @net.send(:initialize, "localhost", 3333) + end + + it "sets the new Net::HTTP instance's address to the passed address" do + @net.address.should == "localhost" + end + + it "sets the new Net::HTTP instance's port to the passed port" do + @net.port.should eql(3333) + end + + it "does not start the new Net::HTTP instance" do + @net.started?.should be_false + end + end +end diff --git a/spec/ruby/library/net/http/http/inspect_spec.rb b/spec/ruby/library/net/http/http/inspect_spec.rb new file mode 100644 index 0000000000..7c5d9bf0eb --- /dev/null +++ b/spec/ruby/library/net/http/http/inspect_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#inspect" do + before :each do + NetHTTPSpecs.start_server + @port = NetHTTPSpecs.port + @http = Net::HTTP.new("localhost", @port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "returns a String representation of self" do + @http.inspect.should be_kind_of(String) + @http.inspect.should == "#<Net::HTTP localhost:#{@port} open=false>" + + @http.start + @http.inspect.should == "#<Net::HTTP localhost:#{@port} open=true>" + end +end diff --git a/spec/ruby/library/net/http/http/is_version_1_1_spec.rb b/spec/ruby/library/net/http/http/is_version_1_1_spec.rb new file mode 100644 index 0000000000..8fa5da6be7 --- /dev/null +++ b/spec/ruby/library/net/http/http/is_version_1_1_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../shared/version_1_1', __FILE__) + +describe "Net::HTTP.is_version_1_1?" do + it_behaves_like :net_http_version_1_1_p, :is_version_1_1? +end diff --git a/spec/ruby/library/net/http/http/is_version_1_2_spec.rb b/spec/ruby/library/net/http/http/is_version_1_2_spec.rb new file mode 100644 index 0000000000..344ac7f7c0 --- /dev/null +++ b/spec/ruby/library/net/http/http/is_version_1_2_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../shared/version_1_2', __FILE__) + +describe "Net::HTTP.is_version_1_2?" do + it_behaves_like :net_http_version_1_2_p, :is_version_1_2? +end diff --git a/spec/ruby/library/net/http/http/lock_spec.rb b/spec/ruby/library/net/http/http/lock_spec.rb new file mode 100644 index 0000000000..e44099f9e5 --- /dev/null +++ b/spec/ruby/library/net/http/http/lock_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#lock" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends a LOCK request to the passed path and returns the response" do + response = @http.lock("/request", "test=test") + response.should be_kind_of(Net::HTTPResponse) + response.body.should == "Request type: LOCK" + end +end diff --git a/spec/ruby/library/net/http/http/mkcol_spec.rb b/spec/ruby/library/net/http/http/mkcol_spec.rb new file mode 100644 index 0000000000..51b0a5b9c0 --- /dev/null +++ b/spec/ruby/library/net/http/http/mkcol_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#mkcol" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends a MKCOL request to the passed path and returns the response" do + response = @http.mkcol("/request") + response.should be_kind_of(Net::HTTPResponse) + response.body.should == "Request type: MKCOL" + end +end diff --git a/spec/ruby/library/net/http/http/move_spec.rb b/spec/ruby/library/net/http/http/move_spec.rb new file mode 100644 index 0000000000..0aa00195f8 --- /dev/null +++ b/spec/ruby/library/net/http/http/move_spec.rb @@ -0,0 +1,25 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#head" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends a MOVE request to the passed path and returns the response" do + response = @http.move("/request") + # HEAD requests have no responses + response.body.should == "Request type: MOVE" + end + + it "returns a Net::HTTPResponse" do + @http.move("/request").should be_kind_of(Net::HTTPResponse) + end +end diff --git a/spec/ruby/library/net/http/http/new_spec.rb b/spec/ruby/library/net/http/http/new_spec.rb new file mode 100644 index 0000000000..b741eb0a4c --- /dev/null +++ b/spec/ruby/library/net/http/http/new_spec.rb @@ -0,0 +1,86 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.new" do + describe "when passed address" do + before :each do + @http = Net::HTTP.new("localhost") + end + + it "returns a Net::HTTP instance" do + @http.proxy?.should be_false + @http.instance_of?(Net::HTTP).should be_true + end + + it "sets the new Net::HTTP instance's address to the passed address" do + @http.address.should == "localhost" + end + + it "sets the new Net::HTTP instance's port to the default HTTP port" do + @http.port.should eql(Net::HTTP.default_port) + end + + it "does not start the new Net::HTTP instance" do + @http.started?.should be_false + end + end + + describe "when passed address, port" do + before :each do + @http = Net::HTTP.new("localhost", 3333) + end + + it "returns a Net::HTTP instance" do + @http.proxy?.should be_false + @http.instance_of?(Net::HTTP).should be_true + end + + it "sets the new Net::HTTP instance's address to the passed address" do + @http.address.should == "localhost" + end + + it "sets the new Net::HTTP instance's port to the passed port" do + @http.port.should eql(3333) + end + + it "does not start the new Net::HTTP instance" do + @http.started?.should be_false + end + end + + describe "when passed address, port, *proxy_options" do + it "returns a Net::HTTP instance" do + http = Net::HTTP.new("localhost", 3333, "localhost") + http.proxy?.should be_true + http.instance_of?(Net::HTTP).should be_true + http.should be_kind_of(Net::HTTP) + end + + it "correctly sets the passed Proxy options" do + http = Net::HTTP.new("localhost", 3333, "localhost") + http.proxy_address.should == "localhost" + http.proxy_port.should eql(80) + http.proxy_user.should be_nil + http.proxy_pass.should be_nil + + http = Net::HTTP.new("localhost", 3333, "localhost", 1234) + http.proxy_address.should == "localhost" + http.proxy_port.should eql(1234) + http.proxy_user.should be_nil + http.proxy_pass.should be_nil + + http = Net::HTTP.new("localhost", 3333, "localhost", 1234, "rubyspec") + http.proxy_address.should == "localhost" + http.proxy_port.should eql(1234) + http.proxy_user.should == "rubyspec" + http.proxy_pass.should be_nil + + http = Net::HTTP.new("localhost", 3333, "localhost", 1234, "rubyspec", "rocks") + http.proxy_address.should == "localhost" + http.proxy_port.should eql(1234) + http.proxy_user.should == "rubyspec" + http.proxy_pass.should == "rocks" + end + end + +end diff --git a/spec/ruby/library/net/http/http/newobj_spec.rb b/spec/ruby/library/net/http/http/newobj_spec.rb new file mode 100644 index 0000000000..88d5881725 --- /dev/null +++ b/spec/ruby/library/net/http/http/newobj_spec.rb @@ -0,0 +1,48 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.newobj" do + before :each do + @net = Net::HTTP.newobj("localhost") + end + + describe "when passed address" do + it "returns a new Net::HTTP instance" do + @net.should be_kind_of(Net::HTTP) + end + + it "sets the new Net::HTTP instance's address to the passed address" do + @net.address.should == "localhost" + end + + it "sets the new Net::HTTP instance's port to the default HTTP port" do + @net.port.should eql(Net::HTTP.default_port) + end + + it "does not start the new Net::HTTP instance" do + @net.started?.should be_false + end + end + + describe "when passed address, port" do + before :each do + @net = Net::HTTP.newobj("localhost", 3333) + end + + it "returns a new Net::HTTP instance" do + @net.should be_kind_of(Net::HTTP) + end + + it "sets the new Net::HTTP instance's address to the passed address" do + @net.address.should == "localhost" + end + + it "sets the new Net::HTTP instance's port to the passed port" do + @net.port.should eql(3333) + end + + it "does not start the new Net::HTTP instance" do + @net.started?.should be_false + end + end +end diff --git a/spec/ruby/library/net/http/http/open_timeout_spec.rb b/spec/ruby/library/net/http/http/open_timeout_spec.rb new file mode 100644 index 0000000000..0142ae6c06 --- /dev/null +++ b/spec/ruby/library/net/http/http/open_timeout_spec.rb @@ -0,0 +1,35 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP#open_timeout" do + ruby_version_is ""..."2.3" do + it "returns the seconds to wait till the connection is open" do + net = Net::HTTP.new("localhost") + net.open_timeout.should be_nil + net.open_timeout = 10 + net.open_timeout.should eql(10) + end + end + + ruby_version_is "2.3" do + it "returns the seconds to wait till the connection is open" do + net = Net::HTTP.new("localhost") + net.open_timeout.should eql(60) + net.open_timeout = 10 + net.open_timeout.should eql(10) + end + end +end + +describe "Net::HTTP#open_timeout=" do + it "sets the seconds to wait till the connection is open" do + net = Net::HTTP.new("localhost") + net.open_timeout = 10 + net.open_timeout.should eql(10) + end + + it "returns the newly set value" do + net = Net::HTTP.new("localhost") + (net.open_timeout = 10).should eql(10) + end +end diff --git a/spec/ruby/library/net/http/http/options_spec.rb b/spec/ruby/library/net/http/http/options_spec.rb new file mode 100644 index 0000000000..9c5e810105 --- /dev/null +++ b/spec/ruby/library/net/http/http/options_spec.rb @@ -0,0 +1,25 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#options" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends an options request to the passed path and returns the response" do + response = @http.options("/request") + + response.body.should == "Request type: OPTIONS" + end + + it "returns a Net::HTTPResponse" do + @http.options("/request").should be_kind_of(Net::HTTPResponse) + end +end diff --git a/spec/ruby/library/net/http/http/port_spec.rb b/spec/ruby/library/net/http/http/port_spec.rb new file mode 100644 index 0000000000..a916f722bf --- /dev/null +++ b/spec/ruby/library/net/http/http/port_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP#port" do + it "returns the current port number" do + net = Net::HTTP.new("localhost", 3333) + net.port.should eql(3333) + end +end diff --git a/spec/ruby/library/net/http/http/post2_spec.rb b/spec/ruby/library/net/http/http/post2_spec.rb new file mode 100644 index 0000000000..298b2277bc --- /dev/null +++ b/spec/ruby/library/net/http/http/post2_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) +require File.expand_path('../shared/request_post', __FILE__) + +describe "Net::HTTP#post2" do + it_behaves_like :net_ftp_request_post, :post2 +end diff --git a/spec/ruby/library/net/http/http/post_form_spec.rb b/spec/ruby/library/net/http/http/post_form_spec.rb new file mode 100644 index 0000000000..4a5d7ec731 --- /dev/null +++ b/spec/ruby/library/net/http/http/post_form_spec.rb @@ -0,0 +1,22 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP.post_form when passed URI" do + before :each do + NetHTTPSpecs.start_server + @port = NetHTTPSpecs.port + end + + after :each do + NetHTTPSpecs.stop_server + end + + it "POSTs the passed form data to the given uri" do + uri = URI.parse("http://localhost:#{@port}/request/body") + data = { test: :data } + + res = Net::HTTP.post_form(uri, data) + res.body.should == "test=data" + end +end diff --git a/spec/ruby/library/net/http/http/post_spec.rb b/spec/ruby/library/net/http/http/post_spec.rb new file mode 100644 index 0000000000..a50663a01d --- /dev/null +++ b/spec/ruby/library/net/http/http/post_spec.rb @@ -0,0 +1,38 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#post" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends an post request to the passed path and returns the response" do + response = @http.post("/request", "test=test") + response.body.should == "Request type: POST" + end + + it "returns a Net::HTTPResponse" do + @http.post("/request", "test=test").should be_kind_of(Net::HTTPResponse) + end + + describe "when passed a block" do + it "yields fragments of the response body to the passed block" do + str = "" + @http.post("/request", "test=test") do |res| + str << res + end + str.should == "Request type: POST" + end + + it "returns a Net::HTTPResponse" do + @http.post("/request", "test=test") {}.should be_kind_of(Net::HTTPResponse) + end + end +end diff --git a/spec/ruby/library/net/http/http/propfind_spec.rb b/spec/ruby/library/net/http/http/propfind_spec.rb new file mode 100644 index 0000000000..c242eeec68 --- /dev/null +++ b/spec/ruby/library/net/http/http/propfind_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#propfind" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends an propfind request to the passed path and returns the response" do + response = @http.propfind("/request", "test=test") + response.body.should == "Request type: PROPFIND" + end + + it "returns a Net::HTTPResponse" do + @http.propfind("/request", "test=test").should be_kind_of(Net::HTTPResponse) + end +end diff --git a/spec/ruby/library/net/http/http/proppatch_spec.rb b/spec/ruby/library/net/http/http/proppatch_spec.rb new file mode 100644 index 0000000000..8567f60719 --- /dev/null +++ b/spec/ruby/library/net/http/http/proppatch_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#proppatch" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends an proppatch request to the passed path and returns the response" do + response = @http.proppatch("/request", "test=test") + response.body.should == "Request type: PROPPATCH" + end + + it "returns a Net::HTTPResponse" do + @http.proppatch("/request", "test=test").should be_kind_of(Net::HTTPResponse) + end +end diff --git a/spec/ruby/library/net/http/http/proxy_address_spec.rb b/spec/ruby/library/net/http/http/proxy_address_spec.rb new file mode 100644 index 0000000000..85f8dfedb4 --- /dev/null +++ b/spec/ruby/library/net/http/http/proxy_address_spec.rb @@ -0,0 +1,31 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.proxy_address" do + describe "when self is no proxy class" do + it "returns nil" do + Net::HTTP.proxy_address.should be_nil + end + end + + describe "when self is a proxy class" do + it "returns the address for self's proxy connection" do + Net::HTTP.Proxy("localhost", 1234, "rspec", "rocks").proxy_address.should == "localhost" + end + end +end + +describe "Net::HTTP#proxy_address" do + describe "when self is no proxy class instance" do + it "returns nil" do + Net::HTTP.new("localhost", 3333).proxy_address.should be_nil + end + end + + describe "when self is a proxy class instance" do + it "returns the password for self's proxy connection" do + http_with_proxy = Net::HTTP.Proxy("localhost", 1234, "rspec", "rocks") + http_with_proxy.new("localhost", 3333).proxy_address.should == "localhost" + end + end +end diff --git a/spec/ruby/library/net/http/http/proxy_class_spec.rb b/spec/ruby/library/net/http/http/proxy_class_spec.rb new file mode 100644 index 0000000000..30d38a93e7 --- /dev/null +++ b/spec/ruby/library/net/http/http/proxy_class_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.proxy_class?" do + it "returns true if sels is a class created with Net::HTTP.Proxy" do + Net::HTTP.proxy_class?.should be_false + Net::HTTP.Proxy("localhost").proxy_class?.should be_true + end +end diff --git a/spec/ruby/library/net/http/http/proxy_pass_spec.rb b/spec/ruby/library/net/http/http/proxy_pass_spec.rb new file mode 100644 index 0000000000..09db6f2877 --- /dev/null +++ b/spec/ruby/library/net/http/http/proxy_pass_spec.rb @@ -0,0 +1,39 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.proxy_pass" do + describe "when self is no proxy class" do + it "returns nil" do + Net::HTTP.proxy_pass.should be_nil + end + end + + describe "when self is a proxy class" do + it "returns nil if no password was set for self's proxy connection" do + Net::HTTP.Proxy("localhost").proxy_pass.should be_nil + end + + it "returns the password for self's proxy connection" do + Net::HTTP.Proxy("localhost", 1234, "rspec", "rocks").proxy_pass.should == "rocks" + end + end +end + +describe "Net::HTTP#proxy_pass" do + describe "when self is no proxy class instance" do + it "returns nil" do + Net::HTTP.new("localhost", 3333).proxy_pass.should be_nil + end + end + + describe "when self is a proxy class instance" do + it "returns nil if no password was set for self's proxy connection" do + Net::HTTP.Proxy("localhost").new("localhost", 3333).proxy_pass.should be_nil + end + + it "returns the password for self's proxy connection" do + http_with_proxy = Net::HTTP.Proxy("localhost", 1234, "rspec", "rocks") + http_with_proxy.new("localhost", 3333).proxy_pass.should == "rocks" + end + end +end diff --git a/spec/ruby/library/net/http/http/proxy_port_spec.rb b/spec/ruby/library/net/http/http/proxy_port_spec.rb new file mode 100644 index 0000000000..0655232c6b --- /dev/null +++ b/spec/ruby/library/net/http/http/proxy_port_spec.rb @@ -0,0 +1,39 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.proxy_port" do + describe "when self is no proxy class" do + it "returns nil" do + Net::HTTP.proxy_port.should be_nil + end + end + + describe "when self is a proxy class" do + it "returns 80 if no port was set for self's proxy connection" do + Net::HTTP.Proxy("localhost").proxy_port.should eql(80) + end + + it "returns the port for self's proxy connection" do + Net::HTTP.Proxy("localhost", 1234, "rspec", "rocks").proxy_port.should eql(1234) + end + end +end + +describe "Net::HTTP#proxy_port" do + describe "when self is no proxy class instance" do + it "returns nil" do + Net::HTTP.new("localhost", 3333).proxy_port.should be_nil + end + end + + describe "when self is a proxy class instance" do + it "returns 80 if no port was set for self's proxy connection" do + Net::HTTP.Proxy("localhost").new("localhost", 3333).proxy_port.should eql(80) + end + + it "returns the port for self's proxy connection" do + http_with_proxy = Net::HTTP.Proxy("localhost", 1234, "rspec", "rocks") + http_with_proxy.new("localhost", 3333).proxy_port.should eql(1234) + end + end +end diff --git a/spec/ruby/library/net/http/http/proxy_user_spec.rb b/spec/ruby/library/net/http/http/proxy_user_spec.rb new file mode 100644 index 0000000000..1beacca642 --- /dev/null +++ b/spec/ruby/library/net/http/http/proxy_user_spec.rb @@ -0,0 +1,39 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.proxy_user" do + describe "when self is no proxy class" do + it "returns nil" do + Net::HTTP.proxy_user.should be_nil + end + end + + describe "when self is a proxy class" do + it "returns nil if no username was set for self's proxy connection" do + Net::HTTP.Proxy("localhost").proxy_user.should be_nil + end + + it "returns the username for self's proxy connection" do + Net::HTTP.Proxy("localhost", 1234, "rspec", "rocks").proxy_user.should == "rspec" + end + end +end + +describe "Net::HTTP#proxy_user" do + describe "when self is no proxy class instance" do + it "returns nil" do + Net::HTTP.new("localhost", 3333).proxy_user.should be_nil + end + end + + describe "when self is a proxy class instance" do + it "returns nil if no username was set for self's proxy connection" do + Net::HTTP.Proxy("localhost").new("localhost", 3333).proxy_user.should be_nil + end + + it "returns the username for self's proxy connection" do + http_with_proxy = Net::HTTP.Proxy("localhost", 1234, "rspec", "rocks") + http_with_proxy.new("localhost", 3333).proxy_user.should == "rspec" + end + end +end diff --git a/spec/ruby/library/net/http/http/put2_spec.rb b/spec/ruby/library/net/http/http/put2_spec.rb new file mode 100644 index 0000000000..a0e832d170 --- /dev/null +++ b/spec/ruby/library/net/http/http/put2_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) +require File.expand_path('../shared/request_put', __FILE__) + +describe "Net::HTTP#put2" do + it_behaves_like :net_ftp_request_put, :put2 +end diff --git a/spec/ruby/library/net/http/http/put_spec.rb b/spec/ruby/library/net/http/http/put_spec.rb new file mode 100644 index 0000000000..ab7e794db0 --- /dev/null +++ b/spec/ruby/library/net/http/http/put_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#put" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends an put request to the passed path and returns the response" do + response = @http.put("/request", "test=test") + response.body.should == "Request type: PUT" + end + + it "returns a Net::HTTPResponse" do + @http.put("/request", "test=test").should be_kind_of(Net::HTTPResponse) + end +end diff --git a/spec/ruby/library/net/http/http/read_timeout_spec.rb b/spec/ruby/library/net/http/http/read_timeout_spec.rb new file mode 100644 index 0000000000..86f2e0246d --- /dev/null +++ b/spec/ruby/library/net/http/http/read_timeout_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP#read_timeout" do + it "returns the seconds to wait until reading one block" do + net = Net::HTTP.new("localhost") + net.read_timeout.should eql(60) + net.read_timeout = 10 + net.read_timeout.should eql(10) + end +end + +describe "Net::HTTP#read_timeout=" do + it "sets the seconds to wait till the connection is open" do + net = Net::HTTP.new("localhost") + net.read_timeout = 10 + net.read_timeout.should eql(10) + end + + it "returns the newly set value" do + net = Net::HTTP.new("localhost") + (net.read_timeout = 10).should eql(10) + end +end diff --git a/spec/ruby/library/net/http/http/request_get_spec.rb b/spec/ruby/library/net/http/http/request_get_spec.rb new file mode 100644 index 0000000000..33b040c622 --- /dev/null +++ b/spec/ruby/library/net/http/http/request_get_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) +require File.expand_path('../shared/request_get', __FILE__) + +describe "Net::HTTP#request_get" do + it_behaves_like :net_ftp_request_get, :get2 +end diff --git a/spec/ruby/library/net/http/http/request_head_spec.rb b/spec/ruby/library/net/http/http/request_head_spec.rb new file mode 100644 index 0000000000..85ff56dcb5 --- /dev/null +++ b/spec/ruby/library/net/http/http/request_head_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) +require File.expand_path('../shared/request_head', __FILE__) + +describe "Net::HTTP#request_head" do + it_behaves_like :net_ftp_request_head, :request_head +end diff --git a/spec/ruby/library/net/http/http/request_post_spec.rb b/spec/ruby/library/net/http/http/request_post_spec.rb new file mode 100644 index 0000000000..937b4d5d59 --- /dev/null +++ b/spec/ruby/library/net/http/http/request_post_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) +require File.expand_path('../shared/request_post', __FILE__) + +describe "Net::HTTP#request_post" do + it_behaves_like :net_ftp_request_post, :request_post +end diff --git a/spec/ruby/library/net/http/http/request_put_spec.rb b/spec/ruby/library/net/http/http/request_put_spec.rb new file mode 100644 index 0000000000..c3a4b6d538 --- /dev/null +++ b/spec/ruby/library/net/http/http/request_put_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) +require File.expand_path('../shared/request_put', __FILE__) + +describe "Net::HTTP#request_put" do + it_behaves_like :net_ftp_request_put, :request_put +end diff --git a/spec/ruby/library/net/http/http/request_spec.rb b/spec/ruby/library/net/http/http/request_spec.rb new file mode 100644 index 0000000000..d1c754df8c --- /dev/null +++ b/spec/ruby/library/net/http/http/request_spec.rb @@ -0,0 +1,109 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#request" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + describe "when passed request_object" do + it "makes a HTTP Request based on the passed request_object" do + response = @http.request(Net::HTTP::Get.new("/request"), "test=test") + response.body.should == "Request type: GET" + + response = @http.request(Net::HTTP::Head.new("/request"), "test=test") + response.body.should be_nil + + response = @http.request(Net::HTTP::Post.new("/request"), "test=test") + response.body.should == "Request type: POST" + + response = @http.request(Net::HTTP::Put.new("/request"), "test=test") + response.body.should == "Request type: PUT" + + response = @http.request(Net::HTTP::Proppatch.new("/request"), "test=test") + response.body.should == "Request type: PROPPATCH" + + response = @http.request(Net::HTTP::Lock.new("/request"), "test=test") + response.body.should == "Request type: LOCK" + + response = @http.request(Net::HTTP::Unlock.new("/request"), "test=test") + response.body.should == "Request type: UNLOCK" + + # TODO: Does not work? + #response = @http.request(Net::HTTP::Options.new("/request"), "test=test") + #response.body.should be_nil + + response = @http.request(Net::HTTP::Propfind.new("/request"), "test=test") + response.body.should == "Request type: PROPFIND" + + response = @http.request(Net::HTTP::Delete.new("/request"), "test=test") + response.body.should == "Request type: DELETE" + + response = @http.request(Net::HTTP::Move.new("/request"), "test=test") + response.body.should == "Request type: MOVE" + + response = @http.request(Net::HTTP::Copy.new("/request"), "test=test") + response.body.should == "Request type: COPY" + + response = @http.request(Net::HTTP::Mkcol.new("/request"), "test=test") + response.body.should == "Request type: MKCOL" + + response = @http.request(Net::HTTP::Trace.new("/request"), "test=test") + response.body.should == "Request type: TRACE" + end + end + + describe "when passed request_object and request_body" do + it "sends the passed request_body when making the HTTP Request" do + response = @http.request(Net::HTTP::Get.new("/request/body"), "test=test") + response.body.should == "test=test" + + response = @http.request(Net::HTTP::Head.new("/request/body"), "test=test") + response.body.should be_nil + + response = @http.request(Net::HTTP::Post.new("/request/body"), "test=test") + response.body.should == "test=test" + + response = @http.request(Net::HTTP::Put.new("/request/body"), "test=test") + response.body.should == "test=test" + + response = @http.request(Net::HTTP::Proppatch.new("/request/body"), "test=test") + response.body.should == "test=test" + + response = @http.request(Net::HTTP::Lock.new("/request/body"), "test=test") + response.body.should == "test=test" + + response = @http.request(Net::HTTP::Unlock.new("/request/body"), "test=test") + response.body.should == "test=test" + + # TODO: Does not work? + #response = @http.request(Net::HTTP::Options.new("/request/body"), "test=test") + #response.body.should be_nil + + response = @http.request(Net::HTTP::Propfind.new("/request/body"), "test=test") + response.body.should == "test=test" + + response = @http.request(Net::HTTP::Delete.new("/request/body"), "test=test") + response.body.should == "test=test" + + response = @http.request(Net::HTTP::Move.new("/request/body"), "test=test") + response.body.should == "test=test" + + response = @http.request(Net::HTTP::Copy.new("/request/body"), "test=test") + response.body.should == "test=test" + + response = @http.request(Net::HTTP::Mkcol.new("/request/body"), "test=test") + response.body.should == "test=test" + + response = @http.request(Net::HTTP::Trace.new("/request/body"), "test=test") + response.body.should == "test=test" + end + end +end diff --git a/spec/ruby/library/net/http/http/request_types_spec.rb b/spec/ruby/library/net/http/http/request_types_spec.rb new file mode 100644 index 0000000000..8855a7db66 --- /dev/null +++ b/spec/ruby/library/net/http/http/request_types_spec.rb @@ -0,0 +1,254 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP::Get" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Get.should < Net::HTTPRequest + end + + it "represents the 'GET'-Request-Method" do + Net::HTTP::Get::METHOD.should == "GET" + end + + it "has no Request Body" do + Net::HTTP::Get::REQUEST_HAS_BODY.should be_false + end + + it "has a Respone Body" do + Net::HTTP::Get::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Head" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Head.should < Net::HTTPRequest + end + + it "represents the 'HEAD'-Request-Method" do + Net::HTTP::Head::METHOD.should == "HEAD" + end + + it "has no Request Body" do + Net::HTTP::Head::REQUEST_HAS_BODY.should be_false + end + + it "has no Respone Body" do + Net::HTTP::Head::RESPONSE_HAS_BODY.should be_false + end +end + +describe "Net::HTTP::Post" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Post.should < Net::HTTPRequest + end + + it "represents the 'POST'-Request-Method" do + Net::HTTP::Post::METHOD.should == "POST" + end + + it "has a Request Body" do + Net::HTTP::Post::REQUEST_HAS_BODY.should be_true + end + + it "has a Respone Body" do + Net::HTTP::Post::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Put" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Put.should < Net::HTTPRequest + end + + it "represents the 'PUT'-Request-Method" do + Net::HTTP::Put::METHOD.should == "PUT" + end + + it "has a Request Body" do + Net::HTTP::Put::REQUEST_HAS_BODY.should be_true + end + + it "has a Respone Body" do + Net::HTTP::Put::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Delete" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Delete.should < Net::HTTPRequest + end + + it "represents the 'DELETE'-Request-Method" do + Net::HTTP::Delete::METHOD.should == "DELETE" + end + + it "has no Request Body" do + Net::HTTP::Delete::REQUEST_HAS_BODY.should be_false + end + + it "has a Respone Body" do + Net::HTTP::Delete::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Options" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Options.should < Net::HTTPRequest + end + + it "represents the 'OPTIONS'-Request-Method" do + Net::HTTP::Options::METHOD.should == "OPTIONS" + end + + it "has no Request Body" do + Net::HTTP::Options::REQUEST_HAS_BODY.should be_false + end + + it "has no Respone Body" do + Net::HTTP::Options::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Trace" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Trace.should < Net::HTTPRequest + end + + it "represents the 'TRACE'-Request-Method" do + Net::HTTP::Trace::METHOD.should == "TRACE" + end + + it "has no Request Body" do + Net::HTTP::Trace::REQUEST_HAS_BODY.should be_false + end + + it "has a Respone Body" do + Net::HTTP::Trace::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Propfind" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Propfind.should < Net::HTTPRequest + end + + it "represents the 'PROPFIND'-Request-Method" do + Net::HTTP::Propfind::METHOD.should == "PROPFIND" + end + + it "has a Request Body" do + Net::HTTP::Propfind::REQUEST_HAS_BODY.should be_true + end + + it "has a Respone Body" do + Net::HTTP::Propfind::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Proppatch" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Proppatch.should < Net::HTTPRequest + end + + it "represents the 'PROPPATCH'-Request-Method" do + Net::HTTP::Proppatch::METHOD.should == "PROPPATCH" + end + + it "has a Request Body" do + Net::HTTP::Proppatch::REQUEST_HAS_BODY.should be_true + end + + it "has a Respone Body" do + Net::HTTP::Proppatch::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Mkcol" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Mkcol.should < Net::HTTPRequest + end + + it "represents the 'MKCOL'-Request-Method" do + Net::HTTP::Mkcol::METHOD.should == "MKCOL" + end + + it "has a Request Body" do + Net::HTTP::Mkcol::REQUEST_HAS_BODY.should be_true + end + + it "has a Respone Body" do + Net::HTTP::Mkcol::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Copy" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Copy.should < Net::HTTPRequest + end + + it "represents the 'COPY'-Request-Method" do + Net::HTTP::Copy::METHOD.should == "COPY" + end + + it "has no Request Body" do + Net::HTTP::Copy::REQUEST_HAS_BODY.should be_false + end + + it "has a Respone Body" do + Net::HTTP::Copy::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Move" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Move.should < Net::HTTPRequest + end + + it "represents the 'MOVE'-Request-Method" do + Net::HTTP::Move::METHOD.should == "MOVE" + end + + it "has no Request Body" do + Net::HTTP::Move::REQUEST_HAS_BODY.should be_false + end + + it "has a Respone Body" do + Net::HTTP::Move::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Lock" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Lock.should < Net::HTTPRequest + end + + it "represents the 'LOCK'-Request-Method" do + Net::HTTP::Lock::METHOD.should == "LOCK" + end + + it "has a Request Body" do + Net::HTTP::Lock::REQUEST_HAS_BODY.should be_true + end + + it "has a Respone Body" do + Net::HTTP::Lock::RESPONSE_HAS_BODY.should be_true + end +end + +describe "Net::HTTP::Unlock" do + it "is a subclass of Net::HTTPRequest" do + Net::HTTP::Unlock.should < Net::HTTPRequest + end + + it "represents the 'UNLOCK'-Request-Method" do + Net::HTTP::Unlock::METHOD.should == "UNLOCK" + end + + it "has a Request Body" do + Net::HTTP::Unlock::REQUEST_HAS_BODY.should be_true + end + + it "has a Respone Body" do + Net::HTTP::Unlock::RESPONSE_HAS_BODY.should be_true + end +end diff --git a/spec/ruby/library/net/http/http/send_request_spec.rb b/spec/ruby/library/net/http/http/send_request_spec.rb new file mode 100644 index 0000000000..5a704496ec --- /dev/null +++ b/spec/ruby/library/net/http/http/send_request_spec.rb @@ -0,0 +1,61 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#send_request" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + + # HEAD is special so handled separately + @methods = %w[ + GET POST PUT DELETE + OPTIONS + PROPFIND PROPPATCH LOCK UNLOCK + ] + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + # TODO: Does only work with GET and POST requests + describe "when passed type, path" do + it "sends a HTTP Request of the passed type to the passed path" do + response = @http.send_request("HEAD", "/request") + response.body.should be_nil + + @methods.each do |method| + response = @http.send_request(method, "/request") + response.body.should == "Request type: #{method}" + end + end + end + + describe "when passed type, path, body" do + it "sends a HTTP Request with the passed body" do + response = @http.send_request("HEAD", "/request/body", "test=test") + response.body.should be_nil + + @methods.each do |method| + response = @http.send_request(method, "/request/body", "test=test") + response.body.should == "test=test" + end + end + end + + describe "when passed type, path, body, headers" do + it "sends a HTTP Request with the passed headers" do + referer = 'https://www.ruby-lang.org/'.freeze + + response = @http.send_request("HEAD", "/request/header", "test=test", "referer" => referer) + response.body.should be_nil + + @methods.each do |method| + response = @http.send_request(method, "/request/header", "test=test", "referer" => referer) + response.body.should include('"referer"=>["' + referer + '"]') + end + end + end +end diff --git a/spec/ruby/library/net/http/http/set_debug_output_spec.rb b/spec/ruby/library/net/http/http/set_debug_output_spec.rb new file mode 100644 index 0000000000..806f468a84 --- /dev/null +++ b/spec/ruby/library/net/http/http/set_debug_output_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require "stringio" +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#set_debug_output when passed io" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.new("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sets the passed io as output stream for debugging" do + io = StringIO.new + + @http.set_debug_output(io) + @http.start + io.string.should_not be_empty + size = io.string.size + + @http.get("/") + io.string.size.should > size + end + + it "outputs a warning when the connection has already been started" do + @http.start + lambda { @http.set_debug_output(StringIO.new) }.should complain("Net::HTTP#set_debug_output called after HTTP started\n") + end +end diff --git a/spec/ruby/library/net/http/http/shared/request_get.rb b/spec/ruby/library/net/http/http/shared/request_get.rb new file mode 100644 index 0000000000..b0eca665d6 --- /dev/null +++ b/spec/ruby/library/net/http/http/shared/request_get.rb @@ -0,0 +1,41 @@ +describe :net_ftp_request_get, shared: true do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + describe "when passed no block" do + it "sends a GET request to the passed path and returns the response" do + response = @http.send(@method, "/request") + response.body.should == "Request type: GET" + end + + it "returns a Net::HTTPResponse object" do + response = @http.send(@method, "/request") + response.should be_kind_of(Net::HTTPResponse) + end + end + + describe "when passed a block" do + it "sends a GET request to the passed path and returns the response" do + response = @http.send(@method, "/request") {} + response.body.should == "Request type: GET" + end + + it "yields the response to the passed block" do + @http.send(@method, "/request") do |response| + response.body.should == "Request type: GET" + end + end + + it "returns a Net::HTTPResponse object" do + response = @http.send(@method, "/request") {} + response.should be_kind_of(Net::HTTPResponse) + end + end +end diff --git a/spec/ruby/library/net/http/http/shared/request_head.rb b/spec/ruby/library/net/http/http/shared/request_head.rb new file mode 100644 index 0000000000..0e669de9ac --- /dev/null +++ b/spec/ruby/library/net/http/http/shared/request_head.rb @@ -0,0 +1,41 @@ +describe :net_ftp_request_head, shared: true do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + describe "when passed no block" do + it "sends a head request to the passed path and returns the response" do + response = @http.send(@method, "/request") + response.body.should be_nil + end + + it "returns a Net::HTTPResponse object" do + response = @http.send(@method, "/request") + response.should be_kind_of(Net::HTTPResponse) + end + end + + describe "when passed a block" do + it "sends a head request to the passed path and returns the response" do + response = @http.send(@method, "/request") {} + response.body.should be_nil + end + + it "yields the response to the passed block" do + @http.send(@method, "/request") do |response| + response.body.should be_nil + end + end + + it "returns a Net::HTTPResponse object" do + response = @http.send(@method, "/request") {} + response.should be_kind_of(Net::HTTPResponse) + end + end +end diff --git a/spec/ruby/library/net/http/http/shared/request_post.rb b/spec/ruby/library/net/http/http/shared/request_post.rb new file mode 100644 index 0000000000..06c5e139f9 --- /dev/null +++ b/spec/ruby/library/net/http/http/shared/request_post.rb @@ -0,0 +1,41 @@ +describe :net_ftp_request_post, shared: true do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + describe "when passed no block" do + it "sends a post request to the passed path and returns the response" do + response = @http.send(@method, "/request", "test=test") + response.body.should == "Request type: POST" + end + + it "returns a Net::HTTPResponse object" do + response = @http.send(@method, "/request", "test=test") + response.should be_kind_of(Net::HTTPResponse) + end + end + + describe "when passed a block" do + it "sends a post request to the passed path and returns the response" do + response = @http.send(@method, "/request", "test=test") {} + response.body.should == "Request type: POST" + end + + it "yields the response to the passed block" do + @http.send(@method, "/request", "test=test") do |response| + response.body.should == "Request type: POST" + end + end + + it "returns a Net::HTTPResponse object" do + response = @http.send(@method, "/request", "test=test") {} + response.should be_kind_of(Net::HTTPResponse) + end + end +end diff --git a/spec/ruby/library/net/http/http/shared/request_put.rb b/spec/ruby/library/net/http/http/shared/request_put.rb new file mode 100644 index 0000000000..6ae791d7e7 --- /dev/null +++ b/spec/ruby/library/net/http/http/shared/request_put.rb @@ -0,0 +1,41 @@ +describe :net_ftp_request_put, shared: true do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + describe "when passed no block" do + it "sends a put request to the passed path and returns the response" do + response = @http.send(@method, "/request", "test=test") + response.body.should == "Request type: PUT" + end + + it "returns a Net::HTTPResponse object" do + response = @http.send(@method, "/request", "test=test") + response.should be_kind_of(Net::HTTPResponse) + end + end + + describe "when passed a block" do + it "sends a put request to the passed path and returns the response" do + response = @http.send(@method, "/request", "test=test") {} + response.body.should == "Request type: PUT" + end + + it "yields the response to the passed block" do + @http.send(@method, "/request", "test=test") do |response| + response.body.should == "Request type: PUT" + end + end + + it "returns a Net::HTTPResponse object" do + response = @http.send(@method, "/request", "test=test") {} + response.should be_kind_of(Net::HTTPResponse) + end + end +end diff --git a/spec/ruby/library/net/http/http/shared/started.rb b/spec/ruby/library/net/http/http/shared/started.rb new file mode 100644 index 0000000000..9ff6272c31 --- /dev/null +++ b/spec/ruby/library/net/http/http/shared/started.rb @@ -0,0 +1,26 @@ +describe :net_http_started_p, shared: true do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.new("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "returns true when self has been started" do + @http.start + @http.send(@method).should be_true + end + + it "returns false when self has not been started yet" do + @http.send(@method).should be_false + end + + it "returns false when self has been stopped again" do + @http.start + @http.finish + @http.send(@method).should be_false + end +end diff --git a/spec/ruby/library/net/http/http/shared/version_1_1.rb b/spec/ruby/library/net/http/http/shared/version_1_1.rb new file mode 100644 index 0000000000..db3d6a986d --- /dev/null +++ b/spec/ruby/library/net/http/http/shared/version_1_1.rb @@ -0,0 +1,6 @@ +describe :net_http_version_1_1_p, shared: true do + it "returns the state of net/http 1.1 features" do + Net::HTTP.version_1_2 + Net::HTTP.send(@method).should be_false + end +end diff --git a/spec/ruby/library/net/http/http/shared/version_1_2.rb b/spec/ruby/library/net/http/http/shared/version_1_2.rb new file mode 100644 index 0000000000..b044182c60 --- /dev/null +++ b/spec/ruby/library/net/http/http/shared/version_1_2.rb @@ -0,0 +1,6 @@ +describe :net_http_version_1_2_p, shared: true do + it "returns the state of net/http 1.2 features" do + Net::HTTP.version_1_2 + Net::HTTP.send(@method).should be_true + end +end diff --git a/spec/ruby/library/net/http/http/socket_type_spec.rb b/spec/ruby/library/net/http/http/socket_type_spec.rb new file mode 100644 index 0000000000..4ec9b68571 --- /dev/null +++ b/spec/ruby/library/net/http/http/socket_type_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP.socket_type" do + it "returns BufferedIO" do + Net::HTTP.socket_type.should == Net::BufferedIO + end +end diff --git a/spec/ruby/library/net/http/http/start_spec.rb b/spec/ruby/library/net/http/http/start_spec.rb new file mode 100644 index 0000000000..407d57e494 --- /dev/null +++ b/spec/ruby/library/net/http/http/start_spec.rb @@ -0,0 +1,111 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP.start" do + before :each do + NetHTTPSpecs.start_server + @port = NetHTTPSpecs.port + end + + after :each do + NetHTTPSpecs.stop_server + end + + describe "when not passed a block" do + before :each do + @http = Net::HTTP.start("localhost", @port) + end + + after :each do + @http.finish if @http.started? + end + + it "returns a new Net::HTTP object for the passed address and port" do + @http.should be_kind_of(Net::HTTP) + @http.address.should == "localhost" + @http.port.should == @port + end + + it "opens the tcp connection" do + @http.started?.should be_true + end + end + + describe "when passed a block" do + it "returns the blocks return value" do + Net::HTTP.start("localhost", @port) { :test }.should == :test + end + + it "yields the new Net::HTTP object to the block" do + yielded = false + Net::HTTP.start("localhost", @port) do |net| + yielded = true + net.should be_kind_of(Net::HTTP) + end + yielded.should be_true + end + + it "opens the tcp connection before yielding" do + Net::HTTP.start("localhost", @port) { |http| http.started?.should be_true } + end + + it "closes the tcp connection after yielding" do + net = nil + Net::HTTP.start("localhost", @port) { |x| net = x } + net.started?.should be_false + end + end +end + +describe "Net::HTTP#start" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.new("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "returns self" do + @http.start.should equal(@http) + end + + it "opens the tcp connection" do + @http.start + @http.started?.should be_true + end + + describe "when self has already been started" do + it "raises an IOError" do + @http.start + lambda { @http.start }.should raise_error(IOError) + end + end + + describe "when passed a block" do + it "returns the blocks return value" do + @http.start { :test }.should == :test + end + + it "yields the new Net::HTTP object to the block" do + yielded = false + @http.start do |http| + yielded = true + http.should equal(@http) + end + yielded.should be_true + end + + it "opens the tcp connection before yielding" do + @http.start { |http| http.started?.should be_true } + end + + it "closes the tcp connection after yielding" do + @http.start { } + @http.started?.should be_false + end + end +end diff --git a/spec/ruby/library/net/http/http/started_spec.rb b/spec/ruby/library/net/http/http/started_spec.rb new file mode 100644 index 0000000000..01c17c93c1 --- /dev/null +++ b/spec/ruby/library/net/http/http/started_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) +require File.expand_path('../shared/started', __FILE__) + +describe "Net::HTTP#started?" do + it_behaves_like :net_http_started_p, :started? +end diff --git a/spec/ruby/library/net/http/http/trace_spec.rb b/spec/ruby/library/net/http/http/trace_spec.rb new file mode 100644 index 0000000000..6cce15fc09 --- /dev/null +++ b/spec/ruby/library/net/http/http/trace_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#trace" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends a TRACE request to the passed path and returns the response" do + response = @http.trace("/request") + response.body.should == "Request type: TRACE" + end + + it "returns a Net::HTTPResponse" do + @http.trace("/request").should be_kind_of(Net::HTTPResponse) + end +end diff --git a/spec/ruby/library/net/http/http/unlock_spec.rb b/spec/ruby/library/net/http/http/unlock_spec.rb new file mode 100644 index 0000000000..12df417e1a --- /dev/null +++ b/spec/ruby/library/net/http/http/unlock_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/http_server', __FILE__) + +describe "Net::HTTP#unlock" do + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "sends an UNLOCK request to the passed path and returns the response" do + response = @http.unlock("/request", "test=test") + response.body.should == "Request type: UNLOCK" + end + + it "returns a Net::HTTPResponse" do + @http.unlock("/request", "test=test").should be_kind_of(Net::HTTPResponse) + end +end diff --git a/spec/ruby/library/net/http/http/use_ssl_spec.rb b/spec/ruby/library/net/http/http/use_ssl_spec.rb new file mode 100644 index 0000000000..19d065ed8e --- /dev/null +++ b/spec/ruby/library/net/http/http/use_ssl_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTP#use_ssl?" do + it "returns false" do + http = Net::HTTP.new("localhost") + http.use_ssl?.should be_false + end +end diff --git a/spec/ruby/library/net/http/http/version_1_1_spec.rb b/spec/ruby/library/net/http/http/version_1_1_spec.rb new file mode 100644 index 0000000000..f13ec9b7f1 --- /dev/null +++ b/spec/ruby/library/net/http/http/version_1_1_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../shared/version_1_1', __FILE__) + +describe "Net::HTTP.version_1_1?" do + it_behaves_like :net_http_version_1_1_p, :version_1_1? +end diff --git a/spec/ruby/library/net/http/http/version_1_2_spec.rb b/spec/ruby/library/net/http/http/version_1_2_spec.rb new file mode 100644 index 0000000000..63c2112422 --- /dev/null +++ b/spec/ruby/library/net/http/http/version_1_2_spec.rb @@ -0,0 +1,20 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../shared/version_1_2', __FILE__) + +describe "Net::HTTP.version_1_2" do + it "turns on net/http 1.2 features" do + Net::HTTP.version_1_2 + + Net::HTTP.version_1_2?.should be_true + Net::HTTP.version_1_1?.should be_false + end + + it "returns true" do + Net::HTTP.version_1_2.should be_true + end +end + +describe "Net::HTTP.version_1_2?" do + it_behaves_like :net_http_version_1_2_p, :version_1_2? +end diff --git a/spec/ruby/library/net/http/httpexceptions/fixtures/classes.rb b/spec/ruby/library/net/http/httpexceptions/fixtures/classes.rb new file mode 100644 index 0000000000..abe8855eff --- /dev/null +++ b/spec/ruby/library/net/http/httpexceptions/fixtures/classes.rb @@ -0,0 +1,5 @@ +module NetHTTPExceptionsSpecs + class Simple < StandardError + include Net::HTTPExceptions + end +end diff --git a/spec/ruby/library/net/http/httpexceptions/initialize_spec.rb b/spec/ruby/library/net/http/httpexceptions/initialize_spec.rb new file mode 100644 index 0000000000..8c0c4934f7 --- /dev/null +++ b/spec/ruby/library/net/http/httpexceptions/initialize_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPExceptions#initialize when passed message, response" do + before :each do + @exception = NetHTTPExceptionsSpecs::Simple.new("error message", "a http response") + end + + it "calls super with the passed message" do + @exception.message.should == "error message" + end + + it "sets self's response to the passed response" do + @exception.response.should == "a http response" + end +end diff --git a/spec/ruby/library/net/http/httpexceptions/response_spec.rb b/spec/ruby/library/net/http/httpexceptions/response_spec.rb new file mode 100644 index 0000000000..becf74bddf --- /dev/null +++ b/spec/ruby/library/net/http/httpexceptions/response_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPExceptions#response" do + it "returns self's response" do + exception = NetHTTPExceptionsSpecs::Simple.new("error message", "a http response") + exception.response.should == "a http response" + end +end diff --git a/spec/ruby/library/net/http/httpgenericrequest/body_exist_spec.rb b/spec/ruby/library/net/http/httpgenericrequest/body_exist_spec.rb new file mode 100644 index 0000000000..cb3565b899 --- /dev/null +++ b/spec/ruby/library/net/http/httpgenericrequest/body_exist_spec.rb @@ -0,0 +1,22 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPGenericRequest#body_exist?" do + it "returns true when the response is expected to have a body" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + request.body_exist?.should be_true + + request = Net::HTTPGenericRequest.new("POST", true, false, "/some/path") + request.body_exist?.should be_false + end + + describe "when $VERBOSE is true" do + it "emits a warning" do + request = Net::HTTPGenericRequest.new("POST", true, false, "/some/path") + lambda { + $VERBOSE = true + request.body_exist? + }.should complain(/body_exist\? is obsolete/) + end + end +end diff --git a/spec/ruby/library/net/http/httpgenericrequest/body_spec.rb b/spec/ruby/library/net/http/httpgenericrequest/body_spec.rb new file mode 100644 index 0000000000..4aa4fff2bc --- /dev/null +++ b/spec/ruby/library/net/http/httpgenericrequest/body_spec.rb @@ -0,0 +1,30 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require "stringio" + +describe "Net::HTTPGenericRequest#body" do + it "returns self's request body" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + request.body.should be_nil + + request.body = "Some Content" + request.body.should == "Some Content" + end +end + +describe "Net::HTTPGenericRequest#body=" do + before :each do + @request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + end + + it "sets self's body content to the passed String" do + @request.body = "Some Content" + @request.body.should == "Some Content" + end + + it "sets self's body stream to nil" do + @request.body_stream = StringIO.new("") + @request.body = "Some Content" + @request.body_stream.should be_nil + end +end diff --git a/spec/ruby/library/net/http/httpgenericrequest/body_stream_spec.rb b/spec/ruby/library/net/http/httpgenericrequest/body_stream_spec.rb new file mode 100644 index 0000000000..df6e02a8c1 --- /dev/null +++ b/spec/ruby/library/net/http/httpgenericrequest/body_stream_spec.rb @@ -0,0 +1,32 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require "stringio" + +describe "Net::HTTPGenericRequest#body_stream" do + it "returns self's body stream Object" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + request.body_stream.should be_nil + + stream = StringIO.new("test") + request.body_stream = stream + request.body_stream.should equal(stream) + end +end + +describe "Net::HTTPGenericRequest#body_stream=" do + before :each do + @request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + @stream = StringIO.new("test") + end + + it "sets self's body stream to the passed Object" do + @request.body_stream = @stream + @request.body_stream.should equal(@stream) + end + + it "sets self's body to nil" do + @request.body = "Some Content" + @request.body_stream = @stream + @request.body.should be_nil + end +end diff --git a/spec/ruby/library/net/http/httpgenericrequest/exec_spec.rb b/spec/ruby/library/net/http/httpgenericrequest/exec_spec.rb new file mode 100644 index 0000000000..b3850d6c74 --- /dev/null +++ b/spec/ruby/library/net/http/httpgenericrequest/exec_spec.rb @@ -0,0 +1,131 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require "stringio" + +describe "Net::HTTPGenericRequest#exec when passed socket, version, path" do + before :each do + @socket = StringIO.new("") + @buffered_socket = Net::BufferedIO.new(@socket) + end + + it "executes the request over the socket to the path using the HTTP version" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + + request.exec(@buffered_socket, "1.1", "/some/path") + str = @socket.string + + str.should =~ %r[POST /some/path HTTP/1.1\r\n] + str.should =~ %r[Accept: \*/\*\r\n] + str[-4..-1].should == "\r\n\r\n" + + request = Net::HTTPGenericRequest.new("GET", true, true, "/some/path", + "Content-Type" => "text/html") + + request.exec(@buffered_socket, "1.0", "/some/other/path") + str = @socket.string + + str.should =~ %r[GET /some/other/path HTTP/1.0\r\n] + str.should =~ %r[Accept: \*/\*\r\n] + str.should =~ %r[Content-Type: text/html\r\n] + str[-4..-1].should == "\r\n\r\n" + end + + describe "when a request body is set" do + it "sets the 'Content-Type' header to 'application/x-www-form-urlencoded' unless the 'Content-Type' header is supplied" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + request.body = "Some Content" + + request.exec(@buffered_socket, "1.1", "/some/other/path") + str = @socket.string + + str.should =~ %r[POST /some/other/path HTTP/1.1\r\n] + str.should =~ %r[Accept: \*/\*\r\n] + str.should =~ %r[Content-Type: application/x-www-form-urlencoded\r\n] + str.should =~ %r[Content-Length: 12\r\n] + str[-16..-1].should == "\r\n\r\nSome Content" + end + + it "correctly sets the 'Content-Length' header and includes the body" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path", + "Content-Type" => "text/html") + request.body = "Some Content" + + request.exec(@buffered_socket, "1.1", "/some/other/path") + str = @socket.string + + str.should =~ %r[POST /some/other/path HTTP/1.1\r\n] + str.should =~ %r[Accept: \*/\*\r\n] + str.should =~ %r[Content-Type: text/html\r\n] + str.should =~ %r[Content-Length: 12\r\n] + str[-16..-1].should == "\r\n\r\nSome Content" + end + end + + describe "when a body stream is set" do + it "sets the 'Content-Type' header to 'application/x-www-form-urlencoded' unless the 'Content-Type' header is supplied" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path", + "Content-Length" => "10") + request.body_stream = StringIO.new("a" * 20) + + request.exec(@buffered_socket, "1.1", "/some/other/path") + str = @socket.string + + str.should =~ %r[POST /some/other/path HTTP/1.1\r\n] + str.should =~ %r[Accept: \*/\*\r\n] + str.should =~ %r[Content-Type: application/x-www-form-urlencoded\r\n] + str.should =~ %r[Content-Length: 10\r\n] + str[-24..-1].should == "\r\n\r\naaaaaaaaaaaaaaaaaaaa" + end + + it "sends the whole stream, regardless of the 'Content-Length' header" do + request = Net::HTTPGenericRequest.new("POST", true, true,"/some/path", + "Content-Type" => "text/html", + "Content-Length" => "10") + request.body_stream = StringIO.new("a" * 20) + + request.exec(@buffered_socket, "1.1", "/some/other/path") + str = @socket.string + + str.should =~ %r[POST /some/other/path HTTP/1.1\r\n] + str.should =~ %r[Accept: \*/\*\r\n] + str.should =~ %r[Content-Type: text/html\r\n] + str.should =~ %r[Content-Length: 10\r\n] + str[-24..-1].should == "\r\n\r\naaaaaaaaaaaaaaaaaaaa" + end + + it "sends the request in chunks when 'Transfer-Encoding' is set to 'chunked'" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path", + "Content-Type" => "text/html", + "Transfer-Encoding" => "chunked") + datasize = 1024 * 10 + request.body_stream = StringIO.new("a" * datasize) + + request.exec(@buffered_socket, "1.1", "/some/other/path") + str = @socket.string + + str.should =~ %r[POST /some/other/path HTTP/1.1\r\n] + str.should =~ %r[Accept: \*/\*\r\n] + str.should =~ %r[Content-Type: text/html\r\n] + str.should =~ %r[Transfer-Encoding: chunked\r\n] + str =~ %r[\r\n\r\n] + str = $' + while datasize > 0 + chunk_size_line, str = str.split(/\r\n/, 2) + chunk_size = chunk_size_line[/\A[0-9A-Fa-f]+/].to_i(16) + str.slice!(0, chunk_size).should == 'a' * chunk_size + datasize -= chunk_size + str.slice!(0, 2).should == "\r\n" + end + datasize.should == 0 + str.should == %"0\r\n\r\n" + end + + it "raises an ArgumentError when the 'Content-Length' is not set or 'Transfer-Encoding' is not set to 'chunked'" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path", + "Content-Type" => "text/html") + request.body_stream = StringIO.new("Some Content") + + lambda { request.exec(@buffered_socket, "1.1", "/some/other/path") }.should raise_error(ArgumentError) + end + end +end diff --git a/spec/ruby/library/net/http/httpgenericrequest/inspect_spec.rb b/spec/ruby/library/net/http/httpgenericrequest/inspect_spec.rb new file mode 100644 index 0000000000..269ce4fe52 --- /dev/null +++ b/spec/ruby/library/net/http/httpgenericrequest/inspect_spec.rb @@ -0,0 +1,25 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPGenericRequest#inspect" do + it "returns a String representation of self" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + request.inspect.should == "#<Net::HTTPGenericRequest POST>" + + request = Net::HTTPGenericRequest.new("GET", false, true, "/some/path") + request.inspect.should == "#<Net::HTTPGenericRequest GET>" + + request = Net::HTTPGenericRequest.new("BLA", true, true, "/some/path") + request.inspect.should == "#<Net::HTTPGenericRequest BLA>" + + # Subclasses + request = Net::HTTP::Get.new("/some/path") + request.inspect.should == "#<Net::HTTP::Get GET>" + + request = Net::HTTP::Post.new("/some/path") + request.inspect.should == "#<Net::HTTP::Post POST>" + + request = Net::HTTP::Trace.new("/some/path") + request.inspect.should == "#<Net::HTTP::Trace TRACE>" + end +end diff --git a/spec/ruby/library/net/http/httpgenericrequest/method_spec.rb b/spec/ruby/library/net/http/httpgenericrequest/method_spec.rb new file mode 100644 index 0000000000..eef87ff3a7 --- /dev/null +++ b/spec/ruby/library/net/http/httpgenericrequest/method_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPGenericRequest#method" do + it "returns self's request method" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + request.method.should == "POST" + + request = Net::HTTPGenericRequest.new("GET", false, true, "/some/path") + request.method.should == "GET" + + request = Net::HTTPGenericRequest.new("BLA", true, true, "/some/path") + request.method.should == "BLA" + end +end diff --git a/spec/ruby/library/net/http/httpgenericrequest/path_spec.rb b/spec/ruby/library/net/http/httpgenericrequest/path_spec.rb new file mode 100644 index 0000000000..e48d37b0e9 --- /dev/null +++ b/spec/ruby/library/net/http/httpgenericrequest/path_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPGenericRequest#path" do + it "returns self's request path" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + request.path.should == "/some/path" + + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/other/path") + request.path.should == "/some/other/path" + end +end diff --git a/spec/ruby/library/net/http/httpgenericrequest/request_body_permitted_spec.rb b/spec/ruby/library/net/http/httpgenericrequest/request_body_permitted_spec.rb new file mode 100644 index 0000000000..1e0b3ab028 --- /dev/null +++ b/spec/ruby/library/net/http/httpgenericrequest/request_body_permitted_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPGenericRequest#request_body_permitted?" do + it "returns true when the request is expected to have a body" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + request.request_body_permitted?.should be_true + + request = Net::HTTPGenericRequest.new("POST", false, true, "/some/path") + request.request_body_permitted?.should be_false + end +end diff --git a/spec/ruby/library/net/http/httpgenericrequest/response_body_permitted_spec.rb b/spec/ruby/library/net/http/httpgenericrequest/response_body_permitted_spec.rb new file mode 100644 index 0000000000..ee5a43e637 --- /dev/null +++ b/spec/ruby/library/net/http/httpgenericrequest/response_body_permitted_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPGenericRequest#response_body_permitted?" do + it "returns true when the response is expected to have a body" do + request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + request.response_body_permitted?.should be_true + + request = Net::HTTPGenericRequest.new("POST", true, false, "/some/path") + request.response_body_permitted?.should be_false + end +end diff --git a/spec/ruby/library/net/http/httpgenericrequest/set_body_internal_spec.rb b/spec/ruby/library/net/http/httpgenericrequest/set_body_internal_spec.rb new file mode 100644 index 0000000000..bff8646d5a --- /dev/null +++ b/spec/ruby/library/net/http/httpgenericrequest/set_body_internal_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPGenericRequest#set_body_internal when passed string" do + before :each do + @request = Net::HTTPGenericRequest.new("POST", true, true, "/some/path") + end + + it "sets self's body to the passed string" do + @request.set_body_internal("Some Content") + @request.body.should == "Some Content" + end + + it "raises an ArgumentError when the body or body_stream of self have already been set" do + @request.body = "Some Content" + lambda { @request.set_body_internal("Some other Content") }.should raise_error(ArgumentError) + + @request.body_stream = "Some Content" + lambda { @request.set_body_internal("Some other Content") }.should raise_error(ArgumentError) + end +end diff --git a/spec/ruby/library/net/http/httpheader/add_field_spec.rb b/spec/ruby/library/net/http/httpheader/add_field_spec.rb new file mode 100644 index 0000000000..b736a92694 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/add_field_spec.rb @@ -0,0 +1,31 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#add_field when passed key, value" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "adds the passed value to the header entry with the passed key" do + @headers.add_field("My-Header", "a") + @headers.get_fields("My-Header").should == ["a"] + + @headers.add_field("My-Header", "b") + @headers.get_fields("My-Header").should == ["a", "b"] + + @headers.add_field("My-Header", "c") + @headers.get_fields("My-Header").should == ["a", "b", "c"] + end + + it "is case-insensitive" do + @headers.add_field("My-Header", "a") + @headers.get_fields("My-Header").should == ["a"] + + @headers.add_field("my-header", "b") + @headers.get_fields("My-Header").should == ["a", "b"] + + @headers.add_field("MY-HEADER", "c") + @headers.get_fields("My-Header").should == ["a", "b", "c"] + end +end diff --git a/spec/ruby/library/net/http/httpheader/basic_auth_spec.rb b/spec/ruby/library/net/http/httpheader/basic_auth_spec.rb new file mode 100644 index 0000000000..b9ed792d46 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/basic_auth_spec.rb @@ -0,0 +1,14 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#basic_auth when passed account, password" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "sets the 'Authorization' Header entry for basic authorization" do + @headers.basic_auth("rubyspec", "rocks") + @headers["Authorization"].should == "Basic cnVieXNwZWM6cm9ja3M=" + end +end diff --git a/spec/ruby/library/net/http/httpheader/canonical_each_spec.rb b/spec/ruby/library/net/http/httpheader/canonical_each_spec.rb new file mode 100644 index 0000000000..a891c12d80 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/canonical_each_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/each_capitalized', __FILE__) + +describe "Net::HTTPHeader#canonical_each" do + it_behaves_like :net_httpheader_each_capitalized, :canonical_each +end diff --git a/spec/ruby/library/net/http/httpheader/chunked_spec.rb b/spec/ruby/library/net/http/httpheader/chunked_spec.rb new file mode 100644 index 0000000000..2b08baf34c --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/chunked_spec.rb @@ -0,0 +1,22 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#chunked?" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns true if the 'Transfer-Encoding' header entry is set to chunked" do + @headers.chunked?.should be_false + + @headers["Transfer-Encoding"] = "bla" + @headers.chunked?.should be_false + + @headers["Transfer-Encoding"] = "blachunkedbla" + @headers.chunked?.should be_false + + @headers["Transfer-Encoding"] = "chunked" + @headers.chunked?.should be_true + end +end diff --git a/spec/ruby/library/net/http/httpheader/content_length_spec.rb b/spec/ruby/library/net/http/httpheader/content_length_spec.rb new file mode 100644 index 0000000000..009eafde85 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/content_length_spec.rb @@ -0,0 +1,54 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#content_length" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns nil if no 'Content-Length' header entry is set" do + @headers.content_length.should be_nil + end + + it "raises a Net::HTTPHeaderSyntaxError when the 'Content-Length' header entry has an invalid format" do + @headers["Content-Length"] = "invalid" + lambda { @headers.content_length }.should raise_error(Net::HTTPHeaderSyntaxError) + end + + it "returns the value of the 'Content-Length' header entry as an Integer" do + @headers["Content-Length"] = "123" + @headers.content_length.should eql(123) + + @headers["Content-Length"] = "123valid" + @headers.content_length.should eql(123) + + @headers["Content-Length"] = "valid123" + @headers.content_length.should eql(123) + end +end + +describe "Net::HTTPHeader#content_length=" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "removes the 'Content-Length' entry if passed false or nil" do + @headers["Content-Length"] = "123" + @headers.content_length = nil + @headers["Content-Lenght"].should be_nil + end + + it "sets the 'Content-Length' entry to the passed value" do + @headers.content_length = "123" + @headers["Content-Length"].should == "123" + + @headers.content_length = "123valid" + @headers["Content-Length"].should == "123" + end + + it "sets the 'Content-Length' entry to 0 if the passed value is not valid" do + @headers.content_length = "invalid123" + @headers["Content-Length"].should == "0" + end +end diff --git a/spec/ruby/library/net/http/httpheader/content_range_spec.rb b/spec/ruby/library/net/http/httpheader/content_range_spec.rb new file mode 100644 index 0000000000..dc601fb365 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/content_range_spec.rb @@ -0,0 +1,32 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#content_range" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns a Range object that represents the 'Content-Range' header entry" do + @headers["Content-Range"] = "bytes 0-499/1234" + @headers.content_range.should == (0..499) + + @headers["Content-Range"] = "bytes 500-1233/1234" + @headers.content_range.should == (500..1233) + end + + it "returns nil when there is no 'Content-Range' header entry" do + @headers.content_range.should be_nil + end + + it "raises a Net::HTTPHeaderSyntaxError when the 'Content-Range' has an invalid format" do + @headers["Content-Range"] = "invalid" + lambda { @headers.content_range }.should raise_error(Net::HTTPHeaderSyntaxError) + + @headers["Content-Range"] = "bytes 123-abc" + lambda { @headers.content_range }.should raise_error(Net::HTTPHeaderSyntaxError) + + @headers["Content-Range"] = "bytes abc-123" + lambda { @headers.content_range }.should raise_error(Net::HTTPHeaderSyntaxError) + end +end diff --git a/spec/ruby/library/net/http/httpheader/content_type_spec.rb b/spec/ruby/library/net/http/httpheader/content_type_spec.rb new file mode 100644 index 0000000000..7c3dfba7e7 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/content_type_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/set_content_type', __FILE__) + +describe "Net::HTTPHeader#content_type" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns the content type string, as per 'Content-Type' header entry" do + @headers["Content-Type"] = "text/html" + @headers.content_type.should == "text/html" + + @headers["Content-Type"] = "text/html;charset=utf-8" + @headers.content_type.should == "text/html" + end + + it "returns nil if the 'Content-Type' header entry does not exist" do + @headers.content_type.should be_nil + end +end + +describe "Net::HTTPHeader#content_type=" do + it_behaves_like :net_httpheader_set_content_type, :content_type= +end diff --git a/spec/ruby/library/net/http/httpheader/delete_spec.rb b/spec/ruby/library/net/http/httpheader/delete_spec.rb new file mode 100644 index 0000000000..9adbfd7813 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/delete_spec.rb @@ -0,0 +1,30 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#delete when passed key" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "removes the header entry with the passed key" do + @headers["My-Header"] = "test" + @headers.delete("My-Header") + + @headers["My-Header"].should be_nil + @headers.size.should eql(0) + end + + it "returns the removed values" do + @headers["My-Header"] = "test" + @headers.delete("My-Header").should == ["test"] + end + + it "is case-insensitive" do + @headers["My-Header"] = "test" + @headers.delete("my-header") + + @headers["My-Header"].should be_nil + @headers.size.should eql(0) + end +end diff --git a/spec/ruby/library/net/http/httpheader/each_capitalized_name_spec.rb b/spec/ruby/library/net/http/httpheader/each_capitalized_name_spec.rb new file mode 100644 index 0000000000..7adba9dec5 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/each_capitalized_name_spec.rb @@ -0,0 +1,35 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#each_capitalized_name" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + @headers["My-Header"] = "test" + @headers.add_field("My-Other-Header", "a") + @headers.add_field("My-Other-Header", "b") + end + + describe "when passed a block" do + it "yields each header key to the passed block (keys capitalized)" do + res = [] + @headers.each_capitalized_name do |key| + res << key + end + res.sort.should == ["My-Header", "My-Other-Header"] + end + end + + describe "when passed no block" do + it "returns an Enumerator" do + enumerator = @headers.each_capitalized_name + enumerator.should be_an_instance_of(Enumerator) + + res = [] + enumerator.each do |key| + res << key + end + res.sort.should == ["My-Header", "My-Other-Header"] + end + end +end diff --git a/spec/ruby/library/net/http/httpheader/each_capitalized_spec.rb b/spec/ruby/library/net/http/httpheader/each_capitalized_spec.rb new file mode 100644 index 0000000000..1dcf18a9d1 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/each_capitalized_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/each_capitalized', __FILE__) + +describe "Net::HTTPHeader#each_capitalized" do + it_behaves_like :net_httpheader_each_capitalized, :each_capitalized +end + diff --git a/spec/ruby/library/net/http/httpheader/each_header_spec.rb b/spec/ruby/library/net/http/httpheader/each_header_spec.rb new file mode 100644 index 0000000000..00d5894282 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/each_header_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/each_header', __FILE__) + +describe "Net::HTTPHeader#each_header" do + it_behaves_like :net_httpheader_each_header, :each_header +end diff --git a/spec/ruby/library/net/http/httpheader/each_key_spec.rb b/spec/ruby/library/net/http/httpheader/each_key_spec.rb new file mode 100644 index 0000000000..5c81830be6 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/each_key_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/each_name', __FILE__) + +describe "Net::HTTPHeader#each_key" do + it_behaves_like :net_httpheader_each_name, :each_key +end diff --git a/spec/ruby/library/net/http/httpheader/each_name_spec.rb b/spec/ruby/library/net/http/httpheader/each_name_spec.rb new file mode 100644 index 0000000000..e785ae249a --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/each_name_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/each_name', __FILE__) + +describe "Net::HTTPHeader#each_name" do + it_behaves_like :net_httpheader_each_name, :each_name +end diff --git a/spec/ruby/library/net/http/httpheader/each_spec.rb b/spec/ruby/library/net/http/httpheader/each_spec.rb new file mode 100644 index 0000000000..e8fb6713c3 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/each_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/each_header', __FILE__) + +describe "Net::HTTPHeader#each" do + it_behaves_like :net_httpheader_each_header, :each +end diff --git a/spec/ruby/library/net/http/httpheader/each_value_spec.rb b/spec/ruby/library/net/http/httpheader/each_value_spec.rb new file mode 100644 index 0000000000..1adb723a53 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/each_value_spec.rb @@ -0,0 +1,35 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#each_value" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + @headers["My-Header"] = "test" + @headers.add_field("My-Other-Header", "a") + @headers.add_field("My-Other-Header", "b") + end + + describe "when passed a block" do + it "yields each header entry's joined values" do + res = [] + @headers.each_value do |value| + res << value + end + res.sort.should == ["a, b", "test"] + end + end + + describe "when passed no block" do + it "returns an Enumerator" do + enumerator = @headers.each_value + enumerator.should be_an_instance_of(Enumerator) + + res = [] + enumerator.each do |key| + res << key + end + res.sort.should == ["a, b", "test"] + end + end +end diff --git a/spec/ruby/library/net/http/httpheader/element_reference_spec.rb b/spec/ruby/library/net/http/httpheader/element_reference_spec.rb new file mode 100644 index 0000000000..bfe6b48ad4 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/element_reference_spec.rb @@ -0,0 +1,39 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#[] when passed key" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns the value of the header entry with the passed key" do + @headers["My-Header"] = "test" + @headers["My-Header"].should == "test" + @headers["My-Other-Header"] = "another test" + @headers["My-Other-Header"].should == "another test" + end + + it "is case-insensitive" do + @headers["My-Header"] = "test" + + @headers['My-Header'].should == "test" + @headers['my-Header'].should == "test" + @headers['My-header'].should == "test" + @headers['my-header'].should == "test" + @headers['MY-HEADER'].should == "test" + end + + it "returns multi-element values joined together" do + @headers["My-Header"] = "test" + @headers.add_field("My-Header", "another test") + @headers.add_field("My-Header", "and one more") + + @headers["My-Header"].should == "test, another test, and one more" + end + + it "returns nil for non-existing entries" do + @headers["My-Header"].should be_nil + @headers["My-Other-Header"].should be_nil + end +end diff --git a/spec/ruby/library/net/http/httpheader/element_set_spec.rb b/spec/ruby/library/net/http/httpheader/element_set_spec.rb new file mode 100644 index 0000000000..a5a2900e94 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/element_set_spec.rb @@ -0,0 +1,41 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#[]= when passed key, value" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "sets the header entry with the passed key to the passed value" do + @headers["My-Header"] = "test" + @headers["My-Header"].should == "test" + + @headers["My-Header"] = "overwritten" + @headers["My-Header"].should == "overwritten" + + @headers["My-Other-Header"] = "another test" + @headers["My-Other-Header"].should == "another test" + end + + it "is case-insensitive" do + @headers['My-Header'] = "test" + @headers['my-Header'] = "another test" + @headers['My-header'] = "and one more test" + @headers['my-header'] = "and another one" + @headers['MY-HEADER'] = "last one" + + @headers["My-Header"].should == "last one" + @headers.size.should eql(1) + end + + it "removes the header entry with the passed key when the value is false or nil" do + @headers['My-Header'] = "test" + @headers['My-Header'] = nil + @headers['My-Header'].should be_nil + + @headers['My-Header'] = "test" + @headers['My-Header'] = false + @headers['My-Header'].should be_nil + end +end diff --git a/spec/ruby/library/net/http/httpheader/fetch_spec.rb b/spec/ruby/library/net/http/httpheader/fetch_spec.rb new file mode 100644 index 0000000000..7182c4396e --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/fetch_spec.rb @@ -0,0 +1,68 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#fetch" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + describe "when passed key" do + it "returns the header entry for the passed key" do + @headers["My-Header"] = "test" + @headers.fetch("My-Header").should == "test" + + @headers.add_field("My-Other-Header", "a") + @headers.add_field("My-Other-Header", "b") + @headers.add_field("My-Other-Header", "c") + @headers.fetch("My-Other-Header").should == "a, b, c" + end + + it "is case-insensitive" do + @headers["My-Header"] = "test" + @headers.fetch("my-header").should == "test" + @headers.fetch("MY-HEADER").should == "test" + end + + it "returns nil when there is no entry for the passed key" do + lambda { @headers.fetch("my-header") }.should raise_error(IndexError) + end + end + + describe "when passed key, default" do + it "returns the header entry for the passed key" do + @headers["My-Header"] = "test" + @headers.fetch("My-Header", "bla").should == "test" + + @headers.add_field("My-Other-Header", "a") + @headers.add_field("My-Other-Header", "b") + @headers.add_field("My-Other-Header", "c") + @headers.fetch("My-Other-Header", "bla").should == "a, b, c" + end + + # TODO: This raises a NoMethodError: undefined method `join' for "bla":String + it "returns the default value when there is no entry for the passed key" do + @headers.fetch("My-Header", "bla").should == "bla" + end + end + + describe "when passed key and block" do + it "returns the header entry for the passed key" do + @headers["My-Header"] = "test" + @headers.fetch("My-Header") {}.should == "test" + + @headers.add_field("My-Other-Header", "a") + @headers.add_field("My-Other-Header", "b") + @headers.add_field("My-Other-Header", "c") + -> { + @result = @headers.fetch("My-Other-Header", "bla") {} + }.should complain(/block supersedes default value argument/) + @result.should == "a, b, c" + end + + # TODO: This raises a NoMethodError: undefined method `join' for "redaeh-ym":String + it "yieldsand returns the block's return value when there is no entry for the passed key" do + @headers.fetch("My-Header") { |key| key.reverse }.should == "redaeh-ym" + end + end +end diff --git a/spec/ruby/library/net/http/httpheader/fixtures/classes.rb b/spec/ruby/library/net/http/httpheader/fixtures/classes.rb new file mode 100644 index 0000000000..b5ec6abd75 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/fixtures/classes.rb @@ -0,0 +1,11 @@ +module NetHTTPHeaderSpecs + class Example + include Net::HTTPHeader + + attr_accessor :body + + def initialize + initialize_http_header({}) + end + end +end diff --git a/spec/ruby/library/net/http/httpheader/form_data_spec.rb b/spec/ruby/library/net/http/httpheader/form_data_spec.rb new file mode 100644 index 0000000000..7781990824 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/form_data_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/set_form_data', __FILE__) + +describe "Net::HTTPHeader#form_data=" do + it_behaves_like :net_httpheader_set_form_data, :form_data= +end diff --git a/spec/ruby/library/net/http/httpheader/get_fields_spec.rb b/spec/ruby/library/net/http/httpheader/get_fields_spec.rb new file mode 100644 index 0000000000..f7828f752c --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/get_fields_spec.rb @@ -0,0 +1,39 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#get_fields when passed key" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns an Array containing the values of the header entry with the passed key" do + @headers["My-Header"] = "a" + @headers.get_fields("My-Header").should == ["a"] + + @headers.add_field("My-Header", "b") + @headers.get_fields("My-Header").should == ["a", "b"] + end + + it "returns a copy of the header entry values" do + @headers["My-Header"] = "a" + + @headers.get_fields("My-Header").clear + @headers.get_fields("My-Header").should == ["a"] + + @headers.get_fields("My-Header") << "b" + @headers.get_fields("My-Header").should == ["a"] + end + + it "returns nil for non-existing header entries" do + @headers.get_fields("My-Header").should be_nil + @headers.get_fields("My-Other-header").should be_nil + end + + it "is case-insensitive" do + @headers["My-Header"] = "test" + @headers.get_fields("My-Header").should == ["test"] + @headers.get_fields("my-header").should == ["test"] + @headers.get_fields("MY-HEADER").should == ["test"] + end +end diff --git a/spec/ruby/library/net/http/httpheader/initialize_http_header_spec.rb b/spec/ruby/library/net/http/httpheader/initialize_http_header_spec.rb new file mode 100644 index 0000000000..ffb56642d4 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/initialize_http_header_spec.rb @@ -0,0 +1,22 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#initialize_http_header when passed Hash" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.allocate + end + + it "initializes the HTTP Header using the passed Hash" do + @headers.initialize_http_header("My-Header" => "test", "My-Other-Header" => "another test") + @headers["My-Header"].should == "test" + @headers["My-Other-Header"].should == "another test" + end + + it "complains about duplicate keys when in verbose mode" do + lambda do + $VERBOSE = true + @headers.initialize_http_header("My-Header" => "test", "my-header" => "another test") + end.should complain(/duplicated HTTP header/) + end +end diff --git a/spec/ruby/library/net/http/httpheader/key_spec.rb b/spec/ruby/library/net/http/httpheader/key_spec.rb new file mode 100644 index 0000000000..0cb5f711dc --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/key_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#key? when passed key" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns true if the header entry with the passed key exists" do + @headers.key?("My-Header").should be_false + @headers["My-Header"] = "test" + @headers.key?("My-Header").should be_true + end + + it "is case-insensitive" do + @headers["My-Header"] = "test" + @headers.key?("my-header").should be_true + @headers.key?("MY-HEADER").should be_true + end +end diff --git a/spec/ruby/library/net/http/httpheader/length_spec.rb b/spec/ruby/library/net/http/httpheader/length_spec.rb new file mode 100644 index 0000000000..50cb1c1f7f --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/length_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/size', __FILE__) + +describe "Net::HTTPHeader#length" do + it_behaves_like :net_httpheader_size, :length +end diff --git a/spec/ruby/library/net/http/httpheader/main_type_spec.rb b/spec/ruby/library/net/http/httpheader/main_type_spec.rb new file mode 100644 index 0000000000..34e2ddbc65 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/main_type_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#main_type" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns the 'main-content-type', as per 'Content-Type' header entry" do + @headers["Content-Type"] = "text/html" + @headers.main_type.should == "text" + + @headers["Content-Type"] = "application/pdf" + @headers.main_type.should == "application" + + @headers["Content-Type"] = "text/html;charset=utf-8" + @headers.main_type.should == "text" + end + + it "returns nil if the 'Content-Type' header entry does not exist" do + @headers.main_type.should be_nil + end +end diff --git a/spec/ruby/library/net/http/httpheader/proxy_basic_auth_spec.rb b/spec/ruby/library/net/http/httpheader/proxy_basic_auth_spec.rb new file mode 100644 index 0000000000..5e7d87af36 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/proxy_basic_auth_spec.rb @@ -0,0 +1,14 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#proxy_basic_auth when passed account, password" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "sets the 'Proxy-Authorization' Header entry for basic authorization" do + @headers.proxy_basic_auth("rubyspec", "rocks") + @headers["Proxy-Authorization"].should == "Basic cnVieXNwZWM6cm9ja3M=" + end +end diff --git a/spec/ruby/library/net/http/httpheader/range_length_spec.rb b/spec/ruby/library/net/http/httpheader/range_length_spec.rb new file mode 100644 index 0000000000..ec3b00d805 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/range_length_spec.rb @@ -0,0 +1,32 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#range_length" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns the length of the Range represented by the 'Content-Range' header entry" do + @headers["Content-Range"] = "bytes 0-499/1234" + @headers.range_length.should eql(500) + + @headers["Content-Range"] = "bytes 500-1233/1234" + @headers.range_length.should eql(734) + end + + it "returns nil when there is no 'Content-Range' header entry" do + @headers.range_length.should be_nil + end + + it "raises a Net::HTTPHeaderSyntaxError when the 'Content-Range' has an invalid format" do + @headers["Content-Range"] = "invalid" + lambda { @headers.range_length }.should raise_error(Net::HTTPHeaderSyntaxError) + + @headers["Content-Range"] = "bytes 123-abc" + lambda { @headers.range_length }.should raise_error(Net::HTTPHeaderSyntaxError) + + @headers["Content-Range"] = "bytes abc-123" + lambda { @headers.range_length }.should raise_error(Net::HTTPHeaderSyntaxError) + end +end diff --git a/spec/ruby/library/net/http/httpheader/range_spec.rb b/spec/ruby/library/net/http/httpheader/range_spec.rb new file mode 100644 index 0000000000..d71d2ed598 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/range_spec.rb @@ -0,0 +1,48 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/set_range', __FILE__) + +describe "Net::HTTPHeader#range" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns a Range object that represents the 'Range' header entry" do + @headers["Range"] = "bytes=0-499" + @headers.range.should == [0..499] + + @headers["Range"] = "bytes=500-1233" + @headers.range.should == [500..1233] + + @headers["Range"] = "bytes=10-" + @headers.range.should == [10..-1] + + @headers["Range"] = "bytes=-10" + @headers.range.should == [-10..-1] + end + + it "returns nil when there is no 'Range' header entry" do + @headers.range.should be_nil + end + + it "raises a Net::HTTPHeaderSyntaxError when the 'Range' has an invalid format" do + @headers["Range"] = "invalid" + lambda { @headers.range }.should raise_error(Net::HTTPHeaderSyntaxError) + + @headers["Range"] = "bytes 123-abc" + lambda { @headers.range }.should raise_error(Net::HTTPHeaderSyntaxError) + + @headers["Range"] = "bytes abc-123" + lambda { @headers.range }.should raise_error(Net::HTTPHeaderSyntaxError) + end + + it "raises a Net::HTTPHeaderSyntaxError when the 'Range' was not specified" do + @headers["Range"] = "bytes=-" + lambda { @headers.range }.should raise_error(Net::HTTPHeaderSyntaxError) + end +end + +describe "Net::HTTPHeader#range=" do + it_behaves_like :net_httpheader_set_range, :range= +end diff --git a/spec/ruby/library/net/http/httpheader/set_content_type_spec.rb b/spec/ruby/library/net/http/httpheader/set_content_type_spec.rb new file mode 100644 index 0000000000..b9a62154ad --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/set_content_type_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/set_content_type', __FILE__) + +describe "Net::HTTPHeader#set_content_type" do + it_behaves_like :net_httpheader_set_content_type, :set_content_type +end diff --git a/spec/ruby/library/net/http/httpheader/set_form_data_spec.rb b/spec/ruby/library/net/http/httpheader/set_form_data_spec.rb new file mode 100644 index 0000000000..e675437ba4 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/set_form_data_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/set_form_data', __FILE__) + +describe "Net::HTTPHeader#set_form_data" do + it_behaves_like :net_httpheader_set_form_data, :set_form_data +end diff --git a/spec/ruby/library/net/http/httpheader/set_range_spec.rb b/spec/ruby/library/net/http/httpheader/set_range_spec.rb new file mode 100644 index 0000000000..af3f00f99c --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/set_range_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/set_range', __FILE__) + +describe "Net::HTTPHeader#set_range" do + it_behaves_like :net_httpheader_set_range, :set_range +end diff --git a/spec/ruby/library/net/http/httpheader/shared/each_capitalized.rb b/spec/ruby/library/net/http/httpheader/shared/each_capitalized.rb new file mode 100644 index 0000000000..3bac409876 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/shared/each_capitalized.rb @@ -0,0 +1,31 @@ +describe :net_httpheader_each_capitalized, shared: true do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + @headers["my-header"] = "test" + @headers.add_field("my-Other-Header", "a") + @headers.add_field("My-Other-header", "b") + end + + describe "when passed a block" do + it "yields each header entry to the passed block (capitalized keys, values joined)" do + res = [] + @headers.send(@method) do |key, value| + res << [key, value] + end + res.sort.should == [["My-Header", "test"], ["My-Other-Header", "a, b"]] + end + end + + describe "when passed no block" do + it "returns an Enumerator" do + enumerator = @headers.send(@method) + enumerator.should be_an_instance_of(Enumerator) + + res = [] + enumerator.each do |*key| + res << key + end + res.sort.should == [["My-Header", "test"], ["My-Other-Header", "a, b"]] + end + end +end diff --git a/spec/ruby/library/net/http/httpheader/shared/each_header.rb b/spec/ruby/library/net/http/httpheader/shared/each_header.rb new file mode 100644 index 0000000000..6bf3a6ddfe --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/shared/each_header.rb @@ -0,0 +1,31 @@ +describe :net_httpheader_each_header, shared: true do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + @headers["My-Header"] = "test" + @headers.add_field("My-Other-Header", "a") + @headers.add_field("My-Other-Header", "b") + end + + describe "when passed a block" do + it "yields each header entry to the passed block (keys in lower case, values joined)" do + res = [] + @headers.send(@method) do |key, value| + res << [key, value] + end + res.sort.should == [["my-header", "test"], ["my-other-header", "a, b"]] + end + end + + describe "when passed no block" do + it "returns an Enumerator" do + enumerator = @headers.send(@method) + enumerator.should be_an_instance_of(Enumerator) + + res = [] + enumerator.each do |*key| + res << key + end + res.sort.should == [["my-header", "test"], ["my-other-header", "a, b"]] + end + end +end diff --git a/spec/ruby/library/net/http/httpheader/shared/each_name.rb b/spec/ruby/library/net/http/httpheader/shared/each_name.rb new file mode 100644 index 0000000000..efc6a09dfd --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/shared/each_name.rb @@ -0,0 +1,31 @@ +describe :net_httpheader_each_name, shared: true do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + @headers["My-Header"] = "test" + @headers.add_field("My-Other-Header", "a") + @headers.add_field("My-Other-Header", "b") + end + + describe "when passed a block" do + it "yields each header key to the passed block (keys in lower case)" do + res = [] + @headers.send(@method) do |key| + res << key + end + res.sort.should == ["my-header", "my-other-header"] + end + end + + describe "when passed no block" do + it "returns an Enumerator" do + enumerator = @headers.send(@method) + enumerator.should be_an_instance_of(Enumerator) + + res = [] + enumerator.each do |key| + res << key + end + res.sort.should == ["my-header", "my-other-header"] + end + end +end diff --git a/spec/ruby/library/net/http/httpheader/shared/set_content_type.rb b/spec/ruby/library/net/http/httpheader/shared/set_content_type.rb new file mode 100644 index 0000000000..b7359bdca6 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/shared/set_content_type.rb @@ -0,0 +1,18 @@ +describe :net_httpheader_set_content_type, shared: true do + describe "when passed type, params" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "sets the 'Content-Type' header entry based on the passed type and params" do + @headers.send(@method, "text/html") + @headers["Content-Type"].should == "text/html" + + @headers.send(@method, "text/html", "charset" => "utf-8") + @headers["Content-Type"].should == "text/html; charset=utf-8" + + @headers.send(@method, "text/html", "charset" => "utf-8", "rubyspec" => "rocks") + @headers["Content-Type"].split(/; /).sort.should == %w[charset=utf-8 rubyspec=rocks text/html] + end + end +end diff --git a/spec/ruby/library/net/http/httpheader/shared/set_form_data.rb b/spec/ruby/library/net/http/httpheader/shared/set_form_data.rb new file mode 100644 index 0000000000..db20b18803 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/shared/set_form_data.rb @@ -0,0 +1,27 @@ +describe :net_httpheader_set_form_data, shared: true do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + describe "when passed params" do + it "automatically set the 'Content-Type' to 'application/x-www-form-urlencoded'" do + @headers.send(@method, "cmd" => "search", "q" => "ruby", "max" => "50") + @headers["Content-Type"].should == "application/x-www-form-urlencoded" + end + + it "sets self's body based on the passed form parameters" do + @headers.send(@method, "cmd" => "search", "q" => "ruby", "max" => "50") + @headers.body.split("&").sort.should == ["cmd=search", "max=50", "q=ruby"] + end + end + + describe "when passed params, separator" do + it "sets self's body based on the passed form parameters and the passed separator" do + @headers.send(@method, {"cmd" => "search", "q" => "ruby", "max" => "50"}, "&") + @headers.body.split("&").sort.should == ["cmd=search", "max=50", "q=ruby"] + + @headers.send(@method, {"cmd" => "search", "q" => "ruby", "max" => "50"}, ";") + @headers.body.split(";").sort.should == ["cmd=search", "max=50", "q=ruby"] + end + end +end diff --git a/spec/ruby/library/net/http/httpheader/shared/set_range.rb b/spec/ruby/library/net/http/httpheader/shared/set_range.rb new file mode 100644 index 0000000000..6a98edbe10 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/shared/set_range.rb @@ -0,0 +1,89 @@ +describe :net_httpheader_set_range, shared: true do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + describe "when passed nil" do + it "returns nil" do + @headers.send(@method, nil).should be_nil + end + + it "deletes the 'Range' header entry" do + @headers["Range"] = "bytes 0-499/1234" + @headers.send(@method, nil) + @headers["Range"].should be_nil + end + end + + describe "when passed Numeric" do + it "sets the 'Range' header entry based on the passed Numeric" do + @headers.send(@method, 10) + @headers["Range"].should == "bytes=0-9" + + @headers.send(@method, -10) + @headers["Range"].should == "bytes=-10" + + @headers.send(@method, 10.9) + @headers["Range"].should == "bytes=0-9" + end + end + + describe "when passed Range" do + it "sets the 'Range' header entry based on the passed Range" do + @headers.send(@method, 10..200) + @headers["Range"].should == "bytes=10-200" + + @headers.send(@method, 1..5) + @headers["Range"].should == "bytes=1-5" + + @headers.send(@method, 1...5) + @headers["Range"].should == "bytes=1-4" + + @headers.send(@method, 234..567) + @headers["Range"].should == "bytes=234-567" + + @headers.send(@method, -5..-1) + @headers["Range"].should == "bytes=-5" + + @headers.send(@method, 1..-1) + @headers["Range"].should == "bytes=1-" + end + + it "raises a Net::HTTPHeaderSyntaxError when the first Range element is negative" do + lambda { @headers.send(@method, -10..5) }.should raise_error(Net::HTTPHeaderSyntaxError) + end + + it "raises a Net::HTTPHeaderSyntaxError when the last Range element is negative" do + lambda { @headers.send(@method, 10..-5) }.should raise_error(Net::HTTPHeaderSyntaxError) + end + + it "raises a Net::HTTPHeaderSyntaxError when the last Range element is smaller than the first" do + lambda { @headers.send(@method, 10..5) }.should raise_error(Net::HTTPHeaderSyntaxError) + end + end + + describe "when passed start, end" do + it "sets the 'Range' header entry based on the passed start and length values" do + @headers.send(@method, 10, 200) + @headers["Range"].should == "bytes=10-209" + + @headers.send(@method, 1, 5) + @headers["Range"].should == "bytes=1-5" + + @headers.send(@method, 234, 567) + @headers["Range"].should == "bytes=234-800" + end + + it "raises a Net::HTTPHeaderSyntaxError when start is negative" do + lambda { @headers.send(@method, -10, 5) }.should raise_error(Net::HTTPHeaderSyntaxError) + end + + it "raises a Net::HTTPHeaderSyntaxError when start + length is negative" do + lambda { @headers.send(@method, 10, -15) }.should raise_error(Net::HTTPHeaderSyntaxError) + end + + it "raises a Net::HTTPHeaderSyntaxError when length is negative" do + lambda { @headers.send(@method, 10, -4) }.should raise_error(Net::HTTPHeaderSyntaxError) + end + end +end diff --git a/spec/ruby/library/net/http/httpheader/shared/size.rb b/spec/ruby/library/net/http/httpheader/shared/size.rb new file mode 100644 index 0000000000..e2b1e4c22b --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/shared/size.rb @@ -0,0 +1,18 @@ +describe :net_httpheader_size, shared: true do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns the number of header entries in self" do + @headers.send(@method).should eql(0) + + @headers["a"] = "b" + @headers.send(@method).should eql(1) + + @headers["b"] = "b" + @headers.send(@method).should eql(2) + + @headers["c"] = "c" + @headers.send(@method).should eql(3) + end +end diff --git a/spec/ruby/library/net/http/httpheader/size_spec.rb b/spec/ruby/library/net/http/httpheader/size_spec.rb new file mode 100644 index 0000000000..3ea3969046 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/size_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/size', __FILE__) + +describe "Net::HTTPHeader#size" do + it_behaves_like :net_httpheader_size, :size +end diff --git a/spec/ruby/library/net/http/httpheader/sub_type_spec.rb b/spec/ruby/library/net/http/httpheader/sub_type_spec.rb new file mode 100644 index 0000000000..65cae5a3f3 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/sub_type_spec.rb @@ -0,0 +1,32 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#sub_type" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns the 'sub-content-type', as per 'Content-Type' header entry" do + @headers["Content-Type"] = "text/html" + @headers.sub_type.should == "html" + + @headers["Content-Type"] = "application/pdf" + @headers.sub_type.should == "pdf" + + @headers["Content-Type"] = "text/html;charset=utf-8" + @headers.sub_type.should == "html" + end + + it "returns nil if no 'sub-content-type' is set" do + @headers["Content-Type"] = "text" + @headers.sub_type.should be_nil + + @headers["Content-Type"] = "text;charset=utf-8" + @headers.sub_type.should be_nil + end + + it "returns nil if the 'Content-Type' header entry does not exist" do + @headers.sub_type.should be_nil + end +end diff --git a/spec/ruby/library/net/http/httpheader/to_hash_spec.rb b/spec/ruby/library/net/http/httpheader/to_hash_spec.rb new file mode 100644 index 0000000000..4b59c78c90 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/to_hash_spec.rb @@ -0,0 +1,25 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#to_hash" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns a Hash representing all Header entries (keys in lower case, values as arrays)" do + @headers.to_hash.should == {} + + @headers["My-Header"] = "test" + @headers.to_hash.should == { "my-header" => ["test"] } + + @headers.add_field("My-Header", "another test") + @headers.to_hash.should == { "my-header" => ["test", "another test"] } + end + + it "does not allow modifying the headers from the returned hash" do + @headers.to_hash["my-header"] = ["test"] + @headers.to_hash.should == {} + @headers.key?("my-header").should be_false + end +end diff --git a/spec/ruby/library/net/http/httpheader/type_params_spec.rb b/spec/ruby/library/net/http/httpheader/type_params_spec.rb new file mode 100644 index 0000000000..f4cd54f101 --- /dev/null +++ b/spec/ruby/library/net/http/httpheader/type_params_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Net::HTTPHeader#type_params" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns additional 'Content-Type' information as a Hash" do + @headers["Content-Type"] = "text/html;charset=utf-8" + @headers.type_params.should == {"charset" => "utf-8"} + + @headers["Content-Type"] = "text/html; charset=utf-8; rubyspec=rocks" + @headers.type_params.should == {"charset" => "utf-8", "rubyspec" => "rocks"} + end + + it "returns an empty Hash when no additional 'Content-Type' information is set" do + @headers.type_params.should == {} + + @headers["Content-Type"] = "text/html" + @headers.type_params.should == {} + end +end diff --git a/spec/ruby/library/net/http/httprequest/initialize_spec.rb b/spec/ruby/library/net/http/httprequest/initialize_spec.rb new file mode 100644 index 0000000000..8abbf13c93 --- /dev/null +++ b/spec/ruby/library/net/http/httprequest/initialize_spec.rb @@ -0,0 +1,45 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +module NetHTTPRequestSpecs + class TestRequest < Net::HTTPRequest + METHOD = "TEST" + REQUEST_HAS_BODY = false + RESPONSE_HAS_BODY = true + end +end + +describe "Net::HTTPRequest#initialize" do + before :each do + @req = NetHTTPRequestSpecs::TestRequest.allocate + end + + it "uses the METHOD constants to set the request method" do + @req.send(:initialize, "/some/path") + @req.method.should == "TEST" + end + + it "uses the REQUEST_HAS_BODY to set whether the Request has a body or not" do + @req.send(:initialize, "/some/path") + @req.request_body_permitted?.should be_false + end + + it "uses the RESPONSE_HAS_BODY to set whether the Response can have a body or not" do + @req.send(:initialize, "/some/path") + @req.response_body_permitted?.should be_true + end + + describe "when passed path" do + it "sets self's path to the passed path" do + @req.send(:initialize, "/some/path") + @req.path.should == "/some/path" + end + end + + describe "when passed path, headers" do + it "uses the passed headers Hash to initialize self's header entries" do + @req.send(:initialize, "/some/path", "Content-Type" => "text/html") + @req["Content-Type"].should == "text/html" + end + end +end diff --git a/spec/ruby/library/net/http/httpresponse/body_permitted_spec.rb b/spec/ruby/library/net/http/httpresponse/body_permitted_spec.rb new file mode 100644 index 0000000000..9688b0759e --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/body_permitted_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse.body_permitted?" do + it "returns true if this response type can have a response body" do + Net::HTTPUnknownResponse.body_permitted?.should == true + Net::HTTPInformation.body_permitted?.should == false + Net::HTTPSuccess.body_permitted?.should == true + Net::HTTPRedirection.body_permitted?.should == true + Net::HTTPClientError.body_permitted?.should == true + Net::HTTPServerError.body_permitted?.should == true + end +end diff --git a/spec/ruby/library/net/http/httpresponse/body_spec.rb b/spec/ruby/library/net/http/httpresponse/body_spec.rb new file mode 100644 index 0000000000..169a6687a2 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/body_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../shared/body', __FILE__) + +describe "Net::HTTPResponse#body" do + it_behaves_like :net_httpresponse_body, :body +end diff --git a/spec/ruby/library/net/http/httpresponse/code_spec.rb b/spec/ruby/library/net/http/httpresponse/code_spec.rb new file mode 100644 index 0000000000..9430af3100 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/code_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#code" do + it "returns the result code string" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + res.code.should == "???" + + res = Net::HTTPInformation.new("1.0", "1xx", "test response") + res.code.should == "1xx" + + res = Net::HTTPSuccess.new("1.0", "2xx", "test response") + res.code.should == "2xx" + + res = Net::HTTPRedirection.new("1.0", "3xx", "test response") + res.code.should == "3xx" + + res = Net::HTTPClientError.new("1.0", "4xx", "test response") + res.code.should == "4xx" + + res = Net::HTTPServerError.new("1.0", "5xx", "test response") + res.code.should == "5xx" + end +end diff --git a/spec/ruby/library/net/http/httpresponse/code_type_spec.rb b/spec/ruby/library/net/http/httpresponse/code_type_spec.rb new file mode 100644 index 0000000000..899c2ed9bf --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/code_type_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#code_type" do + it "returns self's class" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + res.code_type.should == Net::HTTPUnknownResponse + + res = Net::HTTPInformation.new("1.0", "1xx", "test response") + res.code_type.should == Net::HTTPInformation + + res = Net::HTTPSuccess.new("1.0", "2xx", "test response") + res.code_type.should == Net::HTTPSuccess + + res = Net::HTTPRedirection.new("1.0", "3xx", "test response") + res.code_type.should == Net::HTTPRedirection + + res = Net::HTTPClientError.new("1.0", "4xx", "test response") + res.code_type.should == Net::HTTPClientError + + res = Net::HTTPServerError.new("1.0", "5xx", "test response") + res.code_type.should == Net::HTTPServerError + end +end diff --git a/spec/ruby/library/net/http/httpresponse/entity_spec.rb b/spec/ruby/library/net/http/httpresponse/entity_spec.rb new file mode 100644 index 0000000000..3b6be5eb00 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/entity_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require File.expand_path('../shared/body', __FILE__) + +describe "Net::HTTPResponse#entity" do + it_behaves_like :net_httpresponse_body, :entity +end diff --git a/spec/ruby/library/net/http/httpresponse/error_spec.rb b/spec/ruby/library/net/http/httpresponse/error_spec.rb new file mode 100644 index 0000000000..7b0f61e520 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/error_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#error!" do + it "raises self's class 'EXCEPTION_TYPE' Exception" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + lambda { res.error! }.should raise_error(Net::HTTPError) + + res = Net::HTTPInformation.new("1.0", "1xx", "test response") + lambda { res.error! }.should raise_error(Net::HTTPError) + + res = Net::HTTPSuccess.new("1.0", "2xx", "test response") + lambda { res.error! }.should raise_error(Net::HTTPError) + + res = Net::HTTPRedirection.new("1.0", "3xx", "test response") + lambda { res.error! }.should raise_error(Net::HTTPRetriableError) + + res = Net::HTTPClientError.new("1.0", "4xx", "test response") + lambda { res.error! }.should raise_error(Net::HTTPServerException) + + res = Net::HTTPServerError.new("1.0", "5xx", "test response") + lambda { res.error! }.should raise_error(Net::HTTPFatalError) + end +end diff --git a/spec/ruby/library/net/http/httpresponse/error_type_spec.rb b/spec/ruby/library/net/http/httpresponse/error_type_spec.rb new file mode 100644 index 0000000000..a6b0155998 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/error_type_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#error_type" do + it "returns self's class 'EXCEPTION_TYPE' constant" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + res.error_type.should == Net::HTTPError + + res = Net::HTTPInformation.new("1.0", "1xx", "test response") + res.error_type.should == Net::HTTPError + + res = Net::HTTPSuccess.new("1.0", "2xx", "test response") + res.error_type.should == Net::HTTPError + + res = Net::HTTPRedirection.new("1.0", "3xx", "test response") + res.error_type.should == Net::HTTPRetriableError + + res = Net::HTTPClientError.new("1.0", "4xx", "test response") + res.error_type.should == Net::HTTPServerException + + res = Net::HTTPServerError.new("1.0", "5xx", "test response") + res.error_type.should == Net::HTTPFatalError + end +end diff --git a/spec/ruby/library/net/http/httpresponse/exception_type_spec.rb b/spec/ruby/library/net/http/httpresponse/exception_type_spec.rb new file mode 100644 index 0000000000..e1a48e0c12 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/exception_type_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse.exception_type" do + it "returns self's 'EXCEPTION_TYPE' constant" do + Net::HTTPUnknownResponse.exception_type.should == Net::HTTPError + Net::HTTPInformation.exception_type.should == Net::HTTPError + Net::HTTPSuccess.exception_type.should == Net::HTTPError + Net::HTTPRedirection.exception_type.should == Net::HTTPRetriableError + Net::HTTPClientError.exception_type.should == Net::HTTPServerException + Net::HTTPServerError.exception_type.should == Net::HTTPFatalError + end +end diff --git a/spec/ruby/library/net/http/httpresponse/header_spec.rb b/spec/ruby/library/net/http/httpresponse/header_spec.rb new file mode 100644 index 0000000000..21e3dec418 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/header_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#header" do + it "returns self" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + res.response.should equal(res) + end +end diff --git a/spec/ruby/library/net/http/httpresponse/http_version_spec.rb b/spec/ruby/library/net/http/httpresponse/http_version_spec.rb new file mode 100644 index 0000000000..a1192c1cbe --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/http_version_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#http_version" do + it "returns self's http version" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + res.http_version.should == "1.0" + + res = Net::HTTPUnknownResponse.new("1.1", "???", "test response") + res.http_version.should == "1.1" + end +end diff --git a/spec/ruby/library/net/http/httpresponse/initialize_spec.rb b/spec/ruby/library/net/http/httpresponse/initialize_spec.rb new file mode 100644 index 0000000000..dcb6da3afe --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/initialize_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#initialize when passed http_version, response_code, response_message" do + it "sets self http_version, response_code and response_message to the passed values" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + res.http_version.should == "1.0" + res.code.should == "???" + res.message.should == "test response" + end +end diff --git a/spec/ruby/library/net/http/httpresponse/inspect_spec.rb b/spec/ruby/library/net/http/httpresponse/inspect_spec.rb new file mode 100644 index 0000000000..f5463afa96 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/inspect_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require "stringio" + +describe "Net::HTTPResponse#inspect" do + it "returns a String representation of self" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + res.inspect.should == "#<Net::HTTPUnknownResponse ??? test response readbody=false>" + + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + socket = Net::BufferedIO.new(StringIO.new("test body")) + res.reading_body(socket, true) {} + res.inspect.should == "#<Net::HTTPUnknownResponse ??? test response readbody=true>" + end +end diff --git a/spec/ruby/library/net/http/httpresponse/message_spec.rb b/spec/ruby/library/net/http/httpresponse/message_spec.rb new file mode 100644 index 0000000000..57a927aff0 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/message_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#message" do + it "returns self's response message" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + res.message.should == "test response" + end +end diff --git a/spec/ruby/library/net/http/httpresponse/msg_spec.rb b/spec/ruby/library/net/http/httpresponse/msg_spec.rb new file mode 100644 index 0000000000..9b19d34234 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/msg_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#msg" do + it "returns self's response message" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + res.message.should == "test response" + end +end diff --git a/spec/ruby/library/net/http/httpresponse/read_body_spec.rb b/spec/ruby/library/net/http/httpresponse/read_body_spec.rb new file mode 100644 index 0000000000..d71b4144f4 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/read_body_spec.rb @@ -0,0 +1,86 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#read_body" do + before :each do + @res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + @socket = Net::BufferedIO.new(StringIO.new("test body")) + end + + describe "when passed no arguments" do + it "returns the read body" do + @res.reading_body(@socket, true) do + @res.read_body.should == "test body" + end + end + + it "returns the previously read body if called a second time" do + @res.reading_body(@socket, true) do + @res.read_body.should equal(@res.read_body) + end + end + end + + describe "when passed a buffer" do + it "reads the body to the passed buffer" do + @res.reading_body(@socket, true) do + buffer = "" + @res.read_body(buffer) + buffer.should == "test body" + end + end + + it "returns the passed buffer" do + @res.reading_body(@socket, true) do + buffer = "" + @res.read_body(buffer).should equal(buffer) + end + end + + it "raises an IOError if called a second time" do + @res.reading_body(@socket, true) do + @res.read_body("") + lambda { @res.read_body("") }.should raise_error(IOError) + end + end + end + + describe "when passed a block" do + it "reads the body and yields it to the passed block (in chunks)" do + @res.reading_body(@socket, true) do + yielded = false + + buffer = "" + @res.read_body do |body| + yielded = true + buffer << body + end + + yielded.should be_true + buffer.should == "test body" + end + end + + it "returns the ReadAdapter" do + @res.reading_body(@socket, true) do + @res.read_body { nil }.should be_kind_of(Net::ReadAdapter) + end + end + + it "raises an IOError if called a second time" do + @res.reading_body(@socket, true) do + @res.read_body {} + lambda { @res.read_body {} }.should raise_error(IOError) + end + end + end + + describe "when passed buffer and block" do + it "rauses an ArgumentError" do + @res.reading_body(@socket, true) do + lambda { @res.read_body("") {} }.should raise_error(ArgumentError) + end + end + end +end + diff --git a/spec/ruby/library/net/http/httpresponse/read_header_spec.rb b/spec/ruby/library/net/http/httpresponse/read_header_spec.rb new file mode 100644 index 0000000000..6bbabdfe10 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/read_header_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#read_header" do + it "returns self" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + res.response.should equal(res) + end +end diff --git a/spec/ruby/library/net/http/httpresponse/read_new_spec.rb b/spec/ruby/library/net/http/httpresponse/read_new_spec.rb new file mode 100644 index 0000000000..e2fb0f1a05 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/read_new_spec.rb @@ -0,0 +1,22 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse.read_new" do + it "creates a HTTPResponse object based on the response read from the passed socket" do + socket = Net::BufferedIO.new(StringIO.new(<<EOS)) +HTTP/1.1 200 OK +Content-Type: text/html; charset=utf-8 + +test-body +EOS + response = Net::HTTPResponse.read_new(socket) + + response.should be_kind_of(Net::HTTPOK) + response.code.should == "200" + response["Content-Type"].should == "text/html; charset=utf-8" + + response.reading_body(socket, true) do + response.body.should == "test-body\n" + end + end +end diff --git a/spec/ruby/library/net/http/httpresponse/reading_body_spec.rb b/spec/ruby/library/net/http/httpresponse/reading_body_spec.rb new file mode 100644 index 0000000000..5e53e00adc --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/reading_body_spec.rb @@ -0,0 +1,58 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' +require "stringio" + +describe "Net::HTTPResponse#reading_body" do + before :each do + @res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + @socket = Net::BufferedIO.new(StringIO.new("test body")) + end + + describe "when body_allowed is true" do + it "reads and returns the response body for self from the passed socket" do + @res.reading_body(@socket, true) {}.should == "test body" + @res.body.should == "test body" + end + + it "yields the passed block before reading the body" do + yielded = false + + @res.reading_body(@socket, true) do + @res.inspect.should == "#<Net::HTTPUnknownResponse ??? test response readbody=false>" + yielded = true + end + + yielded.should be_true + end + + describe "but the response type is not allowed to have a body" do + before :each do + @res = Net::HTTPInformation.new("1.0", "???", "test response") + end + + it "returns nil" do + @res.reading_body(@socket, false) {}.should be_nil + @res.body.should be_nil + end + + it "yields the passed block" do + yielded = false + @res.reading_body(@socket, true) { yielded = true } + yielded.should be_true + end + end + end + + describe "when body_allowed is false" do + it "returns nil" do + @res.reading_body(@socket, false) {}.should be_nil + @res.body.should be_nil + end + + it "yields the passed block" do + yielded = false + @res.reading_body(@socket, true) { yielded = true } + yielded.should be_true + end + end +end diff --git a/spec/ruby/library/net/http/httpresponse/response_spec.rb b/spec/ruby/library/net/http/httpresponse/response_spec.rb new file mode 100644 index 0000000000..826320be9e --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/response_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#response" do + it "returns self" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + res.response.should equal(res) + end +end diff --git a/spec/ruby/library/net/http/httpresponse/shared/body.rb b/spec/ruby/library/net/http/httpresponse/shared/body.rb new file mode 100644 index 0000000000..91d5fe6375 --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/shared/body.rb @@ -0,0 +1,18 @@ +describe :net_httpresponse_body, shared: true do + before :each do + @res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + @socket = Net::BufferedIO.new(StringIO.new("test body")) + end + + it "returns the read body" do + @res.reading_body(@socket, true) do + @res.send(@method).should == "test body" + end + end + + it "returns the previously read body if called a second time" do + @res.reading_body(@socket, true) do + @res.send(@method).should equal(@res.send(@method)) + end + end +end diff --git a/spec/ruby/library/net/http/httpresponse/value_spec.rb b/spec/ruby/library/net/http/httpresponse/value_spec.rb new file mode 100644 index 0000000000..7e4f7863eb --- /dev/null +++ b/spec/ruby/library/net/http/httpresponse/value_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require 'net/http' + +describe "Net::HTTPResponse#value" do + it "raises an HTTP error for non 2xx HTTP Responses" do + res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + lambda { res.value }.should raise_error(Net::HTTPError) + + res = Net::HTTPInformation.new("1.0", "1xx", "test response") + lambda { res.value }.should raise_error(Net::HTTPError) + + res = Net::HTTPSuccess.new("1.0", "2xx", "test response") + lambda { res.value }.should_not raise_error(Net::HTTPError) + + res = Net::HTTPRedirection.new("1.0", "3xx", "test response") + lambda { res.value }.should raise_error(Net::HTTPRetriableError) + + res = Net::HTTPClientError.new("1.0", "4xx", "test response") + lambda { res.value }.should raise_error(Net::HTTPServerException) + + res = Net::HTTPServerError.new("1.0", "5xx", "test response") + lambda { res.value }.should raise_error(Net::HTTPFatalError) + end +end |