@@ -61,14 +61,20 @@ const postsRouter = c.router({
6161 authorId : z . string ( ) ,
6262 } ) ,
6363 } ,
64- createPostXForm : {
64+ echoPostXForm : {
6565 method : 'POST' ,
66- path : '/posts' ,
66+ path : '/echo' ,
67+ contentType : 'application/x-www-form-urlencoded' ,
68+ body : z . object ( {
69+ foo : z . string ( ) ,
70+ bar : z . string ( ) ,
71+ } ) ,
6772 responses : {
68- 200 : c . response < Post > ( ) ,
73+ 200 : c . otherResponse ( {
74+ contentType : 'text/plain' ,
75+ body : z . string ( ) ,
76+ } ) ,
6977 } ,
70- body : z . string ( ) ,
71- contentType : 'application/x-www-form-urlencoded' ,
7278 } ,
7379 mutationWithQuery : {
7480 method : 'POST' ,
@@ -444,32 +450,65 @@ describe('client', () => {
444450 expect ( result . headers . get ( 'Content-Type' ) ) . toBe ( 'application/json' ) ;
445451 } ) ;
446452
447- it ( 'w/ body and content-type header' , async ( ) => {
448- const value = 'key=value' ;
453+ it ( 'w/ urlencoded body - passing object' , async ( ) => {
449454 fetchMock . postOnce (
450455 {
451- url : 'https://api.com/posts ' ,
456+ url : 'https://api.com/echo ' ,
452457 headers : {
453458 'content-type' : 'application/x-www-form-urlencoded' ,
454459 } ,
455460 } ,
461+ ( _ , req ) => {
462+ expect ( req . body ) . toBeInstanceOf ( URLSearchParams ) ;
463+
464+ return {
465+ body : req . body ! . toString ( ) ,
466+ status : 200 ,
467+ } ;
468+ } ,
469+ ) ;
470+
471+ const result = await client . posts . echoPostXForm ( {
472+ body : {
473+ foo : 'foo' ,
474+ bar : 'bar' ,
475+ } ,
476+ } ) ;
477+
478+ expect ( result . status ) . toBe ( 200 ) ;
479+ expect ( result . headers . get ( 'Content-Type' ) ) . toBe (
480+ 'text/plain;charset=UTF-8' ,
481+ ) ;
482+ expect ( result . body ) . toBe ( 'foo=foo&bar=bar' ) ;
483+ } ) ;
484+
485+ it ( 'w/ urlencoded body - passing string' , async ( ) => {
486+ fetchMock . postOnce (
456487 {
457- body : value ,
458- status : 200 ,
488+ url : 'https://api.com/echo' ,
459489 headers : {
460490 'content-type' : 'application/x-www-form-urlencoded' ,
461491 } ,
462492 } ,
493+ ( _ , req ) => {
494+ expect ( typeof req . body ) . toBe ( 'string' ) ;
495+
496+ return {
497+ body : req . body ,
498+ status : 200 ,
499+ } ;
500+ } ,
463501 ) ;
464502
465- const result = await client . posts . createPostXForm ( {
466- body : 'key=value ' ,
503+ const result = await client . posts . echoPostXForm ( {
504+ body : 'foo=foo&bar=bar ' ,
467505 } ) ;
468506
469507 expect ( result . status ) . toBe ( 200 ) ;
470508 expect ( result . headers . get ( 'Content-Type' ) ) . toBe (
471- 'application/x-www-form-urlencoded ' ,
509+ 'text/plain;charset=UTF-8 ' ,
472510 ) ;
511+ expect ( result . body ) . toBe ( 'foo=foo&bar=bar' ) ;
473512 } ) ;
474513
475514 it ( 'w/ query params' , async ( ) => {
0 commit comments