@@ -40,6 +40,7 @@ def test_headers_are_added_to_the_request
4040 headers : {
4141 "Authorization" => "Bearer token" ,
4242 "Content-Type" => "application/json" ,
43+ "Accept" => "application/json, text/event-stream" ,
4344 } ,
4445 body : request . to_json ,
4546 )
@@ -54,6 +55,53 @@ def test_headers_are_added_to_the_request
5455 client . send_request ( request : request )
5556 end
5657
58+ def test_accept_header_is_included_in_requests
59+ request = {
60+ jsonrpc : "2.0" ,
61+ id : "test_id" ,
62+ method : "tools/list" ,
63+ }
64+
65+ stub_request ( :post , url )
66+ . with (
67+ headers : {
68+ "Accept" => "application/json, text/event-stream" ,
69+ } ,
70+ )
71+ . to_return (
72+ status : 200 ,
73+ headers : { "Content-Type" => "application/json" } ,
74+ body : { result : { tools : [ ] } } . to_json ,
75+ )
76+
77+ client . send_request ( request : request )
78+ end
79+
80+ def test_custom_accept_header_overrides_default
81+ custom_accept = "application/json"
82+ custom_client = HTTP . new ( url : url , headers : { "Accept" => custom_accept } )
83+
84+ request = {
85+ jsonrpc : "2.0" ,
86+ id : "test_id" ,
87+ method : "tools/list" ,
88+ }
89+
90+ stub_request ( :post , url )
91+ . with (
92+ headers : {
93+ "Accept" => custom_accept ,
94+ } ,
95+ )
96+ . to_return (
97+ status : 200 ,
98+ headers : { "Content-Type" => "application/json" } ,
99+ body : { result : { tools : [ ] } } . to_json ,
100+ )
101+
102+ custom_client . send_request ( request : request )
103+ end
104+
57105 def test_send_request_returns_faraday_response
58106 request = {
59107 jsonrpc : "2.0" ,
@@ -194,6 +242,33 @@ def test_send_request_raises_internal_error
194242 assert_equal ( { method : "tools/list" , params : nil } , error . request )
195243 end
196244
245+ def test_send_request_raises_error_for_non_json_response
246+ request = {
247+ jsonrpc : "2.0" ,
248+ id : "test_id" ,
249+ method : "tools/list" ,
250+ }
251+
252+ stub_request ( :post , url )
253+ . with ( body : request . to_json )
254+ . to_return (
255+ status : 200 ,
256+ headers : { "Content-Type" => "text/event-stream" } ,
257+ body : "data: {}\n \n " ,
258+ )
259+
260+ error = assert_raises ( RequestHandlerError ) do
261+ client . send_request ( request : request )
262+ end
263+
264+ assert_equal (
265+ 'Unsupported Content-Type: "text/event-stream". This client only supports JSON responses.' ,
266+ error . message ,
267+ )
268+ assert_equal ( :unsupported_media_type , error . error_type )
269+ assert_equal ( { method : "tools/list" , params : nil } , error . request )
270+ end
271+
197272 private
198273
199274 def stub_request ( method , url )
0 commit comments