api_client 0.5.24-java → 0.5.25-java
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/api_client/base.rb +77 -0
- data/lib/api_client/connection/abstract.rb +81 -0
- data/lib/api_client/connection/basic.rb +129 -0
- data/lib/api_client/connection/json.rb +14 -0
- data/lib/api_client/connection/middlewares/request/json.rb +34 -0
- data/lib/api_client/connection/middlewares/request/logger.rb +64 -0
- data/lib/api_client/connection/middlewares/request/oauth.rb +22 -0
- data/lib/api_client/connection/oauth.rb +18 -0
- data/lib/api_client/errors.rb +31 -0
- data/lib/api_client/mixins/configuration.rb +24 -0
- data/lib/api_client/mixins/connection_hooks.rb +24 -0
- data/lib/api_client/mixins/delegation.rb +23 -0
- data/lib/api_client/mixins/inheritance.rb +19 -0
- data/lib/api_client/mixins/instantiation.rb +29 -0
- data/lib/api_client/mixins/scoping.rb +49 -0
- data/lib/api_client/resource/base.rb +67 -0
- data/lib/api_client/resource/name_resolver.rb +37 -0
- data/lib/api_client/resource/scope.rb +73 -0
- data/lib/api_client/scope.rb +125 -0
- data/lib/api_client/utils.rb +18 -0
- data/lib/api_client/version.rb +3 -0
- data/spec/api_client/base/connection_hook_spec.rb +18 -0
- data/spec/api_client/base/delegation_spec.rb +15 -0
- data/spec/api_client/base/inheritance_spec.rb +44 -0
- data/spec/api_client/base/instantiation_spec.rb +55 -0
- data/spec/api_client/base/marshalling_spec.rb +33 -0
- data/spec/api_client/base/parsing_spec.rb +38 -0
- data/spec/api_client/base/scoping_spec.rb +60 -0
- data/spec/api_client/base_spec.rb +107 -0
- data/spec/api_client/connection/abstract_spec.rb +21 -0
- data/spec/api_client/connection/basic_spec.rb +191 -0
- data/spec/api_client/connection/oauth_spec.rb +27 -0
- data/spec/api_client/connection/request/json_spec.rb +30 -0
- data/spec/api_client/connection/request/logger_spec.rb +18 -0
- data/spec/api_client/connection/request/oauth_spec.rb +26 -0
- data/spec/api_client/resource/base_spec.rb +97 -0
- data/spec/api_client/resource/name_spec.rb +19 -0
- data/spec/api_client/resource/scope_spec.rb +122 -0
- data/spec/api_client/scope_spec.rb +204 -0
- data/spec/api_client/utils_spec.rb +32 -0
- data/spec/support/matchers.rb +5 -0
- metadata +62 -1
@@ -0,0 +1,18 @@
|
|
1
|
+
module ApiClient
|
2
|
+
|
3
|
+
module Utils
|
4
|
+
|
5
|
+
def self.deep_merge(hash, other_hash)
|
6
|
+
other_hash.each_pair do |key,v|
|
7
|
+
if hash[key].is_a?(::Hash) and v.is_a?(::Hash)
|
8
|
+
deep_merge hash[key], v
|
9
|
+
else
|
10
|
+
hash[key] = v
|
11
|
+
end
|
12
|
+
end
|
13
|
+
hash
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ApiClient::Base do
|
4
|
+
|
5
|
+
describe '.connection' do
|
6
|
+
|
7
|
+
it "registers a new connection_hook" do
|
8
|
+
ConnectionHookTestProc = lambda {}
|
9
|
+
class ConnectionHookTest < ApiClient::Base
|
10
|
+
connection &ConnectionHookTestProc
|
11
|
+
end
|
12
|
+
ConnectionHookTest.connection_hooks.size.should == 1
|
13
|
+
ConnectionHookTest.connection_hooks.should == [ConnectionHookTestProc]
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ApiClient::Base do
|
4
|
+
|
5
|
+
it "delegates methods to scope" do
|
6
|
+
scope = double
|
7
|
+
ApiClient::Base.stub(:scope).and_return(scope)
|
8
|
+
[:fetch, :get, :put, :post, :patch, :delete, :headers, :endpoint, :options, :adapter, :params, :raw].each do |method|
|
9
|
+
scope.should_receive(method)
|
10
|
+
ApiClient::Base.send(method)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ApiClient::Base do
|
4
|
+
|
5
|
+
describe "subclasses" do
|
6
|
+
|
7
|
+
it "inherit scopes, hooks, namespace and format" do
|
8
|
+
|
9
|
+
class Level1InheritanceTest < ApiClient::Base
|
10
|
+
end
|
11
|
+
|
12
|
+
Level1InheritanceTest.default_scopes.should == []
|
13
|
+
Level1InheritanceTest.connection_hooks.should == []
|
14
|
+
Level1InheritanceTest.namespace.should == nil
|
15
|
+
Level1InheritanceTest.format.should == :json
|
16
|
+
|
17
|
+
Level1InheritanceTest.default_scopes = ['scope1']
|
18
|
+
Level1InheritanceTest.connection_hooks = ['hook1']
|
19
|
+
Level1InheritanceTest.namespace 'level1'
|
20
|
+
Level1InheritanceTest.format :xml
|
21
|
+
|
22
|
+
ApiClient::Base.default_scopes.should == []
|
23
|
+
ApiClient::Base.connection_hooks.should == []
|
24
|
+
ApiClient::Base.namespace.should == nil
|
25
|
+
ApiClient::Base.format.should == :json
|
26
|
+
|
27
|
+
class Level2InheritanceTest < Level1InheritanceTest
|
28
|
+
namespace "level2"
|
29
|
+
format :yaml
|
30
|
+
end
|
31
|
+
|
32
|
+
Level2InheritanceTest.default_scopes.should == ['scope1']
|
33
|
+
Level2InheritanceTest.connection_hooks.should == ['hook1']
|
34
|
+
Level2InheritanceTest.namespace.should == 'level2'
|
35
|
+
Level2InheritanceTest.format.should == :yaml
|
36
|
+
|
37
|
+
Level1InheritanceTest.namespace.should == 'level1'
|
38
|
+
Level1InheritanceTest.format.should == :xml
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ApiClient::Base do
|
4
|
+
|
5
|
+
describe "build" do
|
6
|
+
|
7
|
+
it "instantiates an array of objects and returns an array if passed an array" do
|
8
|
+
result = ApiClient::Base.build [{ :id => 1 }, { :id => 2}]
|
9
|
+
result.should be_an_instance_of(Array)
|
10
|
+
result.first.should be_an_instance_of(ApiClient::Base)
|
11
|
+
result.last.should be_an_instance_of(ApiClient::Base)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "instantiates an object and returns an object if passed an object" do
|
15
|
+
result = ApiClient::Base.build({ :id => 1 })
|
16
|
+
result.should be_an_instance_of(ApiClient::Base)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "build_one" do
|
22
|
+
|
23
|
+
it "extracts the attributes from a namespace if a namespace is provided" do
|
24
|
+
ApiClient::Base.stub(:namespace).and_return("base")
|
25
|
+
result = ApiClient::Base.build({ "base" => { :id => 1 } })
|
26
|
+
result.should be_an_instance_of(ApiClient::Base)
|
27
|
+
result.keys.should == ['id']
|
28
|
+
result.id.should == 1
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "sub hashes" do
|
34
|
+
|
35
|
+
it "are Hashie::Mashes" do
|
36
|
+
result = ApiClient::Base.build({ :id => 1, :subhash => { :foo => 1 } })
|
37
|
+
result.subhash.should be_a(Hashie::Mash)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "original_scope" do
|
43
|
+
|
44
|
+
it "holds the original scope it was created from" do
|
45
|
+
scope = ApiClient::Base.params(:foo => 1).headers('token' => 'aaa').options("some" => "option")
|
46
|
+
|
47
|
+
instance = scope.build :key => 'value'
|
48
|
+
instance.original_scope.headers.should == {'token' => 'aaa'}
|
49
|
+
instance.original_scope.params.should be_empty
|
50
|
+
instance.original_scope.options.should be_empty
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Marshaling of ApiClient objects" do
|
4
|
+
ConnectionProc = Proc.new {}
|
5
|
+
AlwaysProc = Proc.new {}
|
6
|
+
|
7
|
+
class Entity < ApiClient::Base
|
8
|
+
connection &ConnectionProc
|
9
|
+
always &AlwaysProc
|
10
|
+
|
11
|
+
def mutated_state?
|
12
|
+
@state == "mutated"
|
13
|
+
end
|
14
|
+
|
15
|
+
def mutate_state!
|
16
|
+
@state = "mutated"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "is marshallable by default" do
|
21
|
+
scope = Entity.params(:foo => 1).headers("token" => "aaa").options("some" => "option")
|
22
|
+
entity = scope.build :key => "value"
|
23
|
+
|
24
|
+
entity.mutated_state?.should == false
|
25
|
+
entity.mutate_state!
|
26
|
+
entity.mutated_state?.should == true
|
27
|
+
|
28
|
+
reloaded = Marshal.load(Marshal.dump(entity))
|
29
|
+
|
30
|
+
reloaded.should == entity
|
31
|
+
reloaded.mutated_state?.should == true
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
require "multi_xml"
|
4
|
+
|
5
|
+
describe ApiClient::Base do
|
6
|
+
|
7
|
+
it "parses json if json is set as format" do
|
8
|
+
ApiClient::Base.stub(:format).and_return(:json)
|
9
|
+
parsed = ApiClient::Base.parse('{"a":"1"}')
|
10
|
+
parsed.should == {"a"=> "1"}
|
11
|
+
end
|
12
|
+
|
13
|
+
it "parses xml if xml is set as format" do
|
14
|
+
ApiClient::Base.stub(:format).and_return(:xml)
|
15
|
+
parsed = ApiClient::Base.parse('<a>1</a>')
|
16
|
+
parsed.should == {"a"=> "1"}
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns the string if parser is not found" do
|
20
|
+
ApiClient::Base.stub(:format).and_return(:unknown)
|
21
|
+
parsed = ApiClient::Base.parse('a:1')
|
22
|
+
parsed.should == "a:1"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "extracts the body of a Faraday::Response if it is provided" do
|
26
|
+
response = Faraday::Response.new(:body => '{"a": "1"}')
|
27
|
+
ApiClient::Base.stub(:format).and_return(:json)
|
28
|
+
parsed = ApiClient::Base.parse(response)
|
29
|
+
parsed.should == {"a"=> "1"}
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns nil if the response is 204" do
|
33
|
+
response = Faraday::Response.new(:body => nil, :status => 204)
|
34
|
+
ApiClient::Base.stub(:format).and_return(:json)
|
35
|
+
parsed = ApiClient::Base.parse(response)
|
36
|
+
parsed.should == nil
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ApiClient::Base do
|
4
|
+
|
5
|
+
describe '.always' do
|
6
|
+
|
7
|
+
it "registers a new default_scope" do
|
8
|
+
AlwaysTestProc = lambda {}
|
9
|
+
class AlwaysTest < ApiClient::Base
|
10
|
+
always &AlwaysTestProc
|
11
|
+
end
|
12
|
+
AlwaysTest.default_scopes.size.should == 1
|
13
|
+
AlwaysTest.default_scopes.should == [AlwaysTestProc]
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.scope' do
|
19
|
+
|
20
|
+
it "returns a ApiClient::Scope instance" do
|
21
|
+
ApiClient::Base.scope.should be_an_instance_of(ApiClient::Scope)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '.scope_thread_attribute_name' do
|
27
|
+
|
28
|
+
it "returns the key under which all .scoped calls should be stored" do
|
29
|
+
ApiClient::Base.scope_thread_attribute_name.should == "ApiClient::Base_scope"
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '.scoped' do
|
35
|
+
|
36
|
+
it "stores the scope in the thread context, attached to class name" do
|
37
|
+
mock_scope3 = double
|
38
|
+
ApiClient::Base.scoped(mock_scope3) do
|
39
|
+
Thread.new {
|
40
|
+
mock_scope2 = double
|
41
|
+
ApiClient::Base.scoped(mock_scope2) do
|
42
|
+
ApiClient::Base.scope.should == mock_scope2
|
43
|
+
Thread.current[ApiClient::Base.scope_thread_attribute_name].should == [mock_scope2]
|
44
|
+
end
|
45
|
+
}
|
46
|
+
ApiClient::Base.scope.should == mock_scope3
|
47
|
+
Thread.current[ApiClient::Base.scope_thread_attribute_name].should == [mock_scope3]
|
48
|
+
end
|
49
|
+
Thread.new {
|
50
|
+
mock_scope = double
|
51
|
+
ApiClient::Base.scoped(mock_scope) do
|
52
|
+
ApiClient::Base.scope.should == mock_scope
|
53
|
+
Thread.current[ApiClient::Base.scope_thread_attribute_name].should == [mock_scope]
|
54
|
+
end
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ApiClient::Base do
|
4
|
+
|
5
|
+
it "is a subclass of Hashie::Mash" do
|
6
|
+
ApiClient::Base.should inherit_from(Hashie::Mash)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "responds to #id" do
|
10
|
+
subject.should respond_to("id")
|
11
|
+
end
|
12
|
+
|
13
|
+
class StrictApi < ApiClient::Base
|
14
|
+
def accessor_of_x
|
15
|
+
self.x
|
16
|
+
end
|
17
|
+
|
18
|
+
def strict_attr_reader?
|
19
|
+
true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class StrictDescendant < StrictApi
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when used in an array" do
|
27
|
+
|
28
|
+
subject { [StrictApi.new] }
|
29
|
+
|
30
|
+
it "does not break Array#flatten" do
|
31
|
+
lambda { subject.flatten }.should_not raise_error
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#inspect" do
|
37
|
+
|
38
|
+
it "has a nice inspect" do
|
39
|
+
subject.update(:id => 1).inspect.should == '#<ApiClient::Base id: 1>'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "presents all fields in inspect" do
|
43
|
+
subject.update(:id => 1, :foo => 'OMG')
|
44
|
+
subject.inspect.should == '#<ApiClient::Base id: 1, foo: "OMG">'
|
45
|
+
end
|
46
|
+
|
47
|
+
it "inspects subobjects properly" do
|
48
|
+
subject.update(:id => 1, :sub => [1,2])
|
49
|
+
subject.inspect.should == '#<ApiClient::Base id: 1, sub: #<Hashie::Array [1, 2]>>'
|
50
|
+
end
|
51
|
+
|
52
|
+
it "makes sure id is the first key" do
|
53
|
+
|
54
|
+
subject.update(:foo => 'OMG', :id => 1)
|
55
|
+
subject.inspect.should == '#<ApiClient::Base id: 1, foo: "OMG">'
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "strict_read" do
|
61
|
+
it "fails if the key is missing and strict_read is set" do
|
62
|
+
api = StrictApi.new
|
63
|
+
lambda { api.missing }.should raise_error do |error|
|
64
|
+
error_type = error.class.to_s
|
65
|
+
error_type.should match(/KeyError|IndexError/)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it "doesn't fail if strict_read is not set" do
|
70
|
+
api = ApiClient::Base.new
|
71
|
+
api.missing
|
72
|
+
end
|
73
|
+
|
74
|
+
it "doesn't fail if the key was set after object was created" do
|
75
|
+
api = StrictApi.new
|
76
|
+
lambda { api.not_missing = 1 }.should_not raise_error
|
77
|
+
api.not_missing.should == 1
|
78
|
+
end
|
79
|
+
|
80
|
+
it "doesn't fail for predicate methods if key is not set" do
|
81
|
+
api = StrictApi.new
|
82
|
+
lambda { api.missing? }.should_not raise_error
|
83
|
+
api.missing?.should be_false
|
84
|
+
end
|
85
|
+
|
86
|
+
it "allows to call methods" do
|
87
|
+
api = StrictApi.new(:x => 14)
|
88
|
+
api.accessor_of_x.should == 14
|
89
|
+
end
|
90
|
+
|
91
|
+
it "calling method which asks for missing attribute fails" do
|
92
|
+
api = StrictApi.new
|
93
|
+
lambda { api.accessor_of_x }.should raise_error do |error|
|
94
|
+
error_type = error.class.to_s
|
95
|
+
error_type.should match(/KeyError|IndexError/)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
it "passes strict api configuration to subclasses" do
|
100
|
+
api = StrictDescendant.new
|
101
|
+
lambda { api.missing }.should raise_error do |error|
|
102
|
+
error_type = error.class.to_s
|
103
|
+
error_type.should match(/KeyError|IndexError/)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ApiClient::Connection::Abstract do
|
4
|
+
|
5
|
+
class ConnectionSubclass < ApiClient::Connection::Abstract
|
6
|
+
end
|
7
|
+
|
8
|
+
it "does not raise an error when instantiating a subclass" do
|
9
|
+
lambda {
|
10
|
+
ConnectionSubclass.new("http://google.com")
|
11
|
+
}.should_not raise_error()
|
12
|
+
end
|
13
|
+
|
14
|
+
it "raises an error when instantiating directly and not as a subclass" do
|
15
|
+
lambda {
|
16
|
+
ApiClient::Connection::Abstract.new("http://google.com")
|
17
|
+
}.should raise_error("Cannot instantiate abstract class")
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,191 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ApiClient::Connection::Basic do
|
4
|
+
|
5
|
+
it "has a nice inspect" do
|
6
|
+
instance = ApiClient::Connection::Basic.new("http://google.com")
|
7
|
+
instance.inspect.should == '#<ApiClient::Connection::Basic endpoint: "http://google.com">'
|
8
|
+
end
|
9
|
+
|
10
|
+
it "adds basic middlewares to faraday" do
|
11
|
+
instance = ApiClient::Connection::Basic.new("http://google.com")
|
12
|
+
instance.handler.builder.handlers.collect(&:name).should == ["Faraday::Request::UrlEncoded", "Faraday::Adapter::NetHttp"]
|
13
|
+
end
|
14
|
+
|
15
|
+
it "adds the logger middlewares to faraday if ApiClient.logger is available" do
|
16
|
+
logger = double
|
17
|
+
ApiClient.stub(:logger).and_return(logger)
|
18
|
+
instance = ApiClient::Connection::Basic.new("http://google.com")
|
19
|
+
instance.handler.builder.handlers.collect(&:name).should == [
|
20
|
+
"ApiClient::Connection::Middlewares::Request::Logger",
|
21
|
+
"Faraday::Request::UrlEncoded",
|
22
|
+
"Faraday::Adapter::NetHttp"
|
23
|
+
]
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
it "creates a Faraday object on initialize" do
|
28
|
+
instance = ApiClient::Connection::Basic.new("http://google.com")
|
29
|
+
instance.handler.should be_an_instance_of(Faraday::Connection)
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "requests" do
|
33
|
+
|
34
|
+
before do
|
35
|
+
@instance = ApiClient::Connection::Basic.new("http://google.com")
|
36
|
+
@headers = { "header" => "token" }
|
37
|
+
@params = { "param" => "1", "nested" => { "param" => "1" } }
|
38
|
+
@response = Faraday::Response.new(:status => 200)
|
39
|
+
@faraday_request_params = double
|
40
|
+
@faraday_request = double(:params => @faraday_request_params)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "can perform GET requests" do
|
44
|
+
@instance.handler.
|
45
|
+
should_receive(:run_request).
|
46
|
+
with(:get, '/home', nil, @headers).
|
47
|
+
and_yield(@faraday_request).
|
48
|
+
and_return(@response)
|
49
|
+
@faraday_request_params.should_receive(:update).with(@params)
|
50
|
+
@instance.get "/home", @params, @headers
|
51
|
+
end
|
52
|
+
|
53
|
+
it "can perform POST requests" do
|
54
|
+
@instance.handler.
|
55
|
+
should_receive(:run_request).
|
56
|
+
with(:post, '/home', @params, @headers).
|
57
|
+
and_return(@response)
|
58
|
+
@instance.post "/home", @params, @headers
|
59
|
+
end
|
60
|
+
|
61
|
+
it "can perform PATCH requests" do
|
62
|
+
@instance.handler.
|
63
|
+
should_receive(:run_request).
|
64
|
+
with(:patch, '/home', @params, @headers).
|
65
|
+
and_return(@response)
|
66
|
+
@instance.patch "/home", @params, @headers
|
67
|
+
end
|
68
|
+
|
69
|
+
it "can perform PUT requests" do
|
70
|
+
@instance.handler.
|
71
|
+
should_receive(:run_request).
|
72
|
+
with(:put, '/home', @params, @headers).
|
73
|
+
and_return(@response)
|
74
|
+
@instance.put "/home", @params, @headers
|
75
|
+
end
|
76
|
+
|
77
|
+
it "can perform DELETE requests" do
|
78
|
+
@instance.handler.
|
79
|
+
should_receive(:run_request).
|
80
|
+
with(:delete, '/home', nil, @headers).
|
81
|
+
and_yield(@faraday_request).
|
82
|
+
and_return(@response)
|
83
|
+
@faraday_request_params.should_receive(:update).with(@params)
|
84
|
+
@instance.delete "/home", @params, @headers
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "#handle_response" do
|
90
|
+
let(:request) { double }
|
91
|
+
|
92
|
+
before do
|
93
|
+
@instance = ApiClient::Connection::Basic.new("http://google.com")
|
94
|
+
@response = Faraday::Response.new(:status => 200)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "raises an ApiClient::Errors::ConnectionFailed if there is no response" do
|
98
|
+
lambda {
|
99
|
+
@instance.send :handle_response, request, nil
|
100
|
+
}.should raise_error(ApiClient::Errors::ConnectionFailed, "ApiClient::Errors::ConnectionFailed")
|
101
|
+
end
|
102
|
+
|
103
|
+
it "raises an ApiClient::Errors::Unauthorized if status is 401" do
|
104
|
+
@response.env[:status] = 401
|
105
|
+
lambda {
|
106
|
+
@instance.send :handle_response, request, @response
|
107
|
+
}.should raise_error(ApiClient::Errors::Unauthorized, "Status code: 401")
|
108
|
+
end
|
109
|
+
|
110
|
+
it "raises an ApiClient::Errors::Forbidden if status is 403" do
|
111
|
+
@response.env[:status] = 403
|
112
|
+
lambda {
|
113
|
+
@instance.send :handle_response, request, @response
|
114
|
+
}.should raise_error(ApiClient::Errors::Forbidden, "Status code: 403")
|
115
|
+
end
|
116
|
+
|
117
|
+
it "raises an ApiClient::Errors::NotFound if status is 404" do
|
118
|
+
@response.env[:status] = 404
|
119
|
+
lambda {
|
120
|
+
@instance.send :handle_response, request, @response
|
121
|
+
}.should raise_error(ApiClient::Errors::NotFound, "Status code: 404")
|
122
|
+
end
|
123
|
+
|
124
|
+
it "raises an ApiClient::Errors::BadRequest if status is 400" do
|
125
|
+
@response.env[:status] = 400
|
126
|
+
lambda {
|
127
|
+
@instance.send :handle_response, request, @response
|
128
|
+
}.should raise_error(ApiClient::Errors::BadRequest, "Status code: 400")
|
129
|
+
end
|
130
|
+
|
131
|
+
it "raises an ApiClient::Errors::Unsupported if status is 406" do
|
132
|
+
@response.env[:status] = 406
|
133
|
+
lambda {
|
134
|
+
@instance.send :handle_response, request, @response
|
135
|
+
}.should raise_error(ApiClient::Errors::Unsupported, "Status code: 406")
|
136
|
+
end
|
137
|
+
|
138
|
+
it "raises an ApiClient::Errors::Conflict if status is 409" do
|
139
|
+
@response.env[:status] = 409
|
140
|
+
lambda {
|
141
|
+
@instance.send :handle_response, request, @response
|
142
|
+
}.should raise_error(ApiClient::Errors::Conflict, "Status code: 409")
|
143
|
+
end
|
144
|
+
|
145
|
+
it "raises an ApiClient::Errors::Gone if status is 410" do
|
146
|
+
@response.env[:status] = 410
|
147
|
+
lambda {
|
148
|
+
@instance.send :handle_response, request, @response
|
149
|
+
}.should raise_error(ApiClient::Errors::Gone, "Status code: 410")
|
150
|
+
end
|
151
|
+
|
152
|
+
it "raises an ApiClient::Errors::Unsupported if status is 422" do
|
153
|
+
@response.env[:status] = 422
|
154
|
+
lambda {
|
155
|
+
@instance.send :handle_response, request, @response
|
156
|
+
}.should raise_error(ApiClient::Errors::UnprocessableEntity, @response.body)
|
157
|
+
end
|
158
|
+
|
159
|
+
it "raises an ApiClient::Errors::Locked if status is 423" do
|
160
|
+
@response.env[:status] = 423
|
161
|
+
lambda {
|
162
|
+
@instance.send :handle_response, request, @response
|
163
|
+
}.should raise_error(ApiClient::Errors::Locked, @response.body)
|
164
|
+
end
|
165
|
+
|
166
|
+
it "raises an ApiClient::Errors::TooManyRequests if status is 429" do
|
167
|
+
@response.env[:status] = 429
|
168
|
+
lambda {
|
169
|
+
@instance.send :handle_response, request, @response
|
170
|
+
}.should raise_error(ApiClient::Errors::TooManyRequests, @response.body)
|
171
|
+
end
|
172
|
+
|
173
|
+
it "raises an ApiClient::Errors::Unsupported if status is 300..399" do
|
174
|
+
location = "https://google.com"
|
175
|
+
@response.env[:status] = 302
|
176
|
+
@response.env[:response_headers] = { 'Location' => location }
|
177
|
+
lambda {
|
178
|
+
@instance.send :handle_response, request, @response
|
179
|
+
}.should raise_error(ApiClient::Errors::Redirect, location)
|
180
|
+
end
|
181
|
+
|
182
|
+
it "raises an ApiClient::Errors::ServerError if status is 500..599" do
|
183
|
+
@response.env[:status] = 502
|
184
|
+
lambda {
|
185
|
+
@instance.send :handle_response, request, @response
|
186
|
+
}.should raise_error(ApiClient::Errors::ServerError, "Status code: 502")
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|