faraday_middleware 0.8.8 → 0.9.0
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/Gemfile +1 -1
- data/Rakefile +5 -3
- data/lib/faraday_middleware.rb +5 -1
- data/lib/faraday_middleware/request/method_override.rb +51 -0
- data/lib/faraday_middleware/response/follow_redirects.rb +8 -2
- data/lib/faraday_middleware/version.rb +1 -1
- data/spec/caching_test.rb +1 -1
- data/spec/chunked_spec.rb +15 -15
- data/spec/encode_json_spec.rb +13 -13
- data/spec/follow_redirects_spec.rb +59 -46
- data/spec/helper.rb +3 -0
- data/spec/mashify_spec.rb +30 -30
- data/spec/method_override_spec.rb +92 -0
- data/spec/oauth2_spec.rb +18 -18
- data/spec/oauth_spec.rb +19 -19
- data/spec/parse_dates_spec.rb +14 -17
- data/spec/parse_json_spec.rb +24 -24
- data/spec/parse_marshal_spec.rb +3 -3
- data/spec/parse_xml_spec.rb +13 -13
- data/spec/parse_yaml_spec.rb +10 -10
- data/spec/rashify_spec.rb +22 -22
- metadata +5 -2
data/spec/helper.rb
CHANGED
data/spec/mashify_spec.rb
CHANGED
@@ -2,80 +2,80 @@ require 'helper'
|
|
2
2
|
require 'faraday_middleware/response/mashify'
|
3
3
|
|
4
4
|
describe FaradayMiddleware::Mashify do
|
5
|
-
context
|
6
|
-
it
|
7
|
-
described_class.
|
8
|
-
described_class.
|
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
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
context
|
12
|
+
context "when used" do
|
13
13
|
before(:each) { described_class.mash_class = ::Hashie::Mash }
|
14
14
|
let(:mashify) { described_class.new }
|
15
15
|
|
16
|
-
it
|
16
|
+
it "creates a Hashie::Mash from the body" do
|
17
17
|
env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
|
18
18
|
me = mashify.on_complete(env)
|
19
|
-
me.class.
|
19
|
+
expect(me.class).to eq(Hashie::Mash)
|
20
20
|
end
|
21
21
|
|
22
|
-
it
|
22
|
+
it "handles strings" do
|
23
23
|
env = { :body => "Most amazing string EVER" }
|
24
24
|
me = mashify.on_complete(env)
|
25
|
-
me.
|
25
|
+
expect(me).to eq("Most amazing string EVER")
|
26
26
|
end
|
27
27
|
|
28
|
-
it
|
28
|
+
it "handles arrays" do
|
29
29
|
env = { :body => [123, 456] }
|
30
30
|
values = mashify.on_complete(env)
|
31
|
-
values.first.
|
32
|
-
values.last.
|
31
|
+
expect(values.first).to eq(123)
|
32
|
+
expect(values.last).to eq(456)
|
33
33
|
end
|
34
34
|
|
35
|
-
it
|
35
|
+
it "handles arrays of hashes" do
|
36
36
|
env = { :body => [{ "username" => "sferik" }, { "username" => "pengwynn" }] }
|
37
37
|
us = mashify.on_complete(env)
|
38
|
-
us.first.username.
|
39
|
-
us.last.username.
|
38
|
+
expect(us.first.username).to eq('sferik')
|
39
|
+
expect(us.last.username).to eq('pengwynn')
|
40
40
|
end
|
41
41
|
|
42
|
-
it
|
42
|
+
it "handles nested arrays of hashes" do
|
43
43
|
env = { :body => [[{ "username" => "sferik" }, { "username" => "pengwynn" }]] }
|
44
44
|
us = mashify.on_complete(env)
|
45
|
-
us.first.first.username.
|
46
|
-
us.first.last.username.
|
45
|
+
expect(us.first.first.username).to eq('sferik')
|
46
|
+
expect(us.first.last.username).to eq('pengwynn')
|
47
47
|
end
|
48
48
|
|
49
|
-
it
|
49
|
+
it "handles mixed arrays" do
|
50
50
|
env = { :body => [123, { "username" => "sferik" }, 456] }
|
51
51
|
values = mashify.on_complete(env)
|
52
|
-
values.first.
|
53
|
-
values.last.
|
54
|
-
values[1].username.
|
52
|
+
expect(values.first).to eq(123)
|
53
|
+
expect(values.last).to eq(456)
|
54
|
+
expect(values[1].username).to eq('sferik')
|
55
55
|
end
|
56
56
|
|
57
|
-
it
|
57
|
+
it "allows for use of custom Mash subclasses at the class level" do
|
58
58
|
class MyMash < ::Hashie::Mash; end
|
59
59
|
described_class.mash_class = MyMash
|
60
60
|
|
61
61
|
env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
|
62
62
|
me = mashify.on_complete(env)
|
63
63
|
|
64
|
-
me.class.
|
64
|
+
expect(me.class).to eq(MyMash)
|
65
65
|
end
|
66
66
|
|
67
|
-
it
|
67
|
+
it "allows for use of custom Mash subclasses at the instance level" do
|
68
68
|
class MyMash < ::Hashie::Mash; end
|
69
69
|
mashify = described_class.new(nil, :mash_class => MyMash)
|
70
70
|
|
71
71
|
env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
|
72
72
|
me = mashify.on_complete(env)
|
73
73
|
|
74
|
-
me.class.
|
74
|
+
expect(me.class).to eq(MyMash)
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
-
context
|
78
|
+
context "integration test" do
|
79
79
|
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
|
80
80
|
let(:connection) do
|
81
81
|
Faraday::Connection.new do |builder|
|
@@ -86,14 +86,14 @@ describe FaradayMiddleware::Mashify do
|
|
86
86
|
|
87
87
|
# although it is not good practice to pass a hash as the body, if we add ParseJson
|
88
88
|
# to the middleware stack we end up testing two middlewares instead of one
|
89
|
-
it
|
89
|
+
it "creates a Hash from the body" do
|
90
90
|
stubs.get('/hash') {
|
91
91
|
data = { 'name' => 'Erik Michaels-Ober', 'username' => 'sferik' }
|
92
92
|
[200, {'content-type' => 'application/json; charset=utf-8'}, data]
|
93
93
|
}
|
94
94
|
me = connection.get('/hash').body
|
95
|
-
me.name.
|
96
|
-
me.username.
|
95
|
+
expect(me.name).to eq('Erik Michaels-Ober')
|
96
|
+
expect(me.username).to eq('sferik')
|
97
97
|
end
|
98
98
|
end
|
99
99
|
end
|
@@ -0,0 +1,92 @@
|
|
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
CHANGED
@@ -31,17 +31,17 @@ describe FaradayMiddleware::OAuth2 do
|
|
31
31
|
|
32
32
|
it "doesn't add params" do
|
33
33
|
request = perform(:q => 'hello')
|
34
|
-
query_params(request).
|
34
|
+
expect(query_params(request)).to eq('q' => 'hello')
|
35
35
|
end
|
36
36
|
|
37
37
|
it "doesn't add headers" do
|
38
|
-
auth_header(perform).
|
38
|
+
expect(auth_header(perform)).to be_nil
|
39
39
|
end
|
40
40
|
|
41
41
|
it "creates header for explicit token" do
|
42
42
|
request = perform(:q => 'hello', :access_token => 'abc123')
|
43
|
-
query_params(request).
|
44
|
-
auth_header(request).
|
43
|
+
expect(query_params(request)).to eq('q' => 'hello', 'access_token' => 'abc123')
|
44
|
+
expect(auth_header(request)).to eq(%(Token token="abc123"))
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
@@ -49,23 +49,23 @@ describe FaradayMiddleware::OAuth2 do
|
|
49
49
|
let(:options) { 'XYZ' }
|
50
50
|
|
51
51
|
it "adds token param" do
|
52
|
-
query_params(perform(:q => 'hello')).
|
52
|
+
expect(query_params(perform(:q => 'hello'))).to eq('q' => 'hello', 'access_token' => 'XYZ')
|
53
53
|
end
|
54
54
|
|
55
55
|
it "adds token header" do
|
56
|
-
auth_header(perform).
|
56
|
+
expect(auth_header(perform)).to eq(%(Token token="XYZ"))
|
57
57
|
end
|
58
58
|
|
59
59
|
it "overrides default with explicit token" do
|
60
60
|
request = perform(:q => 'hello', :access_token => 'abc123')
|
61
|
-
query_params(request).
|
62
|
-
auth_header(request).
|
61
|
+
expect(query_params(request)).to eq('q' => 'hello', 'access_token' => 'abc123')
|
62
|
+
expect(auth_header(request)).to eq(%(Token token="abc123"))
|
63
63
|
end
|
64
64
|
|
65
65
|
it "clears default with empty explicit token" do
|
66
66
|
request = perform(:q => 'hello', :access_token => nil)
|
67
|
-
query_params(request).
|
68
|
-
auth_header(request).
|
67
|
+
expect(query_params(request)).to eq('q' => 'hello', 'access_token' => nil)
|
68
|
+
expect(auth_header(request)).to be_nil
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
@@ -74,11 +74,11 @@ describe FaradayMiddleware::OAuth2 do
|
|
74
74
|
subject { perform({:q => 'hello'}, 'Authorization' => 'custom') }
|
75
75
|
|
76
76
|
it "adds token param" do
|
77
|
-
query_params(subject).
|
77
|
+
expect(query_params(subject)).to eq('q' => 'hello', 'access_token' => 'XYZ')
|
78
78
|
end
|
79
79
|
|
80
80
|
it "doesn't override existing header" do
|
81
|
-
auth_header(subject).
|
81
|
+
expect(auth_header(subject)).to eq('custom')
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
@@ -86,13 +86,13 @@ describe FaradayMiddleware::OAuth2 do
|
|
86
86
|
let(:options) { ['XYZ', {:param_name => :oauth}] }
|
87
87
|
|
88
88
|
it "adds token param" do
|
89
|
-
query_params(perform).
|
89
|
+
expect(query_params(perform)).to eq('oauth' => 'XYZ')
|
90
90
|
end
|
91
91
|
|
92
92
|
it "overrides default with explicit token" do
|
93
93
|
request = perform(:oauth => 'abc123')
|
94
|
-
query_params(request).
|
95
|
-
auth_header(request).
|
94
|
+
expect(query_params(request)).to eq('oauth' => 'abc123')
|
95
|
+
expect(auth_header(request)).to eq(%(Token token="abc123"))
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
@@ -100,11 +100,11 @@ describe FaradayMiddleware::OAuth2 do
|
|
100
100
|
let(:options) { [{:param_name => :oauth}] }
|
101
101
|
|
102
102
|
it "doesn't add param" do
|
103
|
-
query_params(perform).
|
103
|
+
expect(query_params(perform)).to be_empty
|
104
104
|
end
|
105
105
|
|
106
106
|
it "overrides default with explicit token" do
|
107
|
-
query_params(perform(:oauth => 'abc123')).
|
107
|
+
expect(query_params(perform(:oauth => 'abc123'))).to eq('oauth' => 'abc123')
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
@@ -112,7 +112,7 @@ describe FaradayMiddleware::OAuth2 do
|
|
112
112
|
let(:options) { ['XYZ', {:param_name => nil}] }
|
113
113
|
|
114
114
|
it "raises error" do
|
115
|
-
expect
|
115
|
+
expect{ make_app }.to raise_error(ArgumentError, ":param_name can't be blank")
|
116
116
|
end
|
117
117
|
end
|
118
118
|
end
|
data/spec/oauth_spec.rb
CHANGED
@@ -36,21 +36,21 @@ describe FaradayMiddleware::OAuth do
|
|
36
36
|
context "invalid options" do
|
37
37
|
let(:options) { nil }
|
38
38
|
|
39
|
-
it "
|
40
|
-
expect
|
39
|
+
it "errors out" do
|
40
|
+
expect{ make_app }.to raise_error(ArgumentError)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
44
|
context "empty options" do
|
45
45
|
let(:options) { [{}] }
|
46
46
|
|
47
|
-
it "
|
47
|
+
it "signs request" do
|
48
48
|
auth = auth_values(perform)
|
49
49
|
expected_keys = %w[ oauth_nonce
|
50
50
|
oauth_signature oauth_signature_method
|
51
51
|
oauth_timestamp oauth_version ]
|
52
52
|
|
53
|
-
auth.keys.
|
53
|
+
expect(auth.keys).to match_array expected_keys
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
@@ -67,27 +67,27 @@ describe FaradayMiddleware::OAuth do
|
|
67
67
|
oauth_signature oauth_signature_method
|
68
68
|
oauth_timestamp oauth_token oauth_version ]
|
69
69
|
|
70
|
-
auth.keys.
|
71
|
-
auth['oauth_version'].
|
72
|
-
auth['oauth_signature_method'].
|
73
|
-
auth['oauth_consumer_key'].
|
74
|
-
auth['oauth_token'].
|
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
75
|
end
|
76
76
|
|
77
77
|
it "doesn't override existing header" do
|
78
78
|
request = perform({}, "Authorization" => "iz me!")
|
79
|
-
auth_header(request).
|
79
|
+
expect(auth_header(request)).to eq("iz me!")
|
80
80
|
end
|
81
81
|
|
82
82
|
it "can override oauth options per-request" do
|
83
83
|
auth = auth_values(perform(:consumer_key => 'CKEY2'))
|
84
84
|
|
85
|
-
auth['oauth_consumer_key'].
|
86
|
-
auth['oauth_token'].
|
85
|
+
expect(auth['oauth_consumer_key']).to eq(%("CKEY2"))
|
86
|
+
expect(auth['oauth_token']).to eq(%("TOKEN"))
|
87
87
|
end
|
88
88
|
|
89
89
|
it "can turn off oauth signing per-request" do
|
90
|
-
auth_header(perform(false)).
|
90
|
+
expect(auth_header(perform(false))).to be_nil
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
@@ -96,8 +96,8 @@ describe FaradayMiddleware::OAuth do
|
|
96
96
|
|
97
97
|
it "adds auth info to the header" do
|
98
98
|
auth = auth_values(perform)
|
99
|
-
auth.
|
100
|
-
auth.
|
99
|
+
expect(auth).to include('oauth_consumer_key')
|
100
|
+
expect(auth).not_to include('oauth_token')
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
@@ -121,21 +121,21 @@ describe FaradayMiddleware::OAuth do
|
|
121
121
|
auth_header_with = auth_header(perform({}, type_json, '{"foo":"bar"}'))
|
122
122
|
auth_header_without = auth_header(perform({}, type_json, {}))
|
123
123
|
|
124
|
-
auth_header_with.
|
124
|
+
expect(auth_header_with).to eq(auth_header_without)
|
125
125
|
end
|
126
126
|
|
127
127
|
it "includes the body parameters with form Content-Type" do
|
128
128
|
auth_header_with = auth_header(perform({}, type_form, {}))
|
129
129
|
auth_header_without = auth_header(perform({}, type_form, value))
|
130
130
|
|
131
|
-
auth_header_with.
|
131
|
+
expect(auth_header_with).not_to eq(auth_header_without)
|
132
132
|
end
|
133
133
|
|
134
134
|
it "includes the body parameters with an unspecified Content-Type" do
|
135
135
|
auth_header_with = auth_header(perform({}, {}, value))
|
136
136
|
auth_header_without = auth_header(perform({}, type_form, value))
|
137
137
|
|
138
|
-
auth_header_with.
|
138
|
+
expect(auth_header_with).to eq(auth_header_without)
|
139
139
|
end
|
140
140
|
|
141
141
|
it "includes the body parameters for form type with string body" do
|
@@ -143,7 +143,7 @@ describe FaradayMiddleware::OAuth do
|
|
143
143
|
value = { 'foo' => ['bar', 'baz', 'wat'] }
|
144
144
|
auth_header_hash = auth_header(perform({}, type_form, value))
|
145
145
|
auth_header_string = auth_header(perform({}, type_form, build_nested_query(value)))
|
146
|
-
auth_header_string.
|
146
|
+
expect(auth_header_string).to eq(auth_header_hash)
|
147
147
|
end
|
148
148
|
|
149
149
|
end
|
data/spec/parse_dates_spec.rb
CHANGED
@@ -11,32 +11,29 @@ describe FaradayMiddleware::ParseDates, :type => :response do
|
|
11
11
|
end
|
12
12
|
}
|
13
13
|
|
14
|
-
it "
|
15
|
-
process({"x" => "2012-02-01T13:14:15Z"}).body["x"].to_s.
|
14
|
+
it "parses dates" do
|
15
|
+
expect(process({"x" => "2012-02-01T13:14:15Z"}).body["x"].to_s).to eq(parsed)
|
16
16
|
end
|
17
17
|
|
18
|
-
it "
|
19
|
-
process({"x" => {"y" => "2012-02-01T13:14:15Z"}}).body["x"]["y"].to_s.
|
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
20
|
end
|
21
21
|
|
22
|
-
it "
|
23
|
-
process({"x" => [{"y" =>"2012-02-01T13:14:15Z"}]}).body["x"][0]["y"].to_s.
|
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
24
|
end
|
25
25
|
|
26
|
-
it "
|
27
|
-
process(nil).body.
|
26
|
+
it "returns nil when body is empty" do
|
27
|
+
expect(process(nil).body).to eq(nil)
|
28
28
|
end
|
29
29
|
|
30
|
-
it "
|
31
|
-
process({"x" => [1,2,3]}).body.
|
30
|
+
it "leaves arrays with ids alone" do
|
31
|
+
expect(process({"x" => [1,2,3]}).body).to eq({"x" => [1,2,3]})
|
32
32
|
end
|
33
33
|
|
34
|
-
it "
|
35
|
-
process({"x" => "2012-02-01T13:14:15Z bla"}).body["x"].to_s.
|
36
|
-
|
37
|
-
process({"x" => "
|
38
|
-
"12012-02-01T13:14:15Z"
|
39
|
-
process({"x" => "2012-02-01T13:14:15Z\nfoo"}).body["x"].to_s.should ==
|
40
|
-
"2012-02-01T13:14:15Z\nfoo"
|
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"
|
41
38
|
end
|
42
39
|
end
|