typhoeus 1.1.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/lib/typhoeus/response/header.rb +9 -5
- data/lib/typhoeus/response/informations.rb +7 -3
- data/lib/typhoeus/response/status.rb +22 -1
- data/lib/typhoeus/version.rb +1 -1
- data/spec/typhoeus/response/header_spec.rb +43 -6
- data/spec/typhoeus/response/informations_spec.rb +12 -1
- data/spec/typhoeus/response/status_spec.rb +54 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df7045af2adbad5bf0b5bc883e8bc259a2f63544
|
4
|
+
data.tar.gz: 16d738d8b00c774fe54a78c32becde597ad69569
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5f8427b91d383cd94bc48c4f944b3bc7dbce619e0eccda0f84d428eb575ff9f7b65530f18789cb1aa99f02e4f03293af9987cffb12686542af13b9fb49bba51
|
7
|
+
data.tar.gz: e01422dc678ff40ac31b695bc1dea8d0c3b843e0ecd2ad61fa386d9b290a0950db93e37289215aedaee3aad60a7cc2acd48efc4baf59ae4fc96f3198d5df460a
|
data/.travis.yml
CHANGED
@@ -6,7 +6,7 @@ module Typhoeus
|
|
6
6
|
# Values can be strings (normal case) or arrays of strings (for duplicates headers)
|
7
7
|
#
|
8
8
|
# @api private
|
9
|
-
class Header < Hash
|
9
|
+
class Header < DelegateClass(Hash)
|
10
10
|
|
11
11
|
# Create a new header.
|
12
12
|
#
|
@@ -15,10 +15,14 @@ module Typhoeus
|
|
15
15
|
#
|
16
16
|
# @param [ String ] raw The raw header.
|
17
17
|
def initialize(raw)
|
18
|
+
super({})
|
18
19
|
@raw = raw
|
19
20
|
@sanitized = {}
|
20
21
|
parse
|
21
|
-
|
22
|
+
end
|
23
|
+
|
24
|
+
def [](key)
|
25
|
+
fetch(key) { @sanitized[key.to_s.downcase] }
|
22
26
|
end
|
23
27
|
|
24
28
|
# Parses the raw header.
|
@@ -32,9 +36,9 @@ module Typhoeus
|
|
32
36
|
process_pair(k, v)
|
33
37
|
end
|
34
38
|
when String
|
35
|
-
raw.
|
39
|
+
raw.split(/\r?\n(?!\s)/).each do |header|
|
36
40
|
header.strip!
|
37
|
-
next if header.empty? || header.start_with?( 'HTTP/
|
41
|
+
next if header.empty? || header.start_with?( 'HTTP/' )
|
38
42
|
process_line(header)
|
39
43
|
end
|
40
44
|
end
|
@@ -47,7 +51,7 @@ module Typhoeus
|
|
47
51
|
# @return [ void ]
|
48
52
|
def process_line(header)
|
49
53
|
key, value = header.split(':', 2)
|
50
|
-
process_pair(key.strip, value.strip)
|
54
|
+
process_pair(key.strip, (value ? value.strip.gsub(/\r?\n\s*/, ' ') : ''))
|
51
55
|
end
|
52
56
|
|
53
57
|
# Sets key value pair for self and @sanitized.
|
@@ -47,9 +47,13 @@ module Typhoeus
|
|
47
47
|
def response_headers
|
48
48
|
return options[:response_headers] if options[:response_headers]
|
49
49
|
if mock? && h = options[:headers]
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
status_code = return_code || "200"
|
51
|
+
reason_phrase = status_code == "200" ? "OK" : "Mock Reason Phrase"
|
52
|
+
status_line = "HTTP/1.1 #{status_code} #{reason_phrase}"
|
53
|
+
actual_headers = h.map{ |k,v| [k, v.respond_to?(:join) ? v.join(',') : v] }.
|
54
|
+
map{ |e| "#{e.first}: #{e.last}" }
|
55
|
+
|
56
|
+
[status_line, *actual_headers].join("\r\n")
|
53
57
|
end
|
54
58
|
end
|
55
59
|
|
@@ -46,7 +46,18 @@ module Typhoeus
|
|
46
46
|
#
|
47
47
|
# @return [ Boolean ] Return true if successful, false else.
|
48
48
|
def success?
|
49
|
-
(mock || return_code == :ok) && response_code &&
|
49
|
+
(mock || return_code == :ok) && response_code && has_good_response_code?
|
50
|
+
end
|
51
|
+
|
52
|
+
# Return wether the response is a failure.
|
53
|
+
#
|
54
|
+
# @example Return if the response was failed.
|
55
|
+
# response.failure?
|
56
|
+
#
|
57
|
+
# @return [ Boolean ] Return true if failure, false else.
|
58
|
+
|
59
|
+
def failure?
|
60
|
+
(mock || return_code == :internal_server_error) && response_code && has_bad_response_code?
|
50
61
|
end
|
51
62
|
|
52
63
|
# Return wether the response is modified.
|
@@ -81,6 +92,16 @@ module Typhoeus
|
|
81
92
|
end
|
82
93
|
end
|
83
94
|
end
|
95
|
+
|
96
|
+
# :nodoc:
|
97
|
+
def has_good_response_code?
|
98
|
+
response_code >= 200 && response_code < 300
|
99
|
+
end
|
100
|
+
|
101
|
+
# :nodoc:
|
102
|
+
def has_bad_response_code?
|
103
|
+
!has_good_response_code?
|
104
|
+
end
|
84
105
|
end
|
85
106
|
end
|
86
107
|
end
|
data/lib/typhoeus/version.rb
CHANGED
@@ -55,7 +55,7 @@ describe Typhoeus::Response::Header do
|
|
55
55
|
Server: gws
|
56
56
|
X-XSS-Protection: 1; mode=block
|
57
57
|
X-Frame-Options: SAMEORIGIN
|
58
|
-
Transfer-Encoding: chunked'
|
58
|
+
Transfer-Encoding: chunked'.gsub(/^\s{8}/, '')
|
59
59
|
end
|
60
60
|
|
61
61
|
it "sets raw" do
|
@@ -93,18 +93,55 @@ describe Typhoeus::Response::Header do
|
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
96
|
-
context 'includes line
|
97
|
-
|
98
|
-
|
99
|
-
|
96
|
+
context 'includes a multi-line header' do
|
97
|
+
let(:raw) do
|
98
|
+
'HTTP/1.1 200 OK
|
99
|
+
Date: Fri, 29 Jun 2012 10:09:23 GMT
|
100
|
+
Content-Security-Policy: default-src "self";
|
101
|
+
img-src * data: "self";
|
102
|
+
upgrade-insecure-requests;'.gsub(/^\s{10}/, '')
|
103
|
+
end
|
100
104
|
|
101
|
-
|
105
|
+
it "joins header parts" do
|
106
|
+
expect(header).to eq({
|
107
|
+
'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT',
|
108
|
+
'Content-Security-Policy' => 'default-src "self"; img-src * data: "self"; upgrade-insecure-requests;'
|
109
|
+
})
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'includes line with only whitespace' do
|
114
|
+
let(:raw) do
|
115
|
+
'HTTP/1.1 200 OK
|
116
|
+
Date: Fri, 29 Jun 2012 10:09:23 GMT
|
117
|
+
|
118
|
+
'.gsub(/^\s{10}/, '')
|
102
119
|
end
|
103
120
|
|
104
121
|
it 'ignores it' do
|
105
122
|
expect(header).to eq({ 'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT' })
|
106
123
|
end
|
107
124
|
end
|
125
|
+
|
126
|
+
context 'with broken headers' do
|
127
|
+
let(:raw) do
|
128
|
+
'HTTP/1.1 200 OK
|
129
|
+
Date:
|
130
|
+
Content-Type
|
131
|
+
'.gsub(/^\s{10}/, '')
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'returns empty string for invalid headers' do
|
135
|
+
expect(header.to_hash).to include({ 'Date' => '', 'Content-Type' => '' })
|
136
|
+
end
|
137
|
+
end
|
108
138
|
end
|
109
139
|
end
|
140
|
+
|
141
|
+
it "can be Marshal'd" do
|
142
|
+
header = Typhoeus::Response::Header.new("Foo: Bar")
|
143
|
+
expect {
|
144
|
+
Marshal.dump(header)
|
145
|
+
}.not_to raise_error
|
146
|
+
end
|
110
147
|
end
|
@@ -74,6 +74,17 @@ describe Typhoeus::Response::Informations do
|
|
74
74
|
expect(response.response_headers).to include("\r\n")
|
75
75
|
end
|
76
76
|
end
|
77
|
+
|
78
|
+
context "when multiple values for a header" do
|
79
|
+
let(:options) { { :mock => true, :headers => {"Length" => 1, "Content-Type" => "text/plain", "set-cookie" => ["cookieone=one","cookietwo=two"] } } }
|
80
|
+
|
81
|
+
it "constructs response_headers" do
|
82
|
+
expect(response.response_headers).to include("Length: 1")
|
83
|
+
expect(response.response_headers).to include("Content-Type: text/plain")
|
84
|
+
expect(response.response_headers).to include("set-cookie: cookieone=one,cookietwo=two")
|
85
|
+
expect(response.response_headers).to include("\r\n")
|
86
|
+
end
|
87
|
+
end
|
77
88
|
end
|
78
89
|
end
|
79
90
|
end
|
@@ -232,7 +243,7 @@ describe Typhoeus::Response::Informations do
|
|
232
243
|
end
|
233
244
|
|
234
245
|
it "returns headers" do
|
235
|
-
expect(response.headers).to include("Length" => "1")
|
246
|
+
expect(response.headers.to_hash).to include("Length" => "1")
|
236
247
|
end
|
237
248
|
end
|
238
249
|
end
|
@@ -128,6 +128,60 @@ describe Typhoeus::Response::Status do
|
|
128
128
|
end
|
129
129
|
end
|
130
130
|
|
131
|
+
describe "#failure?" do
|
132
|
+
context "when response code between 300-526 and 100-300" do
|
133
|
+
let(:options) { {:return_code => return_code, :response_code => 300} }
|
134
|
+
|
135
|
+
context "when mock" do
|
136
|
+
before { response.mock = true }
|
137
|
+
|
138
|
+
context "when return_code :internal_server_error" do
|
139
|
+
let(:return_code) { :internal_server_error }
|
140
|
+
|
141
|
+
it "returns true" do
|
142
|
+
expect(response.failure?).to be_truthy
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context "when return_code nil" do
|
147
|
+
let(:return_code) { nil }
|
148
|
+
|
149
|
+
it "returns true" do
|
150
|
+
expect(response.failure?).to be_truthy
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context "when no mock" do
|
156
|
+
before { response.mock = nil }
|
157
|
+
|
158
|
+
context "when return_code :internal_server_error" do
|
159
|
+
let(:return_code) { :internal_server_error }
|
160
|
+
|
161
|
+
it "returns true" do
|
162
|
+
expect(response.failure?).to be_truthy
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
context "when return_code nil" do
|
167
|
+
let(:return_code) { nil }
|
168
|
+
|
169
|
+
it "returns false" do
|
170
|
+
expect(response.failure?).to be_falsey
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
context "when response code is not 300-526" do
|
177
|
+
let(:options) { {:return_code => :ok, :response_code => 200} }
|
178
|
+
|
179
|
+
it "returns false" do
|
180
|
+
expect(response.failure?).to be_falsey
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
131
185
|
describe "#modified?" do
|
132
186
|
context "when response code 304" do
|
133
187
|
let(:options) { {:return_code => :ok, :response_code => 304} }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typhoeus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Balatero
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2017-08-21 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: ethon
|