faraday 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/LICENSE +1 -1
  2. data/README.rdoc +48 -61
  3. data/Rakefile +3 -1
  4. data/VERSION +1 -1
  5. data/faraday.gemspec +31 -14
  6. data/lib/faraday.rb +33 -26
  7. data/lib/faraday/adapter/net_http.rb +17 -31
  8. data/lib/faraday/adapter/patron.rb +33 -0
  9. data/lib/faraday/adapter/test.rb +96 -0
  10. data/lib/faraday/adapter/typhoeus.rb +43 -59
  11. data/lib/faraday/builder.rb +57 -0
  12. data/lib/faraday/connection.rb +112 -105
  13. data/lib/faraday/middleware.rb +54 -0
  14. data/lib/faraday/request.rb +77 -0
  15. data/lib/faraday/request/active_support_json.rb +20 -0
  16. data/lib/faraday/request/yajl.rb +18 -0
  17. data/lib/faraday/response.rb +41 -26
  18. data/lib/faraday/response/active_support_json.rb +22 -0
  19. data/lib/faraday/response/yajl.rb +20 -0
  20. data/test/adapters/live_test.rb +157 -0
  21. data/test/adapters/test_middleware_test.rb +28 -0
  22. data/test/adapters/typhoeus_test.rb +28 -0
  23. data/test/connection_app_test.rb +49 -0
  24. data/test/connection_test.rb +47 -18
  25. data/test/env_test.rb +35 -0
  26. data/test/helper.rb +5 -2
  27. data/test/live_server.rb +2 -15
  28. data/test/request_middleware_test.rb +19 -0
  29. data/test/response_middleware_test.rb +21 -0
  30. metadata +46 -16
  31. data/lib/faraday/adapter/mock_request.rb +0 -120
  32. data/lib/faraday/loadable.rb +0 -13
  33. data/lib/faraday/request/post_request.rb +0 -30
  34. data/lib/faraday/request/yajl_request.rb +0 -27
  35. data/lib/faraday/response/yajl_response.rb +0 -35
  36. data/lib/faraday/test_connection.rb +0 -5
  37. data/test/adapter/typhoeus_test.rb +0 -26
  38. data/test/adapter_test.rb +0 -118
  39. data/test/response_test.rb +0 -34
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
2
+
3
+ module Adapters
4
+ class TestMiddleware < Faraday::TestCase
5
+ describe "Test Middleware with simple path" do
6
+ before :all do
7
+ @stubs = Faraday::Adapter::Test::Stubs.new
8
+ @conn = Faraday::Connection.new do |builder|
9
+ builder.adapter :test, @stubs
10
+ end
11
+ @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
12
+ @resp = @conn.get('/hello')
13
+ end
14
+
15
+ it "sets status" do
16
+ assert_equal 200, @resp.status
17
+ end
18
+
19
+ it "sets headers" do
20
+ assert_equal 'text/html', @resp.headers['Content-Type']
21
+ end
22
+
23
+ it "sets body" do
24
+ assert_equal 'hello', @resp.body
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
2
+
3
+ if Faraday::Adapter::Typhoeus.loaded?
4
+ module Adapters
5
+ class TestTyphoeus < Faraday::TestCase
6
+ describe "#parse_response_headers" do
7
+ before do
8
+ @adapter = Faraday::Adapter::Typhoeus.new
9
+ end
10
+
11
+ it "leaves http status line out" do
12
+ headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
13
+ assert_equal %w(content-type), headers.keys
14
+ end
15
+
16
+ it "parses lower-cased header name and value" do
17
+ headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
18
+ assert_equal 'text/html', headers['content-type']
19
+ end
20
+
21
+ it "parses lower-cased header name and value with colon" do
22
+ headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nLocation: http://sushi.com/\r\n\r\n")
23
+ assert_equal 'http://sushi.com/', headers['location']
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class TestConnectionApps < Faraday::TestCase
4
+ class TestAdapter
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ [200, {}, env[:test]]
11
+ end
12
+ end
13
+
14
+ class TestMiddleWare
15
+ def initialize(app)
16
+ @app = app
17
+ end
18
+
19
+ def call(env)
20
+ env[:test] = 'hi'
21
+ @app.call(env)
22
+ end
23
+ end
24
+
25
+ before do
26
+ @conn = Faraday::Connection.new do |b|
27
+ b.use TestMiddleWare
28
+ b.use TestAdapter
29
+ end
30
+ end
31
+
32
+ describe "#builder" do
33
+ it "is built from Faraday::Connection constructor" do
34
+ assert_kind_of Faraday::Builder, @conn.builder
35
+ assert_equal 3, @conn.builder.handlers.size
36
+ end
37
+
38
+ it "adds middleware to the Builder stack" do
39
+ assert_kind_of TestMiddleWare, @conn.builder.handlers[2].call(nil)
40
+ assert_kind_of TestAdapter, @conn.builder.handlers[1].call(nil)
41
+ end
42
+ end
43
+
44
+ describe "#to_app" do
45
+ it "returns rack-compatible object" do
46
+ assert @conn.to_app.respond_to?(:call)
47
+ end
48
+ end
49
+ end
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
2
 
3
- class ConnectionTest < Faraday::TestCase
3
+ class TestConnection < Faraday::TestCase
4
4
  describe "#initialize" do
5
5
  it "parses @host out of given url" do
6
6
  conn = Faraday::Connection.new "http://sushi.com"
@@ -39,92 +39,118 @@ class ConnectionTest < Faraday::TestCase
39
39
 
40
40
  it "stores default params from options" do
41
41
  conn = Faraday::Connection.new :params => {:a => 1}
42
- assert_equal 1, conn.params[:a]
42
+ assert_equal 1, conn.params['a']
43
+ end
44
+
45
+ it "stores default params from uri" do
46
+ conn = Faraday::Connection.new "http://sushi.com/fish?a=1", :params => {'b' => '2'}
47
+ assert_equal '1', conn.params['a']
48
+ assert_equal '2', conn.params['b']
43
49
  end
44
50
 
45
51
  it "stores default headers from options" do
46
52
  conn = Faraday::Connection.new :headers => {:a => 1}
47
- assert_equal 1, conn.headers[:a]
53
+ assert_equal '1', conn.headers['A']
48
54
  end
49
55
  end
50
56
 
51
- describe "#build_uri" do
57
+ describe "#build_url" do
52
58
  it "uses Connection#host as default URI host" do
53
59
  conn = Faraday::Connection.new
54
60
  conn.host = 'sushi.com'
55
- uri = conn.build_uri("/sake.html")
61
+ uri = conn.build_url("/sake.html")
56
62
  assert_equal 'sushi.com', uri.host
57
63
  end
58
64
 
59
65
  it "uses Connection#port as default URI port" do
60
66
  conn = Faraday::Connection.new
61
67
  conn.port = 23
62
- uri = conn.build_uri("http://sushi.com")
68
+ uri = conn.build_url("http://sushi.com")
63
69
  assert_equal 23, uri.port
64
70
  end
65
71
 
66
72
  it "uses Connection#scheme as default URI scheme" do
67
73
  conn = Faraday::Connection.new 'http://sushi.com'
68
- uri = conn.build_uri("/sake.html")
74
+ uri = conn.build_url("/sake.html")
69
75
  assert_equal 'http', uri.scheme
70
76
  end
71
77
 
72
78
  it "uses Connection#path_prefix to customize the path" do
73
79
  conn = Faraday::Connection.new
74
80
  conn.path_prefix = '/fish'
75
- uri = conn.build_uri("sake.html")
81
+ uri = conn.build_url("sake.html")
76
82
  assert_equal '/fish/sake.html', uri.path
77
83
  end
78
84
 
79
85
  it "uses '/' Connection#path_prefix to customize the path" do
80
86
  conn = Faraday::Connection.new
81
87
  conn.path_prefix = '/'
82
- uri = conn.build_uri("sake.html")
88
+ uri = conn.build_url("sake.html")
83
89
  assert_equal '/sake.html', uri.path
84
90
  end
85
91
 
86
92
  it "forces Connection#path_prefix to be absolute" do
87
93
  conn = Faraday::Connection.new
88
94
  conn.path_prefix = 'fish'
89
- uri = conn.build_uri("sake.html")
95
+ uri = conn.build_url("sake.html")
90
96
  assert_equal '/fish/sake.html', uri.path
91
97
  end
92
98
 
93
99
  it "ignores Connection#path_prefix trailing slash" do
94
100
  conn = Faraday::Connection.new
95
101
  conn.path_prefix = '/fish/'
96
- uri = conn.build_uri("sake.html")
102
+ uri = conn.build_url("sake.html")
97
103
  assert_equal '/fish/sake.html', uri.path
98
104
  end
99
105
 
100
106
  it "allows absolute URI to ignore Connection#path_prefix" do
101
107
  conn = Faraday::Connection.new
102
108
  conn.path_prefix = '/fish'
103
- uri = conn.build_uri("/sake.html")
109
+ uri = conn.build_url("/sake.html")
104
110
  assert_equal '/sake.html', uri.path
105
111
  end
106
112
 
107
113
  it "parses url/params into #path" do
108
114
  conn = Faraday::Connection.new
109
- uri = conn.build_uri("http://sushi.com/sake.html")
115
+ uri = conn.build_url("http://sushi.com/sake.html")
110
116
  assert_equal '/sake.html', uri.path
111
117
  end
112
118
 
113
119
  it "parses url/params into #query" do
114
120
  conn = Faraday::Connection.new
115
- uri = conn.build_uri("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
121
+ uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
116
122
  assert_equal "a%5Bb%5D=1%20%2B%202", uri.query
117
123
  end
118
124
 
125
+ it "mashes default params and given params together" do
126
+ conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
127
+ url = conn.build_url("nigiri?page=1", :limit => 5)
128
+ assert_match /limit=5/, url.query
129
+ assert_match /page=1/, url.query
130
+ assert_match /format=json/, url.query
131
+ assert_match /token=abc/, url.query
132
+ end
133
+
134
+ it "overrides default params with given params" do
135
+ conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
136
+ url = conn.build_url("nigiri?page=1", :limit => 5, :token => 'def', :format => 'xml')
137
+ assert_match /limit=5/, url.query
138
+ assert_match /page=1/, url.query
139
+ assert_match /format=xml/, url.query
140
+ assert_match /token=def/, url.query
141
+ assert_no_match /format=json/, url.query
142
+ assert_no_match /token=abc/, url.query
143
+ end
144
+
119
145
  it "parses url into #host" do
120
146
  conn = Faraday::Connection.new
121
- uri = conn.build_uri("http://sushi.com/sake.html")
147
+ uri = conn.build_url("http://sushi.com/sake.html")
122
148
  assert_equal "sushi.com", uri.host
123
149
  end
124
150
 
125
151
  it "parses url into #port" do
126
152
  conn = Faraday::Connection.new
127
- uri = conn.build_uri("http://sushi.com/sake.html")
153
+ uri = conn.build_url("http://sushi.com/sake.html")
128
154
  assert_nil uri.port
129
155
  end
130
156
  end
@@ -132,7 +158,10 @@ class ConnectionTest < Faraday::TestCase
132
158
  describe "#params_to_query" do
133
159
  it "converts hash of params to URI-escaped query string" do
134
160
  conn = Faraday::Connection.new
135
- assert_equal "a%5Bb%5D=1%20%2B%202", conn.params_to_query('a[b]' => '1 + 2')
161
+ class << conn
162
+ public :build_query
163
+ end
164
+ assert_equal "a%5Bb%5D=1%20%2B%202", conn.build_query('a[b]' => '1 + 2')
136
165
  end
137
166
  end
138
- end
167
+ end
data/test/env_test.rb ADDED
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class TestEnv < Faraday::TestCase
4
+ describe "Request#create" do
5
+ before :all do
6
+ @conn = Faraday::Connection.new :url => 'http://sushi.com/api'
7
+ @input = {
8
+ :body => 'abc',
9
+ :headers => {'Server' => 'Faraday'}}
10
+ @env_setup = Faraday::Request.create do |req|
11
+ req.url 'foo.json', 'a' => 1
12
+ req['Server'] = 'Faraday'
13
+ req.body = @input[:body]
14
+ end
15
+ @env = @env_setup.to_env_hash(@conn, :get)
16
+ end
17
+
18
+ it "stores method in :method" do
19
+ assert_equal :get, @env[:method]
20
+ end
21
+
22
+ it "stores Addressable::URI in :url" do
23
+ assert_equal 'http://sushi.com/api/foo.json?a=1', @env[:url].to_s
24
+ end
25
+
26
+ it "stores headers in :headers" do
27
+ assert_kind_of Rack::Utils::HeaderHash, @env[:request_headers]
28
+ assert_equal @input[:headers], @env[:request_headers]
29
+ end
30
+
31
+ it "stores body in :body" do
32
+ assert_equal @input[:body], @env[:body]
33
+ end
34
+ end
35
+ end
data/test/helper.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'rubygems'
2
2
  require 'context'
3
- require 'ruby-debug'
4
3
  if ENV['LEFTRIGHT']
5
4
  require 'leftright'
6
5
  end
@@ -17,6 +16,10 @@ end
17
16
 
18
17
  module Faraday
19
18
  class TestCase < Test::Unit::TestCase
20
- LIVE_SERVER = 'http://localhost:4567'
19
+ LIVE_SERVER = case ENV['LIVE']
20
+ when /^http/ then ENV['LIVE']
21
+ when nil then nil
22
+ else 'http://localhost:4567'
23
+ end
21
24
  end
22
25
  end
data/test/live_server.rb CHANGED
@@ -17,16 +17,12 @@ get '/hello' do
17
17
  "hello #{params[:name]}"
18
18
  end
19
19
 
20
- put '/hello' do
21
- "hello #{params[:name]}"
22
- end
23
-
24
20
  post '/echo_name' do
25
- %/{"name":#{params[:name].inspect}}/
21
+ params[:name].inspect
26
22
  end
27
23
 
28
24
  put '/echo_name' do
29
- %/{"name":#{params[:name].inspect}}/
25
+ params[:name].inspect
30
26
  end
31
27
 
32
28
  delete '/delete_with_json' do
@@ -36,12 +32,3 @@ end
36
32
  delete '/delete_with_params' do
37
33
  params[:deleted]
38
34
  end
39
-
40
- get '/params' do
41
- %(params[:a] == #{params[:a]})
42
- end
43
-
44
- get "/headers" do
45
- %(env[HTTP_X_TEST] == #{env["HTTP_X_TEST"]})
46
- end
47
-
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class RequestMiddlewareTest < Faraday::TestCase
4
+ describe "encoding json" do
5
+ [:yajl, :rails_json].each do |key|
6
+ encoder = Faraday::Request.lookup_module(key)
7
+ next if !encoder.loaded?
8
+ it "uses #{encoder}" do
9
+ @connection = Faraday::Connection.new do |b|
10
+ b.use encoder
11
+ b.adapter :test do |stub|
12
+ stub.post('echo_body', '{"a":1}') { |env| [200, {'Content-Type' => 'text/html'}, env[:body]] }
13
+ end
14
+ end
15
+ assert_equal %({"a":1}), @connection.post('echo_body', :a => 1).body
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class ResponseMiddlewareTest < Faraday::TestCase
4
+ describe "parsing json" do
5
+ [:yajl, :rails_json].each do |key|
6
+ parser = Faraday::Response.lookup_module(key)
7
+ next if !parser.loaded?
8
+ it "uses #{parser}" do
9
+ @connection = Faraday::Connection.new do |b|
10
+ b.adapter :test do |stub|
11
+ stub.get('json') { [200, {'Content-Type' => 'text/html'}, "[1,2,3]"] }
12
+ end
13
+ b.use parser
14
+ end
15
+ response = @connection.get('json')
16
+ assert response.success?
17
+ assert_equal [1,2,3], response.body
18
+ end
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - rick
@@ -9,10 +9,29 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-05 00:00:00 -08:00
12
+ date: 2010-01-12 00:00:00 -08:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: addressable
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
16
35
  description: HTTP/REST API client library with pluggable components
17
36
  email: technoweenie@gmail.com
18
37
  executables: []
@@ -31,23 +50,30 @@ files:
31
50
  - VERSION
32
51
  - faraday.gemspec
33
52
  - lib/faraday.rb
34
- - lib/faraday/adapter/mock_request.rb
35
53
  - lib/faraday/adapter/net_http.rb
54
+ - lib/faraday/adapter/patron.rb
55
+ - lib/faraday/adapter/test.rb
36
56
  - lib/faraday/adapter/typhoeus.rb
57
+ - lib/faraday/builder.rb
37
58
  - lib/faraday/connection.rb
38
59
  - lib/faraday/error.rb
39
- - lib/faraday/loadable.rb
40
- - lib/faraday/request/post_request.rb
41
- - lib/faraday/request/yajl_request.rb
60
+ - lib/faraday/middleware.rb
61
+ - lib/faraday/request.rb
62
+ - lib/faraday/request/active_support_json.rb
63
+ - lib/faraday/request/yajl.rb
42
64
  - lib/faraday/response.rb
43
- - lib/faraday/response/yajl_response.rb
44
- - lib/faraday/test_connection.rb
45
- - test/adapter/typhoeus_test.rb
46
- - test/adapter_test.rb
65
+ - lib/faraday/response/active_support_json.rb
66
+ - lib/faraday/response/yajl.rb
67
+ - test/adapters/live_test.rb
68
+ - test/adapters/test_middleware_test.rb
69
+ - test/adapters/typhoeus_test.rb
70
+ - test/connection_app_test.rb
47
71
  - test/connection_test.rb
72
+ - test/env_test.rb
48
73
  - test/helper.rb
49
74
  - test/live_server.rb
50
- - test/response_test.rb
75
+ - test/request_middleware_test.rb
76
+ - test/response_middleware_test.rb
51
77
  has_rdoc: true
52
78
  homepage: http://github.com/technoweenie/faraday
53
79
  licenses: []
@@ -77,9 +103,13 @@ signing_key:
77
103
  specification_version: 3
78
104
  summary: HTTP/REST API client library
79
105
  test_files:
80
- - test/adapter/typhoeus_test.rb
81
- - test/adapter_test.rb
106
+ - test/adapters/live_test.rb
107
+ - test/adapters/test_middleware_test.rb
108
+ - test/adapters/typhoeus_test.rb
109
+ - test/connection_app_test.rb
82
110
  - test/connection_test.rb
111
+ - test/env_test.rb
83
112
  - test/helper.rb
84
113
  - test/live_server.rb
85
- - test/response_test.rb
114
+ - test/request_middleware_test.rb
115
+ - test/response_middleware_test.rb