faraday 0.8.5 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -6,7 +6,7 @@ end
6
6
 
7
7
  group :test do
8
8
  gem 'em-http-request', '~> 1.0', :require => 'em-http'
9
- gem 'em-synchrony', '~> 1.0', :require => ['em-synchrony', 'em-synchrony/em-http'], :platforms => :ruby_19
9
+ gem 'em-synchrony', '~> 1.0', :require => ['em-synchrony', 'em-synchrony/em-http']
10
10
  gem 'excon', '>= 0.16.1'
11
11
  gem 'net-http-persistent', '~> 2.5', :require => false
12
12
  gem 'leftright', '~> 0.9', :require => false
data/lib/faraday.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Faraday
2
- VERSION = "0.8.5"
2
+ VERSION = "0.8.6"
3
3
 
4
4
  class << self
5
5
  attr_accessor :root_path, :lib_path
@@ -57,6 +57,8 @@ module Faraday
57
57
  proxy(options.fetch(:proxy) { ENV['http_proxy'] })
58
58
 
59
59
  yield self if block_given?
60
+
61
+ @headers[:user_agent] ||= "Faraday v#{VERSION}"
60
62
  end
61
63
 
62
64
  # Public: Replace default query parameters.
@@ -8,13 +8,49 @@ rescue LoadError
8
8
  end
9
9
 
10
10
  module Faraday
11
- class CompositeReadIO < ::CompositeReadIO
12
- attr_reader :length
11
+ # Similar but not compatible with ::CompositeReadIO provided by multipart-post.
12
+ class CompositeReadIO
13
+ def initialize(*parts)
14
+ @parts = parts.flatten
15
+ @ios = @parts.map { |part| part.to_io }
16
+ @index = 0
17
+ end
18
+
19
+ def length
20
+ @parts.inject(0) { |sum, part| sum + part.length }
21
+ end
22
+
23
+ def rewind
24
+ @ios.each { |io| io.rewind }
25
+ @index = 0
26
+ end
27
+
28
+ # Read from IOs in order until `length` bytes have been received.
29
+ def read(length = nil, outbuf = nil)
30
+ got_result = false
31
+ outbuf = outbuf ? outbuf.replace("") : ""
32
+
33
+ while io = current_io
34
+ if result = io.read(length)
35
+ got_result ||= !result.nil?
36
+ result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
37
+ outbuf << result
38
+ length -= result.length if length
39
+ break if length == 0
40
+ end
41
+ advance_io
42
+ end
43
+ (!got_result && length) ? nil : outbuf
44
+ end
45
+
46
+ private
47
+
48
+ def current_io
49
+ @ios[@index]
50
+ end
13
51
 
14
- def initialize(parts)
15
- @length = parts.inject(0) { |sum, part| sum + part.length }
16
- ios = parts.map{ |part| part.to_io }
17
- super(*ios)
52
+ def advance_io
53
+ @index += 1
18
54
  end
19
55
  end
20
56
 
@@ -0,0 +1,107 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+ require 'stringio'
3
+
4
+ class CompositeReadIOTest < Test::Unit::TestCase
5
+ Part = Struct.new(:to_io) do
6
+ def length() to_io.string.length end
7
+ end
8
+
9
+ def part(str)
10
+ Part.new StringIO.new(str)
11
+ end
12
+
13
+ def composite_io(*parts)
14
+ Faraday::CompositeReadIO.new(*parts)
15
+ end
16
+
17
+ def test_empty
18
+ io = composite_io
19
+ assert_equal 0, io.length
20
+ assert_equal "", io.read
21
+ end
22
+
23
+ def test_empty_returns_nil_for_limited_read
24
+ assert_nil composite_io.read(1)
25
+ end
26
+
27
+ def test_empty_parts_returns_nil_for_limited_read
28
+ io = composite_io(part(""), part(""))
29
+ assert_nil io.read(1)
30
+ end
31
+
32
+ def test_multipart_read_all
33
+ io = composite_io(part("abcd"), part("1234"))
34
+ assert_equal 8, io.length
35
+ assert_equal "abcd1234", io.read
36
+ end
37
+
38
+ def test_multipart_read_limited
39
+ io = composite_io(part("abcd"), part("1234"))
40
+ assert_equal "abc", io.read(3)
41
+ assert_equal "d12", io.read(3)
42
+ assert_equal "34", io.read(3)
43
+ assert_equal nil, io.read(3)
44
+ assert_equal nil, io.read(3)
45
+ end
46
+
47
+ def test_multipart_read_limited_size_larger_than_part
48
+ io = composite_io(part("abcd"), part("1234"))
49
+ assert_equal "abcd12", io.read(6)
50
+ assert_equal "34", io.read(6)
51
+ assert_equal nil, io.read(6)
52
+ end
53
+
54
+ def test_multipart_read_with_blank_parts
55
+ io = composite_io(part(""), part("abcd"), part(""), part("1234"), part(""))
56
+ assert_equal "abcd12", io.read(6)
57
+ assert_equal "34", io.read(6)
58
+ assert_equal nil, io.read(6)
59
+ end
60
+
61
+ def test_multipart_rewind
62
+ io = composite_io(part("abcd"), part("1234"))
63
+ assert_equal "abc", io.read(3)
64
+ assert_equal "d12", io.read(3)
65
+ io.rewind
66
+ assert_equal "abc", io.read(3)
67
+ assert_equal "d1234", io.read(5)
68
+ assert_equal nil, io.read(3)
69
+ io.rewind
70
+ assert_equal "ab", io.read(2)
71
+ end
72
+
73
+ if IO.respond_to?(:copy_stream)
74
+ def test_compatible_with_copy_stream
75
+ target_io = StringIO.new
76
+ io = composite_io(part("abcd"), part("1234"))
77
+
78
+ Faraday::Timer.timeout(1) do
79
+ IO.copy_stream(io, target_io)
80
+ end
81
+ assert_equal "abcd1234", target_io.string
82
+ end
83
+ end
84
+
85
+ unless RUBY_VERSION < '1.9'
86
+ def test_read_from_multibyte
87
+ File.open(File.dirname(__FILE__) + '/multibyte.txt') do |utf8|
88
+ io = composite_io(part("\x86"), Part.new(utf8))
89
+ assert_equal bin("\x86\xE3\x83\x95\xE3\x82\xA1\xE3\x82\xA4\xE3\x83\xAB\n"), io.read
90
+ end
91
+ end
92
+
93
+ def test_limited_from_multibyte
94
+ File.open(File.dirname(__FILE__) + '/multibyte.txt') do |utf8|
95
+ io = composite_io(part("\x86"), Part.new(utf8))
96
+ assert_equal bin("\x86\xE3\x83"), io.read(3)
97
+ assert_equal bin("\x95\xE3\x82"), io.read(3)
98
+ assert_equal bin("\xA1\xE3\x82\xA4\xE3\x83\xAB\n"), io.read(8)
99
+ end
100
+ end
101
+ end
102
+
103
+ def bin(str)
104
+ str.force_encoding("BINARY") if str.respond_to?(:force_encoding)
105
+ str
106
+ end
107
+ end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.8.5
5
+ version: 0.8.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Rick Olson
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-04 00:00:00.000000000 Z
12
+ date: 2013-02-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  version_requirements: !ruby/object:Gem::Requirement
@@ -110,6 +110,7 @@ files:
110
110
  - test/adapters/test_middleware_test.rb
111
111
  - test/adapters/typhoeus_test.rb
112
112
  - test/authentication_middleware_test.rb
113
+ - test/composite_read_io_test.rb
113
114
  - test/connection_test.rb
114
115
  - test/env_test.rb
115
116
  - test/helper.rb