Skip to content

Commit ce8d63e

Browse files
authored
Fix JSON body reencoding and Rubocop configuration (#95)
1 parent 66c3eae commit ce8d63e

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

.rubocop.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ Metrics/ModuleLength:
2323
Enabled: false
2424
PerceivedComplexity:
2525
Enabled: false
26-
Style/SpaceBeforeFirstArg:
26+
Layout/SpaceBeforeFirstArg:
2727
Enabled: true
2828
Style/ClassAndModuleChildren:
2929
Enabled: false
30-
Style/EmptyLinesAroundBlockBody:
30+
Layout/EmptyLinesAroundBlockBody:
3131
Enabled: true
32-
Style/FileName:
32+
Naming/FileName:
3333
Enabled: true
3434
Style/RescueModifier:
3535
Enabled: true
@@ -39,7 +39,7 @@ Metrics/BlockLength:
3939
Enabled: false
4040
Style/NumericLiterals:
4141
Enabled: false
42-
Style/ExtraSpacing:
42+
Layout/ExtraSpacing:
4343
Enabled: true
4444
AllowForAlignment: false
4545
ForceEqualSignAlignment: false

lib/ruby_http_client.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ def build_request(name, args)
149149
(!@request_headers.key?('Content-Type') ||
150150
@request_headers['Content-Type'] == 'application/json')
151151

152-
@request.body = @request_body.to_json
152+
# If body is a hash, encode it; else leave it alone
153+
@request.body = if @request_body.class == Hash
154+
@request_body.to_json
155+
else
156+
@request_body
157+
end
153158
@request['Content-Type'] = 'application/json'
154159
elsif !@request_body && (name.to_s == 'post')
155160
@request['Content-Type'] = ''

test/test_ruby_http_client.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ def initialize(response)
1212
end
1313
end
1414

15+
class MockResponseWithRequestBody < MockResponse
16+
attr_reader :request_body
17+
18+
def initialize(response)
19+
@request_body = response['request_body']
20+
end
21+
end
22+
1523
class MockRequest < SendGrid::Client
1624
def make_request(_http, _request)
1725
response = {}
@@ -22,6 +30,17 @@ def make_request(_http, _request)
2230
end
2331
end
2432

33+
class MockRequestWithRequestBody < SendGrid::Client
34+
def make_request(_http, request)
35+
response = {}
36+
response['code'] = 200
37+
response['body'] = { 'message' => 'success' }
38+
response['headers'] = { 'headers' => 'test' }
39+
response['request_body'] = request.body
40+
MockResponseWithRequestBody.new(response)
41+
end
42+
end
43+
2544
class TestClient < Minitest::Test
2645
def setup
2746
@headers = JSON.parse('
@@ -158,6 +177,48 @@ def test_build_request_post_multipart
158177
assert_equal('hogebody', client.request.body)
159178
end
160179

180+
def test_json_body_encode
181+
headers = {
182+
'Content-Type' => 'application/json'
183+
}
184+
client = MockRequestWithRequestBody.new(
185+
host: 'https://localhost',
186+
request_headers: headers
187+
)
188+
name = 'post'
189+
args = [{ 'request_body' => { 'this_is' => 'json' } }]
190+
response = client.build_request(name, args)
191+
assert_equal('{"this_is":"json"}', response.request_body)
192+
end
193+
194+
def test_json_body_do_not_reencode
195+
headers = {
196+
'Content-Type' => 'application/json'
197+
}
198+
client = MockRequestWithRequestBody.new(
199+
host: 'https://localhost',
200+
request_headers: headers
201+
)
202+
name = 'post'
203+
args = [{ 'request_body' => '{"this_is":"json"}' }]
204+
response = client.build_request(name, args)
205+
assert_equal('{"this_is":"json"}', response.request_body)
206+
end
207+
208+
def test_json_body_do_not_reencode_simplejson
209+
headers = {
210+
'Content-Type' => 'application/json'
211+
}
212+
client = MockRequestWithRequestBody.new(
213+
host: 'https://localhost',
214+
request_headers: headers
215+
)
216+
name = 'post'
217+
args = [{ 'request_body' => 'true' }]
218+
response = client.build_request(name, args)
219+
assert_equal('true', response.request_body)
220+
end
221+
161222
def add_ssl
162223
uri = URI.parse('https://localhost:4010')
163224
http = Net::HTTP.new(uri.host, uri.port)

0 commit comments

Comments
 (0)