58
58
& self ,
59
59
request_route : Option < Route > ,
60
60
query_parameters : HashMap < String , String > ,
61
+ request_headers : HashMap < String , String > ,
61
62
) -> RenderContext < ServerInfo , Self > ;
62
63
63
64
fn new_template (
@@ -378,6 +379,7 @@ where
378
379
& self ,
379
380
route : Option < Route > ,
380
381
query_parameters : HashMap < String , String > ,
382
+ request_headers : HashMap < String , String > ,
381
383
) -> RenderContext < ServerInfo , Self > {
382
384
RenderContext {
383
385
content_engine : self ,
@@ -389,6 +391,7 @@ where
389
391
request : RequestData {
390
392
route,
391
393
query_parameters,
394
+ request_headers,
392
395
} ,
393
396
} ,
394
397
}
@@ -472,7 +475,7 @@ mod tests {
472
475
. expect ( "Template could not be parsed" ) ;
473
476
let rendered = renderable
474
477
. render (
475
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
478
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
476
479
& [ mime:: TEXT_HTML ] ,
477
480
)
478
481
. expect ( & format ! ( "Template rendering failed for `{}`" , template) ) ;
@@ -531,7 +534,7 @@ mod tests {
531
534
. expect ( "Template could not be parsed" ) ;
532
535
let rendered = renderable
533
536
. render (
534
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
537
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
535
538
& [ mime:: TEXT_HTML ] ,
536
539
)
537
540
. expect ( & format ! ( "Template rendering failed for `{}`" , template) ) ;
@@ -562,7 +565,7 @@ mod tests {
562
565
. expect ( "Content could not be found" ) ;
563
566
let rendered = content
564
567
. render (
565
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
568
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
566
569
& [ mime:: TEXT_HTML ] ,
567
570
)
568
571
. expect ( & format ! (
@@ -630,7 +633,7 @@ mod tests {
630
633
. expect ( "Template could not be parsed" ) ;
631
634
let rendered = renderable
632
635
. render (
633
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
636
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
634
637
& [ mime:: TEXT_HTML ] ,
635
638
)
636
639
. expect ( & format ! ( "Template rendering failed for `{}`" , template) ) ;
@@ -669,7 +672,7 @@ mod tests {
669
672
)
670
673
. expect ( "Template could not be parsed" ) ;
671
674
let result = renderable. render (
672
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
675
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
673
676
& [ mime:: TEXT_HTML ] ,
674
677
) ;
675
678
assert ! (
@@ -698,7 +701,7 @@ mod tests {
698
701
None => panic ! ( "No content was found at '{}'" , route) ,
699
702
Some ( renderable) => {
700
703
let result = renderable. render (
701
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
704
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
702
705
& [ mime:: TEXT_HTML ] ,
703
706
) ;
704
707
assert ! (
@@ -728,7 +731,7 @@ mod tests {
728
731
)
729
732
. expect ( "Template could not be created" ) ;
730
733
let result = template. render (
731
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
734
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
732
735
& [ mime:: TEXT_PLAIN ] ,
733
736
) ;
734
737
@@ -756,7 +759,7 @@ mod tests {
756
759
None => panic ! ( "No content was found at '{}'" , route) ,
757
760
Some ( renderable) => {
758
761
let result = renderable. render (
759
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
762
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
760
763
& [ target_media_type] ,
761
764
) ;
762
765
assert ! (
@@ -789,7 +792,7 @@ mod tests {
789
792
)
790
793
. expect ( "Test template was invalid" )
791
794
. render (
792
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
795
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
793
796
& [ mime:: TEXT_PLAIN ] ,
794
797
)
795
798
. expect ( "Failed to render unregistered template" ) ,
@@ -802,7 +805,7 @@ mod tests {
802
805
. get ( & route ( "/echo-target-media-type" ) )
803
806
. expect ( "Test template does not exist" )
804
807
. render (
805
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
808
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
806
809
& [ mime:: TEXT_HTML ] ,
807
810
)
808
811
. expect ( "Failed to render registered template" ) ,
@@ -835,7 +838,7 @@ mod tests {
835
838
. expect ( "Content could not be found" ) ;
836
839
let rendered = content
837
840
. render (
838
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
841
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
839
842
& [ mime:: TEXT_PLAIN ] ,
840
843
)
841
844
. expect ( & format ! ( "Rendering failed for content at '{}'" , route) ) ;
@@ -866,7 +869,7 @@ mod tests {
866
869
. expect ( "Content could not be found" ) ;
867
870
let rendered = content
868
871
. render (
869
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
872
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
870
873
& [ mime:: TEXT_PLAIN ] ,
871
874
)
872
875
. expect ( & format ! ( "Rendering failed for content at '{}'" , route1) ) ;
@@ -889,7 +892,7 @@ mod tests {
889
892
. expect ( "Content could not be found" ) ;
890
893
let rendered = content
891
894
. render (
892
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
895
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
893
896
& [ mime:: TEXT_PLAIN ] ,
894
897
)
895
898
. expect ( & format ! ( "Rendering failed for content at '{}'" , route2) ) ;
@@ -918,7 +921,7 @@ mod tests {
918
921
. expect ( "Content could not be found" ) ;
919
922
920
923
let result1 = content. render (
921
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
924
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
922
925
& [ mime:: TEXT_PLAIN ] , // Not text/html!
923
926
) ;
924
927
assert ! (
@@ -928,7 +931,7 @@ mod tests {
928
931
) ;
929
932
930
933
let result2 = content. render (
931
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
934
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
932
935
& [ mime:: TEXT_HTML ] ,
933
936
) ;
934
937
assert ! (
@@ -952,7 +955,7 @@ mod tests {
952
955
953
956
let media = content
954
957
. render (
955
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
958
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
956
959
& [ mime:: APPLICATION_OCTET_STREAM ] ,
957
960
)
958
961
. expect ( & format ! (
@@ -985,7 +988,7 @@ mod tests {
985
988
. expect ( "Content could not be found" ) ;
986
989
let rendered = content
987
990
. render (
988
- content_engine. render_context ( None , HashMap :: new ( ) ) ,
991
+ content_engine. render_context ( None , HashMap :: new ( ) , HashMap :: new ( ) ) ,
989
992
& [ mime:: TEXT_PLAIN ] ,
990
993
)
991
994
. expect ( & format ! ( "Rendering failed for content at '{}'" , route) ) ;
@@ -1060,7 +1063,50 @@ mod tests {
1060
1063
. expect ( "Template could not be parsed" ) ;
1061
1064
let rendered = renderable
1062
1065
. render (
1063
- content_engine. render_context ( None , query_parameters) ,
1066
+ content_engine. render_context ( None , query_parameters, HashMap :: new ( ) ) ,
1067
+ & [ mime:: TEXT_PLAIN ] ,
1068
+ )
1069
+ . expect ( & format ! ( "Template rendering failed for `{}`" , template) ) ;
1070
+ let actual_output = media_to_string ( rendered) ;
1071
+
1072
+ assert ! (
1073
+ expected_outputs. contains( & actual_output) ,
1074
+ "Template rendering for `{}` did not produce expected output (any of {:?}), got \" {}\" " ,
1075
+ template,
1076
+ expected_outputs,
1077
+ actual_output,
1078
+ ) ;
1079
+ }
1080
+
1081
+ #[ test]
1082
+ fn templates_receive_request_headers ( ) {
1083
+ let shared_content_engine = TestContentEngine :: from_content_directory (
1084
+ arbitrary_content_directory_with_valid_content ( ) ,
1085
+ ( ) ,
1086
+ )
1087
+ . expect ( "Content engine could not be created" ) ;
1088
+ let content_engine = shared_content_engine. read ( ) . unwrap ( ) ;
1089
+
1090
+ let template = "{{#each request.request-headers}}{{@key}}: {{this}}\n {{/each}}" ;
1091
+
1092
+ let request_headers = hashmap ! [
1093
+ String :: from( "hello" ) => String :: from( "world" ) ,
1094
+ String :: from( "goodbye" ) => String :: from( "moon" ) ,
1095
+ ] ;
1096
+ let expected_outputs = [
1097
+ String :: from ( "hello: world\n goodbye: moon\n " ) ,
1098
+ String :: from ( "goodbye: moon\n hello: world\n " ) ,
1099
+ ] ;
1100
+
1101
+ let renderable = content_engine
1102
+ . new_template (
1103
+ template,
1104
+ MediaType :: from_media_range ( mime:: TEXT_PLAIN ) . unwrap ( ) ,
1105
+ )
1106
+ . expect ( "Template could not be parsed" ) ;
1107
+ let rendered = renderable
1108
+ . render (
1109
+ content_engine. render_context ( None , HashMap :: new ( ) , request_headers) ,
1064
1110
& [ mime:: TEXT_PLAIN ] ,
1065
1111
)
1066
1112
. expect ( & format ! ( "Template rendering failed for `{}`" , template) ) ;
0 commit comments