Skip to content

Commit

Permalink
Update to v2.2.2. Fix handling of application/json requests with empt…
Browse files Browse the repository at this point in the history
…y parameters
  • Loading branch information
Robert Ramsay committed Jun 7, 2018
1 parent 1d62b13 commit d3910ea
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
4 changes: 4 additions & 0 deletions RELEASE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 12 additions & 12 deletions lib/telesign/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion telesign.gemspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
32 changes: 32 additions & 0 deletions test/test_rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d3910ea

Please sign in to comment.