@@ -821,7 +821,12 @@ pub fn serve(
821821
822822#[ cfg( test) ]
823823mod tests {
824- use super :: construct_url;
824+ use super :: { construct_url, create_new_site} ;
825+ use crate :: get_config_file_path;
826+ use libs:: url:: Url ;
827+ use std:: net:: { IpAddr , SocketAddr } ;
828+ use std:: path:: { Path , PathBuf } ;
829+ use std:: str:: FromStr ;
825830
826831 #[ test]
827832 fn test_construct_url_base_url_is_slash ( ) {
@@ -858,4 +863,194 @@ mod tests {
858863 let result = construct_url ( "http://example.com/" , false , 8080 ) ;
859864 assert_eq ! ( result, "http://example.com:8080/" ) ;
860865 }
866+
867+ fn create_and_verify_new_site (
868+ interface : IpAddr ,
869+ interface_port : u16 ,
870+ output_dir : Option < & Path > ,
871+ base_url : Option < & str > ,
872+ no_port_append : bool ,
873+ ws_port : Option < u16 > ,
874+ expected_base_url : String ,
875+ ) {
876+ let cli_dir = Path :: new ( "./test_site" ) . canonicalize ( ) . unwrap ( ) ;
877+ let cli_config = Path :: new ( "./test_site/config.toml" ) . canonicalize ( ) . unwrap ( ) ;
878+
879+ let ( root_dir, config_file) = get_config_file_path ( & cli_dir, & cli_config) ;
880+ assert_eq ! ( cli_dir, root_dir) ;
881+ assert_eq ! ( cli_config, root_dir. join( "config.toml" ) ) ;
882+
883+ let force = false ;
884+ let include_drafts = false ;
885+
886+ let ( site, bind_address, constructed_base_url) = create_new_site (
887+ & root_dir,
888+ interface,
889+ interface_port,
890+ output_dir. as_deref ( ) ,
891+ force,
892+ base_url. as_deref ( ) ,
893+ & config_file,
894+ include_drafts,
895+ no_port_append,
896+ ws_port,
897+ )
898+ . unwrap ( ) ;
899+
900+ assert_eq ! ( bind_address, SocketAddr :: new( interface, interface_port) ) ;
901+ assert_eq ! ( constructed_base_url, expected_base_url) ;
902+ assert ! ( site. base_path. exists( ) ) ;
903+ assert_eq ! ( site. base_path, root_dir) ;
904+ assert_eq ! ( site. config. base_url, constructed_base_url) ;
905+ assert_ne ! ( site. live_reload, None ) ;
906+ assert_ne ! ( site. live_reload, Some ( 1111 ) ) ;
907+ assert_eq ! ( site. output_path, root_dir. join( & site. config. output_dir) ) ;
908+ assert_eq ! ( site. static_path, root_dir. join( "static" ) ) ;
909+
910+ let base_url = Url :: parse ( & expected_base_url) . unwrap ( ) ;
911+ for ( _, permalink) in site. permalinks {
912+ let permalink_url = Url :: parse ( & permalink) . unwrap ( ) ;
913+ assert_eq ! ( base_url. scheme( ) , permalink_url. scheme( ) ) ;
914+ assert_eq ! ( base_url. host( ) , permalink_url. host( ) ) ;
915+ assert_eq ! ( base_url. port( ) , permalink_url. port( ) ) ;
916+ assert ! ( !permalink_url. path( ) . starts_with( "//" ) ) ;
917+ assert ! ( !permalink_url. path( ) . ends_with( "//" ) ) ;
918+ assert ! ( permalink_url. path( ) . starts_with( "/" ) ) ;
919+ assert ! ( permalink_url. path( ) . starts_with( base_url. path( ) ) ) ;
920+ }
921+ }
922+
923+ #[ test]
924+ fn test_create_new_site_without_protocol_with_port_without_mounted_path ( ) {
925+ let interface = IpAddr :: from_str ( "127.0.0.1" ) . unwrap ( ) ;
926+ let interface_port = 1111 ;
927+ let output_dir: Option < PathBuf > = None ;
928+ let base_url: Option < String > = None ;
929+ let no_port_append = false ;
930+ let ws_port: Option < u16 > = None ;
931+ let expected_base_url = String :: from ( "http://127.0.0.1:1111" ) ;
932+
933+ create_and_verify_new_site (
934+ interface,
935+ interface_port,
936+ output_dir. as_deref ( ) ,
937+ base_url. as_deref ( ) ,
938+ no_port_append,
939+ ws_port,
940+ expected_base_url,
941+ ) ;
942+ }
943+
944+ #[ test]
945+ fn test_create_new_site_without_protocol_with_port_with_mounted_path ( ) {
946+ let interface = IpAddr :: from_str ( "127.0.0.1" ) . unwrap ( ) ;
947+ let interface_port = 1111 ;
948+ let output_dir: Option < PathBuf > = None ;
949+ let base_url: Option < String > = Some ( String :: from ( "localhost/path/to/site" ) ) ;
950+ let no_port_append = false ;
951+ let ws_port: Option < u16 > = None ;
952+ let expected_base_url = String :: from ( "http://localhost:1111/path/to/site" ) ;
953+
954+ create_and_verify_new_site (
955+ interface,
956+ interface_port,
957+ output_dir. as_deref ( ) ,
958+ base_url. as_deref ( ) ,
959+ no_port_append,
960+ ws_port,
961+ expected_base_url,
962+ ) ;
963+ }
964+
965+ #[ test]
966+ fn test_create_new_site_without_protocol_without_port_without_mounted_path ( ) {
967+ let interface = IpAddr :: from_str ( "127.0.0.1" ) . unwrap ( ) ;
968+ let interface_port = 1111 ;
969+ let output_dir: Option < PathBuf > = None ;
970+ let base_url: Option < String > = Some ( String :: from ( "example.com" ) ) ;
971+ let no_port_append = true ;
972+ let ws_port: Option < u16 > = None ;
973+ let expected_base_url = String :: from ( "http://example.com" ) ;
974+
975+ // Note that no_port_append only works if we define a base_url
976+
977+ create_and_verify_new_site (
978+ interface,
979+ interface_port,
980+ output_dir. as_deref ( ) ,
981+ base_url. as_deref ( ) ,
982+ no_port_append,
983+ ws_port,
984+ expected_base_url,
985+ ) ;
986+ }
987+
988+ #[ test]
989+ fn test_create_new_site_with_protocol_without_port_without_mounted_path ( ) {
990+ let interface = IpAddr :: from_str ( "127.0.0.1" ) . unwrap ( ) ;
991+ let interface_port = 1111 ;
992+ let output_dir: Option < PathBuf > = None ;
993+ let base_url: Option < String > = Some ( String :: from ( "https://example.com" ) ) ;
994+ let no_port_append = true ;
995+ let ws_port: Option < u16 > = None ;
996+ let expected_base_url = String :: from ( "https://example.com" ) ;
997+
998+ // Note that no_port_append only works if we define a base_url
999+
1000+ create_and_verify_new_site (
1001+ interface,
1002+ interface_port,
1003+ output_dir. as_deref ( ) ,
1004+ base_url. as_deref ( ) ,
1005+ no_port_append,
1006+ ws_port,
1007+ expected_base_url,
1008+ ) ;
1009+ }
1010+
1011+ #[ test]
1012+ fn test_create_new_site_with_protocol_without_port_with_mounted_path ( ) {
1013+ let interface = IpAddr :: from_str ( "127.0.0.1" ) . unwrap ( ) ;
1014+ let interface_port = 1111 ;
1015+ let output_dir: Option < PathBuf > = None ;
1016+ let base_url: Option < String > = Some ( String :: from ( "https://example.com/path/to/site" ) ) ;
1017+ let no_port_append = true ;
1018+ let ws_port: Option < u16 > = None ;
1019+ let expected_base_url = String :: from ( "https://example.com/path/to/site" ) ;
1020+
1021+ // Note that no_port_append only works if we define a base_url
1022+
1023+ create_and_verify_new_site (
1024+ interface,
1025+ interface_port,
1026+ output_dir. as_deref ( ) ,
1027+ base_url. as_deref ( ) ,
1028+ no_port_append,
1029+ ws_port,
1030+ expected_base_url,
1031+ ) ;
1032+ }
1033+
1034+ #[ test]
1035+ fn test_create_new_site_with_protocol_with_port_with_mounted_path ( ) {
1036+ let interface = IpAddr :: from_str ( "127.0.0.1" ) . unwrap ( ) ;
1037+ let interface_port = 1111 ;
1038+ let output_dir: Option < PathBuf > = None ;
1039+ let base_url: Option < String > = Some ( String :: from ( "https://example.com/path/to/site" ) ) ;
1040+ let no_port_append = false ;
1041+ let ws_port: Option < u16 > = None ;
1042+ let expected_base_url = String :: from ( "https://example.com:1111/path/to/site" ) ;
1043+
1044+ // Note that no_port_append only works if we define a base_url
1045+
1046+ create_and_verify_new_site (
1047+ interface,
1048+ interface_port,
1049+ output_dir. as_deref ( ) ,
1050+ base_url. as_deref ( ) ,
1051+ no_port_append,
1052+ ws_port,
1053+ expected_base_url,
1054+ ) ;
1055+ }
8611056}
0 commit comments