typhoeus 0.2.2 → 0.2.3

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.
@@ -1,3 +1,7 @@
1
+ 0.2.3
2
+ -----
3
+ * Code duplication in Typhoeus::Form led to nested URL param errors on POST only. Fixed [dbalatero]
4
+
1
5
  0.2.2
2
6
  -----
3
7
  * Fixed a problem with nested URL params encoding incorrectly [dbalatero]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -220,7 +220,7 @@ module Typhoeus
220
220
  def params
221
221
  @form.nil? ? {} : @form.params
222
222
  end
223
-
223
+
224
224
  def params=(params)
225
225
  @form = Typhoeus::Form.new(params)
226
226
 
@@ -3,45 +3,26 @@ require 'mime/types'
3
3
  module Typhoeus
4
4
  class Form
5
5
  attr_accessor :params
6
+ attr_reader :traversal
6
7
 
7
8
  def initialize(params = {})
8
9
  @params = params
9
10
  end
10
11
 
12
+ def traversal
13
+ @traversal ||= Typhoeus::Utils.traverse_params_hash(params)
14
+ end
15
+
11
16
  def process!
12
- params.each do |key, value|
13
- case value
14
- when Hash
15
- value.keys.each {|sub_key| formadd_param("#{key}[#{sub_key}]", value[sub_key].to_s)}
16
- when Array
17
- value.each {|v| formadd_param(key.to_s, v.to_s)}
18
- when File
19
- filename = File.basename(value.path)
20
- types = MIME::Types.type_for(filename)
21
- formadd_file(
22
- key.to_s,
23
- filename,
24
- types.empty? ? 'application/octet-stream' : types[0].to_s,
25
- File.expand_path(value.path)
26
- )
27
- else
28
- formadd_param(key.to_s, value.to_s)
29
- end
30
- end
17
+ # add params
18
+ traversal[:params].each { |p| formadd_param(p[0], p[1]) }
19
+
20
+ # add files
21
+ traversal[:files].each { |file_args| formadd_file(*file_args) }
31
22
  end
32
23
 
33
24
  def to_s
34
- params.keys.sort_by{|k|k.to_s}.collect do |k|
35
- value = params[k]
36
- if value.is_a? Hash
37
- value.keys.sort_by{|sk|sk.to_s}.collect {|sk| Typhoeus::Utils.escape("#{k}[#{sk}]") + "=" + Typhoeus::Utils.escape(value[sk].to_s)}
38
- elsif value.is_a? Array
39
- key = Typhoeus::Utils.escape(k.to_s)
40
- value.collect { |v| "#{key}=#{Typhoeus::Utils.escape(v.to_s)}" }.join('&')
41
- else
42
- "#{Typhoeus::Utils.escape(k.to_s)}=#{Typhoeus::Utils.escape(params[k].to_s)}"
43
- end
44
- end.flatten.join("&")
25
+ Typhoeus::Utils.traversal_to_param_string(traversal, false)
45
26
  end
46
27
  end
47
- end
28
+ end
@@ -110,23 +110,9 @@ module Typhoeus
110
110
  end
111
111
 
112
112
  def params_string
113
- traverse_hash(params).flatten.join('&')
114
- end
115
-
116
- def traverse_hash(hash, current_key = nil)
117
- hash.keys.sort { |a, b| a.to_s <=> b.to_s }.collect do |key|
118
- new_key = current_key ? "#{current_key}[#{key}]" : key
119
- if hash[key].is_a?(Hash)
120
- traverse_hash(hash[key], new_key)
121
- elsif hash[key].is_a?(Array)
122
- array_key = Typhoeus::Utils.escape("#{new_key}[]")
123
- hash[key].collect { |v| "#{array_key}=#{Typhoeus::Utils.escape(v.to_s)}" }.join('&')
124
- else
125
- "#{Typhoeus::Utils.escape(new_key)}=#{Typhoeus::Utils.escape(hash[key].to_s)}"
126
- end
127
- end
113
+ traversal = Typhoeus::Utils.traverse_params_hash(params)
114
+ Typhoeus::Utils.traversal_to_param_string(traversal)
128
115
  end
129
- private :traverse_hash
130
116
 
131
117
  def on_complete(&block)
132
118
  @on_complete = block
@@ -8,6 +8,44 @@ module Typhoeus
8
8
  end
9
9
  module_function :escape
10
10
 
11
+ # Params are NOT escaped.
12
+ def traverse_params_hash(hash, result = nil, current_key = nil)
13
+ result ||= { :files => [], :params => [] }
14
+
15
+ hash.keys.sort { |a, b| a.to_s <=> b.to_s }.collect do |key|
16
+ new_key = (current_key ? "#{current_key}[#{key}]" : key).to_s
17
+ case hash[key]
18
+ when Hash
19
+ traverse_params_hash(hash[key], result, new_key)
20
+ when Array
21
+ array_key = "#{new_key}[]"
22
+ hash[key].each do |v|
23
+ result[:params] << [array_key, v.to_s]
24
+ end
25
+ when File
26
+ filename = File.basename(hash[key].path)
27
+ types = MIME::Types.type_for(filename)
28
+ result[:files] << [
29
+ new_key,
30
+ filename,
31
+ types.empty? ? 'application/octet-stream' : types[0].to_s,
32
+ File.expand_path(hash[key].path)
33
+ ]
34
+ else
35
+ result[:params] << [new_key, hash[key].to_s]
36
+ end
37
+ end
38
+ result
39
+ end
40
+ module_function :traverse_params_hash
41
+
42
+ def traversal_to_param_string(traversal, escape = true)
43
+ traversal[:params].collect { |param|
44
+ "#{Typhoeus::Utils.escape(param[0])}=#{Typhoeus::Utils.escape(param[1])}"
45
+ }.join('&')
46
+ end
47
+ module_function :traversal_to_param_string
48
+
11
49
  # Return the bytesize of String; uses String#size under Ruby 1.8 and
12
50
  # String#bytesize under 1.9.
13
51
  if ''.respond_to?(:bytesize)
@@ -191,7 +191,7 @@ describe Typhoeus::Easy do
191
191
  :username => ['dbalatero', 'dbalatero2']
192
192
  }
193
193
 
194
- easy.url.should =~ /\?.*username=dbalatero&username=dbalatero2/
194
+ easy.url.should =~ /\?.*username%5B%5D=dbalatero&username%5B%5D=dbalatero2/
195
195
  end
196
196
  end
197
197
 
@@ -36,9 +36,9 @@ describe Typhoeus::Form do
36
36
  :name => "John Smith",
37
37
  :age => "29"
38
38
  })
39
- form.should_receive(:formadd_param).with("colors", "brown")
40
- form.should_receive(:formadd_param).with("colors", "green")
41
- form.should_receive(:formadd_param).with("colors", "white")
39
+ form.should_receive(:formadd_param).with("colors[]", "brown")
40
+ form.should_receive(:formadd_param).with("colors[]", "green")
41
+ form.should_receive(:formadd_param).with("colors[]", "white")
42
42
  form.should_receive(:formadd_param).with("name", "John Smith")
43
43
  form.should_receive(:formadd_param).with("age", "29")
44
44
  form.process!
@@ -52,13 +52,14 @@ describe Typhoeus::Form do
52
52
  form.should_receive(:formadd_file).with("file", "placeholder.txt", "text/plain", anything)
53
53
  form.process!
54
54
  end
55
+
55
56
  it "should handle more than one file" do
56
57
  form = Typhoeus::Form.new(
57
58
  :text_file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.txt"), "r"),
58
59
  :gif_file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.gif"), "r")
59
60
  )
60
- form.should_receive(:formadd_file).with("text_file", "placeholder.txt", "text/plain", anything)
61
61
  form.should_receive(:formadd_file).with("gif_file", "placeholder.gif", "image/gif", anything)
62
+ form.should_receive(:formadd_file).with("text_file", "placeholder.txt", "text/plain", anything)
62
63
  form.process!
63
64
  end
64
65
  it "should default to 'application/octet-stream' if no content type can be determined" do
@@ -100,7 +101,7 @@ describe Typhoeus::Form do
100
101
  :name => "John Smith",
101
102
  :age => "29"
102
103
  })
103
- form.to_s.should == "age=29&colors=brown&colors=green&colors=white&name=John+Smith"
104
+ form.to_s.should == "age=29&colors%5B%5D=brown&colors%5B%5D=green&colors%5B%5D=white&name=John+Smith"
104
105
  end
105
106
  end
106
- end
107
+ end
@@ -102,11 +102,11 @@ describe Typhoeus::Request do
102
102
  end
103
103
 
104
104
  it "can run a POST synchronously" do
105
- response = Typhoeus::Request.post("http://localhost:3000", :params => {:q => "hi"}, :headers => {:foo => "bar"})
105
+ response = Typhoeus::Request.post("http://localhost:3000", :params => {:q => { :a => "hi" } }, :headers => {:foo => "bar"})
106
106
  response.code.should == 200
107
107
  json = JSON.parse(response.body)
108
108
  json["REQUEST_METHOD"].should == "POST"
109
- json["rack.request.query_hash"]["q"].should == "hi"
109
+ json["rack.request.query_hash"]["q"]["a"].should == "hi"
110
110
  end
111
111
 
112
112
  it "can run a PUT synchronously" do
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{typhoeus}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Paul Dix", "David Balatero"]
12
- s.date = %q{2011-02-14}
12
+ s.date = %q{2011-02-19}
13
13
  s.description = %q{Like a modern code version of the mythical beast with 100 serpent heads, Typhoeus runs HTTP requests in parallel while cleanly encapsulating handling logic.}
14
14
  s.email = %q{dbalatero@gmail.com}
15
15
  s.extensions = ["ext/typhoeus/extconf.rb"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typhoeus
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Paul Dix
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-02-14 00:00:00 -08:00
19
+ date: 2011-02-19 00:00:00 -08:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency