faraday_middleware 0.8.8 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- 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/parse_json_spec.rb
CHANGED
@@ -4,17 +4,17 @@ require 'faraday_middleware/response/parse_json'
|
|
4
4
|
describe FaradayMiddleware::ParseJson, :type => :response do
|
5
5
|
context "no type matching" do
|
6
6
|
it "doesn't change nil body" do
|
7
|
-
process(nil).body.
|
7
|
+
expect(process(nil).body).to be_nil
|
8
8
|
end
|
9
9
|
|
10
10
|
it "nullifies empty body" do
|
11
|
-
process('').body.
|
11
|
+
expect(process('').body).to be_nil
|
12
12
|
end
|
13
13
|
|
14
14
|
it "parses json body" do
|
15
15
|
response = process('{"a":1}')
|
16
|
-
response.body.
|
17
|
-
response.env[:raw_body].
|
16
|
+
expect(response.body).to eq('a' => 1)
|
17
|
+
expect(response.env[:raw_body]).to be_nil
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -23,13 +23,13 @@ describe FaradayMiddleware::ParseJson, :type => :response do
|
|
23
23
|
|
24
24
|
it "parses json body" do
|
25
25
|
response = process('{"a":1}')
|
26
|
-
response.body.
|
27
|
-
response.env[:raw_body].
|
26
|
+
expect(response.body).to eq('a' => 1)
|
27
|
+
expect(response.env[:raw_body]).to eq('{"a":1}')
|
28
28
|
end
|
29
29
|
|
30
30
|
it "can opt out of preserving raw" do
|
31
31
|
response = process('{"a":1}', nil, :preserve_raw => false)
|
32
|
-
response.env[:raw_body].
|
32
|
+
expect(response.env[:raw_body]).to be_nil
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -38,12 +38,12 @@ describe FaradayMiddleware::ParseJson, :type => :response do
|
|
38
38
|
|
39
39
|
it "parses json body of correct type" do
|
40
40
|
response = process('{"a":1}', 'application/x-json')
|
41
|
-
response.body.
|
41
|
+
expect(response.body).to eq('a' => 1)
|
42
42
|
end
|
43
43
|
|
44
44
|
it "ignores json body of incorrect type" do
|
45
45
|
response = process('{"a":1}', 'text/json-xml')
|
46
|
-
response.body.
|
46
|
+
expect(response.body).to eq('{"a":1}')
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -51,18 +51,18 @@ describe FaradayMiddleware::ParseJson, :type => :response do
|
|
51
51
|
let(:options) { {:content_type => %w[a/b c/d]} }
|
52
52
|
|
53
53
|
it "parses json body of correct type" do
|
54
|
-
process('{"a":1}', 'a/b').body.
|
55
|
-
process('{"a":1}', 'c/d').body.
|
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
56
|
end
|
57
57
|
|
58
58
|
it "ignores json body of incorrect type" do
|
59
|
-
process('{"a":1}', 'a/d').body.
|
59
|
+
expect(process('{"a":1}', 'a/d').body).not_to be_a(Hash)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
63
|
it "chokes on invalid json" do
|
64
64
|
['{!', '"a"', 'true', 'null', '1'].each do |data|
|
65
|
-
expect
|
65
|
+
expect{ process(data) }.to raise_error(Faraday::Error::ParsingError)
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
@@ -76,37 +76,37 @@ describe FaradayMiddleware::ParseJson, :type => :response do
|
|
76
76
|
|
77
77
|
it "ignores completely incompatible type" do
|
78
78
|
response = process('{"a":1}', 'application/xml')
|
79
|
-
response.body.
|
79
|
+
expect(response.body).to eq('{"a":1}')
|
80
80
|
end
|
81
81
|
|
82
82
|
it "ignores compatible type with bad data" do
|
83
83
|
response = process('var a = 1', 'text/javascript')
|
84
|
-
response.body.
|
85
|
-
response['content-type'].
|
84
|
+
expect(response.body).to eq('var a = 1')
|
85
|
+
expect(response['content-type']).to eq('text/javascript')
|
86
86
|
end
|
87
87
|
|
88
88
|
it "corrects compatible type and data" do
|
89
89
|
response = process('{"a":1}', 'text/javascript')
|
90
|
-
response.body.
|
91
|
-
response['content-type'].
|
90
|
+
expect(response.body).to be_a(Hash)
|
91
|
+
expect(response['content-type']).to eq('application/json')
|
92
92
|
end
|
93
93
|
|
94
94
|
it "corrects compatible type even when data starts with whitespace" do
|
95
95
|
response = process(%( \r\n\t{"a":1}), 'text/javascript')
|
96
|
-
response.body.
|
97
|
-
response['content-type'].
|
96
|
+
expect(response.body).to be_a(Hash)
|
97
|
+
expect(response['content-type']).to eq('application/json')
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
101
|
context "HEAD responses" do
|
102
|
-
it "
|
102
|
+
it "nullifies the body if it's only one space" do
|
103
103
|
response = process(' ')
|
104
|
-
response.body.
|
104
|
+
expect(response.body).to be_nil
|
105
105
|
end
|
106
106
|
|
107
|
-
it "
|
107
|
+
it "nullifies the body if it's two spaces" do
|
108
108
|
response = process(' ')
|
109
|
-
response.body.
|
109
|
+
expect(response.body).to be_nil
|
110
110
|
end
|
111
111
|
end
|
112
112
|
end
|
data/spec/parse_marshal_spec.rb
CHANGED
@@ -3,14 +3,14 @@ require 'faraday_middleware/response/parse_marshal'
|
|
3
3
|
|
4
4
|
describe FaradayMiddleware::ParseMarshal, :type => :response do
|
5
5
|
it "restores a marshaled dump" do
|
6
|
-
process(Marshal.dump(:a => 1)).body.
|
6
|
+
expect(process(Marshal.dump(:a => 1)).body).to be_eql(:a => 1)
|
7
7
|
end
|
8
8
|
|
9
9
|
it "nulifies blank response" do
|
10
|
-
process('').body.
|
10
|
+
expect(process('').body).to be_nil
|
11
11
|
end
|
12
12
|
|
13
13
|
it "chokes on invalid content" do
|
14
|
-
expect
|
14
|
+
expect{ process('abc') }.to raise_error(Faraday::Error::ParsingError)
|
15
15
|
end
|
16
16
|
end
|
data/spec/parse_xml_spec.rb
CHANGED
@@ -7,17 +7,17 @@ describe FaradayMiddleware::ParseXml, :type => :response do
|
|
7
7
|
|
8
8
|
context "no type matching" do
|
9
9
|
it "doesn't change nil body" do
|
10
|
-
process(nil).body.
|
10
|
+
expect(process(nil).body).to be_nil
|
11
11
|
end
|
12
12
|
|
13
13
|
it "turns empty body into empty hash" do
|
14
|
-
process('').body.
|
14
|
+
expect(process('').body).to be_eql({})
|
15
15
|
end
|
16
16
|
|
17
17
|
it "parses xml body" do
|
18
18
|
response = process(xml)
|
19
|
-
response.body.
|
20
|
-
response.env[:raw_body].
|
19
|
+
expect(response.body).to eq(user)
|
20
|
+
expect(response.env[:raw_body]).to be_nil
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -26,13 +26,13 @@ describe FaradayMiddleware::ParseXml, :type => :response do
|
|
26
26
|
|
27
27
|
it "parses xml body" do
|
28
28
|
response = process(xml)
|
29
|
-
response.body.
|
30
|
-
response.env[:raw_body].
|
29
|
+
expect(response.body).to eq(user)
|
30
|
+
expect(response.env[:raw_body]).to eq(xml)
|
31
31
|
end
|
32
32
|
|
33
33
|
it "can opt out of preserving raw" do
|
34
34
|
response = process(xml, nil, :preserve_raw => false)
|
35
|
-
response.env[:raw_body].
|
35
|
+
expect(response.env[:raw_body]).to be_nil
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -41,12 +41,12 @@ describe FaradayMiddleware::ParseXml, :type => :response do
|
|
41
41
|
|
42
42
|
it "parses xml body of correct type" do
|
43
43
|
response = process(xml, 'application/xml')
|
44
|
-
response.body.
|
44
|
+
expect(response.body).to eq(user)
|
45
45
|
end
|
46
46
|
|
47
47
|
it "ignores xml body of incorrect type" do
|
48
48
|
response = process(xml, 'text/html')
|
49
|
-
response.body.
|
49
|
+
expect(response.body).to eq(xml)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
@@ -54,18 +54,18 @@ describe FaradayMiddleware::ParseXml, :type => :response do
|
|
54
54
|
let(:options) { {:content_type => %w[a/b c/d]} }
|
55
55
|
|
56
56
|
it "parses xml body of correct type" do
|
57
|
-
process(xml, 'a/b').body.
|
58
|
-
process(xml, 'c/d').body.
|
57
|
+
expect(process(xml, 'a/b').body).to be_a(Hash)
|
58
|
+
expect(process(xml, 'c/d').body).to be_a(Hash)
|
59
59
|
end
|
60
60
|
|
61
61
|
it "ignores xml body of incorrect type" do
|
62
|
-
process(xml, 'a/d').body.
|
62
|
+
expect(process(xml, 'a/d').body).not_to be_a(Hash)
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
66
|
it "chokes on invalid xml" do
|
67
67
|
['{!', '"a"', 'true', 'null', '1'].each do |data|
|
68
|
-
expect
|
68
|
+
expect{ process(data) }.to raise_error(Faraday::Error::ParsingError)
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
data/spec/parse_yaml_spec.rb
CHANGED
@@ -4,17 +4,17 @@ require 'faraday_middleware/response/parse_yaml'
|
|
4
4
|
describe FaradayMiddleware::ParseYaml, :type => :response do
|
5
5
|
context "no type matching" do
|
6
6
|
it "doesn't change nil body" do
|
7
|
-
process(nil).body.
|
7
|
+
expect(process(nil).body).to be_nil
|
8
8
|
end
|
9
9
|
|
10
10
|
it "returns false for empty body" do
|
11
|
-
process('').body.
|
11
|
+
expect(process('').body).to be_false
|
12
12
|
end
|
13
13
|
|
14
14
|
it "parses yaml body" do
|
15
15
|
response = process('a: 1')
|
16
|
-
response.body.
|
17
|
-
response.env[:raw_body].
|
16
|
+
expect(response.body).to eq('a' => 1)
|
17
|
+
expect(response.env[:raw_body]).to be_nil
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -23,13 +23,13 @@ describe FaradayMiddleware::ParseYaml, :type => :response do
|
|
23
23
|
|
24
24
|
it "parses yaml body" do
|
25
25
|
response = process('a: 1')
|
26
|
-
response.body.
|
27
|
-
response.env[:raw_body].
|
26
|
+
expect(response.body).to eq('a' => 1)
|
27
|
+
expect(response.env[:raw_body]).to eq('a: 1')
|
28
28
|
end
|
29
29
|
|
30
30
|
it "can opt out of preserving raw" do
|
31
31
|
response = process('a: 1', nil, :preserve_raw => false)
|
32
|
-
response.env[:raw_body].
|
32
|
+
expect(response.env[:raw_body]).to be_nil
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -38,16 +38,16 @@ describe FaradayMiddleware::ParseYaml, :type => :response do
|
|
38
38
|
|
39
39
|
it "parses json body of correct type" do
|
40
40
|
response = process('a: 1', 'application/x-yaml')
|
41
|
-
response.body.
|
41
|
+
expect(response.body).to eq('a' => 1)
|
42
42
|
end
|
43
43
|
|
44
44
|
it "ignores json body of incorrect type" do
|
45
45
|
response = process('a: 1', 'text/yaml-xml')
|
46
|
-
response.body.
|
46
|
+
expect(response.body).to eq('a: 1')
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
it "chokes on invalid yaml" do
|
51
|
-
expect
|
51
|
+
expect{ process('{!') }.to raise_error(Faraday::Error::ParsingError)
|
52
52
|
end
|
53
53
|
end
|
data/spec/rashify_spec.rb
CHANGED
@@ -3,52 +3,52 @@ require 'faraday_middleware/response/rashify'
|
|
3
3
|
|
4
4
|
describe FaradayMiddleware::Rashify do
|
5
5
|
|
6
|
-
context
|
6
|
+
context "when used" do
|
7
7
|
let(:rashify) { described_class.new }
|
8
8
|
|
9
|
-
it
|
9
|
+
it "creates a Hashie::Rash from the body" do
|
10
10
|
env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
|
11
11
|
me = rashify.on_complete(env)
|
12
|
-
me.class.
|
12
|
+
expect(me.class).to eq(Hashie::Rash)
|
13
13
|
end
|
14
14
|
|
15
|
-
it
|
15
|
+
it "handles strings" do
|
16
16
|
env = { :body => "Most amazing string EVER" }
|
17
17
|
me = rashify.on_complete(env)
|
18
|
-
me.
|
18
|
+
expect(me).to eq("Most amazing string EVER")
|
19
19
|
end
|
20
20
|
|
21
|
-
it
|
21
|
+
it "handles hashes and decamelcase the keys" do
|
22
22
|
env = { :body => { "name" => "Erik Michaels-Ober", "userName" => "sferik" } }
|
23
23
|
me = rashify.on_complete(env)
|
24
|
-
me.name.
|
25
|
-
me.user_name.
|
24
|
+
expect(me.name).to eq('Erik Michaels-Ober')
|
25
|
+
expect(me.user_name).to eq('sferik')
|
26
26
|
end
|
27
27
|
|
28
|
-
it
|
28
|
+
it "handles arrays" do
|
29
29
|
env = { :body => [123, 456] }
|
30
30
|
values = rashify.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 = rashify.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 mixed arrays" do
|
43
43
|
env = { :body => [123, { "username" => "sferik" }, 456] }
|
44
44
|
values = rashify.on_complete(env)
|
45
|
-
values.first.
|
46
|
-
values.last.
|
47
|
-
values[1].username.
|
45
|
+
expect(values.first).to eq(123)
|
46
|
+
expect(values.last).to eq(456)
|
47
|
+
expect(values[1].username).to eq('sferik')
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
context
|
51
|
+
context "integration test" do
|
52
52
|
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
|
53
53
|
let(:connection) do
|
54
54
|
Faraday::Connection.new do |builder|
|
@@ -59,14 +59,14 @@ describe FaradayMiddleware::Rashify do
|
|
59
59
|
|
60
60
|
# although it is not good practice to pass a hash as the body, if we add ParseJson
|
61
61
|
# to the middleware stack we end up testing two middlewares instead of one
|
62
|
-
it
|
62
|
+
it "creates a Hash from the body" do
|
63
63
|
stubs.get('/hash') {
|
64
64
|
data = { 'name' => 'Erik Michaels-Ober', 'username' => 'sferik' }
|
65
65
|
[200, {'content-type' => 'application/json; charset=utf-8'}, data]
|
66
66
|
}
|
67
67
|
me = connection.get('/hash').body
|
68
|
-
me.name.
|
69
|
-
me.username.
|
68
|
+
expect(me.name).to eq('Erik Michaels-Ober')
|
69
|
+
expect(me.username).to eq('sferik')
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-10-26 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: faraday
|
@@ -170,6 +170,7 @@ files:
|
|
170
170
|
- lib/faraday_middleware/instrumentation.rb
|
171
171
|
- lib/faraday_middleware/rack_compatible.rb
|
172
172
|
- lib/faraday_middleware/request/encode_json.rb
|
173
|
+
- lib/faraday_middleware/request/method_override.rb
|
173
174
|
- lib/faraday_middleware/request/oauth.rb
|
174
175
|
- lib/faraday_middleware/request/oauth2.rb
|
175
176
|
- lib/faraday_middleware/response/caching.rb
|
@@ -190,6 +191,7 @@ files:
|
|
190
191
|
- spec/follow_redirects_spec.rb
|
191
192
|
- spec/helper.rb
|
192
193
|
- spec/mashify_spec.rb
|
194
|
+
- spec/method_override_spec.rb
|
193
195
|
- spec/oauth2_spec.rb
|
194
196
|
- spec/oauth_spec.rb
|
195
197
|
- spec/parse_dates_spec.rb
|
@@ -229,6 +231,7 @@ test_files:
|
|
229
231
|
- spec/follow_redirects_spec.rb
|
230
232
|
- spec/helper.rb
|
231
233
|
- spec/mashify_spec.rb
|
234
|
+
- spec/method_override_spec.rb
|
232
235
|
- spec/oauth2_spec.rb
|
233
236
|
- spec/oauth_spec.rb
|
234
237
|
- spec/parse_dates_spec.rb
|