http-client 0.1.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d9e9eae81102b0be33c27dc85929a220ab8bfee5
4
- data.tar.gz: 4a2f85ca306800e17f5ee29a459ff2747a70eacf
3
+ metadata.gz: 463b25c7e2306882b064f06c3b9d6a0b79b0c17c
4
+ data.tar.gz: a699f8db865ec291272e4405c2ceebb291253845
5
5
  SHA512:
6
- metadata.gz: 770fd61b83f170d613ca39ee167fd652adbb88654e01b13f34fda61e68f93a038d44f8cd719e6327bbe6ba0ecd5cd4c6af94df90c65b5899ba332d3779ba548c
7
- data.tar.gz: 2d96e1124de41b12e14ce29f6de8d9e78029a5b6ed70fb19d25781cc196f040e23a5c477363f01da49f448d624ae120a3825ff104f85b54bf7faa722952119bf
6
+ metadata.gz: f54e15e22e2cc94f29efd2c5a89143a6a9522b0c9afc2c6c9b6e82cbab0a9ba8f7f903033380e26a2c80f0a9da05737d9848630d65a7bdaad2c425e173467f33
7
+ data.tar.gz: f09b9d4be97f9b8c7f3f180fc8555982c286360c4db3f5be35042f2ccc7962b5dc9ddef08c8c9e8b9671f1422c230fb1bb50d750affe6ef900d13e0243d1f3e0
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ == 0.2.1 (2016-09-21)
2
+
3
+ * Module scoped errors.
4
+
5
+ == 0.2.0 (2016-06-10)
6
+
7
+ * Uncompress gzip and deflate responses.
8
+
1
9
  == 0.1.2 (2016-06-09)
2
10
 
3
11
  * Fix YARD documentation.
data/lib/http/client.rb CHANGED
@@ -4,10 +4,12 @@ require 'openssl'
4
4
  require 'uri'
5
5
  require 'mime/types'
6
6
  require 'http-cookie'
7
+ require 'stringio'
8
+ require 'zlib'
7
9
 
8
10
  module HTTP
9
11
  module Client
10
- VERSION = '0.1.2'
12
+ VERSION = '0.2.1'
11
13
 
12
14
  GET = Net::HTTP::Get
13
15
  HEAD = Net::HTTP::Head
@@ -20,6 +22,12 @@ module HTTP
20
22
  SSL_VERIFY_NONE = OpenSSL::SSL::VERIFY_NONE
21
23
  SSL_VERIFY_PEER = OpenSSL::SSL::VERIFY_PEER
22
24
 
25
+ class Error < StandardError;
26
+ end
27
+
28
+ class ArgumentError < Error;
29
+ end
30
+
23
31
  class Request
24
32
  VALID_PARAMETERS = %w(headers files query body auth timeout open_timeout ssl_timeout read_timeout max_redirects ssl_verify jar)
25
33
  DEFAULT_HEADERS = {'User-Agent' => 'HTTP Client API/1.0'}
@@ -147,6 +155,7 @@ module HTTP
147
155
  uri = uri.kind_of?(URI) ? uri : URI.parse(uri)
148
156
  case uri
149
157
  when URI::HTTP, URI::HTTPS
158
+ raise ArgumentError, "Invalid URI #{uri}" if uri.host.nil?
150
159
  uri
151
160
  when URI::Generic
152
161
  if @delegate.uri
@@ -157,6 +166,8 @@ module HTTP
157
166
  else
158
167
  raise ArgumentError, "Invalid URI #{uri}"
159
168
  end
169
+ rescue URI::InvalidURIError => e
170
+ raise ArgumentError, "Invalid URI #{uri}"
160
171
  end
161
172
 
162
173
  def create_request_delegate verb, uri, args
@@ -225,7 +236,7 @@ module HTTP
225
236
  r.body = @delegate.body
226
237
  end
227
238
  else
228
- raise RuntimeError, "response #{code} should not result in redirection."
239
+ raise Error, "response #{code} should not result in redirection."
229
240
  end
230
241
  end
231
242
 
@@ -251,7 +262,7 @@ module HTTP
251
262
  end # Request
252
263
 
253
264
  class Response
254
- attr_reader :last_effective_uri
265
+ attr_reader :last_effective_uri, :response
255
266
 
256
267
  def initialize response, last_effective_uri
257
268
  @response = response
@@ -259,15 +270,27 @@ module HTTP
259
270
  end
260
271
 
261
272
  def code
262
- @response.code.to_i
273
+ response.code.to_i
263
274
  end
264
275
 
265
276
  def headers
266
- @headers ||= Hash[@response.each_header.entries]
277
+ @headers ||= Hash[response.each_header.entries]
267
278
  end
268
279
 
269
280
  def body
270
- @response.body
281
+ case headers['content-encoding'].to_s.downcase
282
+ when 'gzip'
283
+ gz = Zlib::GzipReader.new(StringIO.new(response.body))
284
+ begin
285
+ gz.read
286
+ ensure
287
+ gz.close
288
+ end
289
+ when 'deflate'
290
+ Zlib.inflate(response.body)
291
+ else
292
+ response.body
293
+ end
271
294
  end
272
295
 
273
296
  def inspect
data/test/server.rb CHANGED
@@ -40,12 +40,7 @@ class TestServer
40
40
 
41
41
  private
42
42
  def find_port
43
- @tcp = nil
44
- for port in 32768..65535
45
- @tcp = TCPServer.new('127.0.0.1', port) rescue nil
46
- break if @tcp
47
- end
48
-
43
+ @tcp = TCPServer.new('127.0.0.1', 0) rescue nil
49
44
  @tcp or raise RuntimeError 'Unable to find a local TCP port to bind.'
50
45
  port = @tcp.addr[1]
51
46
  @tcp.close
data/test/test_request.rb CHANGED
@@ -1,14 +1,21 @@
1
+ require 'zlib'
2
+ require 'stringio'
1
3
  require_relative 'helper'
2
4
 
3
5
  describe 'HTTP Client Request' do
4
6
  it 'should reject invalid arguments' do
5
- assert_raises(ArgumentError, 'invalid verb') {HTTP::Client::Request.new(:foo)}
6
- assert_raises(URI::InvalidURIError, 'invalid uri') {HTTP::Client::Request.new(:get, 'http://')}
7
- assert_raises(ArgumentError, 'invalid argument') {HTTP::Client::Request.new(:get, 'http://example.org/', foo: 1)}
7
+ assert_raises(HTTP::Client::ArgumentError, 'invalid verb') {HTTP::Client::Request.new(:foo, 'http://example.org/')}
8
+ assert_raises(HTTP::Client::ArgumentError, 'invalid uri') {HTTP::Client::Request.new(:get, 'http://')}
9
+
10
+ assert_raises(HTTP::Client::ArgumentError, 'invalid argument') do
11
+ HTTP::Client::Request.new(:get, 'http://example.org/', foo: 1)
12
+ end
8
13
  end
9
14
 
10
15
  it 'validates body based on request verb' do
11
- assert_raises(ArgumentError, 'get cannot have body') {HTTP::Client::Request.new(:get, 'http://a.c', files: {test: __FILE__})}
16
+ assert_raises(HTTP::Client::ArgumentError, 'get cannot have body') do
17
+ HTTP::Client::Request.new(:get, 'http://a.c', files: {test: __FILE__})
18
+ end
12
19
  end
13
20
 
14
21
  it 'allows creation of valid request object' do
@@ -158,4 +165,33 @@ describe 'HTTP Client Request' do
158
165
  assert_equal data, qs['this']
159
166
  assert_equal File.basename(__FILE__), qs['this'].filename # people should stop overloading / extending String!
160
167
  end
168
+
169
+ it 'handles gzip and deflate compression schemes.' do
170
+ app = proc do |req|
171
+ case req.path
172
+ when '/gzip'
173
+ [
174
+ 200,
175
+ {'content-encoding' => 'gzip'},
176
+ StringIO.new.tap {|io|
177
+ gz = Zlib::GzipWriter.new(io)
178
+ gz.write("Hello 1")
179
+ gz.close
180
+ }.string
181
+ ]
182
+ when '/deflate'
183
+ [200, {'content-encoding' => 'deflate'}, Zlib.deflate("Hello 2")]
184
+ end
185
+ end
186
+
187
+ server = TestServer.new(app)
188
+ server.run
189
+
190
+ response = HTTP::Client.get("#{server.root}/gzip", headers: {'accept-encoding' => 'gzip;deflate'})
191
+ assert_equal 'Hello 1', response.body, 'gunzip ok'
192
+
193
+ response = HTTP::Client.get("#{server.root}/deflate", headers: {'accept-encoding' => 'gzip;deflate'})
194
+ assert_equal 'Hello 2', response.body, 'inflate ok'
195
+ server.stop
196
+ end
161
197
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bharanee Rathna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-09 00:00:00.000000000 Z
11
+ date: 2016-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types