active_rest_client 1.0.6 → 1.0.7
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/.travis.yml +1 -0
- data/README.md +26 -5
- data/active_rest_client.gemspec +2 -2
- data/lib/active_rest_client/base.rb +31 -0
- data/lib/active_rest_client/configuration.rb +36 -0
- data/lib/active_rest_client/request.rb +34 -1
- data/lib/active_rest_client/result_iterator.rb +18 -0
- data/lib/active_rest_client/version.rb +1 -1
- data/spec/lib/base_spec.rb +68 -20
- data/spec/lib/caching_spec.rb +23 -23
- data/spec/lib/configuration_spec.rb +38 -14
- data/spec/lib/instrumentation_spec.rb +7 -8
- data/spec/lib/lazy_association_loader_spec.rb +7 -7
- data/spec/lib/lazy_loader_spec.rb +5 -5
- data/spec/lib/logger_spec.rb +13 -13
- data/spec/lib/proxy_spec.rb +15 -15
- data/spec/lib/recording_spec.rb +2 -2
- data/spec/lib/request_spec.rb +133 -98
- data/spec/lib/result_iterator_spec.rb +34 -5
- data/spec/lib/validation_spec.rb +1 -1
- data/spec/spec_helper.rb +11 -2
- metadata +6 -6
data/spec/lib/caching_spec.rb
CHANGED
@@ -10,7 +10,7 @@ describe ActiveRestClient::Caching do
|
|
10
10
|
class CachingExample1
|
11
11
|
include ActiveRestClient::Caching
|
12
12
|
end
|
13
|
-
expect(CachingExample1.perform_caching).to
|
13
|
+
expect(CachingExample1.perform_caching).to be_falsey
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should be able to have caching enabled without affecting ActiveRestClient::Base" do
|
@@ -18,19 +18,19 @@ describe ActiveRestClient::Caching do
|
|
18
18
|
include ActiveRestClient::Caching
|
19
19
|
end
|
20
20
|
CachingExample2.perform_caching true
|
21
|
-
expect(CachingExample2.perform_caching).to
|
22
|
-
expect(ActiveRestClient::Base.perform_caching).to
|
21
|
+
expect(CachingExample2.perform_caching).to be_truthy
|
22
|
+
expect(ActiveRestClient::Base.perform_caching).to be_falsey
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should be possible to enable caching for all objects" do
|
26
26
|
class CachingExample3 < ActiveRestClient::Base ; end
|
27
27
|
ActiveRestClient::Base._reset_caching!
|
28
28
|
|
29
|
-
expect(ActiveRestClient::Base.perform_caching).to
|
29
|
+
expect(ActiveRestClient::Base.perform_caching).to be_falsey
|
30
30
|
|
31
31
|
ActiveRestClient::Base.perform_caching = true
|
32
|
-
expect(ActiveRestClient::Base.perform_caching).to
|
33
|
-
expect(CachingExample3.perform_caching).to
|
32
|
+
expect(ActiveRestClient::Base.perform_caching).to be_truthy
|
33
|
+
expect(CachingExample3.perform_caching).to be_truthy
|
34
34
|
|
35
35
|
ActiveRestClient::Base._reset_caching!
|
36
36
|
end
|
@@ -110,8 +110,8 @@ describe ActiveRestClient::Caching do
|
|
110
110
|
status:200,
|
111
111
|
result:@cached_object,
|
112
112
|
etag:@etag)
|
113
|
-
CachingExampleCacheStore5.
|
114
|
-
ActiveRestClient::Connection.
|
113
|
+
expect_any_instance_of(CachingExampleCacheStore5).to receive(:read).once.with("Person:/").and_return(Marshal.dump(cached_response))
|
114
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", hash_including("If-None-Match" => @etag)).and_return(OpenStruct.new(status:304, headers:{}))
|
115
115
|
ret = Person.all
|
116
116
|
expect(ret.first_name).to eq("Johnny")
|
117
117
|
end
|
@@ -139,8 +139,8 @@ describe ActiveRestClient::Caching do
|
|
139
139
|
status:200,
|
140
140
|
result:@cached_object,
|
141
141
|
expires:Time.now + 30)
|
142
|
-
CachingExampleCacheStore5.
|
143
|
-
ActiveRestClient::Connection.
|
142
|
+
expect_any_instance_of(CachingExampleCacheStore5).to receive(:read).once.with("Person:/").and_return(Marshal.dump(cached_response))
|
143
|
+
expect_any_instance_of(ActiveRestClient::Connection).not_to receive(:get)
|
144
144
|
ret = Person.all
|
145
145
|
expect(ret.first_name).to eq("Johnny")
|
146
146
|
end
|
@@ -150,8 +150,8 @@ describe ActiveRestClient::Caching do
|
|
150
150
|
status:200,
|
151
151
|
result:@cached_object,
|
152
152
|
expires:Time.now + 30)
|
153
|
-
CachingExampleCacheStore5.
|
154
|
-
ActiveRestClient::Connection.
|
153
|
+
expect_any_instance_of(CachingExampleCacheStore5).to receive(:read).once.with("Person:/").and_return(Marshal.dump(cached_response))
|
154
|
+
expect_any_instance_of(ActiveRestClient::Connection).not_to receive(:get)
|
155
155
|
p = Person.new(first_name:"Billy")
|
156
156
|
ret = p.all({})
|
157
157
|
expect(ret.first_name).to eq("Johnny")
|
@@ -168,32 +168,32 @@ describe ActiveRestClient::Caching do
|
|
168
168
|
result:object,
|
169
169
|
etag:@etag,
|
170
170
|
expires:Time.now + 30)
|
171
|
-
CachingExampleCacheStore5.
|
172
|
-
ActiveRestClient::Connection.
|
171
|
+
expect_any_instance_of(CachingExampleCacheStore5).to receive(:read).once.with("Person:/").and_return(Marshal.dump(cached_response))
|
172
|
+
expect_any_instance_of(ActiveRestClient::Connection).not_to receive(:get)
|
173
173
|
ret = Person.all
|
174
174
|
expect(ret.first.first_name).to eq("Johnny")
|
175
175
|
expect(ret._status).to eq(200)
|
176
176
|
end
|
177
177
|
|
178
178
|
it "should not write the response to the cache unless if has caching headers" do
|
179
|
-
CachingExampleCacheStore5.
|
180
|
-
CachingExampleCacheStore5.
|
181
|
-
ActiveRestClient::Connection.
|
179
|
+
expect_any_instance_of(CachingExampleCacheStore5).to receive(:read).once.with("Person:/").and_return(nil)
|
180
|
+
expect_any_instance_of(CachingExampleCacheStore5).not_to receive(:write)
|
181
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(status:200, body:"{\"result\":true}", headers:{}))
|
182
182
|
ret = Person.all
|
183
183
|
end
|
184
184
|
|
185
185
|
it "should write the response to the cache if there's an etag" do
|
186
|
-
CachingExampleCacheStore5.
|
187
|
-
CachingExampleCacheStore5.
|
188
|
-
ActiveRestClient::Connection.
|
186
|
+
expect_any_instance_of(CachingExampleCacheStore5).to receive(:read).once.with("Person:/").and_return(nil)
|
187
|
+
expect_any_instance_of(CachingExampleCacheStore5).to receive(:write).once.with("Person:/", an_instance_of(String), {})
|
188
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(status:200, body:"{\"result\":true}", headers:{etag:"1234567890"}))
|
189
189
|
Person.perform_caching true
|
190
190
|
ret = Person.all
|
191
191
|
end
|
192
192
|
|
193
193
|
it "should write the response to the cache if there's a hard expiry" do
|
194
|
-
CachingExampleCacheStore5.
|
195
|
-
CachingExampleCacheStore5.
|
196
|
-
ActiveRestClient::Connection.
|
194
|
+
expect_any_instance_of(CachingExampleCacheStore5).to receive(:read).once.with("Person:/").and_return(nil)
|
195
|
+
expect_any_instance_of(CachingExampleCacheStore5).to receive(:write).once.with("Person:/", an_instance_of(String), an_instance_of(Hash))
|
196
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(status:200, body:"{\"result\":true}", headers:{expires:(Time.now + 30).rfc822}))
|
197
197
|
Person.perform_caching = true
|
198
198
|
ret = Person.all
|
199
199
|
end
|
@@ -8,6 +8,8 @@ describe ActiveRestClient::Configuration do
|
|
8
8
|
class ConfigurationExample
|
9
9
|
include ActiveRestClient::Configuration
|
10
10
|
base_url "http://www.example.com"
|
11
|
+
username "john"
|
12
|
+
password "smith"
|
11
13
|
request_body_type :json
|
12
14
|
end
|
13
15
|
|
@@ -20,12 +22,12 @@ describe ActiveRestClient::Configuration do
|
|
20
22
|
class UnusuedConfigurationExample1
|
21
23
|
include ActiveRestClient::Configuration
|
22
24
|
end
|
23
|
-
expect(UnusuedConfigurationExample1.whiny_missing).to
|
25
|
+
expect(UnusuedConfigurationExample1.whiny_missing).to be_falsey
|
24
26
|
end
|
25
27
|
|
26
28
|
it "should allow whiny missing methods to be enabled" do
|
27
29
|
ConfigurationExample.whiny_missing true
|
28
|
-
expect(ConfigurationExample.whiny_missing).to
|
30
|
+
expect(ConfigurationExample.whiny_missing).to be_truthy
|
29
31
|
end
|
30
32
|
|
31
33
|
it "should remember the set base_url" do
|
@@ -40,11 +42,13 @@ describe ActiveRestClient::Configuration do
|
|
40
42
|
it "should remove a trailing slash from a globally configured base_url" do
|
41
43
|
ActiveRestClient::Base.base_url = "http://general.example.com/"
|
42
44
|
expect(ConfigurationExample.base_url).to eq("http://www.example.com")
|
45
|
+
ActiveRestClient::Base.base_url = ""
|
43
46
|
end
|
44
47
|
|
45
48
|
it "should remember the set base_url on the base class if a more specific one hasn't been set" do
|
46
49
|
ActiveRestClient::Base.base_url = "http://general.example.com"
|
47
50
|
expect(ConfigurationExampleBare.base_url).to eq("http://general.example.com")
|
51
|
+
ActiveRestClient::Base.base_url = ""
|
48
52
|
end
|
49
53
|
|
50
54
|
it "should remove a trailing slash from a specific class configured base_url" do
|
@@ -55,6 +59,26 @@ describe ActiveRestClient::Configuration do
|
|
55
59
|
expect(ConfigurationExample2.base_url).to eq("http://specific.example.com")
|
56
60
|
end
|
57
61
|
|
62
|
+
it "should remember the set username" do
|
63
|
+
expect(ConfigurationExample.username).to eq("john")
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should remember the set username on a class, overriding a general one" do
|
67
|
+
ActiveRestClient::Base.username = "bill"
|
68
|
+
expect(ConfigurationExample.username).to eq("john")
|
69
|
+
ActiveRestClient::Base.username = nil
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should remember the set password" do
|
73
|
+
expect(ConfigurationExample.password).to eq("smith")
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should remember the set password on a class, overriding a general one" do
|
77
|
+
ActiveRestClient::Base.password = "bloggs"
|
78
|
+
expect(ConfigurationExample.password).to eq("smith")
|
79
|
+
ActiveRestClient::Base.password = nil
|
80
|
+
end
|
81
|
+
|
58
82
|
it "should default to a form_encoded request_body_type" do
|
59
83
|
expect(ActiveRestClient::Base.request_body_type).to eq(:form_encoded)
|
60
84
|
end
|
@@ -73,7 +97,7 @@ describe ActiveRestClient::Configuration do
|
|
73
97
|
class LazyLoadingConfigurationExample1
|
74
98
|
include ActiveRestClient::Configuration
|
75
99
|
end
|
76
|
-
expect(LazyLoadingConfigurationExample1.lazy_load?).to
|
100
|
+
expect(LazyLoadingConfigurationExample1.lazy_load?).to be_falsey
|
77
101
|
end
|
78
102
|
|
79
103
|
it "should be able to switch on lazy loading" do
|
@@ -81,14 +105,14 @@ describe ActiveRestClient::Configuration do
|
|
81
105
|
include ActiveRestClient::Configuration
|
82
106
|
lazy_load!
|
83
107
|
end
|
84
|
-
expect(LazyLoadingConfigurationExample2.lazy_load?).to
|
108
|
+
expect(LazyLoadingConfigurationExample2.lazy_load?).to be_truthy
|
85
109
|
end
|
86
110
|
|
87
111
|
it "should default to non-verbose loggingg" do
|
88
112
|
class VerboseConfigurationExample1
|
89
113
|
include ActiveRestClient::Configuration
|
90
114
|
end
|
91
|
-
expect(VerboseConfigurationExample1.verbose).to
|
115
|
+
expect(VerboseConfigurationExample1.verbose).to be_falsey
|
92
116
|
end
|
93
117
|
|
94
118
|
it "should be able to switch on verbose logging" do
|
@@ -100,34 +124,34 @@ describe ActiveRestClient::Configuration do
|
|
100
124
|
include ActiveRestClient::Configuration
|
101
125
|
verbose true
|
102
126
|
end
|
103
|
-
expect(VerboseConfigurationExample2.verbose).to
|
104
|
-
expect(VerboseConfigurationExample3.verbose).to
|
127
|
+
expect(VerboseConfigurationExample2.verbose).to be_truthy
|
128
|
+
expect(VerboseConfigurationExample3.verbose).to be_truthy
|
105
129
|
end
|
106
130
|
|
107
131
|
it "should store a translator given" do
|
108
132
|
expect{ ConfigurationExample.send(:translator) }.to_not raise_error
|
109
|
-
ConfigurationExample.send(:translator, String)
|
110
|
-
expect
|
133
|
+
ConfigurationExample.send(:translator, String.new)
|
134
|
+
expect(ConfigurationExample.translator).to respond_to(:length)
|
111
135
|
end
|
112
136
|
|
113
137
|
it "should store a proxy given" do
|
114
138
|
expect{ ConfigurationExample.send(:proxy) }.to_not raise_error
|
115
|
-
ConfigurationExample.send(:proxy, String)
|
116
|
-
expect
|
139
|
+
ConfigurationExample.send(:proxy, String.new)
|
140
|
+
expect(ConfigurationExample.proxy).to respond_to(:length)
|
117
141
|
end
|
118
142
|
|
119
143
|
describe "faraday_config" do
|
120
144
|
let(:faraday_double){double(:faraday).as_null_object}
|
121
145
|
|
122
146
|
it "should use default adapter if no other block set" do
|
123
|
-
faraday_double.
|
147
|
+
expect(faraday_double).to receive(:adapter).with(:patron)
|
124
148
|
ConfigurationExample.faraday_config.call(faraday_double)
|
125
149
|
end
|
126
150
|
|
127
151
|
it "should us set adapter if no other block set" do
|
128
152
|
ConfigurationExample.adapter = :net_http
|
129
153
|
|
130
|
-
faraday_double.
|
154
|
+
expect(faraday_double).to receive(:adapter).with(:net_http)
|
131
155
|
|
132
156
|
ConfigurationExample.faraday_config.call(faraday_double)
|
133
157
|
end
|
@@ -135,7 +159,7 @@ describe ActiveRestClient::Configuration do
|
|
135
159
|
it "should use the adapter of the passed in faraday_config block" do
|
136
160
|
ConfigurationExample.faraday_config {|faraday| faraday.adapter(:rack)}
|
137
161
|
|
138
|
-
faraday_double.
|
162
|
+
expect(faraday_double).to receive(:adapter).with(:rack)
|
139
163
|
|
140
164
|
ConfigurationExample.faraday_config.call(faraday_double)
|
141
165
|
end
|
@@ -9,29 +9,28 @@ end
|
|
9
9
|
describe ActiveRestClient::Instrumentation do
|
10
10
|
it "should save a load hook to include the instrumentation" do
|
11
11
|
hook_tester = double("HookTester")
|
12
|
-
hook_tester.
|
12
|
+
expect(hook_tester).to receive(:include).with(ActiveRestClient::ControllerInstrumentation)
|
13
13
|
ActiveSupport.run_load_hooks(:action_controller, hook_tester)
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should call ActiveSupport::Notifications.instrument when making any request" do
|
17
|
-
ActiveSupport::Notifications.
|
17
|
+
expect(ActiveSupport::Notifications).to receive(:instrument).with("request_call.active_rest_client", {:name=>"InstrumentationExampleClient#fake"})
|
18
18
|
InstrumentationExampleClient.fake
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should call ActiveSupport::Notifications#request_call when making any request" do
|
22
|
-
ActiveRestClient::Instrumentation.
|
22
|
+
expect_any_instance_of(ActiveRestClient::Instrumentation).to receive(:request_call).with(an_instance_of(ActiveSupport::Notifications::Event))
|
23
23
|
InstrumentationExampleClient.fake
|
24
24
|
end
|
25
25
|
|
26
26
|
|
27
27
|
it "should log time spent in each API call" do
|
28
|
-
ActiveRestClient::Connection.
|
29
|
-
|
30
|
-
should_receive(:get).
|
28
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
29
|
+
to receive(:get).
|
31
30
|
with("/real", an_instance_of(Hash)).
|
32
31
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:200))
|
33
|
-
ActiveRestClient::Logger.
|
34
|
-
ActiveRestClient::Logger.
|
32
|
+
expect(ActiveRestClient::Logger).to receive(:debug).with(/ActiveRestClient.*ms\)/)
|
33
|
+
expect(ActiveRestClient::Logger).to receive(:debug).at_least(:once).with(any_args)
|
35
34
|
InstrumentationExampleClient.real
|
36
35
|
end
|
37
36
|
|
@@ -96,10 +96,10 @@ describe ActiveRestClient::LazyAssociationLoader do
|
|
96
96
|
it "should make the request for a URL if it's accessed" do
|
97
97
|
method_data = {options:{url:"foo"}}
|
98
98
|
request = double("Request").as_null_object
|
99
|
-
request.
|
100
|
-
request.
|
101
|
-
request.
|
102
|
-
ActiveRestClient::Request.
|
99
|
+
allow(request).to receive(:method).and_return(method_data)
|
100
|
+
expect(request).to receive(:object).with(any_args).and_return(Array.new)
|
101
|
+
expect(request).to receive(:call).with(any_args).and_return("")
|
102
|
+
expect(ActiveRestClient::Request).to receive(:new).with(any_args).and_return(request)
|
103
103
|
loader = ActiveRestClient::LazyAssociationLoader.new(:person, url1, request)
|
104
104
|
loader.length
|
105
105
|
end
|
@@ -107,14 +107,14 @@ describe ActiveRestClient::LazyAssociationLoader do
|
|
107
107
|
it "should proxy methods to the underlying object if the request has been made" do
|
108
108
|
loader = ActiveRestClient::LazyAssociationLoader.new(:person, url1, request)
|
109
109
|
object = double("Object")
|
110
|
-
object.
|
110
|
+
expect(object).to receive(:length).and_return(1)
|
111
111
|
loader.instance_variable_set(:@object, object)
|
112
112
|
expect(loader.length).to eq(1)
|
113
113
|
end
|
114
114
|
|
115
115
|
it "should be able to iterate underlying object if it's an array" do
|
116
116
|
loader = ActiveRestClient::LazyAssociationLoader.new(:person, url1, request)
|
117
|
-
ActiveRestClient::Request.
|
117
|
+
expect_any_instance_of(ActiveRestClient::Request).to receive(:call).with(any_args).and_return([1,2,3])
|
118
118
|
test = []
|
119
119
|
loader.each do |item|
|
120
120
|
test << item
|
@@ -124,7 +124,7 @@ describe ActiveRestClient::LazyAssociationLoader do
|
|
124
124
|
|
125
125
|
it "should be able to return the size of the underlying object if it's an array" do
|
126
126
|
loader = ActiveRestClient::LazyAssociationLoader.new(:person, url1, request)
|
127
|
-
ActiveRestClient::Request.
|
127
|
+
expect_any_instance_of(ActiveRestClient::Request).to receive(:call).with(any_args).and_return([1,2,3])
|
128
128
|
expect(loader.size).to eq(3)
|
129
129
|
end
|
130
130
|
|
@@ -5,21 +5,21 @@ describe ActiveRestClient::LazyLoader do
|
|
5
5
|
let(:response) { double("Response") }
|
6
6
|
|
7
7
|
it "should not #call the passed in request during initialisation" do
|
8
|
-
request.
|
8
|
+
expect(request).not_to receive(:call)
|
9
9
|
ActiveRestClient::LazyLoader.new(request)
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should #call the passed in request if you check for response to a message" do
|
13
|
-
request.
|
13
|
+
expect(request).to receive(:call)
|
14
14
|
loader = ActiveRestClient::LazyLoader.new(request)
|
15
15
|
loader.respond_to?(:each)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should #call the passed in request if you call a method and pass through the method" do
|
19
|
-
request.
|
20
|
-
response.
|
19
|
+
expect(request).to receive(:call).and_return(response)
|
20
|
+
expect(response).to receive(:valid).and_return(true)
|
21
21
|
loader = ActiveRestClient::LazyLoader.new(request)
|
22
|
-
expect(loader.valid).to
|
22
|
+
expect(loader.valid).to be_truthy
|
23
23
|
end
|
24
24
|
|
25
25
|
end
|
data/spec/lib/logger_spec.rb
CHANGED
@@ -13,10 +13,10 @@ describe ActiveRestClient::Instrumentation do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
Rails.logger = double("Logger")
|
16
|
-
Rails.logger.
|
17
|
-
Rails.logger.
|
18
|
-
Rails.logger.
|
19
|
-
Rails.logger.
|
16
|
+
expect(Rails.logger).to receive(:debug)
|
17
|
+
expect(Rails.logger).to receive(:info)
|
18
|
+
expect(Rails.logger).to receive(:warn)
|
19
|
+
expect(Rails.logger).to receive(:error)
|
20
20
|
ActiveRestClient::Logger.debug("Hello world")
|
21
21
|
ActiveRestClient::Logger.info("Hello world")
|
22
22
|
ActiveRestClient::Logger.warn("Hello world")
|
@@ -27,28 +27,28 @@ describe ActiveRestClient::Instrumentation do
|
|
27
27
|
it "should write to a logfile if one has been specified" do
|
28
28
|
ActiveRestClient::Logger.logfile = "/dev/null"
|
29
29
|
file = double('file')
|
30
|
-
File.
|
31
|
-
file.
|
30
|
+
expect(File).to receive(:open).with("/dev/null", "a").and_yield(file)
|
31
|
+
expect(file).to receive(:<<).with("Hello world")
|
32
32
|
ActiveRestClient::Logger.debug("Hello world")
|
33
33
|
|
34
34
|
file = double('file')
|
35
|
-
File.
|
36
|
-
file.
|
35
|
+
expect(File).to receive(:open).with("/dev/null", "a").and_yield(file)
|
36
|
+
expect(file).to receive(:<<).with("Hello info")
|
37
37
|
ActiveRestClient::Logger.info("Hello info")
|
38
38
|
|
39
39
|
file = double('file')
|
40
|
-
File.
|
41
|
-
file.
|
40
|
+
expect(File).to receive(:open).with("/dev/null", "a").and_yield(file)
|
41
|
+
expect(file).to receive(:<<).with("Hello error")
|
42
42
|
ActiveRestClient::Logger.error("Hello error")
|
43
43
|
|
44
44
|
file = double('file')
|
45
|
-
File.
|
46
|
-
file.
|
45
|
+
expect(File).to receive(:open).with("/dev/null", "a").and_yield(file)
|
46
|
+
expect(file).to receive(:<<).with("Hello warn")
|
47
47
|
ActiveRestClient::Logger.warn("Hello warn")
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should append to its own messages list if neither Rails nor a logfile has been specified" do
|
51
|
-
File.
|
51
|
+
expect(File).not_to receive(:open)
|
52
52
|
ActiveRestClient::Logger.debug("Hello world")
|
53
53
|
ActiveRestClient::Logger.info("Hello info")
|
54
54
|
ActiveRestClient::Logger.warn("Hello warn")
|
data/spec/lib/proxy_spec.rb
CHANGED
@@ -79,43 +79,43 @@ end
|
|
79
79
|
|
80
80
|
describe ActiveRestClient::Base do
|
81
81
|
it "allows the URL to be changed" do
|
82
|
-
ActiveRestClient::Connection.
|
82
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/getAll?id=1", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{}))
|
83
83
|
ProxyClientExample.all(id:1)
|
84
84
|
end
|
85
85
|
|
86
86
|
it "allows the URL to be replaced" do
|
87
|
-
ActiveRestClient::Connection.
|
87
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/new", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{}))
|
88
88
|
ProxyClientExample.old
|
89
89
|
end
|
90
90
|
|
91
91
|
it "has access to the GET params and allow them to be changed/removed/added" do
|
92
|
-
ActiveRestClient::Connection.
|
92
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/list?age=12&first_name=John", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{}))
|
93
93
|
ProxyClientExample.list(fname:"John", lname:"Smith")
|
94
94
|
end
|
95
95
|
|
96
96
|
it "has access to the POST params and allow them to be changed/removed/added" do
|
97
|
-
ActiveRestClient::Connection.
|
97
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:post).with("/create", {age:12, first_name:"John"}.to_query, instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{}))
|
98
98
|
ProxyClientExample.create(fname:"John", lname:"Smith")
|
99
99
|
end
|
100
100
|
|
101
101
|
it "has access to raw body content for requests" do
|
102
|
-
ActiveRestClient::Connection.
|
102
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/update", "MY-BODY-CONTENT", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{}))
|
103
103
|
ProxyClientExample.update(fname:"John", lname:"Smith")
|
104
104
|
end
|
105
105
|
|
106
106
|
it "handles DELETE requests" do
|
107
|
-
ActiveRestClient::Connection.
|
107
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:delete).with("/remove", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{}))
|
108
108
|
ProxyClientExample.remove
|
109
109
|
end
|
110
110
|
|
111
111
|
it "can return fake JSON data and have this parsed in the normal way" do
|
112
|
-
ActiveRestClient::Connection.
|
112
|
+
expect_any_instance_of(ActiveRestClient::Connection).not_to receive(:get).with("/fake", instance_of(Hash))
|
113
113
|
ret = ProxyClientExample.fake
|
114
114
|
expect(ret.id).to eq(1234)
|
115
115
|
end
|
116
116
|
|
117
117
|
it "can intercept the response and parse the response, alter it and pass it on during the request" do
|
118
|
-
ActiveRestClient::Connection.
|
118
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/change-format", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"fname\":\"Billy\"}", status:200, headers:{}))
|
119
119
|
ret = ProxyClientExample.change_format
|
120
120
|
expect(ret.first_name).to eq("Billy")
|
121
121
|
end
|
@@ -128,7 +128,7 @@ describe ActiveRestClient::Base do
|
|
128
128
|
end
|
129
129
|
|
130
130
|
it "can continue with the request in the normal way, passing it on to the server" do
|
131
|
-
ActiveRestClient::Connection.
|
131
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/not_proxied?id=1", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{}))
|
132
132
|
ProxyClientExample.not_proxied(id:1)
|
133
133
|
end
|
134
134
|
|
@@ -140,12 +140,12 @@ describe ActiveRestClient::Base do
|
|
140
140
|
etag:@etag)
|
141
141
|
|
142
142
|
cache_store = double("CacheStore")
|
143
|
-
cache_store.
|
143
|
+
allow(cache_store).to receive(:read).with(any_args).and_return(nil)
|
144
144
|
ProxyClientExample.perform_caching true
|
145
|
-
ProxyClientExample.
|
145
|
+
allow(ProxyClientExample).to receive(:cache_store).and_return(cache_store)
|
146
146
|
expiry = 10.minutes.from_now.rfc2822
|
147
|
-
ActiveRestClient::Connection.
|
148
|
-
ProxyClientExample.cache_store.
|
147
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/update", "MY-BODY-CONTENT", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{"Expires" => expiry, "ETag" => "123456"}))
|
148
|
+
expect(ProxyClientExample.cache_store).to receive(:write) do |key, object, options|
|
149
149
|
expect(key).to eq("ProxyClientExample:/update")
|
150
150
|
expect(object).to be_an_instance_of(String)
|
151
151
|
unmarshalled = Marshal.load(object)
|
@@ -162,8 +162,8 @@ describe ActiveRestClient::Base do
|
|
162
162
|
end
|
163
163
|
|
164
164
|
it "can force the URL from a filter without it being passed through URL replacement" do
|
165
|
-
ActiveRestClient::Connection.
|
166
|
-
ActiveRestClient::Connection.
|
165
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/hal_test/1", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{}))
|
166
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/this/is/a/test", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{}))
|
167
167
|
expect(ProxyClientExample.hal_test(id:1).test.result).to eq(true)
|
168
168
|
end
|
169
169
|
|