@@ -109,6 +109,26 @@ describe('HTTP Request Tool', () => {
109109 } )
110110 } )
111111
112+ it . concurrent ( 'should respect custom Content-Type headers' , ( ) => {
113+ // Custom Content-Type should not be overridden
114+ const headers = tester . getRequestHeaders ( {
115+ url : 'https://api.example.com' ,
116+ method : 'POST' ,
117+ body : { key : 'value' } ,
118+ headers : [ { Key : 'Content-Type' , Value : 'application/x-www-form-urlencoded' } ] ,
119+ } )
120+ expect ( headers [ 'Content-Type' ] ) . toBe ( 'application/x-www-form-urlencoded' )
121+
122+ // Case-insensitive Content-Type should not be overridden
123+ const headers2 = tester . getRequestHeaders ( {
124+ url : 'https://api.example.com' ,
125+ method : 'POST' ,
126+ body : { key : 'value' } ,
127+ headers : [ { Key : 'content-type' , Value : 'text/plain' } ] ,
128+ } )
129+ expect ( headers2 [ 'content-type' ] ) . toBe ( 'text/plain' )
130+ } )
131+
112132 it ( 'should set dynamic Referer header correctly' , async ( ) => {
113133 const originalWindow = global . window
114134 Object . defineProperty ( global , 'window' , {
@@ -164,6 +184,30 @@ describe('HTTP Request Tool', () => {
164184 } )
165185 } )
166186
187+ describe ( 'Body Construction' , ( ) => {
188+ it . concurrent ( 'should handle JSON bodies correctly' , ( ) => {
189+ const body = { username : 'test' , password : 'secret' }
190+
191+ expect (
192+ tester . getRequestBody ( {
193+ url : 'https://api.example.com' ,
194+ body,
195+ } )
196+ ) . toEqual ( body )
197+ } )
198+
199+ it . concurrent ( 'should handle FormData correctly' , ( ) => {
200+ const formData = { file : 'test.txt' , content : 'file content' }
201+
202+ const result = tester . getRequestBody ( {
203+ url : 'https://api.example.com' ,
204+ formData,
205+ } )
206+
207+ expect ( result ) . toBeInstanceOf ( FormData )
208+ } )
209+ } )
210+
167211 describe ( 'Request Execution' , ( ) => {
168212 it ( 'should apply default and dynamic headers to requests' , async ( ) => {
169213 // Setup mock response
@@ -253,6 +297,59 @@ describe('HTTP Request Tool', () => {
253297 expect ( bodyArg ) . toEqual ( body )
254298 } )
255299
300+ it ( 'should handle POST requests with URL-encoded form data' , async ( ) => {
301+ // Setup mock response
302+ tester . setup ( { result : 'success' } )
303+
304+ // Create test body
305+ const body = { username :
'testuser123' , password :
'testpass456' , email :
'[email protected] ' } 306+
307+ // Execute the tool with form-urlencoded content type
308+ await tester . execute ( {
309+ url : 'https://api.example.com/oauth/token' ,
310+ method : 'POST' ,
311+ body,
312+ headers : [ { cells : { Key : 'Content-Type' , Value : 'application/x-www-form-urlencoded' } } ] ,
313+ } )
314+
315+ // Verify the request was made with correct headers
316+ const fetchCall = ( global . fetch as any ) . mock . calls [ 0 ]
317+ expect ( fetchCall [ 0 ] ) . toBe ( 'https://api.example.com/oauth/token' )
318+ expect ( fetchCall [ 1 ] . method ) . toBe ( 'POST' )
319+ expect ( fetchCall [ 1 ] . headers [ 'Content-Type' ] ) . toBe ( 'application/x-www-form-urlencoded' )
320+
321+ // Verify the body is URL-encoded (should not be JSON stringified)
322+ expect ( fetchCall [ 1 ] . body ) . toBe (
323+ 'username=testuser123&password=testpass456&email=test%40example.com'
324+ )
325+ } )
326+
327+ it ( 'should handle OAuth client credentials requests' , async ( ) => {
328+ // Setup mock response for OAuth token endpoint
329+ tester . setup ( { access_token : 'token123' , token_type : 'Bearer' } )
330+
331+ // Execute OAuth client credentials request
332+ await tester . execute ( {
333+ url : 'https://oauth.example.com/token' ,
334+ method : 'POST' ,
335+ body : { grant_type : 'client_credentials' , scope : 'read write' } ,
336+ headers : [
337+ { cells : { Key : 'Content-Type' , Value : 'application/x-www-form-urlencoded' } } ,
338+ { cells : { Key : 'Authorization' , Value : 'Basic Y2xpZW50OnNlY3JldA==' } } ,
339+ ] ,
340+ } )
341+
342+ // Verify the OAuth request was properly formatted
343+ const fetchCall = ( global . fetch as any ) . mock . calls [ 0 ]
344+ expect ( fetchCall [ 0 ] ) . toBe ( 'https://oauth.example.com/token' )
345+ expect ( fetchCall [ 1 ] . method ) . toBe ( 'POST' )
346+ expect ( fetchCall [ 1 ] . headers [ 'Content-Type' ] ) . toBe ( 'application/x-www-form-urlencoded' )
347+ expect ( fetchCall [ 1 ] . headers . Authorization ) . toBe ( 'Basic Y2xpZW50OnNlY3JldA==' )
348+
349+ // Verify the body is URL-encoded
350+ expect ( fetchCall [ 1 ] . body ) . toBe ( 'grant_type=client_credentials&scope=read+write' )
351+ } )
352+
256353 it ( 'should handle errors correctly' , async ( ) => {
257354 // Setup error response
258355 tester . setup ( mockHttpResponses . error , { ok : false , status : 400 } )
0 commit comments