grace/htmlua
HTML Templating Engine
git clone https://git.gae.moe/grace/htmlua
bfa5d30
render_fixes
1use std :: { 2cell :: { LazyCell , RefCell } , 3fmt :: Write , 4path :: PathBuf , 5rc :: Rc , 6} ; 7 8use anyhow :: { Result , anyhow } ; 9use kuchikiki :: { NodeRef , traits :: TendrilSink } ; 10use markup5ever :: { LocalName , Namespace , QualName } ; 11use mlua :: Lua ; 12use pulldown_cmark :: { Options , Parser , html } ; 13use syntect :: { 14easy :: HighlightLines , 15highlighting :: { Style , ThemeSet } , 16html :: { IncludeBackground , styled_line_to_highlighted_html } , 17parsing :: SyntaxSet , 18util :: LinesWithEndings , 19} ; 20 21use crate :: { helpers :: read_doc_from_file , htmlua_stdlib :: create_htmlua_stdlib , serve :: get_config } ; 22 23 24fn build_lua_with_stdout ( stdout : & Rc < RefCell < String >> ) -> Result < Lua > { 25let lua = Lua :: new () ; 26let globals = lua . globals () ; 27 28let htmlua_table = create_htmlua_stdlib ( & lua , stdout ) . map_err ( | e | anyhow ! ( "Failed to create Lua stdlib: {}" , e )) ? ; 29 30globals 31. set ( "htmlua" , htmlua_table ) 32. map_err ( | e | anyhow ! ( "Failed to set global: {}" , e )) ? ; 33 34Ok ( lua ) 35} 36 37pub fn execute_lua ( document : NodeRef ) -> Result < NodeRef > { 38let stdout : Rc < RefCell < String >> = Rc :: new ( RefCell :: new ( String :: new ())) ; 39let lua = LazyCell :: new ( || build_lua_with_stdout ( & stdout ) . unwrap_or_default ()) ; 40let lua_elements : Vec < _ > = match document . select ( "lua" ) { 41Ok ( e ) => e . collect () , 42Err (()) => return Err ( anyhow ! ( "Unable to find Lua" )) , 43} ; 44 45for node in lua_elements { 46if let Some ( text_node ) = node . as_node () . first_child () { 47if let Some ( lua_code ) = text_node . as_text () { 48stdout . borrow_mut () . clear () ; 49lua . load ( lua_code . borrow () . as_str ()) 50. exec () 51. map_err ( | e | anyhow ! ( "Failed to execute Lua: {}" , e )) ? ; 52node . as_node () 53. insert_before ( NodeRef :: new_text ( stdout . borrow () . as_str ())) ; 54node . as_node () . detach () ; 55} 56} 57} 58 59Ok ( document ) 60} 61 62pub fn process_markdown ( document : NodeRef ) -> Result < NodeRef > { 63let markdown_elements : Vec < _ > = match document . select ( "markdown" ) { 64Ok ( e ) => e . collect () , 65Err (()) => return Err ( anyhow ! ( "Unable to find markdown elements" )) , 66} ; 67for node in markdown_elements { 68if let Some ( text_node ) = node . as_node () . first_child () { 69if let Some ( markdown_text ) = text_node . as_text () { 70let borrowed_text = markdown_text . borrow () ; 71let parser = Parser :: new_ext ( & borrowed_text , Options :: all ()) ; 72let mut html_output = String :: new () ; 73html :: push_html ( & mut html_output , parser ) ; 74let html_fragment = kuchikiki :: parse_html () . one ( html_output ) ; 75for child in html_fragment . children () { 76node . as_node () . insert_before ( child ) ; 77} 78// Remove the original markdown node. 79node . as_node () . detach () ; 80} 81} 82} 83Ok ( document ) 84} 85 86pub fn expand_template ( document : NodeRef , component_path : & PathBuf , include_from : Option <& NodeRef > ) -> Result < NodeRef > { 87if let Some ( from_node ) = include_from { 88for i in document 89. select ( "includeelement" ) 90. map_err ( | () | anyhow ! ( "Error finding includeelement" )) ? 91{ 92if let Some ( name ) = i . attributes . borrow () . get ( "name" ) { 93let exported = from_node 94. select_first ( format ! ( "exportelement.{name}" ) . as_str ()) 95. map_err ( | () | anyhow ! ( "Error finding exportelement" )) ? ; 96exported 97. as_node () 98. children () 99. rev () 100. for_each ( | c | i . as_node () . insert_after ( c )) ; 101} 102} 103while let Ok ( i ) = document . select_first ( "includeelement" ) { 104i . as_node () . detach () ; 105} 106} 107 108for i in document 109. select ( "include" ) 110. map_err ( | () | anyhow ! ( "Error finding include" )) ? 111. collect :: < Vec < _ >> () 112{ 113let attrs = match i . as_node () . as_element () { 114Some ( e ) => e . attributes . borrow () , 115None => continue , 116} ; 117if let Some ( include_path ) = attrs . get ( "path" ) { 118let mut item_path = component_path . clone () ; 119item_path . push ( include_path ) ; 120let new_node = read_doc_from_file ( item_path ) ? ; 121let replaced_node = expand_template ( new_node , component_path , Some ( i . as_node ())) ? ; 122replaced_node 123. select_first ( "html" ) 124. map_err ( | () | anyhow ! ( "Error finding html" )) ? 125. as_node () 126. children () 127. rev () 128. for_each ( | c | i . as_node () . insert_after ( c )) ; 129} 130} 131while let Ok ( i ) = document . select_first ( "include" ) { 132i . as_node () . detach () ; 133} 134Ok ( document ) 135} 136 137pub fn process_syntax_highlighting ( document : NodeRef ) -> Result < NodeRef > { 138let config = get_config () ; 139let ps = SyntaxSet :: load_defaults_newlines () ; 140let mut ts = ThemeSet :: load_defaults () ; 141let syntax_elements : Vec < _ > = match document . select ( "syntaxhighlight" ) { 142Ok ( e ) => e . collect () , 143Err (()) => return Err ( anyhow ! ( "Unable to find syntaxhighlight elements" )) , 144} ; 145if ! syntax_elements . is_empty () && config . syntax_highlighting . load_custom_themes { 146let _ = ts . add_from_folder ( & config . paths . themes ) ; 147} 148for node in syntax_elements { 149let attrs = match node . as_node () . as_element () { 150Some ( e ) => e . attributes . borrow () , 151None => continue , 152} ; 153let language = attrs . get ( "lang" ) . unwrap_or ( "text" ) ; 154let theme_name = attrs . get ( "theme" ) . unwrap_or ( & config . syntax_highlighting . default_theme ) ; 155if let Some ( text_node ) = node . as_node () . first_child () { 156if let Some ( code_text ) = text_node . as_text () { 157let syntax = ps 158. find_syntax_by_extension ( language ) 159. or_else ( || ps . find_syntax_by_name ( language )) 160. unwrap_or_else ( || ps . find_syntax_plain_text ()) ; 161let theme = & ts . themes [ theme_name ] ; 162let mut h = HighlightLines :: new ( syntax , theme ) ; 163let mut html_output = String :: new () ; 164write ! ( html_output , r#"<pre class="syntax-highlight" data-lang="{language}">"# ) ? ; 165html_output . push_str ( "<code>" ) ; 166for line in LinesWithEndings :: from ( & code_text . borrow ()) { 167let ranges : Vec < ( Style , & str ) > = h . highlight_line ( line , & ps ) ? ; 168let escaped = styled_line_to_highlighted_html ( & ranges [ .. ] , IncludeBackground :: No ) ? ; 169html_output . push_str ( & escaped ) ; 170} 171html_output . push_str ( "</code></pre>" ) ; 172let html_fragment = kuchikiki :: parse_html () . one ( html_output ) ; 173for child in html_fragment . children () { 174node . as_node () . insert_before ( child ) ; 175} 176// Remove the original syntaxhighlight node. 177node . as_node () . detach () ; 178} 179} 180} 181Ok ( document ) 182} 183 184pub fn generate_footnotes ( document : NodeRef ) -> Result < NodeRef > { 185let Ok ( footnote_container ) = document . select_first ( "footnotecontainer" ) else { 186return Ok ( document ) ; 187} ; 188 189let ctx_name = QualName :: new ( None , Namespace :: from ( "http://www.w3.org/1999/xhtml" ) , LocalName :: from ( "div" )) ; 190for ( i , footnote ) in document 191. select ( "footnote" ) 192. map_err ( | () | anyhow ! ( "Failed to get footnote" )) ? 193. enumerate () 194{ 195let i = i + 1 ; 196let fn_text = footnote . text_contents () ; 197let sup_tag = kuchikiki :: parse_fragment ( ctx_name . clone () , Vec :: new ()) 198. one ( format ! ( "<a href=#ft-text-{i}><sup id= \" ft-sup-{i} \" title= \" {fn_text} \" >{i}</sup></a>" )) 199. select_first ( "a" ) 200. map_err ( | () | anyhow ! ( "parse err" )) ? ; 201footnote . as_node () . insert_after ( sup_tag . as_node () . clone ()) ; 202let text_tag = kuchikiki :: parse_fragment ( ctx_name . clone () , Vec :: new ()) 203. one ( format ! ( "<p id= \" ft-text-{i} \" ><a href=#ft-sup-{i}>{i}:</a> {fn_text}</p>" )) 204. select_first ( "p" ) 205. map_err ( | () | anyhow ! ( "parse err" )) ? ; 206footnote_container . as_node () . insert_before ( text_tag . as_node () . clone ()) ; 207} 208while let Ok ( i ) = document . select_first ( "footnote" ) { 209i . as_node () . detach () ; 210} 211footnote_container . as_node () . detach () ; 212Ok ( document ) 213} 214 215#[ cfg ( test ) ] 216mod tests { 217use httptest :: { Expectation , ServerPool , matchers :: * , responders :: * } ; 218use markup5ever :: { namespace_url , ns } ; 219 220use super :: * ; 221 222#[ test ] 223fn basic_lua () { 224let page = r#" 225<!DOCTYPE html> 226<html> 227<head> 228<title>Basic HTML Page</title> 229</head> 230<body> 231<h1>Hello World</h1> 232<p>This is a paragraph.</p> 233<span><lua> 234htmlua.println("Test from Lua!") 235</lua></span> 236</body> 237</html>"# ; 238let document = kuchikiki :: parse_html () . one ( page ) ; 239let d = execute_lua ( document ) . unwrap () ; 240let text = d . select_first ( "span" ) . unwrap () . as_node () . text_contents () ; 241assert_eq ! ( text , "Test from Lua! \n " ) ; 242assert ! ( d . select_first ( "lua" ) . is_err ()) ; 243} 244 245#[ test ] 246fn multiple_lua () { 247let page = r#" 248<!DOCTYPE html> 249<html> 250<head> 251<title>Basic HTML Page</title> 252</head> 253<body> 254<h1>Hello World</h1> 255<p>This is a paragraph.</p> 256<span id="ta"><lua> 257htmlua.println("Test from Lua!") 258</lua></span> 259<div> 260<span id="tb"><lua>htmlua.println("test 2")</lua></span> 261</div> 262</body> 263</html>"# ; 264let document = kuchikiki :: parse_html () . one ( page ) ; 265let d = execute_lua ( document ) . unwrap () ; 266let text = d . select_first ( "#ta" ) . unwrap () . as_node () . text_contents () ; 267assert_eq ! ( text , "Test from Lua! \n " ) ; 268let text = d . select_first ( "#tb" ) . unwrap () . as_node () . text_contents () ; 269assert_eq ! ( text , "test 2 \n " ) ; 270assert ! ( d . select_first ( "lua" ) . is_err ()) ; 271} 272 273#[ test ] 274fn basic_include () { 275let page = r#" 276<!DOCTYPE html> 277<html> 278<head> 279<title>Basic HTML Page</title> 280</head> 281<body> 282<h1>Hello World</h1> 283<p>This is a paragraph.</p> 284<include path="comp1.html" /> 285<span><lua> 286htmlua.println("Test from Lua!") 287</lua></span> 288</body> 289</html>"# ; 290let mut p = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" )) ; 291p . push ( "tests/components" ) ; 292let document = kuchikiki :: parse_html () . one ( page ) ; 293let d = expand_template ( document , & p , None ) . unwrap () ; 294let text = d . select_first ( "span" ) . unwrap () . as_node () . text_contents () ; 295assert_eq ! ( text , "included1" ) ; 296assert ! ( d . select_first ( "include" ) . is_err ()) ; 297} 298 299#[ test ] 300fn multiple_include () { 301let page = r#" 302<!DOCTYPE html> 303<html> 304<head> 305<title>Basic HTML Page</title> 306</head> 307<body> 308<h1>Hello World</h1> 309<p>This is a paragraph.</p> 310<include path="multi1_1.html"></include> 311<include path="multi1_2.html"></include> 312<include path="multi1_3.html"></include> 313</body> 314</html>"# ; 315let mut p = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" )) ; 316p . push ( "tests/components" ) ; 317let document = kuchikiki :: parse_html () . one ( page ) ; 318let d = expand_template ( document , & p , None ) . unwrap () ; 319let text = d . select_first ( "#inc1" ) . unwrap () . as_node () . text_contents () ; 320assert_eq ! ( text , "included1" ) ; 321let text = d . select_first ( "#inc2" ) . unwrap () . as_node () . text_contents () ; 322assert_eq ! ( text , "included2" ) ; 323let text = d . select_first ( "#inc3" ) . unwrap () . as_node () . text_contents () ; 324assert_eq ! ( text , "included3" ) ; 325assert ! ( d . select_first ( "include" ) . is_err ()) ; 326} 327 328#[ test ] 329fn basic_lua_include () { 330let page = r#" 331<!DOCTYPE html> 332<html> 333<head> 334<title>Basic HTML Page</title> 335</head> 336<body> 337<h1>Hello World</h1> 338<p>This is a paragraph.</p> 339<span><lua> 340htmlua.println("Test from Lua!") 341</lua></span> 342<include path="comp1.html" /> 343</body> 344</html>"# ; 345let mut p = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" )) ; 346p . push ( "tests/components" ) ; 347let document = kuchikiki :: parse_html () . one ( page ) ; 348let d = execute_lua ( expand_template ( document , & p , None ) . unwrap ()) . unwrap () ; 349let text = d . select_first ( "span" ) . unwrap () . as_node () . text_contents () ; 350assert_eq ! ( text , "Test from Lua! \n " ) ; 351assert ! ( d . select_first ( "lua" ) . is_err ()) ; 352let text = d . select_first ( "#inc1" ) . unwrap () . as_node () . text_contents () ; 353assert_eq ! ( text , "included1" ) ; 354assert ! ( d . select_first ( "include" ) . is_err ()) ; 355} 356 357#[ test ] 358fn recursive_include () { 359let page = r#" 360<!DOCTYPE html> 361<html> 362<head> 363<title>Basic HTML Page</title> 364</head> 365<body> 366<h1>Hello World</h1> 367<include path="comp2_level1.html" /> 368</body> 369</html>"# ; 370let mut p = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" )) ; 371p . push ( "tests/components" ) ; 372let document = kuchikiki :: parse_html () . one ( page ) ; 373let d = expand_template ( document , & p , None ) . unwrap () ; 374let text = d . select_first ( "span" ) . unwrap () . as_node () . text_contents () ; 375assert_eq ! ( text , "included_twice" ) ; 376assert ! ( d . select_first ( "include" ) . is_err ()) ; 377} 378 379#[ test ] 380fn export_element_include () { 381let page = r#" 382<!DOCTYPE html> 383<html> 384<head> 385<title>Basic HTML Page</title> 386</head> 387<body> 388<h1>Hello World</h1> 389<include path="with_include1.html"> 390<exportelement class="el1"><span id="ta">element 1</span></exportelement> 391<exportelement class="el2"><span id="tb">element 2</span></exportelement> 392</include> 393</body> 394</html>"# ; 395let mut p = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" )) ; 396p . push ( "tests/components" ) ; 397let document = kuchikiki :: parse_html () . one ( page ) ; 398let d = expand_template ( document , & p , None ) . unwrap () ; 399let text = d . select_first ( "#ta" ) . unwrap () . as_node () . text_contents () ; 400assert_eq ! ( text , "element 1" ) ; 401let text = d . select_first ( "#tb" ) . unwrap () . as_node () . text_contents () ; 402assert_eq ! ( text , "element 2" ) ; 403assert ! ( d . select_first ( "exportelement" ) . is_err ()) ; 404assert ! ( d . select_first ( "includeelement" ) . is_err ()) ; 405} 406 407#[ test ] 408fn include_base () { 409let page = r#" 410<include path="include_base.html"> 411<exportelement class="t"><title>title_test</title></exportelement> 412<exportelement class="b"><span id="tb">body_el</span></exportelement> 413</include> 414"# ; 415let mut p = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" )) ; 416p . push ( "tests/components" ) ; 417let ctx_name = QualName :: new ( None , ns ! ( html ) , LocalName :: from ( "div" )) ; 418let document = kuchikiki :: parse_fragment ( ctx_name , Vec :: new ()) . one ( page ) ; 419let d = expand_template ( document , & p , None ) . unwrap () ; 420let text = d 421. select_first ( "head" ) 422. unwrap () 423. as_node () 424. select_first ( "title" ) 425. unwrap () 426. as_node () 427. text_contents () ; 428assert_eq ! ( text , "title_test" ) ; 429let text = d . select_first ( "#tb" ) . unwrap () . as_node () . text_contents () ; 430assert_eq ! ( text , "body_el" ) ; 431assert ! ( d . select_first ( "exportelement" ) . is_err ()) ; 432assert ! ( d . select_first ( "includeelement" ) . is_err ()) ; 433} 434 435#[ test ] 436fn footnotes () { 437let page = r" 438<!DOCTYPE html> 439<html> 440<head> 441<title>Basic HTML Page</title> 442</head> 443<body> 444<div> 445<p>asdf</p> 446<p>asdf<footnote>um actually</footnote></p> 447<p>asdf</p> 448<p>asdf<footnote>no</footnote></p> 449</div> 450<div> 451<footnotecontainer></footnotecontainer> 452</div> 453</body> 454</html>" ; 455let document = kuchikiki :: parse_html () . one ( page ) ; 456let d = generate_footnotes ( document ) . unwrap () ; 457let sup1 = d . select_first ( "#ft-sup-1" ) . unwrap () ; 458assert_eq ! ( sup1 . attributes . borrow () . get ( "title" ) . unwrap () , "um actually" ) ; 459assert_eq ! ( sup1 . text_contents () , "1" ) ; 460 461let sup2 = d . select_first ( "#ft-sup-2" ) . unwrap () ; 462assert_eq ! ( sup2 . attributes . borrow () . get ( "title" ) . unwrap () , "no" ) ; 463assert_eq ! ( sup2 . text_contents () , "2" ) ; 464 465let ft_text_1 = d . select_first ( "#ft-text-1" ) . unwrap () ; 466assert ! ( ft_text_1 . text_contents () . contains ( "um actually" )) ; 467 468let ft_text_2 = d . select_first ( "#ft-text-2" ) . unwrap () ; 469assert ! ( ft_text_2 . text_contents () . contains ( "no" )) ; 470 471assert ! ( d . select_first ( "footnote" ) . is_err ()) ; 472assert ! ( d . select_first ( "footnotecontainer" ) . is_err ()) ; 473} 474 475#[ test ] 476fn basic_markdown () { 477let page = r" 478<!DOCTYPE html> 479<html> 480<head> 481<title>Markdown Test</title> 482</head> 483<body> 484<h1>Hello World</h1> 485<div> 486<markdown> 487# This is a heading 488This is a paragraph with **bold text** and *italic text*. 489 490- Item 1 491- Item 2 492- Item 3 493</markdown> 494</div> 495</body> 496</html>" ; 497let document = kuchikiki :: parse_html () . one ( page ) ; 498let d = process_markdown ( document ) . unwrap () ; 499assert ! ( d . select_first ( "markdown" ) . is_err ()) ; 500assert ! ( d . select_first ( "p" ) . is_ok ()) ; 501assert ! ( d . select_first ( "ul" ) . is_ok ()) ; 502} 503 504#[ test ] 505fn basic_syntax_highlighting () { 506let page = r#" 507<!DOCTYPE html> 508<html> 509<head> 510<title>Syntax Highlighting Test</title> 511</head> 512<body> 513<h1>Code Example</h1> 514<syntaxhighlight lang="rust"> 515fn main() { 516println!("Hello, world!"); 517let x = 42; 518let y = "test"; 519} 520</syntaxhighlight> 521</body> 522</html>"# ; 523let document = kuchikiki :: parse_html () . one ( page ) ; 524let d = process_syntax_highlighting ( document ) . unwrap () ; 525assert ! ( d . select_first ( "syntaxhighlight" ) . is_err ()) ; 526assert ! ( d . select_first ( "pre" ) . is_ok ()) ; 527assert ! ( d . select_first ( "code" ) . is_ok ()) ; 528let pre = d . select_first ( "pre" ) . unwrap () ; 529let attrs = pre . attributes . borrow () ; 530assert_eq ! ( attrs . get ( "data-lang" ) , Some ( "rust" )) ; 531assert ! ( attrs . get ( "class" ) . unwrap () . contains ( "syntax-highlight" )) ; 532} 533 534static SERVER_POOL : ServerPool = ServerPool :: new ( 2 ) ; 535 536#[ test ] 537fn basic_get () { 538let server = SERVER_POOL . get_server () ; 539 540server . expect ( 541Expectation :: matching ( request :: method_path ( "GET" , "/test/1" )) . respond_with ( status_code ( 200 ) . body ( "ret" )) , 542) ; 543 544let stdout = Rc :: new ( RefCell :: new ( String :: new ())) ; 545let lua = build_lua_with_stdout ( & stdout ) . unwrap () ; 546let code = format ! ( "htmlua.print(htmlua.http.get( \" {} \" ).body)" , server . url ( "/test/1" )) ; 547lua . load ( code ) . exec () . unwrap () ; 548assert_eq ! ( stdout . borrow () . as_str () , "ret" ) ; 549} 550 551#[ test ] 552fn basic_post () { 553let server = SERVER_POOL . get_server () ; 554 555server . expect ( 556Expectation :: matching ( request :: method_path ( "POST" , "/test/1" )) . respond_with ( status_code ( 200 ) . body ( "ret" )) , 557) ; 558 559let stdout = Rc :: new ( RefCell :: new ( String :: new ())) ; 560let lua = build_lua_with_stdout ( & stdout ) . unwrap () ; 561let code = format ! ( "htmlua.print(htmlua.http.post( \" {} \" ).body)" , server . url ( "/test/1" )) ; 562lua . load ( code ) . exec () . unwrap () ; 563assert_eq ! ( stdout . borrow () . as_str () , "ret" ) ; 564} 565 566#[ test ] 567fn get_with_data () { 568let server = SERVER_POOL . get_server () ; 569 570server . expect ( 571Expectation :: matching ( request :: query ( url_decoded ( contains (( "a" , "b" ))))) 572. respond_with ( status_code ( 200 ) . body ( "ret" )) , 573) ; 574 575let stdout = Rc :: new ( RefCell :: new ( String :: new ())) ; 576let lua = build_lua_with_stdout ( & stdout ) . unwrap () ; 577let code = format ! ( 578" 579data = {{}} 580data.a = \" b \" 581htmlua.print(htmlua.http.get_with_data( \" {} \" , data).body) 582" , 583server . url ( "/test/1" ) 584) ; 585lua . load ( code ) . exec () . unwrap () ; 586assert_eq ! ( stdout . borrow () . as_str () , "ret" ) ; 587} 588 589#[ test ] 590fn post_with_data_form () { 591let server = SERVER_POOL . get_server () ; 592 593server . expect ( 594Expectation :: matching ( all_of ! [ request :: method_path ( "POST" , "/test/1" ) , request :: body ( "a=b" )]) 595. respond_with ( status_code ( 200 ) . body ( "ret" )) , 596) ; 597 598let stdout = Rc :: new ( RefCell :: new ( String :: new ())) ; 599let lua = build_lua_with_stdout ( & stdout ) . unwrap () ; 600let code = format ! ( 601" 602data = {{}} 603data.a = \" b \" 604htmlua.print(htmlua.http.post_with_data_form( \" {} \" , data).body) 605" , 606server . url ( "/test/1" ) 607) ; 608lua . load ( code ) . exec () . unwrap () ; 609assert_eq ! ( stdout . borrow () . as_str () , "ret" ) ; 610} 611 612#[ test ] 613fn post_with_data_json () { 614let server = SERVER_POOL . get_server () ; 615 616server . expect ( 617Expectation :: matching ( all_of ! [ request :: method_path ( "POST" , "/test/1" ) , request :: body ( r#"{"a":"b"}"# )]) 618. respond_with ( status_code ( 200 ) . body ( "ret" )) , 619) ; 620 621let stdout = Rc :: new ( RefCell :: new ( String :: new ())) ; 622let lua = build_lua_with_stdout ( & stdout ) . unwrap () ; 623let code = format ! ( 624" 625data = {{}} 626data.a = \" b \" 627htmlua.print(htmlua.http.post_with_data_json( \" {} \" , data).body) 628" , 629server . url ( "/test/1" ) 630) ; 631lua . load ( code ) . exec () . unwrap () ; 632assert_eq ! ( stdout . borrow () . as_str () , "ret" ) ; 633} 634 635#[ test ] 636fn table_request () { 637let server = SERVER_POOL . get_server () ; 638 639server . expect ( 640Expectation :: matching ( all_of ! [ 641request :: method_path ( "GET" , "/test/1" ) , 642request :: body ( "cool" ) , 643request :: headers ( contains (( "testhdr" , "val" ))) , 644request :: headers ( contains (( "authorization" , "Basic dXNlcjpwYXNz" ))) , 645]) 646. respond_with ( status_code ( 200 ) . body ( "ret" )) , 647) ; 648 649let stdout = Rc :: new ( RefCell :: new ( String :: new ())) ; 650let lua = build_lua_with_stdout ( & stdout ) . unwrap () ; 651let code = format ! ( 652" 653req = {{}} 654req.url = \" {} \" 655req.method = \" GET \" 656req.headers = {{}} 657req.headers.testhdr = \" val \" 658req.basic_auth = {{}} 659req.basic_auth.username = \" user \" 660req.basic_auth.password = \" pass \" 661req.body = \" cool \" 662 663htmlua.print(htmlua.http.request(req).body) 664" , 665server . url ( "/test/1" ) 666) ; 667lua . load ( code ) . exec () . unwrap () ; 668assert_eq ! ( stdout . borrow () . as_str () , "ret" ) ; 669} 670}