@@ -6,17 +6,23 @@ use std::thread;
66use std:: thread:: JoinHandle ;
77
88fn build_line ( directive : String , domains : domains:: Collection ) -> String {
9- let mut directive_line: String = directive. to_string ( ) ;
10- directive_line. push_str ( ":" ) ;
9+ let mut directive_line: String = directive. clone ( ) ;
10+ let mut directive_check: String = directive. clone ( ) ;
11+ directive_check. push_str ( "; " ) ;
1112
1213 for domain in domains. domains {
13- if domain. directive . contains ( & directive. to_string ( ) ) {
14+ if domain. directive . contains ( & directive) {
1415 directive_line. push_str ( " " ) ;
1516 directive_line. push_str ( domain. domain . as_str ( ) ) ;
1617 }
1718 }
1819
1920 directive_line. push_str ( "; " ) ;
21+
22+ if directive_line == directive_check {
23+ return String :: from ( "" ) ;
24+ }
25+
2026 directive_line
2127}
2228
@@ -28,7 +34,7 @@ fn build_lines(directives: Vec<String>, domains: domains::Collection) -> Vec<Joi
2834 let mut threads: Vec < JoinHandle < String > > = vec ! [ ] ;
2935
3036 for directive in directives {
31- threads. push ( self :: create_thread ( directive. to_string ( ) , domains. clone ( ) ) ) ;
37+ threads. push ( self :: create_thread ( directive, domains. clone ( ) ) ) ;
3238 }
3339
3440 threads
@@ -78,7 +84,28 @@ mod directives_test {
7884
7985 let connect_src: String = super :: build_line ( String :: from ( "connect-src" ) , json) ;
8086
81- assert_eq ! ( connect_src, String :: from( "connect-src: *.example.com; " ) ) ;
87+ assert_eq ! ( connect_src, String :: from( "connect-src *.example.com; " ) ) ;
88+ }
89+
90+ #[ test]
91+ fn test_build_line_no_directive ( ) {
92+ let directives: Vec < String > = vec ! [ String :: from( "connect-src" ) , String :: from( "script-src" ) ] ;
93+
94+ let item = domains:: Item {
95+ domain : String :: from ( "*.example.com" ) ,
96+ directive : directives,
97+ } ;
98+
99+ let mut domain_list: Vec < domains:: Item > = Vec :: new ( ) ;
100+ domain_list. push ( item) ;
101+
102+ let json = domains:: Collection {
103+ domains : domain_list,
104+ } ;
105+
106+ let default_src: String = super :: build_line ( String :: from ( "default-src" ) , json) ;
107+
108+ assert_eq ! ( default_src, String :: from( "" ) ) ;
82109 }
83110
84111 #[ test]
@@ -96,7 +123,7 @@ mod directives_test {
96123
97124 assert_eq ! (
98125 csp. unwrap( ) ,
99- String :: from( "script-src: test.com; connect-src: example.com test.com;" )
126+ String :: from( "script-src test.com; connect-src example.com test.com;" )
100127 ) ;
101128 }
102129}
0 commit comments