faraday_middleware 0.9.0 → 0.9.2

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/.travis.yml DELETED
@@ -1,10 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - rbx-18mode
4
- - rbx-19mode
5
- - jruby-18mode
6
- - jruby-19mode
7
- - 1.8.7
8
- - 1.9.2
9
- - 1.9.3
10
- - ruby-head
data/Gemfile DELETED
@@ -1,11 +0,0 @@
1
- source 'http://rubygems.org'
2
-
3
- platforms :mri_19 do
4
- gem 'simplecov'
5
- gem 'cane', '~> 2.2.2'
6
- end
7
-
8
- gem 'json', :platforms => [:ruby_18, :jruby]
9
- gem 'jruby-openssl', '~> 0.7', :platforms => :jruby
10
-
11
- gemspec
data/Rakefile DELETED
@@ -1,29 +0,0 @@
1
- if defined? RUBY_ENGINE and 'ruby' == RUBY_ENGINE and RUBY_VERSION.index('1.9') == 0
2
- task :default => [:enable_coverage, :spec, :test, :quality]
3
- else
4
- task :default => [:spec, :test]
5
- end
6
-
7
- require 'bundler'
8
- Bundler::GemHelper.install_tasks
9
-
10
- require 'rspec/core/rake_task'
11
- RSpec::Core::RakeTask.new(:spec)
12
-
13
- task :enable_coverage do
14
- ENV['COVERAGE'] = 'yes'
15
- end
16
-
17
- desc %(Run Test::Unit tests)
18
- task :test do
19
- sh 'ruby', '-Ilib', 'spec/caching_test.rb'
20
- end
21
-
22
- desc %(Check code quality metrics with Cane)
23
- task :quality do
24
- sh 'cane',
25
- '--abc-max=15',
26
- '--style-measure=110',
27
- '--gte=coverage/covered_percent,99',
28
- '--max-violations=0'
29
- end
data/spec/caching_test.rb DELETED
@@ -1,155 +0,0 @@
1
- require 'test/unit'
2
- require 'forwardable'
3
- require 'fileutils'
4
- require 'rack/cache'
5
- require 'faraday'
6
- require 'faraday_middleware/response/caching'
7
- require 'faraday_middleware/rack_compatible'
8
-
9
- class CachingTest < Test::Unit::TestCase
10
- class TestCache < Hash
11
- def read(key)
12
- if cached = self[key]
13
- Marshal.load(cached)
14
- end
15
- end
16
-
17
- def write(key, data)
18
- self[key] = Marshal.dump(data)
19
- end
20
-
21
- def fetch(key)
22
- read(key) || yield.tap { |data| write(key, data) }
23
- end
24
- end
25
-
26
- class Lint < Struct.new(:app)
27
- def call(env)
28
- app.call(env).on_complete do
29
- raise "no headers" unless env[:response_headers].is_a? Hash
30
- raise "no response" unless env[:response].is_a? Faraday::Response
31
- raise "env not identical" unless env[:response].env.object_id == env.object_id
32
- end
33
- end
34
- end
35
-
36
- def setup
37
- @cache = TestCache.new
38
-
39
- request_count = 0
40
- response = lambda { |env|
41
- [200, {'Content-Type' => 'text/plain'}, "request:#{request_count+=1}"]
42
- }
43
-
44
- @conn = Faraday.new do |b|
45
- b.use Lint
46
- b.use FaradayMiddleware::Caching, @cache
47
- b.adapter :test do |stub|
48
- stub.get('/', &response)
49
- stub.get('/?foo=bar', &response)
50
- stub.post('/', &response)
51
- stub.get('/other', &response)
52
- end
53
- end
54
- end
55
-
56
- extend Forwardable
57
- def_delegators :@conn, :get, :post
58
-
59
- def test_cache_get
60
- assert_equal 'request:1', get('/').body
61
- assert_equal 'request:1', get('/').body
62
- assert_equal 'request:2', get('/other').body
63
- assert_equal 'request:2', get('/other').body
64
- end
65
-
66
- def test_response_has_request_params
67
- get('/') # make cache
68
- response = get('/')
69
- assert_equal :get, response.env[:method]
70
- assert_equal '/', response.env[:url].request_uri
71
- end
72
-
73
- def test_cache_query_params
74
- assert_equal 'request:1', get('/').body
75
- assert_equal 'request:2', get('/?foo=bar').body
76
- assert_equal 'request:2', get('/?foo=bar').body
77
- assert_equal 'request:1', get('/').body
78
- end
79
-
80
- def test_doesnt_cache_post
81
- assert_equal 'request:1', post('/').body
82
- assert_equal 'request:2', post('/').body
83
- assert_equal 'request:3', post('/').body
84
- end
85
- end
86
-
87
- # RackCompatible + Rack::Cache
88
- class HttpCachingTest < Test::Unit::TestCase
89
- include FileUtils
90
-
91
- CACHE_DIR = File.expand_path('../../tmp/cache', __FILE__)
92
-
93
- # middleware to check whether "rack.errors" is free of error reports
94
- class RackErrorsComplainer < Struct.new(:app)
95
- def call(env)
96
- response = app.call(env)
97
- error_stream = env['rack.errors'].string
98
- raise %(unexpected error in 'rack.errors') if error_stream.include? 'error'
99
- response
100
- end
101
- end
102
-
103
- def setup
104
- rm_r CACHE_DIR if File.exists? CACHE_DIR
105
- # force reinitializing cache dirs
106
- Rack::Cache::Storage.instance.clear
107
-
108
- request_count = 0
109
- response = lambda { |env|
110
- [200, { 'Content-Type' => 'text/plain',
111
- 'Cache-Control' => 'public, max-age=900',
112
- },
113
- "request:#{request_count+=1}"]
114
- }
115
-
116
- @conn = Faraday.new do |b|
117
- b.use RackErrorsComplainer
118
-
119
- b.use FaradayMiddleware::RackCompatible, Rack::Cache::Context,
120
- :metastore => "file:#{CACHE_DIR}/rack/meta",
121
- :entitystore => "file:#{CACHE_DIR}/rack/body",
122
- :verbose => true
123
-
124
- b.adapter :test do |stub|
125
- stub.get('/', &response)
126
- stub.post('/', &response)
127
- end
128
- end
129
- end
130
-
131
- extend Forwardable
132
- def_delegators :@conn, :get, :post
133
-
134
- def test_cache_get
135
- response = get('/', :user_agent => 'test')
136
- assert_equal 'request:1', response.body
137
- assert_equal :get, response.env[:method]
138
- assert_equal 200, response.status
139
-
140
- response = get('/', :user_agent => 'test')
141
- assert_equal 'request:1', response.body
142
- assert_equal 'text/plain', response['content-type']
143
- assert_equal :get, response.env[:method]
144
- assert response.env[:request].respond_to?(:fetch)
145
- assert_equal 200, response.status
146
-
147
- assert_equal 'request:2', post('/').body
148
- end
149
-
150
- def test_doesnt_cache_post
151
- assert_equal 'request:1', get('/').body
152
- assert_equal 'request:2', post('/').body
153
- assert_equal 'request:3', post('/').body
154
- end
155
- end unless defined? RUBY_ENGINE and "rbx" == RUBY_ENGINE # rbx bug #1522
data/spec/chunked_spec.rb DELETED
@@ -1,78 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/response/chunked'
3
-
4
- describe FaradayMiddleware::Chunked, :type => :response do
5
- context "no transfer-encoding" do
6
- it "doesn't change nil body" do
7
- expect(process(nil).body).to be_nil
8
- end
9
-
10
- it "doesn't change an empty body" do
11
- expect(process('').body).to eq('')
12
- end
13
-
14
- it "doesn't change a normal body" do
15
- expect(process('asdf').body).to eq('asdf')
16
- end
17
- end
18
-
19
- context "transfer-encoding gzip" do
20
- let(:headers) { {"transfer-encoding" => "gzip"}}
21
-
22
- it "doesn't change nil body" do
23
- expect(process(nil).body).to be_nil
24
- end
25
-
26
- it "doesn't change an empty body" do
27
- expect(process('').body).to eq('')
28
- end
29
-
30
- it "doesn't change a normal body" do
31
- expect(process('asdf').body).to eq('asdf')
32
- end
33
- end
34
-
35
- context "transfer-encoding chunked" do
36
- let(:headers) { {"transfer-encoding" => "chunked"}}
37
-
38
- it "doesn't change nil body" do
39
- expect(process(nil).body).to be_nil
40
- end
41
-
42
- it "doesn't change an empty body" do
43
- expect(process('').body).to eq('')
44
- end
45
-
46
- it "parses a basic chunked body" do
47
- expect(process("10\r\nasdfghjklasdfghj\r\n0\r\n").body).to eq('asdfghjklasdfghj')
48
- end
49
-
50
- it "parses a chunked body with no ending chunk" do
51
- expect(process("10\r\nasdfghjklasdfghj\r\n").body).to eq('asdfghjklasdfghj')
52
- end
53
-
54
- it "parses a chunked body with no trailing CRLF on the data chunk" do
55
- expect(process("10\r\nasdfghjklasdfghj0\r\n").body).to eq('asdfghjklasdfghj')
56
- end
57
-
58
- it "parses a chunked body with an extension" do
59
- expect(process("10;foo=bar\r\nasdfghjklasdfghj\r\n0\r\n").body).to eq('asdfghjklasdfghj')
60
- end
61
-
62
- it "parses a chunked body with two extensions" do
63
- expect(process("10;foo=bar;bar=baz\r\nasdfghjklasdfghj\r\n0\r\n").body).to eq('asdfghjklasdfghj')
64
- end
65
-
66
- it "parses a chunked body with two chunks" do
67
- expect(process("8\r\nasdfghjk\r\n8\r\nlasdfghj\r\n0\r\n").body).to eq('asdfghjklasdfghj')
68
- end
69
- end
70
-
71
- context "transfer-encoding chunked,chunked" do
72
- let(:headers) { {"transfer-encoding" => "chunked,chunked"}}
73
-
74
- it "parses a basic chunked body" do
75
- expect(process("10\r\nasdfghjklasdfghj\r\n0\r\n").body).to eq('asdfghjklasdfghj')
76
- end
77
- end
78
- end
@@ -1,95 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/request/encode_json'
3
-
4
- describe FaradayMiddleware::EncodeJson do
5
- let(:middleware) { described_class.new(lambda{|env| env}) }
6
-
7
- def process(body, content_type = nil)
8
- env = {:body => body, :request_headers => Faraday::Utils::Headers.new}
9
- env[:request_headers]['content-type'] = content_type if content_type
10
- middleware.call(env)
11
- end
12
-
13
- def result_body() result[:body] end
14
- def result_type() result[:request_headers]['content-type'] end
15
-
16
- context "no body" do
17
- let(:result) { process(nil) }
18
-
19
- it "doesn't change body" do
20
- expect(result_body).to be_nil
21
- end
22
-
23
- it "doesn't add content type" do
24
- expect(result_type).to be_nil
25
- end
26
- end
27
-
28
- context "empty body" do
29
- let(:result) { process('') }
30
-
31
- it "doesn't change body" do
32
- expect(result_body).to be_empty
33
- end
34
-
35
- it "doesn't add content type" do
36
- expect(result_type).to be_nil
37
- end
38
- end
39
-
40
- context "string body" do
41
- let(:result) { process('{"a":1}') }
42
-
43
- it "doesn't change body" do
44
- expect(result_body).to eq('{"a":1}')
45
- end
46
-
47
- it "adds content type" do
48
- expect(result_type).to eq('application/json')
49
- end
50
- end
51
-
52
- context "object body" do
53
- let(:result) { process({:a => 1}) }
54
-
55
- it "encodes body" do
56
- expect(result_body).to eq('{"a":1}')
57
- end
58
-
59
- it "adds content type" do
60
- expect(result_type).to eq('application/json')
61
- end
62
- end
63
-
64
- context "empty object body" do
65
- let(:result) { process({}) }
66
-
67
- it "encodes body" do
68
- expect(result_body).to eq('{}')
69
- end
70
- end
71
-
72
- context "object body with json type" do
73
- let(:result) { process({:a => 1}, 'application/json; charset=utf-8') }
74
-
75
- it "encodes body" do
76
- expect(result_body).to eq('{"a":1}')
77
- end
78
-
79
- it "doesn't change content type" do
80
- expect(result_type).to eq('application/json; charset=utf-8')
81
- end
82
- end
83
-
84
- context "object body with incompatible type" do
85
- let(:result) { process({:a => 1}, 'application/xml; charset=utf-8') }
86
-
87
- it "doesn't change body" do
88
- expect(result_body).to eq({:a => 1})
89
- end
90
-
91
- it "doesn't change content type" do
92
- expect(result_type).to eq('application/xml; charset=utf-8')
93
- end
94
- end
95
- end
@@ -1,218 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/response/follow_redirects'
3
- require 'faraday'
4
-
5
- # expose a method in Test adapter that should have been public
6
- Faraday::Adapter::Test::Stubs.class_eval { public :new_stub }
7
-
8
- describe FaradayMiddleware::FollowRedirects do
9
- let(:middleware_options) { Hash.new }
10
-
11
- shared_examples_for "a successful redirection" do |status_code|
12
- it "follows the redirection for a GET request" do
13
- expect(connection do |stub|
14
- stub.get('/permanent') { [status_code, {'Location' => '/found'}, ''] }
15
- stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, 'fin'] }
16
- end.get('/permanent').body).to eq 'fin'
17
- end
18
-
19
- it "follows the redirection for a HEAD request" do
20
- expect(connection do |stub|
21
- stub.head('/permanent') { [status_code, {'Location' => '/found'}, ''] }
22
- stub.head('/found') { [200, {'Content-Type' => 'text/plain'}, ''] }
23
- end.head('/permanent').status).to eq 200
24
- end
25
-
26
- it "follows the redirection for a OPTIONS request" do
27
- expect(connection do |stub|
28
- stub.new_stub(:options, '/permanent') { [status_code, {'Location' => '/found'}, ''] }
29
- stub.new_stub(:options, '/found') { [200, {'Content-Type' => 'text/plain'}, ''] }
30
- end.run_request(:options, '/permanent', nil, nil).status).to eq 200
31
- end
32
- end
33
-
34
- shared_examples_for "a forced GET redirection" do |status_code|
35
- [:put, :post, :delete, :patch].each do |method|
36
- it "a #{method.to_s.upcase} request is converted to a GET" do
37
- expect(connection do |stub|
38
- stub.new_stub(method, '/redirect') {
39
- [status_code, {'Location' => '/found'}, 'elsewhere']
40
- }
41
- stub.get('/found') { |env|
42
- body = env[:body] and body.empty? && (body = nil)
43
- [200, {'Content-Type' => 'text/plain'}, body.inspect]
44
- }
45
- end.run_request(method, '/redirect', 'request data', nil).body).to eq('nil')
46
- end
47
- end
48
- end
49
-
50
- shared_examples_for "a replayed redirection" do |status_code|
51
- it "redirects with the original request headers" do
52
- conn = connection do |stub|
53
- stub.get('/redirect') {
54
- [status_code, {'Location' => '/found'}, '']
55
- }
56
- stub.get('/found') { |env|
57
- [200, {'Content-Type' => 'text/plain'}, env[:request_headers]['X-Test-Value']]
58
- }
59
- end
60
-
61
- response = conn.get('/redirect') { |req|
62
- req.headers['X-Test-Value'] = 'success'
63
- }
64
-
65
- expect(response.body).to eq('success')
66
- end
67
-
68
- [:put, :post, :delete, :patch].each do |method|
69
- it "replays a #{method.to_s.upcase} request" do
70
- expect(connection do |stub|
71
- stub.new_stub(method, '/redirect') { [status_code, {'Location' => '/found'}, ''] }
72
- stub.new_stub(method, '/found') { [200, {'Content-Type' => 'text/plain'}, 'fin'] }
73
- end.run_request(method, '/redirect', nil, nil).body).to eq 'fin'
74
- end
75
- end
76
-
77
- [:put, :post, :patch].each do |method|
78
- it "forwards request body for a #{method.to_s.upcase} request" do
79
- conn = connection do |stub|
80
- stub.new_stub(method, '/redirect') {
81
- [status_code, {'Location' => '/found'}, '']
82
- }
83
- stub.new_stub(method, '/found') { |env|
84
- [200, {'Content-Type' => 'text/plain'}, env[:body]]
85
- }
86
- end
87
-
88
- response = conn.run_request(method, '/redirect', 'original data', nil)
89
- expect(response.body).to eq('original data')
90
- end
91
- end
92
- end
93
-
94
-
95
- it "returns non-redirect response results" do
96
- expect(connection do |stub|
97
- stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, 'fin'] }
98
- end.get('/found').body).to eq 'fin'
99
- end
100
-
101
- it "follows a single redirection" do
102
- expect(connection do |stub|
103
- stub.get('/') { [301, {'Location' => '/found'}, ''] }
104
- stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, 'fin'] }
105
- end.get('/').body).to eq 'fin'
106
- end
107
-
108
- it "follows many redirections" do
109
- expect(connection do |stub|
110
- stub.get('/') { [301, {'Location' => '/redirect1'}, ''] }
111
- stub.get('/redirect1') { [301, {'Location' => '/redirect2'}, ''] }
112
- stub.get('/redirect2') { [301, {'Location' => '/found'}, ''] }
113
- stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, 'fin'] }
114
- end.get('/').body).to eq 'fin'
115
- end
116
-
117
- it "raises a FaradayMiddleware::RedirectLimitReached after 3 redirections (by default)" do
118
- conn = connection do |stub|
119
- stub.get('/') { [301, {'Location' => '/redirect1'}, ''] }
120
- stub.get('/redirect1') { [301, {'Location' => '/redirect2'}, ''] }
121
- stub.get('/redirect2') { [301, {'Location' => '/redirect3'}, ''] }
122
- stub.get('/redirect3') { [301, {'Location' => '/found'}, ''] }
123
- stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, 'fin'] }
124
- end
125
-
126
- expect{ conn.get('/') }.to raise_error(FaradayMiddleware::RedirectLimitReached)
127
- end
128
-
129
- it "raises a FaradayMiddleware::RedirectLimitReached after the initialized limit" do
130
- conn = connection(:limit => 1) do |stub|
131
- stub.get('/') { [301, {'Location' => '/redirect1'}, ''] }
132
- stub.get('/redirect1') { [301, {'Location' => '/found'}, ''] }
133
- stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, 'fin'] }
134
- end
135
-
136
- expect{ conn.get('/') }.to raise_error(FaradayMiddleware::RedirectLimitReached)
137
- end
138
-
139
- context "when cookies option" do
140
-
141
- let(:cookies) { 'cookie1=abcdefg; cookie2=1234567; cookie3=awesome' }
142
-
143
- context "is :all" do
144
- it "puts all cookies from the response into the next request" do
145
- expect(connection(:cookies => :all) do |stub|
146
- stub.get('/') { [301, {'Location' => '/found', 'Cookies' => cookies }, ''] }
147
- stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, ''] }
148
- end.get('/').env[:request_headers][:cookies]).to eq(cookies)
149
- end
150
-
151
- it "not set cookies header on request when response has no cookies" do
152
- expect(connection(:cookies => :all) do |stub|
153
- stub.get('/') { [301, {'Location' => '/found'}, ''] }
154
- stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, ''] }
155
- end.get('/').env[:request_headers].has_key?('Cookies')).to eq(false)
156
- end
157
- end
158
-
159
- context "is an array of cookie names" do
160
- it "puts selected cookies from the response into the next request" do
161
- expect(connection(:cookies => ['cookie2']) do |stub|
162
- stub.get('/') { [301, {'Location' => '/found', 'Cookies' => cookies }, ''] }
163
- stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, ''] }
164
- end.get('/').env[:request_headers][:cookies]).to eq('cookie2=1234567')
165
- end
166
- end
167
- end
168
-
169
- context "for an HTTP 301 response" do
170
- it_behaves_like 'a successful redirection', 301
171
- it_behaves_like 'a forced GET redirection', 301
172
- end
173
-
174
- context "for an HTTP 302 response" do
175
- it_behaves_like 'a successful redirection', 302
176
-
177
- context "by default" do
178
- it_behaves_like 'a forced GET redirection', 302
179
- end
180
-
181
- context "with standards compliancy enabled" do
182
- let(:middleware_options) { { :standards_compliant => true } }
183
- it_behaves_like 'a replayed redirection', 302
184
- end
185
- end
186
-
187
- context "for an HTTP 303 response" do
188
- it_behaves_like 'a successful redirection', 303
189
- it_behaves_like 'a forced GET redirection', 303
190
- end
191
-
192
- context "for an HTTP 307 response" do
193
- it_behaves_like 'a successful redirection', 307
194
- it_behaves_like 'a replayed redirection', 307
195
- end
196
-
197
- # checks env hash in request phase for basic validity
198
- class Lint < Struct.new(:app)
199
- def call(env)
200
- if env[:status] or env[:response] or env[:response_headers]
201
- raise "invalid request: #{env.inspect}"
202
- end
203
- app.call(env)
204
- end
205
- end
206
-
207
- private
208
-
209
- def connection(options = middleware_options)
210
- Faraday.new do |c|
211
- c.use described_class, options
212
- c.use Lint
213
- c.adapter :test do |stub|
214
- yield(stub) if block_given?
215
- end
216
- end
217
- end
218
- end
data/spec/helper.rb DELETED
@@ -1,47 +0,0 @@
1
- if ENV['COVERAGE']
2
- require 'simplecov'
3
-
4
- SimpleCov.formatter = Class.new do
5
- def format(result)
6
- SimpleCov::Formatter::HTMLFormatter.new.format(result) unless ENV['CI']
7
- File.open('coverage/covered_percent', 'w') do |f|
8
- f.puts result.source_files.covered_percent.to_i
9
- end
10
- end
11
- end
12
-
13
- SimpleCov.start do
14
- # add_filter 'faraday_middleware.rb'
15
- add_filter 'backwards_compatibility.rb'
16
- end
17
- end
18
-
19
- require 'rspec'
20
-
21
- module ResponseMiddlewareExampleGroup
22
- def self.included(base)
23
- base.let(:options) { Hash.new }
24
- base.let(:headers) { Hash.new }
25
- base.let(:middleware) {
26
- described_class.new(lambda {|env|
27
- Faraday::Response.new(env)
28
- }, options)
29
- }
30
- end
31
-
32
- def process(body, content_type = nil, options = {})
33
- env = {
34
- :body => body, :request => options,
35
- :response_headers => Faraday::Utils::Headers.new(headers)
36
- }
37
- env[:response_headers]['content-type'] = content_type if content_type
38
- middleware.call(env)
39
- end
40
- end
41
-
42
- RSpec.configure do |config|
43
- config.include ResponseMiddlewareExampleGroup, :type => :response
44
- config.expect_with :rspec do |c|
45
- c.syntax = :expect
46
- end
47
- end