jeff 1.2.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3867d229eb05451a032899de8e28246d02498e6d
4
- data.tar.gz: 3d03ba5a26edb20d7528c323e2526ff11293e0cd
3
+ metadata.gz: 26d4ecc099e2a707deb20d81eb0e99c146f4e4a4
4
+ data.tar.gz: c3c7d33f9cad57ec5bcfe55dacb7cc44f1175bfb
5
5
  SHA512:
6
- metadata.gz: e513eb4d6bbbf3653362fcaf1d5afc458ee7281509711a95d8d379af208b21686a6dc3aed1e62cfb81e3562a32d59f24dc7ed1b251d37427d4ae2390f12c785e
7
- data.tar.gz: 7b3f7c9ad65ecea40f25d666099929e85e5c2476c3cd078412e9df44c1335f75915af0a845d8b31e9a054d9a7fd49cb2211581094aaee9638df9f2051e4c360e
6
+ metadata.gz: dd29ac1f134dd1a98c841e63e7c1f17e1a8a82282abde2ccc0e694daf1dc347d853d477e87a929f2f4982e49acaaea88cf41c59b22f3f3bef30389cb6c9b5281
7
+ data.tar.gz: bbab285296bf3ddee692e21337ea04540af2104c9bfe6eb98d0b6939fdc20e0f93a19002845355b89f4b91031d2a1f8d3d7c2aefeb11a8868a968c31ea021cf4
@@ -1,3 +1,7 @@
1
+ # 1.3.0 15/4/2015
2
+
3
+ - Handle overly long request queries
4
+
1
5
  # 1.2.0 23/1/2015
2
6
 
3
7
  - Discard parameters with nil values
@@ -20,7 +20,9 @@ module Jeff
20
20
  end
21
21
 
22
22
  def to_s
23
- values.sort.map { |k, v| "#{k}=#{ Utils.escape(v) }" }.join("&")
23
+ values
24
+ .sort { |a, b| a[0].to_s <=> b[0].to_s }
25
+ .map { |k, v| "#{k}=#{ Utils.escape(v) }" }.join("&")
24
26
  end
25
27
  end
26
28
 
@@ -126,20 +128,29 @@ module Jeff
126
128
  eval <<-DEF
127
129
  def #{method}(options = {})
128
130
  options.store(:method, :#{method})
129
- connection.request(build_options(options))
131
+ add_md5_digest options
132
+ sign options
133
+
134
+ begin
135
+ connection.request(options)
136
+ rescue Excon::Errors::RequestURITooLong
137
+ raise if options[:body] || options[:method] != :post
138
+ move_query_to_body options
139
+ retry
140
+ end
130
141
  end
131
142
  DEF
132
143
  end
133
144
 
134
145
  private
135
146
 
136
- def build_options(options)
137
- # Add Content-MD5 header if uploading a file.
138
- if options.has_key?(:body)
139
- md5 = Content.new(options[:body]).md5
140
- (options[:headers] ||= {}).store("Content-MD5", md5)
141
- end
147
+ def add_md5_digest(options)
148
+ return unless options.has_key?(:body)
149
+ md5 = Content.new(options[:body]).md5
150
+ (options[:headers] ||= {}).store("Content-MD5", md5)
151
+ end
142
152
 
153
+ def sign(options)
143
154
  # Build query string.
144
155
  query_values = default_query_values.merge(options.fetch(:query, {}))
145
156
  query_string = Query.new(query_values).to_s
@@ -149,8 +160,13 @@ module Jeff
149
160
  .new(options[:method], connection.data[:host], options[:path] || connection.data[:path], query_string)
150
161
  .sign_with(aws_secret_access_key)
151
162
 
152
- # Return options after appending an escaped signature to query.
153
- options.merge(query: "#{query_string}&Signature=#{Utils.escape(signature)}")
163
+ # Append escaped signature to query.
164
+ options.store(:query, "#{query_string}&Signature=#{Utils.escape(signature)}")
165
+ end
166
+
167
+ def move_query_to_body(options)
168
+ (options[:headers] ||= {}).store("Content-Type", "application/x-www-form-urlencoded")
169
+ options.store(:body, options.delete(:query))
154
170
  end
155
171
 
156
172
  def default_query_values
@@ -1,3 +1,3 @@
1
1
  module Jeff
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -54,6 +54,11 @@ class TestJeff < Minitest::Test
54
54
  assert_equal "A1=1&A10=1", query.to_s
55
55
  end
56
56
 
57
+ def test_handles_symbol_keys
58
+ query = Jeff::Query.new(foo: 1, bar: 2)
59
+ assert_equal "bar=2&foo=1", query.to_s
60
+ end
61
+
57
62
  def test_sets_user_agent_header
58
63
  client = @klass.new
59
64
  client.aws_endpoint = "http://example.com/"
@@ -70,37 +75,55 @@ class TestJeff < Minitest::Test
70
75
  def test_does_not_escape_tilde
71
76
  assert_equal "~%2C", Jeff::Utils.escape("~,")
72
77
  end
78
+ end
79
+
80
+ class TestJeffInAction < Minitest::Test
81
+ def setup
82
+ klass = Class.new { include Jeff }
83
+ @client = klass.new
84
+ @client.aws_endpoint = "http://example.com/"
85
+ @client.aws_access_key_id = "foo"
86
+ @client.aws_secret_access_key = "bar"
87
+ end
88
+
89
+ def teardown
90
+ Excon.stubs.clear
91
+ end
73
92
 
74
93
  Excon::HTTP_VERBS.each do |method|
75
94
  define_method "test_makes_#{method}_request" do
76
- Excon.stub({ }, { status: 200 })
77
- client = @klass.new
78
- client.aws_endpoint = "http://example.com/"
79
- client.aws_access_key_id = "foo"
80
- client.aws_secret_access_key = "bar"
81
- assert_equal 200, client.send(method, mock: true).status
82
- Excon.stubs.clear
95
+ Excon.stub({}, { status: 200 })
96
+ assert_equal 200, @client.send(method, mock: true).status
83
97
  end
84
98
  end
85
99
 
86
100
  def test_adds_content_md5_request_header_if_given_a_request_body
87
- Excon.stub({ }) do |params|
88
- { body: params[:headers]["Content-MD5"] }
101
+ Excon.stub({}) do |request_params|
102
+ { body: request_params[:headers]["Content-MD5"] }
89
103
  end
90
- client = @klass.new
91
- client.aws_endpoint = "http://example.com/"
92
- client.aws_access_key_id = "foo"
93
- client.aws_secret_access_key = "bar"
94
- refute_empty client.post(body: "foo", mock: true).body
95
- Excon.stubs.clear
104
+ refute_empty @client.post(body: "foo", mock: true).body
96
105
  end
97
106
 
98
- def test_integration
99
- client = @klass.new
100
- client.aws_access_key_id = "foo"
101
- client.aws_secret_access_key = "bar"
102
- client.aws_endpoint = "https://mws.amazonservices.com/Sellers/2011-07-01"
103
- res = client.post(query: { "Action" => "GetServiceStatus"})
107
+ def test_moves_query_to_body_if_uri_is_too_long
108
+ Excon.stub({}) do |request_params|
109
+ { status: request_params[:query] ? 414 : 200 }
110
+ end
111
+
112
+ res = @client.post(query: { foo: "bar" }, mock: true)
113
+ assert_equal 200, res.status
114
+
115
+ assert_raises(Excon::Errors::RequestURITooLong) do
116
+ @client.post(query: { foo: "bar" }, body: "baz", mock: true)
117
+ end
118
+
119
+ assert_raises(Excon::Errors::RequestURITooLong) do
120
+ @client.get(query: { foo: "bar" }, mock: true)
121
+ end
122
+ end
123
+
124
+ def test_gets_from_an_actual_endpoint
125
+ @client.aws_endpoint = "https://mws.amazonservices.com/Sellers/2011-07-01"
126
+ res = @client.post(query: { "Action" => "GetServiceStatus"})
104
127
  assert_equal 200, res.status
105
128
  end
106
129
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jeff
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hakan Ensari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-23 00:00:00.000000000 Z
11
+ date: 2015-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon