diff options
Diffstat (limited to 'spec/ruby/library/net-http/httpresponse')
21 files changed, 430 insertions, 0 deletions
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..68965de4a1 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/body_permitted_spec.rb @@ -0,0 +1,13 @@ +require_relative '../../../spec_helper' +require 'net/http' + +describe "Net::HTTPResponse.body_permitted?" do + it "returns true if this response type can have a response body" do + Net::HTTPUnknownResponse.should.body_permitted? + Net::HTTPInformation.should_not.body_permitted? + Net::HTTPSuccess.should.body_permitted? + Net::HTTPRedirection.should.body_permitted? + Net::HTTPClientError.should.body_permitted? + Net::HTTPServerError.should.body_permitted? + 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..ddfcd834c4 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/body_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../../spec_helper' +require 'net/http' +require_relative 'shared/body' + +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..699062ad97 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/code_spec.rb @@ -0,0 +1,24 @@ +require_relative '../../../spec_helper' +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..beb661cbbe --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/code_type_spec.rb @@ -0,0 +1,24 @@ +require_relative '../../../spec_helper' +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..ca8c4b29c0 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/entity_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../../spec_helper' +require 'net/http' +require_relative 'shared/body' + +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..6ced90fa23 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/error_spec.rb @@ -0,0 +1,24 @@ +require_relative '../../../spec_helper' +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") + -> { res.error! }.should raise_error(Net::HTTPError) + + res = Net::HTTPInformation.new("1.0", "1xx", "test response") + -> { res.error! }.should raise_error(Net::HTTPError) + + res = Net::HTTPSuccess.new("1.0", "2xx", "test response") + -> { res.error! }.should raise_error(Net::HTTPError) + + res = Net::HTTPRedirection.new("1.0", "3xx", "test response") + -> { res.error! }.should raise_error(Net::HTTPRetriableError) + + res = Net::HTTPClientError.new("1.0", "4xx", "test response") + -> { res.error! }.should raise_error(Net::HTTPClientException) + + res = Net::HTTPServerError.new("1.0", "5xx", "test response") + -> { 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..3969621a5e --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/error_type_spec.rb @@ -0,0 +1,24 @@ +require_relative '../../../spec_helper' +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::HTTPClientException + + 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..dd2761a744 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/exception_type_spec.rb @@ -0,0 +1,13 @@ +require_relative '../../../spec_helper' +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::HTTPClientException + 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..a403dbd2c3 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/header_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' +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..a3e413a360 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/http_version_spec.rb @@ -0,0 +1,12 @@ +require_relative '../../../spec_helper' +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..673c11a245 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/initialize_spec.rb @@ -0,0 +1,11 @@ +require_relative '../../../spec_helper' +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..43071ec8cd --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/inspect_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../../spec_helper' +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..5ba73bb449 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/message_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' +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..04f5836d7a --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/msg_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' +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..4530a26bfc --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/read_body_spec.rb @@ -0,0 +1,86 @@ +require_relative '../../../spec_helper' +require 'net/http' +require 'stringio' + +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(+"") + -> { @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 {} + -> { @res.read_body {} }.should raise_error(IOError) + end + end + end + + describe "when passed buffer and block" do + it "raises an ArgumentError" do + @res.reading_body(@socket, true) do + -> { @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..3ea4ee834b --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/read_header_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' +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..82f7a47ce8 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/read_new_spec.rb @@ -0,0 +1,23 @@ +require_relative '../../../spec_helper' +require 'net/http' +require 'stringio' + +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..637a2806f8 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/reading_body_spec.rb @@ -0,0 +1,58 @@ +require_relative '../../../spec_helper' +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..caa0ca2d19 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/response_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' +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..618e3936fb --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/shared/body.rb @@ -0,0 +1,20 @@ +require 'stringio' + +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..2df8beaa10 --- /dev/null +++ b/spec/ruby/library/net-http/httpresponse/value_spec.rb @@ -0,0 +1,24 @@ +require_relative '../../../spec_helper' +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") + -> { res.value }.should raise_error(Net::HTTPError) + + res = Net::HTTPInformation.new("1.0", "1xx", "test response") + -> { res.value }.should raise_error(Net::HTTPError) + + res = Net::HTTPSuccess.new("1.0", "2xx", "test response") + -> { res.value }.should_not raise_error(Net::HTTPError) + + res = Net::HTTPRedirection.new("1.0", "3xx", "test response") + -> { res.value }.should raise_error(Net::HTTPRetriableError) + + res = Net::HTTPClientError.new("1.0", "4xx", "test response") + -> { res.value }.should raise_error(Net::HTTPClientException) + + res = Net::HTTPServerError.new("1.0", "5xx", "test response") + -> { res.value }.should raise_error(Net::HTTPFatalError) + end +end |