Skip to content

Commit d3910ea

Browse files
author
Robert Ramsay
committed
Update to v2.2.2. Fix handling of application/json requests with empty parameters
1 parent 1d62b13 commit d3910ea

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

RELEASE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2.2.2
2+
3+
- Fixed issue handling empty parameters for application/json content-type
4+
15
2.2.1
26

37
- Added support for application/json content-type

lib/telesign/rest.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
require 'net/http/persistent'
88

99
module Telesign
10-
SDK_VERSION = '2.2.1'
10+
SDK_VERSION = '2.2.2'
1111

1212
# The TeleSign RestClient is a generic HTTP REST client that can be extended to make requests against any of
1313
# TeleSign's REST API endpoints.
@@ -18,7 +18,7 @@ module Telesign
1818
# See https://developer.telesign.com for detailed API documentation.
1919
class RestClient
2020

21-
@user_agent = "TeleSignSDK/ruby-{#{SDK_VERSION} #{RUBY_DESCRIPTION} net/http/persistent"
21+
@@user_agent = "TeleSignSDK/ruby-{#{SDK_VERSION} #{RUBY_DESCRIPTION} net/http/persistent"
2222

2323
# A simple HTTP Response object to abstract the underlying net/http library response.
2424

@@ -194,20 +194,20 @@ def execute(method_function, method_name, resource, **params)
194194

195195
request = method_function.new(resource_uri.request_uri)
196196

197-
unless params.empty?
198-
if %w[POST PUT].include? method_name
199-
if content_type == "application/x-www-form-urlencoded"
197+
encoded_fields = ''
198+
if %w[POST PUT].include? method_name
199+
if content_type == "application/x-www-form-urlencoded"
200+
unless params.empty?
200201
encoded_fields = URI.encode_www_form(params, Encoding::UTF_8)
201202
request.set_form_data(params)
202-
else
203-
encoded_fields = params.to_json
204-
request.body = encoded_fields
205-
request.set_content_type("application/json")
206203
end
207204
else
208-
encoded_fields = []
209-
resource_uri.query = URI.encode_www_form(params, Encoding::UTF_8)
205+
encoded_fields = params.to_json
206+
request.body = encoded_fields
207+
request.set_content_type("application/json")
210208
end
209+
else
210+
resource_uri.query = URI.encode_www_form(params, Encoding::UTF_8)
211211
end
212212

213213
headers = RestClient.generate_telesign_headers(@customer_id,
@@ -216,7 +216,7 @@ def execute(method_function, method_name, resource, **params)
216216
resource,
217217
content_type,
218218
encoded_fields,
219-
user_agent: @user_agent)
219+
user_agent: @@user_agent)
220220

221221
headers.each do |k, v|
222222
request[k] = v

telesign.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'telesign'
3-
s.version = '2.2.1'
3+
s.version = '2.2.2'
44
s.licenses = ['MIT']
55
s.date = '2017-05-25'
66
s.summary = 'TeleSign Ruby SDK'

test/test_rest.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,36 @@ def test_rest_client_delete
202202
assert_requested :delete, 'http://localhost/test/resource', headers: {'x-ts-nonce' => /.*\S.*/}
203203
assert_requested :delete, 'http://localhost/test/resource', headers: {'Date' => /.*\S.*/}
204204
end
205+
206+
def test_phoneid
207+
stub_request(:post, 'localhost/v1/phoneid/1234567890').to_return(body: '{}')
208+
209+
client = Telesign::PhoneIdClient::new(@customer_id,
210+
@api_key,
211+
rest_endpoint: 'http://localhost')
212+
client.phoneid('1234567890')
213+
214+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890'
215+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890', body: '{}'
216+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'Content-Type' => 'application/json'}
217+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'x-ts-auth-method' => 'HMAC-SHA256'}
218+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'x-ts-nonce' => /.*\S.*/}
219+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'Date' => /.*\S.*/}
220+
end
221+
222+
def test_phoneid_with_addons
223+
stub_request(:post, 'localhost/v1/phoneid/1234567890').to_return(body: '{}')
224+
225+
client = Telesign::PhoneIdClient::new(@customer_id,
226+
@api_key,
227+
rest_endpoint: 'http://localhost')
228+
client.phoneid('1234567890', addons: {'contact': {}})
229+
230+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890'
231+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890', body: '{"addons":{"contact":{}}}'
232+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'Content-Type' => 'application/json'}
233+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'x-ts-auth-method' => 'HMAC-SHA256'}
234+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'x-ts-nonce' => /.*\S.*/}
235+
assert_requested :post, 'http://localhost/v1/phoneid/1234567890', headers: {'Date' => /.*\S.*/}
236+
end
205237
end

0 commit comments

Comments
 (0)