faraday 0.2.3 → 0.2.4

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.2.4
data/faraday.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{faraday}
8
- s.version = "0.2.3"
8
+ s.version = "0.2.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["rick"]
12
- s.date = %q{2010-02-06}
12
+ s.date = %q{2010-02-18}
13
13
  s.description = %q{HTTP/REST API client library with pluggable components}
14
14
  s.email = %q{technoweenie@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -73,7 +73,7 @@ module Faraday
73
73
 
74
74
  class Stub < Struct.new(:path, :body, :block)
75
75
  def matches?(request_path, request_body)
76
- request_path == path && request_body == body
76
+ request_path == path && (body.to_s.size.zero? || request_body == body)
77
77
  end
78
78
 
79
79
  def to_s
@@ -4,152 +4,121 @@ if Faraday::TestCase::LIVE_SERVER
4
4
  module Adapters
5
5
  class LiveTest < Faraday::TestCase
6
6
  Faraday::Adapter.all_loaded_constants.each do |adapter|
7
- describe "with #{adapter} adapter" do
8
- before do
9
- @connection = Faraday::Connection.new LIVE_SERVER do
10
- use adapter
11
- end
12
- end
7
+ define_method "test_#{adapter}_GET_retrieves_the_response_body" do
8
+ assert_equal 'hello world', create_connection(adapter).get('hello_world').body
9
+ end
13
10
 
14
- describe "#get" do
15
- it "raises on 404" do
16
- assert_raise(Faraday::Error::ResourceNotFound) { @connection.get('/nothing') }
17
- end
11
+ define_method "test_#{adapter}_GET_send_url_encoded_params" do
12
+ resp = create_connection(adapter).get do |req|
13
+ req.url 'hello', 'name' => 'zack'
14
+ end
15
+ assert_equal('hello zack', resp.body)
16
+ end
18
17
 
19
- it "retrieves the response body" do
20
- assert_equal 'hello world', @connection.get('hello_world').body
21
- end
18
+ define_method "test_#{adapter}_GET_retrieves_the_response_headers" do
19
+ assert_equal 'text/html', create_connection(adapter).get('hello_world').headers['content-type']
20
+ end
22
21
 
23
- it "send url-encoded params" do
24
- resp = @connection.get do |req|
25
- req.url 'hello', 'name' => 'zack'
26
- end
27
- assert_equal('hello zack', resp.body)
28
- end
22
+ define_method "test_#{adapter}_POST_send_url_encoded_params" do
23
+ resp = create_connection(adapter).post do |req|
24
+ req.url 'echo_name'
25
+ req.body = {'name' => 'zack'}
26
+ end
27
+ assert_equal %("zack"), resp.body
28
+ end
29
29
 
30
- it "retrieves the response headers" do
31
- assert_equal 'text/html', @connection.get('hello_world').headers['content-type']
32
- end
30
+ define_method "test_#{adapter}_POST_send_url_encoded_nested_params" do
31
+ resp = create_connection(adapter).post do |req|
32
+ req.url 'echo_name'
33
+ req.body = {'name' => {'first' => 'zack'}}
33
34
  end
35
+ assert_equal %({"first"=>"zack"}), resp.body
36
+ end
34
37
 
35
- describe "#post" do
36
- it "raises on 404" do
37
- assert_raise(Faraday::Error::ResourceNotFound) { @connection.post('/nothing') }
38
- end
38
+ define_method "test_#{adapter}_POST_retrieves_the_response_headers" do
39
+ assert_equal 'text/html', create_connection(adapter).post('echo_name').headers['content-type']
40
+ end
39
41
 
40
- it "send url-encoded params" do
41
- resp = @connection.post do |req|
42
- req.url 'echo_name'
43
- req.body = {'name' => 'zack'}
44
- end
45
- assert_equal %("zack"), resp.body
42
+ # http://github.com/toland/patron/issues/#issue/9
43
+ if ENV['FORCE'] || adapter != Faraday::Adapter::Patron
44
+ define_method "test_#{adapter}_PUT_send_url_encoded_params" do
45
+ resp = create_connection(adapter).put do |req|
46
+ req.url 'echo_name'
47
+ req.body = {'name' => 'zack'}
46
48
  end
49
+ assert_equal %("zack"), resp.body
50
+ end
47
51
 
48
- it "send url-encoded nested params" do
49
- resp = @connection.post do |req|
50
- req.url 'echo_name'
51
- req.body = {'name' => {'first' => 'zack'}}
52
- end
53
- assert_equal %({"first"=>"zack"}), resp.body
52
+ define_method "test_#{adapter}_PUT_send_url_encoded_nested_params" do
53
+ resp = create_connection(adapter).put do |req|
54
+ req.url 'echo_name'
55
+ req.body = {'name' => {'first' => 'zack'}}
54
56
  end
57
+ assert_equal %({"first"=>"zack"}), resp.body
58
+ end
55
59
 
56
- it "retrieves the response headers" do
57
- assert_equal 'text/html', @connection.post('echo_name').headers['content-type']
58
- end
60
+ define_method "test_#{adapter}_PUT_retrieves_the_response_headers" do
61
+ assert_equal 'text/html', create_connection(adapter).put('echo_name').headers['content-type']
59
62
  end
63
+ end
60
64
 
61
- # http://github.com/toland/patron/issues/#issue/9
62
- if ENV['FORCE'] || adapter != Faraday::Adapter::Patron
63
- describe "#put" do
64
- it "raises on 404" do
65
- assert_raise(Faraday::Error::ResourceNotFound) { @connection.put('/nothing') }
66
- end
67
-
68
- it "send url-encoded params" do
69
- resp = @connection.put do |req|
70
- req.url 'echo_name'
71
- req.body = {'name' => 'zack'}
72
- end
73
- assert_equal %("zack"), resp.body
74
- end
75
-
76
- it "send url-encoded nested params" do
77
- resp = @connection.put do |req|
78
- req.url 'echo_name'
79
- req.body = {'name' => {'first' => 'zack'}}
80
- end
81
- assert_equal %({"first"=>"zack"}), resp.body
82
- end
83
-
84
- it "retrieves the response headers" do
85
- assert_equal 'text/html', @connection.put('echo_name').headers['content-type']
86
- end
65
+ # http://github.com/pauldix/typhoeus/issues#issue/7
66
+ if ENV['FORCE'] || adapter != Faraday::Adapter::Typhoeus
67
+ define_method "test_#{adapter}_HEAD_send_url_encoded_params" do
68
+ resp = create_connection(adapter).head do |req|
69
+ req.url 'hello', 'name' => 'zack'
87
70
  end
71
+ assert_equal 'text/html', resp.headers['content-type']
88
72
  end
89
73
 
90
- # http://github.com/pauldix/typhoeus/issues#issue/7
91
- if ENV['FORCE'] || adapter != Faraday::Adapter::Typhoeus
92
- describe "#head" do
93
- it "raises on 404" do
94
- assert_raise(Faraday::Error::ResourceNotFound) { @connection.head('/nothing') }
95
- end
96
-
97
- it "send url-encoded params" do
98
- resp = @connection.head do |req|
99
- req.url 'hello', 'name' => 'zack'
100
- end
101
- assert_equal 'text/html', resp.headers['content-type']
102
- end
103
-
104
- it "retrieves no response body" do
105
- assert_equal '', @connection.head('hello_world').body.to_s
106
- end
107
-
108
- it "retrieves the response headers" do
109
- assert_equal 'text/html', @connection.head('hello_world').headers['content-type']
110
- end
111
- end
74
+ define_method "test_#{adapter}_HEAD_retrieves_no_response_body" do
75
+ assert_equal '', create_connection(adapter).head('hello_world').body.to_s
112
76
  end
113
77
 
114
- describe "#delete" do
115
- it "raises on 404" do
116
- assert_raise(Faraday::Error::ResourceNotFound) { @connection.delete('/nothing') }
117
- end
78
+ define_method "test_#{adapter}_HEAD_retrieves_the_response_headers" do
79
+ assert_equal 'text/html', create_connection(adapter).head('hello_world').headers['content-type']
80
+ end
81
+ end
118
82
 
119
- it "retrieves the response headers" do
120
- assert_equal 'text/html', @connection.delete('delete_with_json').headers['content-type']
121
- end
83
+ define_method "test_#{adapter}_DELETE_retrieves_the_response_headers" do
84
+ assert_equal 'text/html', create_connection(adapter).delete('delete_with_json').headers['content-type']
85
+ end
122
86
 
123
- it "retrieves the body" do
124
- assert_match /deleted/, @connection.delete('delete_with_json').body
125
- end
126
- end
87
+ define_method "test_#{adapter}_DELETE_retrieves_the_body" do
88
+ assert_match /deleted/, create_connection(adapter).delete('delete_with_json').body
89
+ end
127
90
 
128
- describe "async requests" do
129
- it "clears parallel manager after running a single request" do
130
- assert !@connection.in_parallel?
131
- resp = @connection.get('hello_world')
132
- assert !@connection.in_parallel?
133
- assert_equal 'hello world', @connection.get('hello_world').body
134
- end
91
+ define_method "test_#{adapter}_async_requests_clear_parallel_manager_after_running_a_single_request" do
92
+ connection = create_connection(adapter)
93
+ assert !connection.in_parallel?
94
+ resp = connection.get('hello_world')
95
+ assert !connection.in_parallel?
96
+ assert_equal 'hello world', connection.get('hello_world').body
97
+ end
98
+
99
+ define_method "test_#{adapter}_async_requests_uses_parallel_manager_to_run_multiple_json_requests" do
100
+ resp1, resp2 = nil, nil
135
101
 
136
- it "uses parallel manager to run multiple json requests" do
137
- resp1, resp2 = nil, nil
138
-
139
- @connection.in_parallel(adapter.setup_parallel_manager) do
140
- resp1 = @connection.get('json')
141
- resp2 = @connection.get('json')
142
- if adapter.supports_parallel_requests?
143
- assert @connection.in_parallel?
144
- assert_nil resp1.body
145
- assert_nil resp2.body
146
- end
147
- end
148
- assert !@connection.in_parallel?
149
- assert_equal '[1,2,3]', resp1.body
150
- assert_equal '[1,2,3]', resp2.body
102
+ connection = create_connection(adapter)
103
+
104
+ connection.in_parallel(adapter.setup_parallel_manager) do
105
+ resp1 = connection.get('json')
106
+ resp2 = connection.get('json')
107
+ if adapter.supports_parallel_requests?
108
+ assert connection.in_parallel?
109
+ assert_nil resp1.body
110
+ assert_nil resp2.body
151
111
  end
152
112
  end
113
+ assert !connection.in_parallel?
114
+ assert_equal '[1,2,3]', resp1.body
115
+ assert_equal '[1,2,3]', resp2.body
116
+ end
117
+ end
118
+
119
+ def create_connection(adapter)
120
+ Faraday::Connection.new LIVE_SERVER do |b|
121
+ b.use adapter
153
122
  end
154
123
  end
155
124
  end
@@ -2,27 +2,25 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
2
2
 
3
3
  module Adapters
4
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')
5
+ def setup
6
+ @stubs = Faraday::Adapter::Test::Stubs.new
7
+ @conn = Faraday::Connection.new do |builder|
8
+ builder.adapter :test, @stubs
13
9
  end
10
+ @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
11
+ @resp = @conn.get('/hello')
12
+ end
14
13
 
15
- it "sets status" do
16
- assert_equal 200, @resp.status
17
- end
14
+ def test_middleware_with_simple_path_sets_status
15
+ assert_equal 200, @resp.status
16
+ end
18
17
 
19
- it "sets headers" do
20
- assert_equal 'text/html', @resp.headers['Content-Type']
21
- end
18
+ def test_middleware_with_simple_path_sets_headers
19
+ assert_equal 'text/html', @resp.headers['Content-Type']
20
+ end
22
21
 
23
- it "sets body" do
24
- assert_equal 'hello', @resp.body
25
- end
22
+ def test_middleware_with_simple_path_sets_body
23
+ assert_equal 'hello', @resp.body
26
24
  end
27
25
  end
28
26
  end
@@ -3,25 +3,23 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
3
3
  if Faraday::Adapter::Typhoeus.loaded?
4
4
  module Adapters
5
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
6
+ def setup
7
+ @adapter = Faraday::Adapter::Typhoeus.new
8
+ end
9
+
10
+ def test_parse_response_headers_leaves_http_status_line_out
11
+ headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
12
+ assert_equal %w(content-type), headers.keys
13
+ end
14
+
15
+ def test_parse_response_headers_parses_lower_cased_header_name_and_value
16
+ headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
17
+ assert_equal 'text/html', headers['content-type']
18
+ end
19
+
20
+ def test_parse_response_headers_parses_lower_cased_header_name_and_value_with_colon
21
+ headers = @adapter.parse_response_headers("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nLocation: http://sushi.com/\r\n\r\n")
22
+ assert_equal 'http://sushi.com/', headers['location']
25
23
  end
26
24
  end
27
25
  end
@@ -22,28 +22,24 @@ class TestConnectionApps < Faraday::TestCase
22
22
  end
23
23
  end
24
24
 
25
- before do
25
+ def setup
26
26
  @conn = Faraday::Connection.new do |b|
27
27
  b.use TestMiddleWare
28
28
  b.use TestAdapter
29
29
  end
30
30
  end
31
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
32
+ def test_builder_is_built_from_faraday_connection
33
+ assert_kind_of Faraday::Builder, @conn.builder
34
+ assert_equal 3, @conn.builder.handlers.size
35
+ end
37
36
 
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
37
+ def test_builder_adds_middleware_to_builder_stack
38
+ assert_kind_of TestMiddleWare, @conn.builder.handlers[2].call(nil)
39
+ assert_kind_of TestAdapter, @conn.builder.handlers[1].call(nil)
42
40
  end
43
41
 
44
- describe "#to_app" do
45
- it "returns rack-compatible object" do
46
- assert @conn.to_app.respond_to?(:call)
47
- end
42
+ def test_to_app_returns_rack_object
43
+ assert @conn.to_app.respond_to?(:call)
48
44
  end
49
45
  end
@@ -1,167 +1,161 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
2
 
3
3
  class TestConnection < Faraday::TestCase
4
- describe "#initialize" do
5
- it "parses @host out of given url" do
6
- conn = Faraday::Connection.new "http://sushi.com"
7
- assert_equal 'sushi.com', conn.host
8
- end
4
+ def test_initialize_parses_host_out_of_given_url
5
+ conn = Faraday::Connection.new "http://sushi.com"
6
+ assert_equal 'sushi.com', conn.host
7
+ end
9
8
 
10
- it "parses nil @port out of given url" do
11
- conn = Faraday::Connection.new "http://sushi.com"
12
- assert_nil conn.port
13
- end
9
+ def test_initialize_parses_nil_port_out_of_given_url
10
+ conn = Faraday::Connection.new "http://sushi.com"
11
+ assert_nil conn.port
12
+ end
14
13
 
15
- it "parses @scheme out of given url" do
16
- conn = Faraday::Connection.new "http://sushi.com"
17
- assert_equal 'http', conn.scheme
18
- end
14
+ def test_initialize_parses_scheme_out_of_given_url
15
+ conn = Faraday::Connection.new "http://sushi.com"
16
+ assert_equal 'http', conn.scheme
17
+ end
19
18
 
20
- it "parses @port out of given url" do
21
- conn = Faraday::Connection.new "http://sushi.com:815"
22
- assert_equal 815, conn.port
23
- end
19
+ def test_initialize_parses_port_out_of_given_url
20
+ conn = Faraday::Connection.new "http://sushi.com:815"
21
+ assert_equal 815, conn.port
22
+ end
24
23
 
25
- it "parses nil @path_prefix out of given url" do
26
- conn = Faraday::Connection.new "http://sushi.com"
27
- assert_equal '/', conn.path_prefix
28
- end
24
+ def test_initialize_parses_nil_path_prefix_out_of_given_url
25
+ conn = Faraday::Connection.new "http://sushi.com"
26
+ assert_equal '/', conn.path_prefix
27
+ end
29
28
 
30
- it "parses @path_prefix out of given url" do
31
- conn = Faraday::Connection.new "http://sushi.com/fish"
32
- assert_equal '/fish', conn.path_prefix
33
- end
29
+ def test_initialize_parses_path_prefix_out_of_given_url
30
+ conn = Faraday::Connection.new "http://sushi.com/fish"
31
+ assert_equal '/fish', conn.path_prefix
32
+ end
34
33
 
35
- it "parses @path_prefix out of given url option" do
36
- conn = Faraday::Connection.new :url => "http://sushi.com/fish"
37
- assert_equal '/fish', conn.path_prefix
38
- end
34
+ def test_initialize_parses_path_prefix_out_of_given_url_option
35
+ conn = Faraday::Connection.new :url => "http://sushi.com/fish"
36
+ assert_equal '/fish', conn.path_prefix
37
+ end
39
38
 
40
- it "stores default params from options" do
41
- conn = Faraday::Connection.new :params => {:a => 1}
42
- assert_equal 1, conn.params['a']
43
- end
39
+ def test_initialize_stores_default_params_from_options
40
+ conn = Faraday::Connection.new :params => {:a => 1}
41
+ assert_equal 1, conn.params['a']
42
+ end
44
43
 
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']
49
- end
44
+ def test_initialize_stores_default_params_from_uri
45
+ conn = Faraday::Connection.new "http://sushi.com/fish?a=1", :params => {'b' => '2'}
46
+ assert_equal '1', conn.params['a']
47
+ assert_equal '2', conn.params['b']
48
+ end
50
49
 
51
- it "stores default headers from options" do
52
- conn = Faraday::Connection.new :headers => {:a => 1}
53
- assert_equal '1', conn.headers['A']
54
- end
50
+ def test_initialize_stores_default_headers_from_options
51
+ conn = Faraday::Connection.new :headers => {:a => 1}
52
+ assert_equal '1', conn.headers['A']
55
53
  end
56
54
 
57
- describe "#build_url" do
58
- it "uses Connection#host as default URI host" do
59
- conn = Faraday::Connection.new
60
- conn.host = 'sushi.com'
61
- uri = conn.build_url("/sake.html")
62
- assert_equal 'sushi.com', uri.host
63
- end
55
+ def test_build_url_uses_connection_host_as_default_uri_host
56
+ conn = Faraday::Connection.new
57
+ conn.host = 'sushi.com'
58
+ uri = conn.build_url("/sake.html")
59
+ assert_equal 'sushi.com', uri.host
60
+ end
64
61
 
65
- it "uses Connection#port as default URI port" do
66
- conn = Faraday::Connection.new
67
- conn.port = 23
68
- uri = conn.build_url("http://sushi.com")
69
- assert_equal 23, uri.port
70
- end
62
+ def test_build_url_uses_connection_port_as_default_uri_port
63
+ conn = Faraday::Connection.new
64
+ conn.port = 23
65
+ uri = conn.build_url("http://sushi.com")
66
+ assert_equal 23, uri.port
67
+ end
71
68
 
72
- it "uses Connection#scheme as default URI scheme" do
73
- conn = Faraday::Connection.new 'http://sushi.com'
74
- uri = conn.build_url("/sake.html")
75
- assert_equal 'http', uri.scheme
76
- end
69
+ def test_build_url_uses_connection_scheme_as_default_uri_scheme
70
+ conn = Faraday::Connection.new 'http://sushi.com'
71
+ uri = conn.build_url("/sake.html")
72
+ assert_equal 'http', uri.scheme
73
+ end
77
74
 
78
- it "uses Connection#path_prefix to customize the path" do
79
- conn = Faraday::Connection.new
80
- conn.path_prefix = '/fish'
81
- uri = conn.build_url("sake.html")
82
- assert_equal '/fish/sake.html', uri.path
83
- end
75
+ def test_build_url_uses_connection_path_prefix_to_customize_path
76
+ conn = Faraday::Connection.new
77
+ conn.path_prefix = '/fish'
78
+ uri = conn.build_url("sake.html")
79
+ assert_equal '/fish/sake.html', uri.path
80
+ end
84
81
 
85
- it "uses '/' Connection#path_prefix to customize the path" do
86
- conn = Faraday::Connection.new
87
- conn.path_prefix = '/'
88
- uri = conn.build_url("sake.html")
89
- assert_equal '/sake.html', uri.path
90
- end
82
+ def test_build_url_uses_root_connection_path_prefix_to_customize_path
83
+ conn = Faraday::Connection.new
84
+ conn.path_prefix = '/'
85
+ uri = conn.build_url("sake.html")
86
+ assert_equal '/sake.html', uri.path
87
+ end
91
88
 
92
- it "forces Connection#path_prefix to be absolute" do
93
- conn = Faraday::Connection.new
94
- conn.path_prefix = 'fish'
95
- uri = conn.build_url("sake.html")
96
- assert_equal '/fish/sake.html', uri.path
97
- end
89
+ def test_build_url_forces_connection_path_prefix_to_be_absolute
90
+ conn = Faraday::Connection.new
91
+ conn.path_prefix = 'fish'
92
+ uri = conn.build_url("sake.html")
93
+ assert_equal '/fish/sake.html', uri.path
94
+ end
98
95
 
99
- it "ignores Connection#path_prefix trailing slash" do
100
- conn = Faraday::Connection.new
101
- conn.path_prefix = '/fish/'
102
- uri = conn.build_url("sake.html")
103
- assert_equal '/fish/sake.html', uri.path
104
- end
96
+ def test_build_url_ignores_connection_path_prefix_trailing_slash
97
+ conn = Faraday::Connection.new
98
+ conn.path_prefix = '/fish/'
99
+ uri = conn.build_url("sake.html")
100
+ assert_equal '/fish/sake.html', uri.path
101
+ end
105
102
 
106
- it "allows absolute URI to ignore Connection#path_prefix" do
107
- conn = Faraday::Connection.new
108
- conn.path_prefix = '/fish'
109
- uri = conn.build_url("/sake.html")
110
- assert_equal '/sake.html', uri.path
111
- end
103
+ def test_build_url_allows_absolute_uri_to_ignore_connection_path_prefix
104
+ conn = Faraday::Connection.new
105
+ conn.path_prefix = '/fish'
106
+ uri = conn.build_url("/sake.html")
107
+ assert_equal '/sake.html', uri.path
108
+ end
112
109
 
113
- it "parses url/params into #path" do
114
- conn = Faraday::Connection.new
115
- uri = conn.build_url("http://sushi.com/sake.html")
116
- assert_equal '/sake.html', uri.path
117
- end
110
+ def test_build_url_parses_url_params_into_path
111
+ conn = Faraday::Connection.new
112
+ uri = conn.build_url("http://sushi.com/sake.html")
113
+ assert_equal '/sake.html', uri.path
114
+ end
118
115
 
119
- it "parses url/params into #query" do
120
- conn = Faraday::Connection.new
121
- uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
122
- assert_equal "a%5Bb%5D=1%20%2B%202", uri.query
123
- end
116
+ def test_build_url_parses_url_params_into_query
117
+ conn = Faraday::Connection.new
118
+ uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
119
+ assert_equal "a%5Bb%5D=1%20%2B%202", uri.query
120
+ end
124
121
 
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
122
+ def test_build_url_mashes_default_and_given_params_together
123
+ conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
124
+ url = conn.build_url("nigiri?page=1", :limit => 5)
125
+ assert_match /limit=5/, url.query
126
+ assert_match /page=1/, url.query
127
+ assert_match /format=json/, url.query
128
+ assert_match /token=abc/, url.query
129
+ end
133
130
 
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
131
+ def test_build_url_overrides_default_params_with_given_params
132
+ conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
133
+ url = conn.build_url("nigiri?page=1", :limit => 5, :token => 'def', :format => 'xml')
134
+ assert_match /limit=5/, url.query
135
+ assert_match /page=1/, url.query
136
+ assert_match /format=xml/, url.query
137
+ assert_match /token=def/, url.query
138
+ assert_no_match /format=json/, url.query
139
+ assert_no_match /token=abc/, url.query
140
+ end
144
141
 
145
- it "parses url into #host" do
146
- conn = Faraday::Connection.new
147
- uri = conn.build_url("http://sushi.com/sake.html")
148
- assert_equal "sushi.com", uri.host
149
- end
142
+ def test_build_url_parses_url_into_host
143
+ conn = Faraday::Connection.new
144
+ uri = conn.build_url("http://sushi.com/sake.html")
145
+ assert_equal "sushi.com", uri.host
146
+ end
150
147
 
151
- it "parses url into #port" do
152
- conn = Faraday::Connection.new
153
- uri = conn.build_url("http://sushi.com/sake.html")
154
- assert_nil uri.port
155
- end
148
+ def test_build_url_parses_url_into_port
149
+ conn = Faraday::Connection.new
150
+ uri = conn.build_url("http://sushi.com/sake.html")
151
+ assert_nil uri.port
156
152
  end
157
153
 
158
- describe "#params_to_query" do
159
- it "converts hash of params to URI-escaped query string" do
160
- conn = Faraday::Connection.new
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')
154
+ def test_params_to_query_converts_hash_of_params_to_uri_escaped_query_string
155
+ conn = Faraday::Connection.new
156
+ class << conn
157
+ public :build_query
165
158
  end
159
+ assert_equal "a%5Bb%5D=1%20%2B%202", conn.build_query('a[b]' => '1 + 2')
166
160
  end
167
161
  end
data/test/env_test.rb CHANGED
@@ -1,35 +1,33 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
2
 
3
3
  class TestEnv < Faraday::TestCase
4
- describe "Request#create" do
5
- before :all do
6
- @conn = Faraday::Connection.new :url => 'http://sushi.com/api', :headers => {'Mime-Version' => '1.0'}
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)
4
+ def setup
5
+ @conn = Faraday::Connection.new :url => 'http://sushi.com/api', :headers => {'Mime-Version' => '1.0'}
6
+ @input = {
7
+ :body => 'abc',
8
+ :headers => {'Server' => 'Faraday'}}
9
+ @env_setup = Faraday::Request.create do |req|
10
+ req.url 'foo.json', 'a' => 1
11
+ req['Server'] = 'Faraday'
12
+ req.body = @input[:body]
16
13
  end
14
+ @env = @env_setup.to_env_hash(@conn, :get)
15
+ end
17
16
 
18
- it "stores method in :method" do
19
- assert_equal :get, @env[:method]
20
- end
17
+ def test_request_create_stores_method
18
+ assert_equal :get, @env[:method]
19
+ end
21
20
 
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
21
+ def test_request_create_stores_addressable_uri
22
+ assert_equal 'http://sushi.com/api/foo.json?a=1', @env[:url].to_s
23
+ end
25
24
 
26
- it "stores headers in :headers" do
27
- assert_kind_of Rack::Utils::HeaderHash, @env[:request_headers]
28
- assert_equal @input[:headers].merge('Mime-Version' => '1.0'), @env[:request_headers]
29
- end
25
+ def test_request_create_stores_headers
26
+ assert_kind_of Rack::Utils::HeaderHash, @env[:request_headers]
27
+ assert_equal @input[:headers].merge('Mime-Version' => '1.0'), @env[:request_headers]
28
+ end
30
29
 
31
- it "stores body in :body" do
32
- assert_equal @input[:body], @env[:body]
33
- end
30
+ def test_request_create_stores_body
31
+ assert_equal @input[:body], @env[:body]
34
32
  end
35
33
  end
data/test/helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'rubygems'
2
- require 'context'
2
+ require 'test/unit'
3
3
  if ENV['LEFTRIGHT']
4
4
  require 'leftright'
5
5
  end
@@ -21,5 +21,9 @@ module Faraday
21
21
  when nil then nil
22
22
  else 'http://localhost:4567'
23
23
  end
24
+
25
+ def test_default
26
+ assert true
27
+ end
24
28
  end
25
29
  end
@@ -1,18 +1,23 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
2
 
3
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
4
+ [:yajl, :rails_json].each do |key|
5
+ encoder = Faraday::Request.lookup_module(key)
6
+ next if !encoder.loaded?
7
+
8
+ define_method "test_encodes_json_with_#{key}" do
9
+ raw_json = create_json_connection(encoder).post('echo_body', :a => 1).body
10
+ raw_json.gsub! /: 1/, ':1' # sometimes rails_json adds a space
11
+ assert_equal %({"a":1}), raw_json
12
+ end
13
+ end
14
+
15
+ private
16
+ def create_json_connection(encoder)
17
+ Faraday::Connection.new do |b|
18
+ b.use encoder
19
+ b.adapter :test do |stub|
20
+ stub.post('echo_body') { |env| [200, {'Content-Type' => 'text/html'}, env[:body]] }
16
21
  end
17
22
  end
18
23
  end
@@ -1,45 +1,37 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
2
 
3
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} to parse json content" 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
4
+ [:yajl, :rails_json].each do |key|
5
+ encoder = Faraday::Response.lookup_module(key)
6
+ next if !encoder.loaded?
7
+
8
+ define_method "test_uses_#{key}_to_parse_json_content" do
9
+ response = create_json_connection(encoder).get('json')
10
+ assert response.success?
11
+ assert_equal [1,2,3], response.body
12
+ end
19
13
 
20
- it "uses #{parser} to skip blank content" do
21
- @connection = Faraday::Connection.new do |b|
22
- b.adapter :test do |stub|
23
- stub.get('blank') { [200, {'Content-Type' => 'text/html'}, ''] }
24
- end
25
- b.use parser
26
- end
27
- response = @connection.get('blank')
28
- assert response.success?
29
- assert_equal nil, response.body
30
- end
14
+ define_method "test_uses_#{key}_to_skip_blank_content" do
15
+ response = create_json_connection(encoder).get('blank')
16
+ assert response.success?
17
+ assert !response.body
18
+ end
19
+
20
+ define_method "test_uses_#{key}_to_skip_nil_content" do
21
+ response = create_json_connection(encoder).get('nil')
22
+ assert response.success?
23
+ assert !response.body
24
+ end
25
+ end
31
26
 
32
- it "uses #{parser} to skip nil content" do
33
- @connection = Faraday::Connection.new do |b|
34
- b.adapter :test do |stub|
35
- stub.get('nil') { [200, {'Content-Type' => 'text/html'}, nil] }
36
- end
37
- b.use parser
38
- end
39
- response = @connection.get('nil')
40
- assert response.success?
41
- assert_equal nil, response.body
27
+ def create_json_connection(encoder)
28
+ Faraday::Connection.new do |b|
29
+ b.adapter :test do |stub|
30
+ stub.get('json') { [200, {'Content-Type' => 'text/html'}, "[1,2,3]"] }
31
+ stub.get('blank') { [200, {'Content-Type' => 'text/html'}, ''] }
32
+ stub.get('nil') { [200, {'Content-Type' => 'text/html'}, nil] }
42
33
  end
34
+ b.use encoder
43
35
  end
44
36
  end
45
37
  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.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - rick
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-06 00:00:00 -08:00
12
+ date: 2010-02-18 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency