faraday_middleware 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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(faraday_env(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,221 +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
- if defined?(Faraday::Env) && !env.is_a?(Faraday::Env)
204
- raise "expected Faraday::Env, got #{env.class}"
205
- end
206
- app.call(env)
207
- end
208
- end
209
-
210
- private
211
-
212
- def connection(options = middleware_options)
213
- Faraday.new do |c|
214
- c.use described_class, options
215
- c.use Lint
216
- c.adapter :test do |stub|
217
- yield(stub) if block_given?
218
- end
219
- end
220
- end
221
- end
@@ -1,59 +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.printf "%.2f", result.source_files.covered_percent
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
- require 'faraday'
21
-
22
- module EnvCompatibility
23
- def faraday_env(env)
24
- if defined?(Faraday::Env)
25
- Faraday::Env.from(env)
26
- else
27
- env
28
- end
29
- end
30
- end
31
-
32
- module ResponseMiddlewareExampleGroup
33
- def self.included(base)
34
- base.let(:options) { Hash.new }
35
- base.let(:headers) { Hash.new }
36
- base.let(:middleware) {
37
- described_class.new(lambda {|env|
38
- Faraday::Response.new(env)
39
- }, options)
40
- }
41
- end
42
-
43
- def process(body, content_type = nil, options = {})
44
- env = {
45
- :body => body, :request => options,
46
- :response_headers => Faraday::Utils::Headers.new(headers)
47
- }
48
- env[:response_headers]['content-type'] = content_type if content_type
49
- middleware.call(faraday_env(env))
50
- end
51
- end
52
-
53
- RSpec.configure do |config|
54
- config.include EnvCompatibility
55
- config.include ResponseMiddlewareExampleGroup, :type => :response
56
- config.expect_with :rspec do |c|
57
- c.syntax = :expect
58
- end
59
- end
@@ -1,70 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/response/mashify'
3
-
4
- describe FaradayMiddleware::Mashify do
5
- MyMash = Struct.new(:body)
6
-
7
- context "when used", :type => :response do
8
- it "creates a Hashie::Mash from the body" do
9
- body = { "name" => "Erik Michaels-Ober", "username" => "sferik" }
10
- me = process(body).body
11
- expect(me.name).to eq("Erik Michaels-Ober")
12
- expect(me.username).to eq("sferik")
13
- end
14
-
15
- it "handles strings" do
16
- body = "Most amazing string EVER"
17
- me = process(body).body
18
- expect(me).to eq("Most amazing string EVER")
19
- end
20
-
21
- it "handles arrays" do
22
- body = [123, 456]
23
- values = process(body).body
24
- expect(values).to eq([123, 456])
25
- end
26
-
27
- it "handles arrays of hashes" do
28
- body = [{ "username" => "sferik" }, { "username" => "pengwynn" }]
29
- us = process(body).body
30
- expect(us.first.username).to eq('sferik')
31
- expect(us.last.username).to eq('pengwynn')
32
- end
33
-
34
- it "handles nested arrays of hashes" do
35
- body = [[{ "username" => "sferik" }, { "username" => "pengwynn" }]]
36
- us = process(body).body.first
37
- expect(us.first.username).to eq('sferik')
38
- expect(us.last.username).to eq('pengwynn')
39
- end
40
-
41
- it "handles mixed arrays" do
42
- body = [123, { "username" => "sferik" }, 456]
43
- values = process(body).body
44
- expect(values.first).to eq(123)
45
- expect(values.last).to eq(456)
46
- expect(values[1].username).to eq('sferik')
47
- end
48
-
49
- it "allows for use of custom Mash subclasses at the class level" do
50
- original_class = described_class.mash_class
51
- described_class.mash_class = MyMash
52
-
53
- begin
54
- me = process({}).body
55
- expect(me).to be_instance_of(MyMash)
56
- ensure
57
- described_class.mash_class = original_class
58
- end
59
- end
60
- end
61
-
62
- context "custom mash subclass", :type => :response do
63
- let(:options) { {:mash_class => MyMash} }
64
-
65
- it "instance level" do
66
- me = process({}).body
67
- expect(me).to be_instance_of(MyMash)
68
- end
69
- end
70
- end
@@ -1,92 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/request/method_override'
3
-
4
- describe FaradayMiddleware::MethodOverride do
5
-
6
- let(:middleware) { described_class.new(lambda {|env| env }, *options) }
7
- let(:env) { middleware.call faraday_env(request_env(request_method)) }
8
-
9
- def request_env(method)
10
- { :method => method,
11
- :request_headers => Faraday::Utils::Headers.new
12
- }
13
- end
14
-
15
- shared_examples "overrides method" do |method|
16
- it "sets physical method to POST" do
17
- expect(env[:method]).to eq(:post)
18
- end
19
-
20
- it "sets header to #{method}" do
21
- expect(env[:request_headers]['X-Http-Method-Override']).to eq(method)
22
- end
23
- end
24
-
25
- shared_examples "doesn't override method" do |method|
26
- it "keeps original method" do
27
- expect(env[:method]).to eq(method)
28
- end
29
-
30
- it "doesn't set header value" do
31
- expect(env[:request_headers]).not_to have_key('X-Http-Method-Override')
32
- end
33
-
34
- end
35
-
36
- context "with default options" do
37
- let(:options) { nil }
38
-
39
- context "GET" do
40
- let(:request_method) { :get }
41
- include_examples "doesn't override method", :get
42
- end
43
-
44
- context "POST" do
45
- let(:request_method) { :post }
46
- include_examples "doesn't override method", :post
47
- end
48
-
49
- context "PUT" do
50
- let(:request_method) { :put }
51
- include_examples "overrides method", 'PUT'
52
- end
53
- end
54
-
55
- context "configured to rewrite [:patch, :delete]" do
56
- let(:options) { [{ :rewrite => [:patch, :delete] }] }
57
-
58
- context "PUT" do
59
- let(:request_method) { :put }
60
- include_examples "doesn't override method", :put
61
- end
62
-
63
- context "PATCH" do
64
- let(:request_method) { :patch }
65
- include_examples "overrides method", 'PATCH'
66
- end
67
-
68
- context "DELETE" do
69
- let(:request_method) { :delete }
70
- include_examples "overrides method", 'DELETE'
71
- end
72
- end
73
-
74
- context "configured to rewrite ['PATCH']" do
75
- let(:options) { [{ :rewrite => %w[PATCH] }] }
76
-
77
- context "PATCH" do
78
- let(:request_method) { :patch }
79
- include_examples "overrides method", 'PATCH'
80
- end
81
- end
82
-
83
- context "with invalid option" do
84
- let(:options) { [{ :hello => 'world' }] }
85
- let(:request_method) { :get }
86
-
87
- it "raises key error" do
88
- expect{ env }.to raise_error(IndexError, /key [\s\w]*not found/)
89
- end
90
- end
91
-
92
- end