diff --git a/RELEASE b/RELEASE index 3c517b0..b14b192 100644 --- a/RELEASE +++ b/RELEASE @@ -1,3 +1,7 @@ +2.2.2 + +- Fixed issue handling empty parameters for application/json content-type + 2.2.1 - Added support for application/json content-type diff --git a/lib/telesign/rest.rb b/lib/telesign/rest.rb index 96ee831..77bb912 100644 --- a/lib/telesign/rest.rb +++ b/lib/telesign/rest.rb @@ -7,7 +7,7 @@ require 'net/http/persistent' module Telesign - SDK_VERSION = '2.2.1' + SDK_VERSION = '2.2.2' # The TeleSign RestClient is a generic HTTP REST client that can be extended to make requests against any of # TeleSign's REST API endpoints. @@ -18,7 +18,7 @@ module Telesign # See https://developer.telesign.com for detailed API documentation. class RestClient - @user_agent = "TeleSignSDK/ruby-{#{SDK_VERSION} #{RUBY_DESCRIPTION} net/http/persistent" + @@user_agent = "TeleSignSDK/ruby-{#{SDK_VERSION} #{RUBY_DESCRIPTION} net/http/persistent" # A simple HTTP Response object to abstract the underlying net/http library response. @@ -194,20 +194,20 @@ def execute(method_function, method_name, resource, **params) request = method_function.new(resource_uri.request_uri) - unless params.empty? - if %w[POST PUT].include? method_name - if content_type == "application/x-www-form-urlencoded" + encoded_fields = '' + if %w[POST PUT].include? method_name + if content_type == "application/x-www-form-urlencoded" + unless params.empty? encoded_fields = URI.encode_www_form(params, Encoding::UTF_8) request.set_form_data(params) - else - encoded_fields = params.to_json - request.body = encoded_fields - request.set_content_type("application/json") end else - encoded_fields = [] - resource_uri.query = URI.encode_www_form(params, Encoding::UTF_8) + encoded_fields = params.to_json + request.body = encoded_fields + request.set_content_type("application/json") end + else + resource_uri.query = URI.encode_www_form(params, Encoding::UTF_8) end headers = RestClient.generate_telesign_headers(@customer_id, @@ -216,7 +216,7 @@ def execute(method_function, method_name, resource, **params) resource, content_type, encoded_fields, - user_agent: @user_agent) + user_agent: @@user_agent) headers.each do |k, v| request[k] = v diff --git a/telesign.gemspec b/telesign.gemspec index 68a6ff9..16fea16 100644 --- a/telesign.gemspec +++ b/telesign.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'telesign' - s.version = '2.2.1' + s.version = '2.2.2' s.licenses = ['MIT'] s.date = '2017-05-25' s.summary = 'TeleSign Ruby SDK' diff --git a/test/test_rest.rb b/test/test_rest.rb index df260cf..554010f 100644 --- a/test/test_rest.rb +++ b/test/test_rest.rb @@ -202,4 +202,36 @@ def test_rest_client_delete assert_requested :delete, 'http://localhost/test/resource', headers: {'x-ts-nonce' => /.*\S.*/} assert_requested :delete, 'http://localhost/test/resource', headers: {'Date' => /.*\S.*/} end + + def test_phoneid + stub_request(:post, 'localhost/v1/phoneid/1234567890').to_return(body: '{}') + + client = Telesign::PhoneIdClient::new(@customer_id, + @api_key, + rest_endpoint: 'http://localhost') + client.phoneid('1234567890') + + assert_requested :post, 'http://localhost/v1/phoneid/1234567890' + assert_requested :post, 'http://localhost/v1/phoneid/1234567890', body: '{}' + assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'Content-Type' => 'application/json'} + assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'x-ts-auth-method' => 'HMAC-SHA256'} + assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'x-ts-nonce' => /.*\S.*/} + assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'Date' => /.*\S.*/} + end + + def test_phoneid_with_addons + stub_request(:post, 'localhost/v1/phoneid/1234567890').to_return(body: '{}') + + client = Telesign::PhoneIdClient::new(@customer_id, + @api_key, + rest_endpoint: 'http://localhost') + client.phoneid('1234567890', addons: {'contact': {}}) + + assert_requested :post, 'http://localhost/v1/phoneid/1234567890' + assert_requested :post, 'http://localhost/v1/phoneid/1234567890', body: '{"addons":{"contact":{}}}' + assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'Content-Type' => 'application/json'} + assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'x-ts-auth-method' => 'HMAC-SHA256'} + assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'x-ts-nonce' => /.*\S.*/} + assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'Date' => /.*\S.*/} + end end