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,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(faraday_env(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).fetch('access_token')).to_not eq('XYZ')
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
@@ -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(faraday_env(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,44 +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 dates with milliseconds" do
19
- date_str = "2012-02-01T13:14:15.123Z"
20
- expect(process({"x" => date_str}).body["x"]).to eq(Time.parse(date_str))
21
- end
22
-
23
- it "parses nested dates in hash" do
24
- expect(process({"x" => {"y" => "2012-02-01T13:14:15Z"}}).body["x"]["y"].to_s).to eq(parsed)
25
- end
26
-
27
- it "parses nested dates in arrays" do
28
- expect(process({"x" => [{"y" =>"2012-02-01T13:14:15Z"}]}).body["x"][0]["y"].to_s).to eq(parsed)
29
- end
30
-
31
- it "returns nil when body is empty" do
32
- expect(process(nil).body).to eq(nil)
33
- end
34
-
35
- it "leaves arrays with ids alone" do
36
- expect(process({"x" => [1,2,3]}).body).to eq({"x" => [1,2,3]})
37
- end
38
-
39
- it "does not parse date-like things" do
40
- expect(process({"x" => "2012-02-01T13:14:15Z bla"}).body["x"].to_s).to eq "2012-02-01T13:14:15Z bla"
41
- expect(process({"x" => "12012-02-01T13:14:15Z"}).body["x"].to_s).to eq "12012-02-01T13:14:15Z"
42
- expect(process({"x" => "2012-02-01T13:14:15Z\nfoo"}).body["x"].to_s).to eq "2012-02-01T13:14:15Z\nfoo"
43
- end
44
- 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
@@ -1,71 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/response/parse_xml'
3
-
4
- describe FaradayMiddleware::ParseXml, :type => :response do
5
- let(:xml) { '<user><name>Erik Michaels-Ober</name><screen_name>sferik</screen_name></user>' }
6
- let(:user) { {'user' => {'name' => 'Erik Michaels-Ober', 'screen_name' => 'sferik'} } }
7
-
8
- context "no type matching" do
9
- it "doesn't change nil body" do
10
- expect(process(nil).body).to be_nil
11
- end
12
-
13
- it "turns empty body into empty hash" do
14
- expect(process('').body).to be_eql({})
15
- end
16
-
17
- it "parses xml body" do
18
- response = process(xml)
19
- expect(response.body).to eq(user)
20
- expect(response.env[:raw_body]).to be_nil
21
- end
22
- end
23
-
24
- context "with preserving raw" do
25
- let(:options) { {:preserve_raw => true} }
26
-
27
- it "parses xml body" do
28
- response = process(xml)
29
- expect(response.body).to eq(user)
30
- expect(response.env[:raw_body]).to eq(xml)
31
- end
32
-
33
- it "can opt out of preserving raw" do
34
- response = process(xml, nil, :preserve_raw => false)
35
- expect(response.env[:raw_body]).to be_nil
36
- end
37
- end
38
-
39
- context "with regexp type matching" do
40
- let(:options) { {:content_type => /\bxml$/} }
41
-
42
- it "parses xml body of correct type" do
43
- response = process(xml, 'application/xml')
44
- expect(response.body).to eq(user)
45
- end
46
-
47
- it "ignores xml body of incorrect type" do
48
- response = process(xml, 'text/html')
49
- expect(response.body).to eq(xml)
50
- end
51
- end
52
-
53
- context "with array type matching" do
54
- let(:options) { {:content_type => %w[a/b c/d]} }
55
-
56
- it "parses xml body of correct type" do
57
- expect(process(xml, 'a/b').body).to be_a(Hash)
58
- expect(process(xml, 'c/d').body).to be_a(Hash)
59
- end
60
-
61
- it "ignores xml body of incorrect type" do
62
- expect(process(xml, 'a/d').body).not_to be_a(Hash)
63
- end
64
- end
65
-
66
- it "chokes on invalid xml" do
67
- ['{!', '"a"', 'true', 'null', '1'].each do |data|
68
- expect{ process(data) }.to raise_error(Faraday::Error::ParsingError)
69
- end
70
- end
71
- end