faraday 0.4.6 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,5 +22,16 @@ module Adapters
22
22
  def test_middleware_with_simple_path_sets_body
23
23
  assert_equal 'hello', @resp.body
24
24
  end
25
+
26
+ def test_middleware_can_be_called_several_times
27
+ assert_equal 'hello', @conn.get("/hello").body
28
+ end
29
+
30
+ def test_middleware_allow_different_outcomes_for_the_same_request
31
+ @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
32
+ @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
33
+ assert_equal 'hello', @conn.get("/hello").body
34
+ assert_equal 'world', @conn.get("/hello").body
35
+ end
25
36
  end
26
37
  end
@@ -58,6 +58,11 @@ class TestConnection < Faraday::TestCase
58
58
  assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', conn.headers['Authorization']
59
59
  end
60
60
 
61
+ def test_auto_parses_basic_auth_from_url
62
+ conn = Faraday::Connection.new :url => "http://aladdin:opensesame@sushi.com/fish"
63
+ assert_equal 'Basic YWxhZGRpbjpvcGVuc2VzYW1l', conn.headers['Authorization']
64
+ end
65
+
61
66
  def test_token_auth_sets_authorization_header
62
67
  conn = Faraday::Connection.new
63
68
  conn.token_auth 'abcdef'
@@ -174,12 +179,6 @@ class TestConnection < Faraday::TestCase
174
179
  assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
175
180
  end
176
181
 
177
- def test_proxy_accepts_string
178
- conn = Faraday::Connection.new
179
- conn.proxy 'http://proxy.com'
180
- assert_equal 'proxy.com', conn.proxy.host
181
- end
182
-
183
182
  def test_proxy_accepts_string
184
183
  conn = Faraday::Connection.new
185
184
  conn.proxy 'http://proxy.com'
@@ -276,4 +275,4 @@ class TestConnection < Faraday::TestCase
276
275
  mware = conn.builder.handlers[1].call({})
277
276
  assert_kind_of Faraday::Adapter::NetHttp, mware
278
277
  end
279
- end
278
+ end
@@ -34,7 +34,7 @@ class TestEnv < Faraday::TestCase
34
34
  assert_equal @input[:body], @env[:body]
35
35
  end
36
36
 
37
- def test_request_create_stores_ssl_options
37
+ def test_request_create_stores_timeout_options
38
38
  assert_equal 3, @env[:request][:timeout]
39
39
  assert_equal 5, @env[:request][:open_timeout]
40
40
  end
@@ -53,4 +53,4 @@ class TestEnv < Faraday::TestCase
53
53
  end
54
54
  env_setup.to_env_hash(connection, :get)
55
55
  end
56
- end
56
+ end
@@ -0,0 +1,42 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class FormPostTest < Faraday::TestCase
4
+ def setup
5
+ @app = Faraday::Adapter.new nil
6
+ @env = {:request_headers => {}}
7
+ end
8
+
9
+ def test_processes_nested_body
10
+ @env[:body] = {:a => 1, :b => {:c => 2}}
11
+ @app.process_body_for_request @env
12
+ assert_match /^|\&a=1/, @env[:body]
13
+ assert_match /^|\&b\[c\]=2/, @env[:body]
14
+ assert_equal Faraday::Adapter::FORM_TYPE, @env[:request_headers]['Content-Type']
15
+ end
16
+
17
+ def test_processes_with_custom_type
18
+ @env[:body] = {:a => 1}
19
+ @env[:request_headers]['Content-Type'] = 'test/type'
20
+ @app.process_body_for_request @env
21
+ assert_equal 'a=1', @env[:body]
22
+ assert_equal 'test/type', @env[:request_headers]['Content-Type']
23
+ end
24
+
25
+ def test_processes_nil_body
26
+ @env[:body] = nil
27
+ @app.process_body_for_request @env
28
+ assert_nil @env[:body]
29
+ end
30
+
31
+ def test_processes_empty_body
32
+ @env[:body] = ''
33
+ @app.process_body_for_request @env
34
+ assert_equal '', @env[:body]
35
+ end
36
+
37
+ def test_processes_string_body
38
+ @env[:body] = 'abc'
39
+ @app.process_body_for_request @env
40
+ assert_equal 'abc', @env[:body]
41
+ end
42
+ end
@@ -1,10 +1,14 @@
1
1
  require 'rubygems'
2
2
  gem 'rack', '>= 1.0.1'
3
- gem 'addressable', '2.1.1'
3
+ gem 'addressable', '>= 2.1.1'
4
4
 
5
5
  require 'test/unit'
6
6
  if ENV['LEFTRIGHT']
7
- require 'leftright'
7
+ begin
8
+ require 'leftright'
9
+ rescue LoadError
10
+ puts "Run `gem install leftright` to install leftright."
11
+ end
8
12
  end
9
13
 
10
14
  $LOAD_PATH.unshift(File.dirname(__FILE__))
@@ -1,4 +1,3 @@
1
- require 'rubygems'
2
1
  require 'sinatra'
3
2
 
4
3
  get '/hello_world' do
@@ -9,6 +8,12 @@ get '/json' do
9
8
  "[1,2,3]"
10
9
  end
11
10
 
11
+ post '/file' do
12
+ "file %s %s" % [
13
+ params[:uploaded_file][:filename],
14
+ params[:uploaded_file][:type]]
15
+ end
16
+
12
17
  post '/hello' do
13
18
  "hello #{params[:name]}"
14
19
  end
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ Faraday::CompositeReadIO.send :attr_reader, :ios
4
+
5
+ class MultipartTest < Faraday::TestCase
6
+ def setup
7
+ @app = Faraday::Adapter.new nil
8
+ @env = {:request_headers => {}}
9
+ end
10
+
11
+ def test_processes_nested_body
12
+ # assume params are out of order
13
+ regexes = [
14
+ /name\=\"a\"/,
15
+ /name=\"b\[c\]\"\; filename\=\"multipart_test\.rb\"/,
16
+ /name=\"b\[d\]\"/]
17
+ @env[:body] = {:a => 1, :b => {:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}}
18
+ @app.process_body_for_request @env
19
+ @env[:body].send(:ios).map(&:read).each do |io|
20
+ if re = regexes.detect { |r| io =~ r }
21
+ regexes.delete re
22
+ end
23
+ end
24
+ assert_equal [], regexes
25
+ assert_kind_of CompositeReadIO, @env[:body]
26
+ assert_equal "%s;boundary=%s" %
27
+ [Faraday::Adapter::MULTIPART_TYPE, Faraday::Adapter::DEFAULT_BOUNDARY],
28
+ @env[:request_headers]['Content-Type']
29
+ end
30
+
31
+ def test_processes_nil_body
32
+ @env[:body] = nil
33
+ @app.process_body_for_request @env
34
+ assert_nil @env[:body]
35
+ end
36
+
37
+ def test_processes_empty_body
38
+ @env[:body] = ''
39
+ @app.process_body_for_request @env
40
+ assert_equal '', @env[:body]
41
+ end
42
+
43
+ def test_processes_string_body
44
+ @env[:body] = 'abc'
45
+ @app.process_body_for_request @env
46
+ assert_equal 'abc', @env[:body]
47
+ end
48
+ end
@@ -6,9 +6,11 @@ class RequestMiddlewareTest < Faraday::TestCase
6
6
  next if !encoder.loaded?
7
7
 
8
8
  define_method "test_encodes_json_with_#{key}" do
9
- raw_json = create_json_connection(encoder).post('echo_body', :a => 1).body
9
+ resp = create_json_connection(encoder).post('echo_body', :a => 1)
10
+ raw_json = resp.body
10
11
  raw_json.gsub! /: 1/, ':1' # sometimes rails_json adds a space
11
12
  assert_equal %({"a":1}), raw_json
13
+ assert_match /json/, resp.headers['Content-Type']
12
14
  end
13
15
  end
14
16
 
@@ -17,7 +19,12 @@ private
17
19
  Faraday::Connection.new do |b|
18
20
  b.use encoder
19
21
  b.adapter :test do |stub|
20
- stub.post('echo_body') { |env| [200, {'Content-Type' => 'text/html'}, env[:body]] }
22
+ stub.post('echo_body') do |env|
23
+ [200,
24
+ {'Content-Type' => env[:request_headers]['Content-Type']},
25
+ env[:body]
26
+ ]
27
+ end
21
28
  end
22
29
  end
23
30
  end
@@ -4,7 +4,7 @@ class ResponseMiddlewareTest < Faraday::TestCase
4
4
  [:yajl, :rails_json].each do |key|
5
5
  encoder = Faraday::Response.lookup_module(key)
6
6
  next if !encoder.loaded?
7
-
7
+
8
8
  define_method "test_uses_#{key}_to_parse_json_content" do
9
9
  response = create_json_connection(encoder).get('json')
10
10
  assert response.success?
@@ -22,6 +22,12 @@ class ResponseMiddlewareTest < Faraday::TestCase
22
22
  assert response.success?
23
23
  assert !response.body
24
24
  end
25
+
26
+ define_method "test_use_#{key}_to_raise_Faraday_Error_Parsing_with_no_json_content" do
27
+ assert_raises Faraday::Error::ParsingError do
28
+ response = create_json_connection(encoder).get('bad_json')
29
+ end
30
+ end
25
31
  end
26
32
 
27
33
  def create_json_connection(encoder)
@@ -30,6 +36,7 @@ class ResponseMiddlewareTest < Faraday::TestCase
30
36
  stub.get('json') { [200, {'Content-Type' => 'text/html'}, "[1,2,3]"] }
31
37
  stub.get('blank') { [200, {'Content-Type' => 'text/html'}, ''] }
32
38
  stub.get('nil') { [200, {'Content-Type' => 'text/html'}, nil] }
39
+ stub.get("bad_json") {[200, {'Content-Type' => 'text/html'}, '<body></body>']}
33
40
  end
34
41
  b.use encoder
35
42
  end
metadata CHANGED
@@ -4,39 +4,53 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
8
- - 6
9
- version: 0.4.6
7
+ - 5
8
+ - 0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
- - rick
12
+ - Rick Olson
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-28 00:00:00 -07:00
17
+ date: 2010-10-08 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rack
21
+ name: rake
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
25
+ - - ~>
26
26
  - !ruby/object:Gem::Version
27
27
  segments:
28
- - 1
29
28
  - 0
30
- - 1
31
- version: 1.0.1
32
- type: :runtime
29
+ - 8
30
+ - 7
31
+ version: 0.8.7
32
+ type: :development
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
35
- name: addressable
35
+ name: sinatra
36
36
  prerelease: false
37
37
  requirement: &id002 !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 0
44
+ - 0
45
+ version: 1.0.0
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: addressable
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
40
54
  - !ruby/object:Gem::Version
41
55
  segments:
42
56
  - 2
@@ -44,25 +58,53 @@ dependencies:
44
58
  - 1
45
59
  version: 2.1.1
46
60
  type: :runtime
47
- version_requirements: *id002
48
- description: HTTP/REST API client library with pluggable components
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: multipart-post
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 1
71
+ - 0
72
+ - 1
73
+ version: 1.0.1
74
+ type: :runtime
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: rack
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 1
85
+ - 2
86
+ - 1
87
+ version: 1.2.1
88
+ type: :runtime
89
+ version_requirements: *id005
90
+ description: HTTP/REST API client library.
49
91
  email: technoweenie@gmail.com
50
92
  executables: []
51
93
 
52
94
  extensions: []
53
95
 
54
- extra_rdoc_files:
55
- - LICENSE
56
- - README.rdoc
96
+ extra_rdoc_files: []
97
+
57
98
  files:
58
- - .document
59
- - .gitignore
99
+ - Gemfile
100
+ - Gemfile.lock
60
101
  - LICENSE
61
- - README.rdoc
102
+ - README.md
62
103
  - Rakefile
63
- - VERSION
64
104
  - faraday.gemspec
65
105
  - lib/faraday.rb
106
+ - lib/faraday/adapter.rb
107
+ - lib/faraday/adapter/action_dispatch.rb
66
108
  - lib/faraday/adapter/net_http.rb
67
109
  - lib/faraday/adapter/patron.rb
68
110
  - lib/faraday/adapter/test.rb
@@ -77,14 +119,18 @@ files:
77
119
  - lib/faraday/response.rb
78
120
  - lib/faraday/response/active_support_json.rb
79
121
  - lib/faraday/response/yajl.rb
122
+ - lib/faraday/upload_io.rb
123
+ - lib/faraday/utils.rb
80
124
  - test/adapters/live_test.rb
81
125
  - test/adapters/test_middleware_test.rb
82
126
  - test/adapters/typhoeus_test.rb
83
127
  - test/connection_app_test.rb
84
128
  - test/connection_test.rb
85
129
  - test/env_test.rb
130
+ - test/form_post_test.rb
86
131
  - test/helper.rb
87
132
  - test/live_server.rb
133
+ - test/multipart_test.rb
88
134
  - test/request_middleware_test.rb
89
135
  - test/response_middleware_test.rb
90
136
  has_rdoc: true
@@ -92,8 +138,8 @@ homepage: http://github.com/technoweenie/faraday
92
138
  licenses: []
93
139
 
94
140
  post_install_message:
95
- rdoc_options:
96
- - --charset=UTF-8
141
+ rdoc_options: []
142
+
97
143
  require_paths:
98
144
  - lib
99
145
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -112,11 +158,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
158
  version: "0"
113
159
  requirements: []
114
160
 
115
- rubyforge_project:
161
+ rubyforge_project: faraday
116
162
  rubygems_version: 1.3.6
117
163
  signing_key:
118
- specification_version: 3
119
- summary: HTTP/REST API client library
164
+ specification_version: 2
165
+ summary: HTTP/REST API client library.
120
166
  test_files:
121
167
  - test/adapters/live_test.rb
122
168
  - test/adapters/test_middleware_test.rb
@@ -124,7 +170,7 @@ test_files:
124
170
  - test/connection_app_test.rb
125
171
  - test/connection_test.rb
126
172
  - test/env_test.rb
127
- - test/helper.rb
128
- - test/live_server.rb
173
+ - test/form_post_test.rb
174
+ - test/multipart_test.rb
129
175
  - test/request_middleware_test.rb
130
176
  - test/response_middleware_test.rb
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/.gitignore DELETED
@@ -1,21 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC