Skip to content

fix: JSON body reencoding and Rubocop configuration #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ Metrics/ModuleLength:
Enabled: false
PerceivedComplexity:
Enabled: false
Style/SpaceBeforeFirstArg:
Layout/SpaceBeforeFirstArg:
Enabled: true
Style/ClassAndModuleChildren:
Enabled: false
Style/EmptyLinesAroundBlockBody:
Layout/EmptyLinesAroundBlockBody:
Enabled: true
Style/FileName:
Naming/FileName:
Enabled: true
Style/RescueModifier:
Enabled: true
Expand All @@ -39,7 +39,7 @@ Metrics/BlockLength:
Enabled: false
Style/NumericLiterals:
Enabled: false
Style/ExtraSpacing:
Layout/ExtraSpacing:
Enabled: true
AllowForAlignment: false
ForceEqualSignAlignment: false
7 changes: 6 additions & 1 deletion lib/ruby_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ def build_request(name, args)
(!@request_headers.key?('Content-Type') ||
@request_headers['Content-Type'] == 'application/json')

@request.body = @request_body.to_json
# If body is a hash, encode it; else leave it alone
@request.body = if @request_body.class == Hash
@request_body.to_json
else
@request_body
end
@request['Content-Type'] = 'application/json'
elsif !@request_body && (name.to_s == 'post')
@request['Content-Type'] = ''
Expand Down
61 changes: 61 additions & 0 deletions test/test_ruby_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ def initialize(response)
end
end

class MockResponseWithRequestBody < MockResponse
attr_reader :request_body

def initialize(response)
@request_body = response['request_body']
end
end

class MockRequest < SendGrid::Client
def make_request(_http, _request)
response = {}
Expand All @@ -22,6 +30,17 @@ def make_request(_http, _request)
end
end

class MockRequestWithRequestBody < SendGrid::Client
def make_request(_http, request)
response = {}
response['code'] = 200
response['body'] = { 'message' => 'success' }
response['headers'] = { 'headers' => 'test' }
response['request_body'] = request.body
MockResponseWithRequestBody.new(response)
end
end

class TestClient < Minitest::Test
def setup
@headers = JSON.parse('
Expand Down Expand Up @@ -158,6 +177,48 @@ def test_build_request_post_multipart
assert_equal('hogebody', client.request.body)
end

def test_json_body_encode
headers = {
'Content-Type' => 'application/json'
}
client = MockRequestWithRequestBody.new(
host: 'https://localhost',
request_headers: headers
)
name = 'post'
args = [{ 'request_body' => { 'this_is' => 'json' } }]
response = client.build_request(name, args)
assert_equal('{"this_is":"json"}', response.request_body)
end

def test_json_body_do_not_reencode
headers = {
'Content-Type' => 'application/json'
}
client = MockRequestWithRequestBody.new(
host: 'https://localhost',
request_headers: headers
)
name = 'post'
args = [{ 'request_body' => '{"this_is":"json"}' }]
response = client.build_request(name, args)
assert_equal('{"this_is":"json"}', response.request_body)
end

def test_json_body_do_not_reencode_simplejson
headers = {
'Content-Type' => 'application/json'
}
client = MockRequestWithRequestBody.new(
host: 'https://localhost',
request_headers: headers
)
name = 'post'
args = [{ 'request_body' => 'true' }]
response = client.build_request(name, args)
assert_equal('true', response.request_body)
end

def add_ssl
uri = URI.parse('https://localhost:4010')
http = Net::HTTP.new(uri.host, uri.port)
Expand Down