@@ -490,6 +490,118 @@ m.on('http', (req, res) => {
490490 r .Equal (t , map [string ]any {"foo" : "yuh" }, res .Data )
491491 },
492492 },
493+ {
494+ name : "rebuild function not defined" ,
495+ script : `
496+ const m = require('mokapi')
497+ m.on('http', (req, res) => {
498+ res.rebuild();
499+ })
500+ ` ,
501+ run : func (evt common.EventEmitter ) []* common.Action {
502+ res := & common.HttpEventResponse {Data : map [string ]any {"foo" : "bar" }}
503+ return evt .Emit ("http" , & common.HttpEventRequest {}, res )
504+ },
505+ test : func (t * testing.T , actions []* common.Action , err error ) {
506+ r .NoError (t , err )
507+
508+ r .Nil (t , actions [0 ].Error )
509+
510+ var res * common.HttpEventResponse
511+ err = json .Unmarshal ([]byte (actions [0 ].Parameters [1 ].(string )), & res )
512+ r .Equal (t , map [string ]any {"foo" : "bar" }, res .Data )
513+ },
514+ },
515+ {
516+ name : "rebuild function updates data" ,
517+ script : `
518+ const m = require('mokapi')
519+ m.on('http', (req, res) => {
520+ res.rebuild();
521+ })
522+ ` ,
523+ run : func (evt common.EventEmitter ) []* common.Action {
524+ res := & common.HttpEventResponse {Data : map [string ]any {"foo" : "bar" }}
525+ res .Rebuild = func (statusCode int , contentType string ) {
526+ res .Data = map [string ]any {"foo" : "yuh" }
527+ }
528+ return evt .Emit ("http" , & common.HttpEventRequest {}, res )
529+ },
530+ test : func (t * testing.T , actions []* common.Action , err error ) {
531+ r .NoError (t , err )
532+
533+ r .Nil (t , actions [0 ].Error )
534+
535+ var res * common.HttpEventResponse
536+ err = json .Unmarshal ([]byte (actions [0 ].Parameters [1 ].(string )), & res )
537+ r .Equal (t , map [string ]any {"foo" : "yuh" }, res .Data )
538+ },
539+ },
540+ {
541+ name : "rebuild function with parameters" ,
542+ script : `
543+ const m = require('mokapi')
544+ m.on('http', (req, res) => {
545+ res.rebuild(200, 'application/json');
546+ })
547+ ` ,
548+ run : func (evt common.EventEmitter ) []* common.Action {
549+ res := & common.HttpEventResponse {Data : map [string ]any {"foo" : "bar" }}
550+ res .Rebuild = func (statusCode int , contentType string ) {
551+ res .Data = map [string ]any {"statusCode" : statusCode , "contentType" : contentType }
552+ }
553+ return evt .Emit ("http" , & common.HttpEventRequest {}, res )
554+ },
555+ test : func (t * testing.T , actions []* common.Action , err error ) {
556+ r .NoError (t , err )
557+
558+ r .Nil (t , actions [0 ].Error )
559+
560+ var res * common.HttpEventResponse
561+ err = json .Unmarshal ([]byte (actions [0 ].Parameters [1 ].(string )), & res )
562+ r .Equal (t , map [string ]any {"statusCode" : float64 (200 ), "contentType" : "application/json" }, res .Data )
563+ },
564+ },
565+ {
566+ name : "rebuild function wrong type statusCode" ,
567+ script : `
568+ const m = require('mokapi')
569+ m.on('http', (req, res) => {
570+ res.rebuild({ }, 'application/json');
571+ })
572+ ` ,
573+ run : func (evt common.EventEmitter ) []* common.Action {
574+ res := & common.HttpEventResponse {Data : map [string ]any {"foo" : "bar" }}
575+ res .Rebuild = func (statusCode int , contentType string ) {}
576+ return evt .Emit ("http" , & common.HttpEventRequest {}, res )
577+ },
578+ test : func (t * testing.T , actions []* common.Action , err error ) {
579+ r .NoError (t , err )
580+
581+ r .NotNil (t , actions [0 ].Error )
582+ r .Equal (t , "response.rebuild failed: statusCode must be a number: got Object" , actions [0 ].Error .Message )
583+ },
584+ },
585+ {
586+ name : "rebuild function wrong type contentType" ,
587+ script : `
588+ const m = require('mokapi')
589+ m.on('http', (req, res) => {
590+ res.rebuild(100, 200);
591+ })
592+ ` ,
593+ run : func (evt common.EventEmitter ) []* common.Action {
594+ res := & common.HttpEventResponse {Data : map [string ]any {"foo" : "bar" }}
595+ res .Rebuild = func (statusCode int , contentType string ) {}
596+ return evt .Emit ("http" , & common.HttpEventRequest {}, res )
597+ },
598+ test : func (t * testing.T , actions []* common.Action , err error ) {
599+ r .NoError (t , err )
600+
601+ r .NotNil (t , actions [0 ].Error )
602+ r .Equal (t , "response.rebuild failed: contentType must be a string: got Integer" , actions [0 ].Error .Message )
603+ },
604+ },
493605 }
494606
495607 for _ , tc := range testcases {
0 commit comments