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/spec/mashify_spec.rb DELETED
@@ -1,99 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/response/mashify'
3
-
4
- describe FaradayMiddleware::Mashify do
5
- context "during configuration" do
6
- it "allows for a custom Mash class to be set" do
7
- expect(described_class).to respond_to(:mash_class)
8
- expect(described_class).to respond_to(:mash_class=)
9
- end
10
- end
11
-
12
- context "when used" do
13
- before(:each) { described_class.mash_class = ::Hashie::Mash }
14
- let(:mashify) { described_class.new }
15
-
16
- it "creates a Hashie::Mash from the body" do
17
- env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
18
- me = mashify.on_complete(env)
19
- expect(me.class).to eq(Hashie::Mash)
20
- end
21
-
22
- it "handles strings" do
23
- env = { :body => "Most amazing string EVER" }
24
- me = mashify.on_complete(env)
25
- expect(me).to eq("Most amazing string EVER")
26
- end
27
-
28
- it "handles arrays" do
29
- env = { :body => [123, 456] }
30
- values = mashify.on_complete(env)
31
- expect(values.first).to eq(123)
32
- expect(values.last).to eq(456)
33
- end
34
-
35
- it "handles arrays of hashes" do
36
- env = { :body => [{ "username" => "sferik" }, { "username" => "pengwynn" }] }
37
- us = mashify.on_complete(env)
38
- expect(us.first.username).to eq('sferik')
39
- expect(us.last.username).to eq('pengwynn')
40
- end
41
-
42
- it "handles nested arrays of hashes" do
43
- env = { :body => [[{ "username" => "sferik" }, { "username" => "pengwynn" }]] }
44
- us = mashify.on_complete(env)
45
- expect(us.first.first.username).to eq('sferik')
46
- expect(us.first.last.username).to eq('pengwynn')
47
- end
48
-
49
- it "handles mixed arrays" do
50
- env = { :body => [123, { "username" => "sferik" }, 456] }
51
- values = mashify.on_complete(env)
52
- expect(values.first).to eq(123)
53
- expect(values.last).to eq(456)
54
- expect(values[1].username).to eq('sferik')
55
- end
56
-
57
- it "allows for use of custom Mash subclasses at the class level" do
58
- class MyMash < ::Hashie::Mash; end
59
- described_class.mash_class = MyMash
60
-
61
- env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
62
- me = mashify.on_complete(env)
63
-
64
- expect(me.class).to eq(MyMash)
65
- end
66
-
67
- it "allows for use of custom Mash subclasses at the instance level" do
68
- class MyMash < ::Hashie::Mash; end
69
- mashify = described_class.new(nil, :mash_class => MyMash)
70
-
71
- env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
72
- me = mashify.on_complete(env)
73
-
74
- expect(me.class).to eq(MyMash)
75
- end
76
- end
77
-
78
- context "integration test" do
79
- let(:stubs) { Faraday::Adapter::Test::Stubs.new }
80
- let(:connection) do
81
- Faraday::Connection.new do |builder|
82
- builder.adapter :test, stubs
83
- builder.use described_class
84
- end
85
- end
86
-
87
- # although it is not good practice to pass a hash as the body, if we add ParseJson
88
- # to the middleware stack we end up testing two middlewares instead of one
89
- it "creates a Hash from the body" do
90
- stubs.get('/hash') {
91
- data = { 'name' => 'Erik Michaels-Ober', 'username' => 'sferik' }
92
- [200, {'content-type' => 'application/json; charset=utf-8'}, data]
93
- }
94
- me = connection.get('/hash').body
95
- expect(me.name).to eq('Erik Michaels-Ober')
96
- expect(me.username).to eq('sferik')
97
- end
98
- end
99
- 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 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 not found/)
89
- end
90
- end
91
-
92
- end
data/spec/oauth2_spec.rb DELETED
@@ -1,118 +0,0 @@
1
- require 'helper'
2
- require 'uri'
3
- require 'faraday_middleware/request/oauth2'
4
- require 'faraday/utils'
5
-
6
- describe FaradayMiddleware::OAuth2 do
7
-
8
- def query_params(env)
9
- Faraday::Utils.parse_query env[:url].query
10
- end
11
-
12
- def auth_header(env)
13
- env[:request_headers]['Authorization']
14
- end
15
-
16
- def perform(params = {}, headers = {})
17
- env = {
18
- :url => URI('http://example.com/?' + Faraday::Utils.build_query(params)),
19
- :request_headers => Faraday::Utils::Headers.new.update(headers)
20
- }
21
- app = make_app
22
- app.call(env)
23
- end
24
-
25
- def make_app
26
- described_class.new(lambda{|env| env}, *Array(options))
27
- end
28
-
29
- context "no token configured" do
30
- let(:options) { nil }
31
-
32
- it "doesn't add params" do
33
- request = perform(:q => 'hello')
34
- expect(query_params(request)).to eq('q' => 'hello')
35
- end
36
-
37
- it "doesn't add headers" do
38
- expect(auth_header(perform)).to be_nil
39
- end
40
-
41
- it "creates header for explicit token" do
42
- request = perform(:q => 'hello', :access_token => 'abc123')
43
- expect(query_params(request)).to eq('q' => 'hello', 'access_token' => 'abc123')
44
- expect(auth_header(request)).to eq(%(Token token="abc123"))
45
- end
46
- end
47
-
48
- context "default token configured" do
49
- let(:options) { 'XYZ' }
50
-
51
- it "adds token param" do
52
- expect(query_params(perform(:q => 'hello'))).to eq('q' => 'hello', 'access_token' => 'XYZ')
53
- end
54
-
55
- it "adds token header" do
56
- expect(auth_header(perform)).to eq(%(Token token="XYZ"))
57
- end
58
-
59
- it "overrides default with explicit token" do
60
- request = perform(:q => 'hello', :access_token => 'abc123')
61
- expect(query_params(request)).to eq('q' => 'hello', 'access_token' => 'abc123')
62
- expect(auth_header(request)).to eq(%(Token token="abc123"))
63
- end
64
-
65
- it "clears default with empty explicit token" do
66
- request = perform(:q => 'hello', :access_token => nil)
67
- expect(query_params(request)).to eq('q' => 'hello', 'access_token' => nil)
68
- expect(auth_header(request)).to be_nil
69
- end
70
- end
71
-
72
- context "existing Authorization header" do
73
- let(:options) { 'XYZ' }
74
- subject { perform({:q => 'hello'}, 'Authorization' => 'custom') }
75
-
76
- it "adds token param" do
77
- expect(query_params(subject)).to eq('q' => 'hello', 'access_token' => 'XYZ')
78
- end
79
-
80
- it "doesn't override existing header" do
81
- expect(auth_header(subject)).to eq('custom')
82
- end
83
- end
84
-
85
- context "custom param name configured" do
86
- let(:options) { ['XYZ', {:param_name => :oauth}] }
87
-
88
- it "adds token param" do
89
- expect(query_params(perform)).to eq('oauth' => 'XYZ')
90
- end
91
-
92
- it "overrides default with explicit token" do
93
- request = perform(:oauth => 'abc123')
94
- expect(query_params(request)).to eq('oauth' => 'abc123')
95
- expect(auth_header(request)).to eq(%(Token token="abc123"))
96
- end
97
- end
98
-
99
- context "options without token configuration" do
100
- let(:options) { [{:param_name => :oauth}] }
101
-
102
- it "doesn't add param" do
103
- expect(query_params(perform)).to be_empty
104
- end
105
-
106
- it "overrides default with explicit token" do
107
- expect(query_params(perform(:oauth => 'abc123'))).to eq('oauth' => 'abc123')
108
- end
109
- end
110
-
111
- context "invalid param name configured" do
112
- let(:options) { ['XYZ', {:param_name => nil}] }
113
-
114
- it "raises error" do
115
- expect{ make_app }.to raise_error(ArgumentError, ":param_name can't be blank")
116
- end
117
- end
118
- end
data/spec/oauth_spec.rb DELETED
@@ -1,151 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/request/oauth'
3
- require 'uri'
4
- require 'forwardable'
5
-
6
- describe FaradayMiddleware::OAuth do
7
- def auth_header(env)
8
- env[:request_headers]['Authorization']
9
- end
10
-
11
- def auth_values(env)
12
- if auth = auth_header(env)
13
- raise "invalid header: #{auth.inspect}" unless auth.sub!('OAuth ', '')
14
- Hash[*auth.split(/, |=/)]
15
- end
16
- end
17
-
18
- def perform(oauth_options = {}, headers = {}, params = {})
19
- env = {
20
- :url => URI('http://example.com/'),
21
- :request_headers => Faraday::Utils::Headers.new.update(headers),
22
- :request => {},
23
- :body => params
24
- }
25
- unless oauth_options.is_a? Hash and oauth_options.empty?
26
- env[:request][:oauth] = oauth_options
27
- end
28
- app = make_app
29
- app.call(env)
30
- end
31
-
32
- def make_app
33
- described_class.new(lambda{|env| env}, *Array(options))
34
- end
35
-
36
- context "invalid options" do
37
- let(:options) { nil }
38
-
39
- it "errors out" do
40
- expect{ make_app }.to raise_error(ArgumentError)
41
- end
42
- end
43
-
44
- context "empty options" do
45
- let(:options) { [{}] }
46
-
47
- it "signs request" do
48
- auth = auth_values(perform)
49
- expected_keys = %w[ oauth_nonce
50
- oauth_signature oauth_signature_method
51
- oauth_timestamp oauth_version ]
52
-
53
- expect(auth.keys).to match_array expected_keys
54
- end
55
- end
56
-
57
- context "configured with consumer and token" do
58
- let(:options) do
59
- [{ :consumer_key => 'CKEY', :consumer_secret => 'CSECRET',
60
- :token => 'TOKEN', :token_secret => 'TSECRET'
61
- }]
62
- end
63
-
64
- it "adds auth info to the header" do
65
- auth = auth_values(perform)
66
- expected_keys = %w[ oauth_consumer_key oauth_nonce
67
- oauth_signature oauth_signature_method
68
- oauth_timestamp oauth_token oauth_version ]
69
-
70
- expect(auth.keys).to match_array expected_keys
71
- expect(auth['oauth_version']).to eq(%("1.0"))
72
- expect(auth['oauth_signature_method']).to eq(%("HMAC-SHA1"))
73
- expect(auth['oauth_consumer_key']).to eq(%("CKEY"))
74
- expect(auth['oauth_token']).to eq(%("TOKEN"))
75
- end
76
-
77
- it "doesn't override existing header" do
78
- request = perform({}, "Authorization" => "iz me!")
79
- expect(auth_header(request)).to eq("iz me!")
80
- end
81
-
82
- it "can override oauth options per-request" do
83
- auth = auth_values(perform(:consumer_key => 'CKEY2'))
84
-
85
- expect(auth['oauth_consumer_key']).to eq(%("CKEY2"))
86
- expect(auth['oauth_token']).to eq(%("TOKEN"))
87
- end
88
-
89
- it "can turn off oauth signing per-request" do
90
- expect(auth_header(perform(false))).to be_nil
91
- end
92
- end
93
-
94
- context "configured without token" do
95
- let(:options) { [{ :consumer_key => 'CKEY', :consumer_secret => 'CSECRET' }] }
96
-
97
- it "adds auth info to the header" do
98
- auth = auth_values(perform)
99
- expect(auth).to include('oauth_consumer_key')
100
- expect(auth).not_to include('oauth_token')
101
- end
102
- end
103
-
104
- context "handling body parameters" do
105
- let(:options) { [{ :consumer_key => 'CKEY',
106
- :consumer_secret => 'CSECRET',
107
- :nonce => '547fed103e122eecf84c080843eedfe6',
108
- :timestamp => '1286830180'}] }
109
-
110
- let(:value) { {'foo' => 'bar'} }
111
-
112
- let(:type_json) { {'Content-Type' => 'application/json'} }
113
- let(:type_form) { {'Content-Type' => 'application/x-www-form-urlencoded'} }
114
-
115
- extend Forwardable
116
- query_method = :build_nested_query
117
- query_module = ::Faraday::Utils.respond_to?(query_method) ? 'Faraday::Utils' : 'Rack::Utils'
118
- def_delegator query_module, query_method
119
-
120
- it "does not include the body for JSON" do
121
- auth_header_with = auth_header(perform({}, type_json, '{"foo":"bar"}'))
122
- auth_header_without = auth_header(perform({}, type_json, {}))
123
-
124
- expect(auth_header_with).to eq(auth_header_without)
125
- end
126
-
127
- it "includes the body parameters with form Content-Type" do
128
- auth_header_with = auth_header(perform({}, type_form, {}))
129
- auth_header_without = auth_header(perform({}, type_form, value))
130
-
131
- expect(auth_header_with).not_to eq(auth_header_without)
132
- end
133
-
134
- it "includes the body parameters with an unspecified Content-Type" do
135
- auth_header_with = auth_header(perform({}, {}, value))
136
- auth_header_without = auth_header(perform({}, type_form, value))
137
-
138
- expect(auth_header_with).to eq(auth_header_without)
139
- end
140
-
141
- it "includes the body parameters for form type with string body" do
142
- # simulates the behavior of Faraday::MiddleWare::UrlEncoded
143
- value = { 'foo' => ['bar', 'baz', 'wat'] }
144
- auth_header_hash = auth_header(perform({}, type_form, value))
145
- auth_header_string = auth_header(perform({}, type_form, build_nested_query(value)))
146
- expect(auth_header_string).to eq(auth_header_hash)
147
- end
148
-
149
- end
150
-
151
- end
@@ -1,39 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/response/parse_dates'
3
- require 'json'
4
-
5
- describe FaradayMiddleware::ParseDates, :type => :response do
6
- let(:parsed){
7
- if RUBY_VERSION > "1.9"
8
- "2012-02-01 13:14:15 UTC"
9
- else
10
- "Wed Feb 01 13:14:15 UTC 2012"
11
- end
12
- }
13
-
14
- it "parses dates" do
15
- expect(process({"x" => "2012-02-01T13:14:15Z"}).body["x"].to_s).to eq(parsed)
16
- end
17
-
18
- it "parses nested dates in hash" do
19
- expect(process({"x" => {"y" => "2012-02-01T13:14:15Z"}}).body["x"]["y"].to_s).to eq(parsed)
20
- end
21
-
22
- it "parses nested dates in arrays" do
23
- expect(process({"x" => [{"y" =>"2012-02-01T13:14:15Z"}]}).body["x"][0]["y"].to_s).to eq(parsed)
24
- end
25
-
26
- it "returns nil when body is empty" do
27
- expect(process(nil).body).to eq(nil)
28
- end
29
-
30
- it "leaves arrays with ids alone" do
31
- expect(process({"x" => [1,2,3]}).body).to eq({"x" => [1,2,3]})
32
- end
33
-
34
- it "does not parse date-like things" do
35
- expect(process({"x" => "2012-02-01T13:14:15Z bla"}).body["x"].to_s).to eq "2012-02-01T13:14:15Z bla"
36
- expect(process({"x" => "12012-02-01T13:14:15Z"}).body["x"].to_s).to eq "12012-02-01T13:14:15Z"
37
- expect(process({"x" => "2012-02-01T13:14:15Z\nfoo"}).body["x"].to_s).to eq "2012-02-01T13:14:15Z\nfoo"
38
- end
39
- end
@@ -1,112 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/response/parse_json'
3
-
4
- describe FaradayMiddleware::ParseJson, :type => :response do
5
- context "no type matching" do
6
- it "doesn't change nil body" do
7
- expect(process(nil).body).to be_nil
8
- end
9
-
10
- it "nullifies empty body" do
11
- expect(process('').body).to be_nil
12
- end
13
-
14
- it "parses json body" do
15
- response = process('{"a":1}')
16
- expect(response.body).to eq('a' => 1)
17
- expect(response.env[:raw_body]).to be_nil
18
- end
19
- end
20
-
21
- context "with preserving raw" do
22
- let(:options) { {:preserve_raw => true} }
23
-
24
- it "parses json body" do
25
- response = process('{"a":1}')
26
- expect(response.body).to eq('a' => 1)
27
- expect(response.env[:raw_body]).to eq('{"a":1}')
28
- end
29
-
30
- it "can opt out of preserving raw" do
31
- response = process('{"a":1}', nil, :preserve_raw => false)
32
- expect(response.env[:raw_body]).to be_nil
33
- end
34
- end
35
-
36
- context "with regexp type matching" do
37
- let(:options) { {:content_type => /\bjson$/} }
38
-
39
- it "parses json body of correct type" do
40
- response = process('{"a":1}', 'application/x-json')
41
- expect(response.body).to eq('a' => 1)
42
- end
43
-
44
- it "ignores json body of incorrect type" do
45
- response = process('{"a":1}', 'text/json-xml')
46
- expect(response.body).to eq('{"a":1}')
47
- end
48
- end
49
-
50
- context "with array type matching" do
51
- let(:options) { {:content_type => %w[a/b c/d]} }
52
-
53
- it "parses json body of correct type" do
54
- expect(process('{"a":1}', 'a/b').body).to be_a(Hash)
55
- expect(process('{"a":1}', 'c/d').body).to be_a(Hash)
56
- end
57
-
58
- it "ignores json body of incorrect type" do
59
- expect(process('{"a":1}', 'a/d').body).not_to be_a(Hash)
60
- end
61
- end
62
-
63
- it "chokes on invalid json" do
64
- ['{!', '"a"', 'true', 'null', '1'].each do |data|
65
- expect{ process(data) }.to raise_error(Faraday::Error::ParsingError)
66
- end
67
- end
68
-
69
- context "with mime type fix" do
70
- let(:middleware) {
71
- app = described_class::MimeTypeFix.new(lambda {|env|
72
- Faraday::Response.new(env)
73
- }, :content_type => /^text\//)
74
- described_class.new(app, :content_type => 'application/json')
75
- }
76
-
77
- it "ignores completely incompatible type" do
78
- response = process('{"a":1}', 'application/xml')
79
- expect(response.body).to eq('{"a":1}')
80
- end
81
-
82
- it "ignores compatible type with bad data" do
83
- response = process('var a = 1', 'text/javascript')
84
- expect(response.body).to eq('var a = 1')
85
- expect(response['content-type']).to eq('text/javascript')
86
- end
87
-
88
- it "corrects compatible type and data" do
89
- response = process('{"a":1}', 'text/javascript')
90
- expect(response.body).to be_a(Hash)
91
- expect(response['content-type']).to eq('application/json')
92
- end
93
-
94
- it "corrects compatible type even when data starts with whitespace" do
95
- response = process(%( \r\n\t{"a":1}), 'text/javascript')
96
- expect(response.body).to be_a(Hash)
97
- expect(response['content-type']).to eq('application/json')
98
- end
99
- end
100
-
101
- context "HEAD responses" do
102
- it "nullifies the body if it's only one space" do
103
- response = process(' ')
104
- expect(response.body).to be_nil
105
- end
106
-
107
- it "nullifies the body if it's two spaces" do
108
- response = process(' ')
109
- expect(response.body).to be_nil
110
- end
111
- end
112
- end
@@ -1,16 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/response/parse_marshal'
3
-
4
- describe FaradayMiddleware::ParseMarshal, :type => :response do
5
- it "restores a marshaled dump" do
6
- expect(process(Marshal.dump(:a => 1)).body).to be_eql(:a => 1)
7
- end
8
-
9
- it "nulifies blank response" do
10
- expect(process('').body).to be_nil
11
- end
12
-
13
- it "chokes on invalid content" do
14
- expect{ process('abc') }.to raise_error(Faraday::Error::ParsingError)
15
- end
16
- end