@@ -12,6 +12,14 @@ def initialize(response)
12
12
end
13
13
end
14
14
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
+
15
23
class MockRequest < SendGrid ::Client
16
24
def make_request ( _http , _request )
17
25
response = { }
@@ -22,6 +30,17 @@ def make_request(_http, _request)
22
30
end
23
31
end
24
32
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
+
25
44
class TestClient < Minitest ::Test
26
45
def setup
27
46
@headers = JSON . parse ( '
@@ -158,6 +177,48 @@ def test_build_request_post_multipart
158
177
assert_equal ( 'hogebody' , client . request . body )
159
178
end
160
179
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
+
161
222
def add_ssl
162
223
uri = URI . parse ( 'https://localhost:4010' )
163
224
http = Net ::HTTP . new ( uri . host , uri . port )
0 commit comments