@@ -12,6 +12,14 @@ def initialize(response)
1212 end
1313end
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+
1523class MockRequest < SendGrid ::Client
1624 def make_request ( _http , _request )
1725 response = { }
@@ -22,6 +30,17 @@ def make_request(_http, _request)
2230 end
2331end
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+
2544class 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