3scale_client 2.4.0 → 2.4.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.
- checksums.yaml +4 -4
- data/lib/3scale/client.rb +1 -0
- data/lib/3scale/client/http_client.rb +35 -13
- data/lib/3scale/client/version.rb +1 -1
- data/test/client_test.rb +57 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5238de4c88e0a7855f14b85f3fc667954a3c1d84
|
4
|
+
data.tar.gz: dbaf66645cf58a69fb8af3126145bac76b7fe5f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ec89086c7ea68dd50f5c5aa86338bfa45b9e5c079ef0526743f0db772ccb79ec67b97261e9ea92d088a6ed94dcef8de10038aba6796cbbbb2a52e2a6581a03e
|
7
|
+
data.tar.gz: efefcbfd4a99502127ee99dc0dddbec290d0216c0ce0e8f165a644d5967039db537570062db5161d2282fc5a71b2d8921a8f4ca6dc122cb474417ac1aeca2e65
|
data/lib/3scale/client.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
require 'forwardable'
|
2
|
+
require '3scale/client/version'
|
2
3
|
|
3
4
|
module ThreeScale
|
4
5
|
class Client
|
5
6
|
class HTTPClient
|
6
7
|
extend Forwardable
|
7
8
|
def_delegators :@http, :get, :post, :use_ssl?, :active?
|
9
|
+
USER_CLIENT_HEADER = ['X-3scale-User-Agent', "plugin-ruby-v#{VERSION}"]
|
8
10
|
|
9
11
|
def initialize(options)
|
10
12
|
|
@@ -18,11 +20,32 @@ module ThreeScale
|
|
18
20
|
@http.ssl! if @secure
|
19
21
|
end
|
20
22
|
|
21
|
-
class
|
23
|
+
class BaseClient
|
22
24
|
def initialize(host)
|
25
|
+
@host = host
|
26
|
+
end
|
27
|
+
|
28
|
+
def get(path)
|
29
|
+
get = Net::HTTP::Get.new(path)
|
30
|
+
get.add_field(*USER_CLIENT_HEADER)
|
31
|
+
get.add_field('Host', @host)
|
32
|
+
get
|
33
|
+
end
|
34
|
+
|
35
|
+
def post(path, payload)
|
36
|
+
post = Net::HTTP::Post.new(path)
|
37
|
+
post.add_field(*USER_CLIENT_HEADER)
|
38
|
+
post.add_field('Host', @host)
|
39
|
+
post.set_form_data(payload)
|
40
|
+
post
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class Persistent < BaseClient
|
45
|
+
def initialize(host)
|
46
|
+
super
|
23
47
|
require 'net/http/persistent'
|
24
48
|
@http = ::Net::HTTP::Persistent.new
|
25
|
-
@host = host
|
26
49
|
@protocol = 'http'
|
27
50
|
end
|
28
51
|
|
@@ -32,31 +55,26 @@ module ThreeScale
|
|
32
55
|
|
33
56
|
def get(path)
|
34
57
|
uri = full_uri(path)
|
35
|
-
|
36
|
-
@http.request(uri, get)
|
58
|
+
@http.request(uri, super)
|
37
59
|
end
|
38
60
|
|
39
61
|
|
40
62
|
def post(path, payload)
|
41
63
|
uri = full_uri(path)
|
42
|
-
|
43
|
-
post.set_form_data(payload)
|
44
|
-
|
45
|
-
@http.request(uri, post)
|
64
|
+
@http.request(uri, super)
|
46
65
|
end
|
47
66
|
|
48
67
|
def full_uri(path)
|
49
68
|
URI.join "#{@protocol}://#{@host}", path
|
50
69
|
end
|
51
|
-
|
52
70
|
end
|
53
71
|
|
54
|
-
class NetHttp
|
72
|
+
class NetHttp < BaseClient
|
55
73
|
extend Forwardable
|
56
|
-
def_delegators :@http, :
|
74
|
+
def_delegators :@http, :use_ssl?, :active?
|
57
75
|
|
58
76
|
def initialize(host)
|
59
|
-
|
77
|
+
super
|
60
78
|
@http = Net::HTTP.new(@host, 80)
|
61
79
|
end
|
62
80
|
|
@@ -65,8 +83,12 @@ module ThreeScale
|
|
65
83
|
@http.use_ssl = true
|
66
84
|
end
|
67
85
|
|
86
|
+
def get(path)
|
87
|
+
@http.request(super)
|
88
|
+
end
|
89
|
+
|
68
90
|
def post(path, payload)
|
69
|
-
@http.
|
91
|
+
@http.request(super)
|
70
92
|
end
|
71
93
|
end
|
72
94
|
end
|
data/test/client_test.rb
CHANGED
@@ -338,18 +338,18 @@ class ThreeScale::ClientTest < Test::Unit::TestCase
|
|
338
338
|
Net::HTTPSuccess.stubs(:===).with(http_response).returns(true)
|
339
339
|
|
340
340
|
payload = {
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
341
|
+
'transactions[0][app_id]' => 'foo',
|
342
|
+
'transactions[0][timestamp]' => CGI.escape('2010-04-27 15:42:17 0200'),
|
343
|
+
'transactions[0][usage][hits]' => '1',
|
344
|
+
'transactions[1][app_id]' => 'bar',
|
345
|
+
'transactions[1][timestamp]' => CGI.escape('2010-04-27 15:55:12 0200'),
|
346
|
+
'transactions[1][usage][hits]' => '1',
|
347
|
+
'provider_key' => '1234abcd'
|
348
348
|
}
|
349
349
|
|
350
350
|
@client.http.expects(:post)
|
351
|
-
|
352
|
-
|
351
|
+
.with('/transactions.xml', payload)
|
352
|
+
.returns(http_response)
|
353
353
|
|
354
354
|
@client.report({:app_id => 'foo',
|
355
355
|
:usage => {'hits' => 1},
|
@@ -385,6 +385,53 @@ class ThreeScale::ClientTest < Test::Unit::TestCase
|
|
385
385
|
end
|
386
386
|
end
|
387
387
|
|
388
|
+
def test_authorize_client_header_sent
|
389
|
+
success_body = '<?xml version="1.0" encoding="UTF-8"?><status><authorized>true</authorized><plan>Default</plan><usage_reports><usage_report metric="hits" period="minute"><period_start>2014-08-22 09:06:00 +0000</period_start><period_end>2014-08-22 09:07:00 +0000</period_end><max_value>5</max_value><current_value>0</current_value></usage_report></usage_reports></status>'
|
390
|
+
version = ThreeScale::Client::VERSION
|
391
|
+
FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=foo&app_id=foo",
|
392
|
+
:status => ['200', 'OK'],
|
393
|
+
:body => success_body)
|
394
|
+
|
395
|
+
client = ThreeScale::Client.new(:provider_key => 'foo')
|
396
|
+
response = client.authorize(:app_id => 'foo')
|
397
|
+
|
398
|
+
assert response.success?
|
399
|
+
request = FakeWeb.last_request
|
400
|
+
assert_equal "plugin-ruby-v#{version}", request["X-3scale-User-Agent"]
|
401
|
+
assert_equal "su1.3scale.net", request["host"]
|
402
|
+
|
403
|
+
end
|
404
|
+
|
405
|
+
def test_report_client_header_sent
|
406
|
+
success_body = '<?xml version="1.0" encoding="UTF-8"?><status><authorized>true</authorized><plan>Default</plan><usage_reports><usage_report metric="hits" period="minute"><period_start>2014-08-22 09:06:00 +0000</period_start><period_end>2014-08-22 09:07:00 +0000</period_end><max_value>5</max_value><current_value>0</current_value></usage_report></usage_reports></status>'
|
407
|
+
version = ThreeScale::Client::VERSION
|
408
|
+
FakeWeb.register_uri(:post, "http://#{@host}/transactions.xml",
|
409
|
+
:status => ['200', 'OK'],
|
410
|
+
:body => success_body)
|
411
|
+
client = ThreeScale::Client.new(:provider_key => 'foo')
|
412
|
+
response = client.report({:app_id => 'abc', :usage => {'hits' => 1}})
|
413
|
+
|
414
|
+
request = FakeWeb.last_request
|
415
|
+
assert_equal "plugin-ruby-v#{version}", request["X-3scale-User-Agent"]
|
416
|
+
assert_equal "su1.3scale.net", request["host"]
|
417
|
+
end
|
418
|
+
|
419
|
+
def test_authrep_client_header_sent
|
420
|
+
success_body = '<?xml version="1.0" encoding="UTF-8"?><status><authorized>true</authorized><plan>Default</plan><usage_reports><usage_report metric="hits" period="minute"><period_start>2014-08-22 09:06:00 +0000</period_start><period_end>2014-08-22 09:07:00 +0000</period_end><max_value>5</max_value><current_value>0</current_value></usage_report></usage_reports></status>'
|
421
|
+
version = ThreeScale::Client::VERSION
|
422
|
+
FakeWeb.register_uri(:get, "http://#{@host}/transactions/authrep.xml?provider_key=foo&app_id=abc&%5Busage%5D%5Bhits%5D=1",
|
423
|
+
:status => ['200', 'OK'],
|
424
|
+
:body => success_body)
|
425
|
+
|
426
|
+
client = ThreeScale::Client.new(:provider_key => 'foo')
|
427
|
+
response = client.authrep(:app_id => 'abc')
|
428
|
+
|
429
|
+
assert response.success?
|
430
|
+
request = FakeWeb.last_request
|
431
|
+
assert_equal "plugin-ruby-v#{version}", request["X-3scale-User-Agent"]
|
432
|
+
assert_equal "su1.3scale.net", request["host"]
|
433
|
+
end
|
434
|
+
|
388
435
|
private
|
389
436
|
|
390
437
|
#OPTIMIZE this tricky test helper relies on fakeweb catching the urls requested by the client
|
@@ -414,4 +461,4 @@ class ThreeScale::PersistentClientTest < ThreeScale::ClientTest
|
|
414
461
|
def client(options = {})
|
415
462
|
ThreeScale::Client.new({:provider_key => '1234abcd', :persistent => true}.merge(options))
|
416
463
|
end
|
417
|
-
end
|
464
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: 3scale_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.4.
|
4
|
+
version: 2.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michal Cichra
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2014-
|
15
|
+
date: 2014-09-01 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: bundler
|