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