@@ -77,6 +77,45 @@ router.get('/fetch', async (ctx) => {
7777 ctx . body = data
7878} )
7979
80+ // Test case 1: text/plain + JSON body
81+ router . get ( '/post-text-plain' , async ( ctx ) => {
82+ const res = await axios . post (
83+ 'https://jsonplaceholder.typicode.com/posts' ,
84+ JSON . stringify ( { title : 'foo' , body : 'bar' , userId : 1 } ) ,
85+ { headers : { 'Content-Type' : 'text/plain' } }
86+ )
87+ ctx . body = res . data
88+ } )
89+
90+ // Test case 2: application/x-www-form-urlencoded
91+ router . get ( '/post-form' , async ( ctx ) => {
92+ const res = await axios . post (
93+ 'https://jsonplaceholder.typicode.com/posts' ,
94+ 'title=foo&body=bar&userId=1' ,
95+ { headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } }
96+ )
97+ ctx . body = res . data
98+ } )
99+
100+ // Test case 3: pure text body
101+ router . get ( '/post-pure-text' , async ( ctx ) => {
102+ const res = await axios . post (
103+ 'https://jsonplaceholder.typicode.com/posts' ,
104+ 'This is a plain text message' ,
105+ { headers : { 'Content-Type' : 'text/plain' } }
106+ )
107+ ctx . body = res . data
108+ } )
109+
110+ // Test case 4: Buffer body
111+ router . get ( '/post-buffer' , async ( ctx ) => {
112+ const buffer = Buffer . from ( JSON . stringify ( { title : 'buffer' , body : 'test' , userId : 1 } ) )
113+ const res = await axios . post ( 'https://jsonplaceholder.typicode.com/posts' , buffer , {
114+ headers : { 'Content-Type' : 'application/json' }
115+ } )
116+ ctx . body = res . data
117+ } )
118+
80119// SSE test endpoint - server side
81120router . get ( '/sse-server' , async ( ctx ) => {
82121 ctx . set ( 'Content-Type' , 'text/event-stream' )
@@ -90,7 +129,9 @@ router.get('/sse-server', async (ctx) => {
90129 let count = 0
91130 const interval = setInterval ( ( ) => {
92131 count ++
93- res . write ( `event: message\nid: ${ count } \ndata: {"count": ${ count } , "time": "${ new Date ( ) . toISOString ( ) } "}\n\n` )
132+ res . write (
133+ `event: message\nid: ${ count } \ndata: {"count": ${ count } , "time": "${ new Date ( ) . toISOString ( ) } "}\n\n`
134+ )
94135 if ( count >= 5 ) {
95136 clearInterval ( interval )
96137 res . end ( )
@@ -102,18 +143,18 @@ router.get('/sse-server', async (ctx) => {
102143router . get ( '/sse-fetch' , async ( ctx ) => {
103144 // Fetch SSE from our own server
104145 const response = await fetch ( 'http://localhost:3001/sse-server' )
105-
146+
106147 // Consume the stream to capture events
107148 const reader = response . body . getReader ( )
108149 const decoder = new TextDecoder ( )
109150 let result = ''
110-
151+
111152 while ( true ) {
112153 const { done, value } = await reader . read ( )
113154 if ( done ) break
114155 result += decoder . decode ( value , { stream : true } )
115156 }
116-
157+
117158 ctx . body = { message : 'SSE stream consumed' , data : result }
118159} )
119160
0 commit comments