diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/library/net/http/httpgenericrequest | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) |
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
[Misc #13792] [ruby-core:82287]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/library/net/http/httpgenericrequest')
10 files changed, 312 insertions, 0 deletions
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 |