diff options
author | Manas kashyap <manaskashyaptech@gmail.com> | 2018-05-13 14:40:51 +0000 |
---|---|---|
committer | Manas kashyap <manaskashyaptech@gmail.com> | 2018-05-13 14:40:51 +0000 |
commit | 7e24d6c1c9224d6d800da2f3b85ff890cbcd51fa (patch) | |
tree | 9be1ca3b948f330798037f1745304ac896dfbc6d | |
parent | b97d20e3ddbaa118b25a983a6213e2796bfd0e0e (diff) | |
parent | ca149347003ae3c06d31f7d2dfedbcea17d8292b (diff) |
Update upstream source from tag 'upstream/1.3.0'
Update to upstream version '1.3.0'
with Debian dir 5552a648d434013bdd03edd194451c423e1aa708
-rw-r--r-- | .travis.yml | 1 | ||||
-rw-r--r-- | lib/typhoeus/response/header.rb | 14 | ||||
-rw-r--r-- | lib/typhoeus/response/informations.rb | 10 | ||||
-rw-r--r-- | lib/typhoeus/response/status.rb | 23 | ||||
-rw-r--r-- | lib/typhoeus/version.rb | 2 | ||||
-rw-r--r-- | spec/typhoeus/response/header_spec.rb | 49 | ||||
-rw-r--r-- | spec/typhoeus/response/informations_spec.rb | 13 | ||||
-rw-r--r-- | spec/typhoeus/response/status_spec.rb | 54 |
8 files changed, 149 insertions, 17 deletions
diff --git a/.travis.yml b/.travis.yml index c4a213b..8121e3c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ rvm: - 2.1.8 - 2.2.4 - 2.3.0 + - 2.4.1 - ruby-head - jruby-head - jruby-18mode diff --git a/lib/typhoeus/response/header.rb b/lib/typhoeus/response/header.rb index 8ec96d1..f12a742 100644 --- a/lib/typhoeus/response/header.rb +++ b/lib/typhoeus/response/header.rb @@ -6,7 +6,7 @@ module Typhoeus # Values can be strings (normal case) or arrays of strings (for duplicates headers) # # @api private - class Header < Hash + class Header < DelegateClass(Hash) # Create a new header. # @@ -15,10 +15,14 @@ module Typhoeus # # @param [ String ] raw The raw header. def initialize(raw) + super({}) @raw = raw @sanitized = {} parse - set_default_proc_on(self, lambda { |h, k| @sanitized[k.to_s.downcase] }) + end + + def [](key) + fetch(key) { @sanitized[key.to_s.downcase] } end # Parses the raw header. @@ -32,9 +36,9 @@ module Typhoeus process_pair(k, v) end when String - raw.lines.each do |header| + raw.split(/\r?\n(?!\s)/).each do |header| header.strip! - next if header.empty? || header.start_with?( 'HTTP/1.' ) + next if header.empty? || header.start_with?( 'HTTP/' ) process_line(header) end end @@ -47,7 +51,7 @@ module Typhoeus # @return [ void ] def process_line(header) key, value = header.split(':', 2) - process_pair(key.strip, value.strip) + process_pair(key.strip, (value ? value.strip.gsub(/\r?\n\s*/, ' ') : '')) end # Sets key value pair for self and @sanitized. diff --git a/lib/typhoeus/response/informations.rb b/lib/typhoeus/response/informations.rb index eaf4edb..73544dd 100644 --- a/lib/typhoeus/response/informations.rb +++ b/lib/typhoeus/response/informations.rb @@ -47,9 +47,13 @@ module Typhoeus def response_headers return options[:response_headers] if options[:response_headers] if mock? && h = options[:headers] - h.map{ |k,v| [k, v.respond_to?(:join) ? v.join : v] }. - map{ |e| "#{e.first}: #{e.last}" }. - join("\r\n") + status_code = return_code || "200" + reason_phrase = status_code == "200" ? "OK" : "Mock Reason Phrase" + status_line = "HTTP/1.1 #{status_code} #{reason_phrase}" + actual_headers = h.map{ |k,v| [k, v.respond_to?(:join) ? v.join(',') : v] }. + map{ |e| "#{e.first}: #{e.last}" } + + [status_line, *actual_headers].join("\r\n") end end diff --git a/lib/typhoeus/response/status.rb b/lib/typhoeus/response/status.rb index 2971f8b..1b04535 100644 --- a/lib/typhoeus/response/status.rb +++ b/lib/typhoeus/response/status.rb @@ -46,7 +46,18 @@ module Typhoeus # # @return [ Boolean ] Return true if successful, false else. def success? - (mock || return_code == :ok) && response_code && response_code >= 200 && response_code < 300 + (mock || return_code == :ok) && response_code && has_good_response_code? + end + + # Return wether the response is a failure. + # + # @example Return if the response was failed. + # response.failure? + # + # @return [ Boolean ] Return true if failure, false else. + + def failure? + (mock || return_code == :internal_server_error) && response_code && has_bad_response_code? end # Return wether the response is modified. @@ -81,6 +92,16 @@ module Typhoeus end end end + + # :nodoc: + def has_good_response_code? + response_code >= 200 && response_code < 300 + end + + # :nodoc: + def has_bad_response_code? + !has_good_response_code? + end end end end diff --git a/lib/typhoeus/version.rb b/lib/typhoeus/version.rb index 5d5d03f..1695847 100644 --- a/lib/typhoeus/version.rb +++ b/lib/typhoeus/version.rb @@ -1,5 +1,5 @@ module Typhoeus # The current Typhoeus version. - VERSION = '1.1.2' + VERSION = '1.3.0' end diff --git a/spec/typhoeus/response/header_spec.rb b/spec/typhoeus/response/header_spec.rb index 8dd4d5a..922d99e 100644 --- a/spec/typhoeus/response/header_spec.rb +++ b/spec/typhoeus/response/header_spec.rb @@ -55,7 +55,7 @@ describe Typhoeus::Response::Header do Server: gws X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN - Transfer-Encoding: chunked' + Transfer-Encoding: chunked'.gsub(/^\s{8}/, '') end it "sets raw" do @@ -93,18 +93,55 @@ describe Typhoeus::Response::Header do end end - context 'includes line with only whitespace' do - let(:raw) do - 'HTTP/1.1 200 OK - Date: Fri, 29 Jun 2012 10:09:23 GMT + context 'includes a multi-line header' do + let(:raw) do + 'HTTP/1.1 200 OK + Date: Fri, 29 Jun 2012 10:09:23 GMT + Content-Security-Policy: default-src "self"; + img-src * data: "self"; + upgrade-insecure-requests;'.gsub(/^\s{10}/, '') + end -' + it "joins header parts" do + expect(header).to eq({ + 'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT', + 'Content-Security-Policy' => 'default-src "self"; img-src * data: "self"; upgrade-insecure-requests;' + }) + end + end + + context 'includes line with only whitespace' do + let(:raw) do + 'HTTP/1.1 200 OK + Date: Fri, 29 Jun 2012 10:09:23 GMT + + '.gsub(/^\s{10}/, '') end it 'ignores it' do expect(header).to eq({ 'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT' }) end end + + context 'with broken headers' do + let(:raw) do + 'HTTP/1.1 200 OK + Date: + Content-Type + '.gsub(/^\s{10}/, '') + end + + it 'returns empty string for invalid headers' do + expect(header.to_hash).to include({ 'Date' => '', 'Content-Type' => '' }) + end + end end end + + it "can be Marshal'd" do + header = Typhoeus::Response::Header.new("Foo: Bar") + expect { + Marshal.dump(header) + }.not_to raise_error + end end diff --git a/spec/typhoeus/response/informations_spec.rb b/spec/typhoeus/response/informations_spec.rb index 0382ec7..896931a 100644 --- a/spec/typhoeus/response/informations_spec.rb +++ b/spec/typhoeus/response/informations_spec.rb @@ -74,6 +74,17 @@ describe Typhoeus::Response::Informations do expect(response.response_headers).to include("\r\n") end end + + context "when multiple values for a header" do + let(:options) { { :mock => true, :headers => {"Length" => 1, "Content-Type" => "text/plain", "set-cookie" => ["cookieone=one","cookietwo=two"] } } } + + it "constructs response_headers" do + expect(response.response_headers).to include("Length: 1") + expect(response.response_headers).to include("Content-Type: text/plain") + expect(response.response_headers).to include("set-cookie: cookieone=one,cookietwo=two") + expect(response.response_headers).to include("\r\n") + end + end end end end @@ -232,7 +243,7 @@ describe Typhoeus::Response::Informations do end it "returns headers" do - expect(response.headers).to include("Length" => "1") + expect(response.headers.to_hash).to include("Length" => "1") end end end diff --git a/spec/typhoeus/response/status_spec.rb b/spec/typhoeus/response/status_spec.rb index 7a1843c..64ba1e0 100644 --- a/spec/typhoeus/response/status_spec.rb +++ b/spec/typhoeus/response/status_spec.rb @@ -128,6 +128,60 @@ describe Typhoeus::Response::Status do end end + describe "#failure?" do + context "when response code between 300-526 and 100-300" do + let(:options) { {:return_code => return_code, :response_code => 300} } + + context "when mock" do + before { response.mock = true } + + context "when return_code :internal_server_error" do + let(:return_code) { :internal_server_error } + + it "returns true" do + expect(response.failure?).to be_truthy + end + end + + context "when return_code nil" do + let(:return_code) { nil } + + it "returns true" do + expect(response.failure?).to be_truthy + end + end + end + + context "when no mock" do + before { response.mock = nil } + + context "when return_code :internal_server_error" do + let(:return_code) { :internal_server_error } + + it "returns true" do + expect(response.failure?).to be_truthy + end + end + + context "when return_code nil" do + let(:return_code) { nil } + + it "returns false" do + expect(response.failure?).to be_falsey + end + end + end + end + + context "when response code is not 300-526" do + let(:options) { {:return_code => :ok, :response_code => 200} } + + it "returns false" do + expect(response.failure?).to be_falsey + end + end + end + describe "#modified?" do context "when response code 304" do let(:options) { {:return_code => :ok, :response_code => 304} } |