@@ -624,15 +624,19 @@ fn register_route(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArgumen
624
624
// Create mock request and response objects to test function behavior
625
625
let mock_req = create_mock_request ( scope, method, & path) ;
626
626
let mock_res = create_mock_response ( scope) ;
627
-
627
+
628
628
// Execute the function with mock objects
629
629
let args = [ mock_req. into ( ) , mock_res. into ( ) ] ;
630
- if let Some ( _result) = func. call ( scope, v8:: undefined ( scope) . into ( ) , & args) {
630
+ let undefined = v8:: undefined ( scope) ;
631
+ if let Some ( _result) = func. call ( scope, undefined. into ( ) , & args) {
631
632
// Extract the response pattern from the mock response
632
633
extract_response_pattern ( scope, mock_res, method, & path)
633
634
} else {
634
635
// Fallback if execution fails
635
- format ! ( "function(req, res) {{ res.send('Default response for {} {}'); }}" , method, path)
636
+ format ! (
637
+ "function(req, res) {{ res.send('Default response for {} {}'); }}" ,
638
+ method, path
639
+ )
636
640
}
637
641
} else {
638
642
// Fallback for functions that can't be converted
@@ -669,76 +673,76 @@ fn create_mock_request<'a>(
669
673
path : & str ,
670
674
) -> v8:: Local < ' a , v8:: Object > {
671
675
let req_obj = v8:: Object :: new ( scope) ;
672
-
676
+
673
677
// Set method
674
678
let method_key = v8:: String :: new ( scope, "method" ) . unwrap ( ) ;
675
679
let method_val = v8:: String :: new ( scope, method) . unwrap ( ) ;
676
680
req_obj. set ( scope, method_key. into ( ) , method_val. into ( ) ) ;
677
-
681
+
678
682
// Set path
679
683
let path_key = v8:: String :: new ( scope, "path" ) . unwrap ( ) ;
680
684
let path_val = v8:: String :: new ( scope, path) . unwrap ( ) ;
681
685
req_obj. set ( scope, path_key. into ( ) , path_val. into ( ) ) ;
682
-
686
+
683
687
// Set url
684
688
let url_key = v8:: String :: new ( scope, "url" ) . unwrap ( ) ;
685
689
req_obj. set ( scope, url_key. into ( ) , path_val. into ( ) ) ;
686
-
690
+
687
691
// Set empty params and query
688
692
let params_key = v8:: String :: new ( scope, "params" ) . unwrap ( ) ;
689
693
let params_obj = v8:: Object :: new ( scope) ;
690
694
req_obj. set ( scope, params_key. into ( ) , params_obj. into ( ) ) ;
691
-
695
+
692
696
let query_key = v8:: String :: new ( scope, "query" ) . unwrap ( ) ;
693
697
let query_obj = v8:: Object :: new ( scope) ;
694
698
req_obj. set ( scope, query_key. into ( ) , query_obj. into ( ) ) ;
695
-
699
+
696
700
// Set empty headers
697
701
let headers_key = v8:: String :: new ( scope, "headers" ) . unwrap ( ) ;
698
702
let headers_obj = v8:: Object :: new ( scope) ;
699
703
req_obj. set ( scope, headers_key. into ( ) , headers_obj. into ( ) ) ;
700
-
704
+
701
705
req_obj
702
706
}
703
707
704
708
fn create_mock_response < ' a > ( scope : & mut v8:: HandleScope < ' a > ) -> v8:: Local < ' a , v8:: Object > {
705
709
let res_obj = v8:: Object :: new ( scope) ;
706
-
710
+
707
711
// Internal state
708
712
let status_key = v8:: String :: new ( scope, "_statusCode" ) . unwrap ( ) ;
709
713
let status_val = v8:: Number :: new ( scope, 200.0 ) ;
710
714
res_obj. set ( scope, status_key. into ( ) , status_val. into ( ) ) ;
711
-
715
+
712
716
let headers_key = v8:: String :: new ( scope, "_headers" ) . unwrap ( ) ;
713
717
let headers_obj = v8:: Object :: new ( scope) ;
714
718
res_obj. set ( scope, headers_key. into ( ) , headers_obj. into ( ) ) ;
715
-
719
+
716
720
let body_key = v8:: String :: new ( scope, "_body" ) . unwrap ( ) ;
717
721
let body_val = v8:: String :: new ( scope, "" ) . unwrap ( ) ;
718
722
res_obj. set ( scope, body_key. into ( ) , body_val. into ( ) ) ;
719
-
723
+
720
724
let finished_key = v8:: String :: new ( scope, "_finished" ) . unwrap ( ) ;
721
725
let finished_val = v8:: Boolean :: new ( scope, false ) ;
722
726
res_obj. set ( scope, finished_key. into ( ) , finished_val. into ( ) ) ;
723
-
727
+
724
728
// Response method - status
725
729
let status_method_key = v8:: String :: new ( scope, "status" ) . unwrap ( ) ;
726
730
let status_method_template = v8:: FunctionTemplate :: new ( scope, mock_res_status) ;
727
731
let status_method = status_method_template. get_function ( scope) . unwrap ( ) ;
728
732
res_obj. set ( scope, status_method_key. into ( ) , status_method. into ( ) ) ;
729
-
733
+
730
734
// Response method - send
731
735
let send_key = v8:: String :: new ( scope, "send" ) . unwrap ( ) ;
732
736
let send_template = v8:: FunctionTemplate :: new ( scope, mock_res_send) ;
733
737
let send_method = send_template. get_function ( scope) . unwrap ( ) ;
734
738
res_obj. set ( scope, send_key. into ( ) , send_method. into ( ) ) ;
735
-
739
+
736
740
// Response method - json
737
741
let json_key = v8:: String :: new ( scope, "json" ) . unwrap ( ) ;
738
742
let json_template = v8:: FunctionTemplate :: new ( scope, mock_res_json) ;
739
743
let json_method = json_template. get_function ( scope) . unwrap ( ) ;
740
744
res_obj. set ( scope, json_key. into ( ) , json_method. into ( ) ) ;
741
-
745
+
742
746
res_obj
743
747
}
744
748
@@ -753,7 +757,7 @@ fn mock_res_status(
753
757
let status_key = v8:: String :: new ( scope, "_statusCode" ) . unwrap ( ) ;
754
758
this. set ( scope, status_key. into ( ) , status_code) ;
755
759
}
756
- rv. set ( args. this ( ) ) ;
760
+ rv. set ( args. this ( ) . into ( ) ) ;
757
761
}
758
762
759
763
fn mock_res_send (
@@ -764,7 +768,7 @@ fn mock_res_send(
764
768
if args. length ( ) > 0 {
765
769
let this = args. this ( ) ;
766
770
let data = args. get ( 0 ) ;
767
-
771
+
768
772
// Set content-type to text/html if not set
769
773
let headers_key = v8:: String :: new ( scope, "_headers" ) . unwrap ( ) ;
770
774
if let Some ( headers_val) = this. get ( scope, headers_key. into ( ) ) {
@@ -776,18 +780,18 @@ fn mock_res_send(
776
780
}
777
781
}
778
782
}
779
-
783
+
780
784
// Set body
781
785
let body_key = v8:: String :: new ( scope, "_body" ) . unwrap ( ) ;
782
786
let data_str = data. to_string ( scope) . unwrap ( ) ;
783
787
this. set ( scope, body_key. into ( ) , data_str. into ( ) ) ;
784
-
788
+
785
789
// Mark as finished
786
790
let finished_key = v8:: String :: new ( scope, "_finished" ) . unwrap ( ) ;
787
791
let finished_val = v8:: Boolean :: new ( scope, true ) ;
788
792
this. set ( scope, finished_key. into ( ) , finished_val. into ( ) ) ;
789
793
}
790
- rv. set ( args. this ( ) ) ;
794
+ rv. set ( args. this ( ) . into ( ) ) ;
791
795
}
792
796
793
797
fn mock_res_json (
@@ -798,7 +802,7 @@ fn mock_res_json(
798
802
if args. length ( ) > 0 {
799
803
let this = args. this ( ) ;
800
804
let data = args. get ( 0 ) ;
801
-
805
+
802
806
// Set content-type to application/json
803
807
let headers_key = v8:: String :: new ( scope, "_headers" ) . unwrap ( ) ;
804
808
if let Some ( headers_val) = this. get ( scope, headers_key. into ( ) ) {
@@ -808,18 +812,25 @@ fn mock_res_json(
808
812
headers_obj. set ( scope, ct_key. into ( ) , ct_val. into ( ) ) ;
809
813
}
810
814
}
811
-
815
+
812
816
// Convert data to JSON string and set as body
813
817
let body_key = v8:: String :: new ( scope, "_body" ) . unwrap ( ) ;
814
- let json_str = v8:: JSON :: stringify ( scope, data) . unwrap ( ) ;
818
+ let json_key = v8:: String :: new ( scope, "JSON" ) . unwrap ( ) ;
819
+ let global = scope. get_current_context ( ) . global ( scope) ;
820
+ let json_obj = global. get ( scope, json_key. into ( ) ) . unwrap ( ) ;
821
+ let stringify_key = v8:: String :: new ( scope, "stringify" ) . unwrap ( ) ;
822
+ let json_obj = v8:: Local :: < v8:: Object > :: try_from ( json_obj) . unwrap ( ) ;
823
+ let stringify_fn = json_obj. get ( scope, stringify_key. into ( ) ) . unwrap ( ) ;
824
+ let stringify_fn = v8:: Local :: < v8:: Function > :: try_from ( stringify_fn) . unwrap ( ) ;
825
+ let json_str = stringify_fn. call ( scope, json_obj. into ( ) , & [ data] ) . unwrap ( ) ;
815
826
this. set ( scope, body_key. into ( ) , json_str. into ( ) ) ;
816
-
827
+
817
828
// Mark as finished
818
829
let finished_key = v8:: String :: new ( scope, "_finished" ) . unwrap ( ) ;
819
830
let finished_val = v8:: Boolean :: new ( scope, true ) ;
820
831
this. set ( scope, finished_key. into ( ) , finished_val. into ( ) ) ;
821
832
}
822
- rv. set ( args. this ( ) ) ;
833
+ rv. set ( args. this ( ) . into ( ) ) ;
823
834
}
824
835
825
836
fn extract_response_pattern (
@@ -833,7 +844,7 @@ fn extract_response_pattern(
833
844
if let Some ( body_val) = mock_res. get ( scope, body_key. into ( ) ) {
834
845
let body_str = body_val. to_string ( scope) . unwrap ( ) ;
835
846
let body = body_str. to_rust_string_lossy ( scope) ;
836
-
847
+
837
848
// Get content-type to determine response method
838
849
let headers_key = v8:: String :: new ( scope, "_headers" ) . unwrap ( ) ;
839
850
if let Some ( headers_val) = mock_res. get ( scope, headers_key. into ( ) ) {
@@ -842,26 +853,35 @@ fn extract_response_pattern(
842
853
if let Some ( ct_val) = headers_obj. get ( scope, ct_key. into ( ) ) {
843
854
let ct_str = ct_val. to_string ( scope) . unwrap ( ) ;
844
855
let content_type = ct_str. to_rust_string_lossy ( scope) ;
845
-
856
+
846
857
if content_type. contains ( "application/json" ) {
847
858
// User called res.json()
848
859
return format ! ( "function(req, res) {{ res.json({}); }}" , body) ;
849
860
} else {
850
861
// User called res.send() or similar
851
- return format ! ( "function(req, res) {{ res.send('{}'); }}" , body. replace( "'" , "\\ '" ) ) ;
862
+ return format ! (
863
+ "function(req, res) {{ res.send('{}'); }}" ,
864
+ body. replace( "'" , "\\ '" )
865
+ ) ;
852
866
}
853
867
}
854
868
}
855
869
}
856
-
870
+
857
871
// Default to send if we have a body but no content-type
858
872
if !body. is_empty ( ) {
859
- return format ! ( "function(req, res) {{ res.send('{}'); }}" , body. replace( "'" , "\\ '" ) ) ;
873
+ return format ! (
874
+ "function(req, res) {{ res.send('{}'); }}" ,
875
+ body. replace( "'" , "\\ '" )
876
+ ) ;
860
877
}
861
878
}
862
-
879
+
863
880
// Fallback if we couldn't extract anything meaningful
864
- format ! ( "function(req, res) {{ res.send('Response from {} {}'); }}" , method, path)
881
+ format ! (
882
+ "function(req, res) {{ res.send('Response from {} {}'); }}" ,
883
+ method, path
884
+ )
865
885
}
866
886
867
887
fn parse_route_pattern ( path : & str ) -> ( String , Vec < String > ) {
0 commit comments