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.
@@ -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
@@ -1,53 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/response/parse_yaml'
3
-
4
- describe FaradayMiddleware::ParseYaml, :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 "returns false for empty body" do
11
- expect(process('').body).to be_false
12
- end
13
-
14
- it "parses yaml 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 yaml 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 => /\byaml$/} }
38
-
39
- it "parses json body of correct type" do
40
- response = process('a: 1', 'application/x-yaml')
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/yaml-xml')
46
- expect(response.body).to eq('a: 1')
47
- end
48
- end
49
-
50
- it "chokes on invalid yaml" do
51
- expect{ process('{!') }.to raise_error(Faraday::Error::ParsingError)
52
- end
53
- end
data/spec/rashify_spec.rb DELETED
@@ -1,72 +0,0 @@
1
- require 'helper'
2
- require 'faraday_middleware/response/rashify'
3
-
4
- describe FaradayMiddleware::Rashify do
5
-
6
- context "when used" do
7
- let(:rashify) { described_class.new }
8
-
9
- it "creates a Hashie::Rash from the body" do
10
- env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
11
- me = rashify.on_complete(env)
12
- expect(me.class).to eq(Hashie::Rash)
13
- end
14
-
15
- it "handles strings" do
16
- env = { :body => "Most amazing string EVER" }
17
- me = rashify.on_complete(env)
18
- expect(me).to eq("Most amazing string EVER")
19
- end
20
-
21
- it "handles hashes and decamelcase the keys" do
22
- env = { :body => { "name" => "Erik Michaels-Ober", "userName" => "sferik" } }
23
- me = rashify.on_complete(env)
24
- expect(me.name).to eq('Erik Michaels-Ober')
25
- expect(me.user_name).to eq('sferik')
26
- end
27
-
28
- it "handles arrays" do
29
- env = { :body => [123, 456] }
30
- values = rashify.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 = rashify.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 mixed arrays" do
43
- env = { :body => [123, { "username" => "sferik" }, 456] }
44
- values = rashify.on_complete(env)
45
- expect(values.first).to eq(123)
46
- expect(values.last).to eq(456)
47
- expect(values[1].username).to eq('sferik')
48
- end
49
- end
50
-
51
- context "integration test" do
52
- let(:stubs) { Faraday::Adapter::Test::Stubs.new }
53
- let(:connection) do
54
- Faraday::Connection.new do |builder|
55
- builder.adapter :test, stubs
56
- builder.use described_class
57
- end
58
- end
59
-
60
- # although it is not good practice to pass a hash as the body, if we add ParseJson
61
- # to the middleware stack we end up testing two middlewares instead of one
62
- it "creates a Hash from the body" do
63
- stubs.get('/hash') {
64
- data = { 'name' => 'Erik Michaels-Ober', 'username' => 'sferik' }
65
- [200, {'content-type' => 'application/json; charset=utf-8'}, data]
66
- }
67
- me = connection.get('/hash').body
68
- expect(me.name).to eq('Erik Michaels-Ober')
69
- expect(me.username).to eq('sferik')
70
- end
71
- end
72
- end