Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

client encodes usage values. Tests for authrep added. TODO some test …

…cases are still wip and the test helper in use needs to be cleaned up
  • Loading branch information...
commit 2275b20172c8499d058ecf004ca4999867ff2078 1 parent 9e0bfb0
@joahking joahking authored
Showing with 50 additions and 1 deletion.
  1. +1 −1  lib/3scale/client.rb
  2. +49 −0 test/client_test.rb
View
2  lib/3scale/client.rb
@@ -68,7 +68,7 @@ def authrep(options)
usage = []
options_usage.each_pair do |metric, value|
escaped_metric = CGI.escape "[usage][#{metric}]"
- usage << "#{escaped_metric}=#{value}"
+ usage << "#{escaped_metric}=#{CGI.escape(value.to_s)}"
end
path += "&#{usage.join('&')}"
View
49 test/client_test.rb
@@ -31,6 +31,35 @@ def test_custom_host
assert_equal 'example.com', client.host
end
+ def test_authrep_usage_is_encoded
+ assert_authrep_url_with_params "&%5Busage%5D%5Bmethod%5D=666"
+
+ @client.authrep({:usage => {:method=> 666}})
+ end
+
+ def test_authrep_usage_values_are_encoded
+ assert_authrep_url_with_params "&%5Busage%5D%5Bhits%5D=%230"
+
+ @client.authrep({:usage => {:hits => "#0"}})
+ end
+
+ def test_authrep_usage_defaults_to_hits_1
+ assert_authrep_url_with_params "&%5Busage%5D%5Bhits%5D=1"
+
+ @client.authrep({})
+ end
+
+ def test_authrep_supports_app_id_app_key_auth_mode
+ assert_authrep_url_with_params "&app_id=appid&app_key=appkey&%5Busage%5D%5Bhits%5D=1"
+
+ @client.authrep(:app_id => "appid", :app_key => "appkey")
+ end
+
+ #TODO these authrep tests
+ def test_authrep_supports_api_key_auth_mode; end
+ def test_authrep_log_is_encoded;end
+ def test_authrep_passes_all_params_to_backend;end
+
def test_successful_authorize
body = '<status>
<authorized>true</authorized>
@@ -309,4 +338,24 @@ def test_report_with_server_error
@client.report({:app_id => 'foo', :usage => {'hits' => 1}})
end
end
+
+ private
+
+ #OPTIMIZE this tricky test helper relies on fakeweb catching the urls requested by the client
+ # it is brittle: it depends in the correct order or params in the url
+ #
+ def assert_authrep_url_with_params(str)
+ authrep_url = "http://#{@host}/transactions/authrep.xml?provider_key=#{@client.provider_key}"
+ params = str # unless str.scan(/log/)
+ params << "&%5Busage%5D%5Bhits%5D=1" unless params.scan(/usage.*hits/)
+ parsed_authrep_url = URI.parse(authrep_url + params)
+ # set to have the client working
+ body = '<status>
+ <authorized>true</authorized>
+ <plan>Ultimate</plan>
+ </status>'
+
+ # this is the actual assertion, if fakeweb raises the client is submiting with wrong params
+ FakeWeb.register_uri(:get, parsed_authrep_url, :status => ['200', 'OK'], :body => body)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.