1
+ #![ feature( plugin, decl_macro) ]
2
+ #![ plugin( rocket_codegen) ]
3
+
1
4
extern crate rocket;
2
5
extern crate rocket_contrib;
3
6
@@ -6,9 +9,17 @@ mod templates_tests {
6
9
use std:: env;
7
10
use std:: path:: PathBuf ;
8
11
9
- use rocket:: Rocket ;
12
+ use rocket:: { Rocket , http :: RawStr } ;
10
13
use rocket:: config:: { Config , Environment } ;
11
- use rocket_contrib:: Template ;
14
+ use rocket_contrib:: { Template , TemplateMetadata } ;
15
+
16
+ #[ get( "/<engine>/<name>" ) ]
17
+ fn template_check ( md : TemplateMetadata , engine : & RawStr , name : & RawStr ) -> Option < ( ) > {
18
+ match md. contains_template ( & format ! ( "{}/{}" , engine, name) ) {
19
+ true => Some ( ( ) ) ,
20
+ false => None
21
+ }
22
+ }
12
23
13
24
fn template_root ( ) -> PathBuf {
14
25
let cwd = env:: current_dir ( ) . expect ( "current working directory" ) ;
@@ -20,13 +31,16 @@ mod templates_tests {
20
31
. extra ( "template_dir" , template_root ( ) . to_str ( ) . expect ( "template directory" ) )
21
32
. expect ( "valid configuration" ) ;
22
33
23
- :: rocket:: custom ( config, true ) . attach ( Template :: fairing ( ) )
34
+ :: rocket:: custom ( config) . attach ( Template :: fairing ( ) )
35
+ . mount ( "/" , routes ! [ template_check] )
24
36
}
25
37
26
38
#[ cfg( feature = "tera_templates" ) ]
27
39
mod tera_tests {
28
40
use super :: * ;
29
41
use std:: collections:: HashMap ;
42
+ use rocket:: http:: Status ;
43
+ use rocket:: local:: Client ;
30
44
31
45
const UNESCAPED_EXPECTED : & ' static str
32
46
= "\n h_start\n title: _test_\n h_end\n \n \n <script />\n \n foot\n " ;
@@ -48,12 +62,31 @@ mod templates_tests {
48
62
let template = Template :: show ( & rocket, "tera/html_test" , & map) ;
49
63
assert_eq ! ( template, Some ( ESCAPED_EXPECTED . into( ) ) ) ;
50
64
}
65
+
66
+ #[ test]
67
+ fn test_template_metadata_with_tera ( ) {
68
+ let client = Client :: new ( rocket ( ) ) . unwrap ( ) ;
69
+
70
+ let response = client. get ( "/tera/txt_test" ) . dispatch ( ) ;
71
+ assert_eq ! ( response. status( ) , Status :: Ok ) ;
72
+
73
+ let response = client. get ( "/tera/html_test" ) . dispatch ( ) ;
74
+ assert_eq ! ( response. status( ) , Status :: Ok ) ;
75
+
76
+ let response = client. get ( "/tera/not_existing" ) . dispatch ( ) ;
77
+ assert_eq ! ( response. status( ) , Status :: NotFound ) ;
78
+
79
+ let response = client. get ( "/hbs/txt_test" ) . dispatch ( ) ;
80
+ assert_eq ! ( response. status( ) , Status :: NotFound ) ;
81
+ }
51
82
}
52
83
53
84
#[ cfg( feature = "handlebars_templates" ) ]
54
85
mod handlebars_tests {
55
86
use super :: * ;
56
87
use std:: collections:: HashMap ;
88
+ use rocket:: http:: Status ;
89
+ use rocket:: local:: Client ;
57
90
58
91
const EXPECTED : & ' static str
59
92
= "Hello _test_!\n \n <main> <script /> hi </main>\n Done.\n \n " ;
@@ -69,5 +102,19 @@ mod templates_tests {
69
102
let template = Template :: show ( & rocket, "hbs/test" , & map) ;
70
103
assert_eq ! ( template, Some ( EXPECTED . into( ) ) ) ;
71
104
}
105
+
106
+ #[ test]
107
+ fn test_template_metadata_with_handlebars ( ) {
108
+ let client = Client :: new ( rocket ( ) ) . unwrap ( ) ;
109
+
110
+ let response = client. get ( "/hbs/test" ) . dispatch ( ) ;
111
+ assert_eq ! ( response. status( ) , Status :: Ok ) ;
112
+
113
+ let response = client. get ( "/hbs/not_existing" ) . dispatch ( ) ;
114
+ assert_eq ! ( response. status( ) , Status :: NotFound ) ;
115
+
116
+ let response = client. get ( "/tera/test" ) . dispatch ( ) ;
117
+ assert_eq ! ( response. status( ) , Status :: NotFound ) ;
118
+ }
72
119
}
73
120
}
0 commit comments