diff options
Diffstat (limited to 'spec/ruby/library/net/http/httpresponse')
21 files changed, 427 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..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 |