rest-core 3.2.0 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +23 -0
- data/Gemfile +1 -1
- data/README.md +11 -10
- data/Rakefile +2 -1
- data/lib/rest-core.rb +0 -1
- data/lib/rest-core/builder.rb +59 -12
- data/lib/rest-core/client/universal.rb +11 -10
- data/lib/rest-core/middleware.rb +20 -0
- data/lib/rest-core/middleware/cache.rb +12 -10
- data/lib/rest-core/middleware/default_headers.rb +2 -2
- data/lib/rest-core/middleware/default_payload.rb +2 -26
- data/lib/rest-core/middleware/default_query.rb +2 -10
- data/lib/rest-core/middleware/json_request.rb +2 -1
- data/lib/rest-core/middleware/json_response.rb +5 -2
- data/lib/rest-core/test.rb +3 -12
- data/lib/rest-core/version.rb +1 -1
- data/rest-core.gemspec +11 -12
- data/task/gemgem.rb +1 -5
- data/test/test_auth_basic.rb +4 -4
- data/test/test_builder.rb +20 -4
- data/test/test_cache.rb +19 -20
- data/test/test_clash.rb +1 -1
- data/test/test_clash_response.rb +11 -11
- data/test/test_client.rb +10 -10
- data/test/test_client_oauth1.rb +3 -3
- data/test/test_config.rb +1 -1
- data/test/test_default_headers.rb +13 -0
- data/test/test_default_payload.rb +11 -3
- data/test/test_default_query.rb +12 -4
- data/test/test_error_detector.rb +1 -1
- data/test/test_error_detector_http.rb +1 -1
- data/test/test_error_handler.rb +5 -5
- data/test/test_event_source.rb +16 -16
- data/test/test_follow_redirect.rb +6 -6
- data/test/test_future.rb +2 -2
- data/test/test_json_request.rb +9 -4
- data/test/test_json_response.rb +9 -9
- data/test/test_oauth1_header.rb +9 -9
- data/test/test_oauth2_header.rb +3 -3
- data/test/test_parse_link.rb +4 -4
- data/test/test_payload.rb +21 -21
- data/test/test_promise.rb +7 -7
- data/test/test_query_response.rb +5 -5
- data/test/test_rest-client.rb +7 -6
- data/test/test_simple.rb +5 -5
- data/test/test_smash.rb +1 -1
- data/test/test_smash_response.rb +11 -11
- data/test/test_thread_pool.rb +1 -1
- data/test/test_timeout.rb +3 -3
- data/test/test_universal.rb +12 -2
- metadata +9 -10
- data/lib/rest-core/wrapper.rb +0 -72
- data/test/test_wrapper.rb +0 -36
data/task/gemgem.rb
CHANGED
@@ -34,7 +34,7 @@ module Gemgem
|
|
34
34
|
s.executables = bin_files
|
35
35
|
end
|
36
36
|
spec_create.call(spec)
|
37
|
-
spec.homepage
|
37
|
+
spec.homepage ||= "https://github.com/godfat/#{spec.name}"
|
38
38
|
self.spec = spec
|
39
39
|
end
|
40
40
|
|
@@ -227,10 +227,6 @@ end # of gem namespace
|
|
227
227
|
desc 'Run tests'
|
228
228
|
task :test do
|
229
229
|
next if Gemgem.test_files.empty?
|
230
|
-
|
231
|
-
require 'bacon'
|
232
|
-
Bacon.extend(Bacon::TestUnitOutput)
|
233
|
-
Bacon.summary_on_exit
|
234
230
|
Gemgem.test_files.each{ |file| require "#{Gemgem.dir}/#{file[0..-4]}" }
|
235
231
|
end
|
236
232
|
|
data/test/test_auth_basic.rb
CHANGED
@@ -8,11 +8,11 @@ describe RC::AuthBasic do
|
|
8
8
|
|
9
9
|
env = {RC::REQUEST_HEADERS => {}}
|
10
10
|
|
11
|
-
|
11
|
+
would 'do nothing' do
|
12
12
|
@auth.call({}){ |res| res.should.eq({}) }
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
would 'send Authorization header' do
|
16
16
|
@auth.instance_eval{@username = 'Aladdin'}
|
17
17
|
@auth.instance_eval{@password = 'open sesame'}
|
18
18
|
|
@@ -30,12 +30,12 @@ describe RC::AuthBasic do
|
|
30
30
|
}
|
31
31
|
end
|
32
32
|
|
33
|
-
|
33
|
+
would 'leave a log if username are not both provided' do
|
34
34
|
@auth.instance_eval{@username = 'Aladdin'}
|
35
35
|
@auth.call(env){ |res| res[RC::LOG].size.should.eq 1 }
|
36
36
|
end
|
37
37
|
|
38
|
-
|
38
|
+
would 'leave a log if password are not both provided' do
|
39
39
|
@auth.instance_eval{@password = 'open sesame'}
|
40
40
|
@auth.call(env){ |res| res[RC::LOG].size.should.eq 1 }
|
41
41
|
end
|
data/test/test_builder.rb
CHANGED
@@ -2,17 +2,33 @@
|
|
2
2
|
require 'rest-core/test'
|
3
3
|
|
4
4
|
describe RC::Builder do
|
5
|
-
|
6
|
-
RC::Builder.client.new.app.should.kind_of RC::Engine
|
5
|
+
would 'default client app is a kind of RestCore::Engine' do
|
6
|
+
RC::Builder.client.new.app.should.kind_of? RC::Engine
|
7
7
|
end
|
8
8
|
|
9
|
-
|
9
|
+
would 'default app is a kind of RestCore::Engine' do
|
10
|
+
RC::Builder.new.to_app.should.kind_of? RC::Engine
|
11
|
+
end
|
12
|
+
|
13
|
+
would 'switch default_engine to RestCore::RestClient' do
|
14
|
+
builder = Class.new(RC::Builder)
|
15
|
+
builder.default_engine = RC::RestClient
|
16
|
+
builder.new.to_app.class.should.eq RC::RestClient
|
17
|
+
end
|
18
|
+
|
19
|
+
would 'switch default_engine to RestCore::Dry' do
|
10
20
|
builder = RC::Builder.dup
|
11
21
|
builder.default_engine = RC::Dry
|
12
22
|
builder.client.new.app.class.should.eq RC::Dry
|
13
23
|
end
|
14
24
|
|
15
|
-
|
25
|
+
would 'accept middleware without a member' do
|
26
|
+
RC::Builder.client{
|
27
|
+
use Class.new.send(:include, RC::Middleware)
|
28
|
+
}.members.should.eq []
|
29
|
+
end
|
30
|
+
|
31
|
+
would 'not have duplicated fields' do
|
16
32
|
middleware = Class.new do
|
17
33
|
def self.members; [:value]; end
|
18
34
|
include RC::Middleware
|
data/test/test_cache.rb
CHANGED
@@ -13,13 +13,12 @@ describe RC::Cache do
|
|
13
13
|
|
14
14
|
def json_client
|
15
15
|
RC::Builder.client do
|
16
|
-
use RC::
|
17
|
-
|
18
|
-
end
|
16
|
+
use RC::JsonResponse, true
|
17
|
+
use RC::Cache, {}, 3600
|
19
18
|
end.new
|
20
19
|
end
|
21
20
|
|
22
|
-
|
21
|
+
would 'basic 0' do
|
23
22
|
c = RC::Builder.client do
|
24
23
|
use RC::Cache, {}, 3600
|
25
24
|
run Class.new{
|
@@ -50,7 +49,7 @@ describe RC::Cache do
|
|
50
49
|
RC::RESPONSE_KEY => RC::RESPONSE_STATUS).should.eq 200
|
51
50
|
end
|
52
51
|
|
53
|
-
|
52
|
+
would 'basic 1' do
|
54
53
|
path = 'http://a'
|
55
54
|
stub_request(:get , path).to_return(:body => 'OK')
|
56
55
|
stub_request(:post, path).to_return(:body => 'OK')
|
@@ -69,7 +68,7 @@ describe RC::Cache do
|
|
69
68
|
should.eq('OK')
|
70
69
|
end
|
71
70
|
|
72
|
-
|
71
|
+
would 'not raise error if headers is nil' do
|
73
72
|
path = 'http://a'
|
74
73
|
stub_request(:get , path).to_return(:body => 'OK', :headers => nil)
|
75
74
|
c = simple_client
|
@@ -77,7 +76,7 @@ describe RC::Cache do
|
|
77
76
|
c.get(path).should.eq 'OK'
|
78
77
|
end
|
79
78
|
|
80
|
-
|
79
|
+
would 'head then get' do
|
81
80
|
c = simple_client
|
82
81
|
path = 'http://example.com'
|
83
82
|
stub_request(:head, path).to_return(:headers => {'A' => 'B'})
|
@@ -86,7 +85,7 @@ describe RC::Cache do
|
|
86
85
|
c.get(path).should.eq('body')
|
87
86
|
end
|
88
87
|
|
89
|
-
|
88
|
+
would 'only [] and []= should be implemented' do
|
90
89
|
cache = Class.new do
|
91
90
|
def initialize ; @h = {} ; end
|
92
91
|
def [] key ; @h[key] ; end
|
@@ -105,14 +104,14 @@ describe RC::Cache do
|
|
105
104
|
c.get('4').should.eq '5'
|
106
105
|
end
|
107
106
|
|
108
|
-
|
107
|
+
would 'cache the original response' do
|
109
108
|
c = json_client
|
110
109
|
stub_request(:get, 'http://me').to_return(:body => body = '{"a":"b"}')
|
111
110
|
c.get('http://me').should.eq 'a' => 'b'
|
112
111
|
c.cache.values.first.should.eq "200\n\n\n#{body}"
|
113
112
|
end
|
114
113
|
|
115
|
-
|
114
|
+
would 'cache multiple headers' do
|
116
115
|
c = simple_client
|
117
116
|
stub_request(:get, 'http://me').to_return(:headers =>
|
118
117
|
{'Apple' => 'Orange', 'Orange' => 'Apple'})
|
@@ -121,7 +120,7 @@ describe RC::Cache do
|
|
121
120
|
2.times{ c.get(*args).should.eq expected }
|
122
121
|
end
|
123
122
|
|
124
|
-
|
123
|
+
would 'preserve promise and REQUEST_URI' do
|
125
124
|
c = simple_client
|
126
125
|
uri = 'http://me?a=b'
|
127
126
|
stub_request(:get, uri)
|
@@ -129,7 +128,7 @@ describe RC::Cache do
|
|
129
128
|
2.times{ c.get(*args).yield[RC::REQUEST_URI].should.eq uri }
|
130
129
|
end
|
131
130
|
|
132
|
-
|
131
|
+
would 'preserve promise and preserve wrapped call' do
|
133
132
|
c = json_client
|
134
133
|
stub_request(:get, 'http://me').to_return(:body => body = '{"a":"b"}')
|
135
134
|
args = ['http://me', {}, {RC::RESPONSE_KEY => RC::PROMISE}]
|
@@ -138,7 +137,7 @@ describe RC::Cache do
|
|
138
137
|
end
|
139
138
|
end
|
140
139
|
|
141
|
-
|
140
|
+
would 'multiline response' do
|
142
141
|
c = simple_client
|
143
142
|
stub_request(:get, 'http://html').to_return(:body => body = "a\n\nb")
|
144
143
|
c.get('http://html').should.eq body
|
@@ -146,7 +145,7 @@ describe RC::Cache do
|
|
146
145
|
c.get('http://html').should.eq body
|
147
146
|
end
|
148
147
|
|
149
|
-
|
148
|
+
would "follow redirect with cache.update correctly" do
|
150
149
|
c = RC::Builder.client do
|
151
150
|
use RC::FollowRedirect, 10
|
152
151
|
use RC::Cache, {}, nil
|
@@ -160,7 +159,7 @@ describe RC::Cache do
|
|
160
159
|
c.get(x, {}, 'cache.update' => true).should.eq 'OK'
|
161
160
|
end
|
162
161
|
|
163
|
-
|
162
|
+
would 'not cache post/put/delete' do
|
164
163
|
[:put, :post, :delete].each{ |meth|
|
165
164
|
url, body = "https://cache", 'ok'
|
166
165
|
stub_request(meth, url).to_return(:body => body).times(3)
|
@@ -178,13 +177,13 @@ describe RC::Cache do
|
|
178
177
|
}
|
179
178
|
end
|
180
179
|
|
181
|
-
|
180
|
+
would 'not cache dry run' do
|
182
181
|
c = simple_client
|
183
182
|
c.url('test')
|
184
183
|
c.cache.should.eq({})
|
185
184
|
end
|
186
185
|
|
187
|
-
|
186
|
+
would 'not cache hijacking' do
|
188
187
|
stub_request(:get, 'http://a').to_return(:body => 'ok')
|
189
188
|
c = simple_client
|
190
189
|
2.times do
|
@@ -195,7 +194,7 @@ describe RC::Cache do
|
|
195
194
|
c.cache.should.eq({})
|
196
195
|
end
|
197
196
|
|
198
|
-
|
197
|
+
would 'update cache if there is cache option set to false' do
|
199
198
|
url, body = "https://cache", 'ok'
|
200
199
|
stub_request(:get, url).to_return(:body => body)
|
201
200
|
c = simple_client
|
@@ -219,12 +218,12 @@ describe RC::Cache do
|
|
219
218
|
@cache
|
220
219
|
end
|
221
220
|
|
222
|
-
|
221
|
+
would 'respect in options' do
|
223
222
|
c = RC::Builder.client{use RC::Cache, nil, nil}.new
|
224
223
|
c.get(@url, {}, :cache => @cache, :expires_in => 3).should.eq @body
|
225
224
|
end
|
226
225
|
|
227
|
-
|
226
|
+
would 'respect in middleware' do
|
228
227
|
c = RC::Builder.client{use RC::Cache, nil, 3}.new(:cache => @cache)
|
229
228
|
c.get(@url).should.eq @body
|
230
229
|
end
|
data/test/test_clash.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
require 'rest-core/test'
|
3
3
|
|
4
4
|
describe RC::Clash do
|
5
|
-
|
5
|
+
would 'never give nil for non-existing values' do
|
6
6
|
h = {0 => 1, 2 => {3 => 4, 5 => [6, {7 => 8}]}, 9 => false, 10 => nil}
|
7
7
|
c = RC::Clash.new(h)
|
8
8
|
c[0] .should.eq(1)
|
data/test/test_clash_response.rb
CHANGED
@@ -5,19 +5,19 @@ describe RC::ClashResponse do
|
|
5
5
|
describe 'app' do
|
6
6
|
app = RC::ClashResponse.new(RC::Dry.new, true)
|
7
7
|
|
8
|
-
|
8
|
+
would 'do nothing' do
|
9
9
|
env = {RC::RESPONSE_BODY => []}
|
10
10
|
app.call(env) do |res|
|
11
11
|
res.should.eq(env)
|
12
|
-
res[RC::RESPONSE_BODY].should.kind_of(Array)
|
12
|
+
res[RC::RESPONSE_BODY].should.kind_of?(Array)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
would 'clash' do
|
17
17
|
app.call(RC::RESPONSE_BODY => {}) do |res|
|
18
18
|
body = res[RC::RESPONSE_BODY]
|
19
|
-
body.should.kind_of(RC::Clash)
|
20
|
-
body.should.empty
|
19
|
+
body.should.kind_of?(RC::Clash)
|
20
|
+
body.should.empty?
|
21
21
|
body[0].should.eq({})
|
22
22
|
body[0][0].should.eq({})
|
23
23
|
end
|
@@ -34,22 +34,22 @@ describe RC::ClashResponse do
|
|
34
34
|
}
|
35
35
|
end
|
36
36
|
|
37
|
-
|
37
|
+
would 'do nothing' do
|
38
38
|
b = client.new(:clash_response => false).get(''){ |res|
|
39
39
|
res.should.eq(body)
|
40
|
-
res.should.kind_of(Hash)
|
40
|
+
res.should.kind_of?(Hash)
|
41
41
|
}.get('')
|
42
42
|
b.should.eq(body)
|
43
|
-
b.should.kind_of(Hash)
|
43
|
+
b.should.kind_of?(Hash)
|
44
44
|
end
|
45
45
|
|
46
|
-
|
46
|
+
would 'clash' do
|
47
47
|
b = client.new.get(''){ |res|
|
48
48
|
res.should.eq(body)
|
49
|
-
res.should.kind_of(RC::Clash)
|
49
|
+
res.should.kind_of?(RC::Clash)
|
50
50
|
}.get('')
|
51
51
|
b.should.eq(body)
|
52
|
-
b.should.kind_of(RC::Clash)
|
52
|
+
b.should.kind_of?(RC::Clash)
|
53
53
|
end
|
54
54
|
end
|
55
55
|
end
|
data/test/test_client.rb
CHANGED
@@ -9,7 +9,7 @@ describe RC::Simple do
|
|
9
9
|
|
10
10
|
url = 'http://localhost/'
|
11
11
|
|
12
|
-
|
12
|
+
would 'do simple request' do
|
13
13
|
c = RC::Simple.new
|
14
14
|
[:get, :post, :delete, :put, :patch].each do |method|
|
15
15
|
stub_request(method, url).to_return(:body => '[]')
|
@@ -23,7 +23,7 @@ describe RC::Simple do
|
|
23
23
|
c.options(url).should.eq('A' => 'B')
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
would 'call the callback' do
|
27
27
|
[:get, :post, :delete, :put, :patch].each do |method|
|
28
28
|
stub_request(method, url).to_return(:body => '123')
|
29
29
|
(client = RC::Simple.new).send(method, url){ |res|
|
@@ -44,7 +44,7 @@ describe RC::Simple do
|
|
44
44
|
client.wait
|
45
45
|
end
|
46
46
|
|
47
|
-
|
47
|
+
would 'wait for all the requests' do
|
48
48
|
t, i, m = 5, 0, Mutex.new
|
49
49
|
stub_request(:get, url).to_return do
|
50
50
|
m.synchronize{ i += 1 }
|
@@ -54,11 +54,11 @@ describe RC::Simple do
|
|
54
54
|
client = RC::Builder.client
|
55
55
|
t.times{ client.new.get(url) }
|
56
56
|
client.wait
|
57
|
-
client.promises.should.empty
|
57
|
+
client.promises.should.empty?
|
58
58
|
i.should.eq t
|
59
59
|
end
|
60
60
|
|
61
|
-
|
61
|
+
would 'cleanup promises' do
|
62
62
|
stub_request(:get, url)
|
63
63
|
client = RC::Builder.client
|
64
64
|
5.times{ client.new.get(url) }
|
@@ -66,17 +66,17 @@ describe RC::Simple do
|
|
66
66
|
GC.start # can only force GC run on MRI, so we mock for jruby and rubinius
|
67
67
|
stub(any_instance_of(WeakRef)).weakref_alive?{false}
|
68
68
|
client.new.get(url)
|
69
|
-
client.promises.size.should
|
69
|
+
client.promises.size.should.lt 6
|
70
70
|
client.shutdown
|
71
|
-
client.promises.should.empty
|
71
|
+
client.promises.should.empty?
|
72
72
|
end
|
73
73
|
|
74
|
-
|
74
|
+
would 'have correct to_i' do
|
75
75
|
stub_request(:get, url).to_return(:body => '123')
|
76
76
|
RC::Simple.new.get(url).to_i.should.eq 123
|
77
77
|
end
|
78
78
|
|
79
|
-
|
79
|
+
would 'use defaults' do
|
80
80
|
client = RC::Builder.client do
|
81
81
|
use RC::Timeout, 4
|
82
82
|
end
|
@@ -107,7 +107,7 @@ describe RC::Simple do
|
|
107
107
|
c.timeout.should.eq false # false would disable default
|
108
108
|
end
|
109
109
|
|
110
|
-
|
110
|
+
would 'work for inheritance' do
|
111
111
|
stub_request(:get, url).to_return(:body => '123')
|
112
112
|
Class.new(RC::Simple).new.get(url).should.eq '123'
|
113
113
|
end
|
data/test/test_client_oauth1.rb
CHANGED
@@ -13,7 +13,7 @@ describe RC::ClientOauth1 do
|
|
13
13
|
|
14
14
|
client.send(:include, RC::ClientOauth1)
|
15
15
|
|
16
|
-
|
16
|
+
would 'restore data with correct sig' do
|
17
17
|
data = {'a' => 'b', 'c' => 'd'}
|
18
18
|
sig = Digest::MD5.hexdigest('e&a=b&c=d')
|
19
19
|
data_sig = data.merge('sig' => sig)
|
@@ -36,7 +36,7 @@ describe RC::ClientOauth1 do
|
|
36
36
|
c.data.should.eq({})
|
37
37
|
end
|
38
38
|
|
39
|
-
|
39
|
+
would 'have correct default data' do
|
40
40
|
c = client.new
|
41
41
|
c.data.should.eq({})
|
42
42
|
c.data = nil
|
@@ -44,7 +44,7 @@ describe RC::ClientOauth1 do
|
|
44
44
|
c.data['a'].should.eq 'b'
|
45
45
|
end
|
46
46
|
|
47
|
-
|
47
|
+
would 'authorize' do
|
48
48
|
stub_request(:post, 'http://localhost').
|
49
49
|
to_return(:body => 'oauth_token=abc')
|
50
50
|
|
data/test/test_config.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
|
2
|
+
require 'rest-core/test'
|
3
|
+
|
4
|
+
describe RC::DefaultHeaders do
|
5
|
+
app = RC::DefaultHeaders.new(RC::Dry.new, 'a' => 'b')
|
6
|
+
|
7
|
+
would 'also merge the very default headers' do
|
8
|
+
app.call('headers' => {'b' => 'c'},
|
9
|
+
RC::REQUEST_HEADERS => {'c' => 'd'}) do |r|
|
10
|
+
r[RC::REQUEST_HEADERS].should.eq 'a' => 'b', 'b' => 'c', 'c' => 'd'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -5,11 +5,11 @@ describe RC::DefaultPayload do
|
|
5
5
|
app = RC::DefaultPayload.new(RC::Dry.new, {})
|
6
6
|
env = {RC::REQUEST_PAYLOAD => {}}
|
7
7
|
|
8
|
-
|
8
|
+
would 'do nothing' do
|
9
9
|
app.call(env){ |r| r[RC::REQUEST_PAYLOAD].should.eq({}) }
|
10
10
|
end
|
11
11
|
|
12
|
-
|
12
|
+
would 'merge payload' do
|
13
13
|
app.instance_eval{@payload = {'pay' => 'load'}}
|
14
14
|
|
15
15
|
app.call(env){ |r| r.should.eq({RC::REQUEST_PAYLOAD =>
|
@@ -22,7 +22,15 @@ describe RC::DefaultPayload do
|
|
22
22
|
{'pay' => 'load'}.merge(format)})}
|
23
23
|
end
|
24
24
|
|
25
|
-
|
25
|
+
would 'also merge the very default payload' do
|
26
|
+
a = RC::DefaultPayload.new(RC::Dry.new, 'a' => 'b')
|
27
|
+
a.call('payload' => {'b' => 'c'},
|
28
|
+
RC::REQUEST_PAYLOAD => {'c' => 'd'}) do |r|
|
29
|
+
r[RC::REQUEST_PAYLOAD].should.eq 'a' => 'b', 'b' => 'c', 'c' => 'd'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
would 'accept non-hash payload' do
|
26
34
|
u = RC::Universal.new(:log_method => false)
|
27
35
|
env = {RC::REQUEST_PAYLOAD => 'payload'}
|
28
36
|
u.request_full(env, u.dry)[RC::REQUEST_PAYLOAD].should.eq('payload')
|