comparison src/luan/modules/http/Http_test.luan @ 1520:d9a5405a3102

try statement
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 21 Jun 2020 18:14:13 -0600
parents b3c4fcf29a53
children b82767112d8e
comparison
equal deleted inserted replaced
1519:3ebf9781707c 1520:d9a5405a3102
1 local Luan = require "luan:Luan.luan" 1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error 2 local error = Luan.error
3 local set_metatable = Luan.set_metatable or error() 3 local set_metatable = Luan.set_metatable or error()
4 local try = Luan.try or error()
5 local Package = require "luan:Package.luan" 4 local Package = require "luan:Package.luan"
6 local Io = require "luan:Io.luan" 5 local Io = require "luan:Io.luan"
7 local String = require "luan:String.luan" 6 local String = require "luan:String.luan"
8 local matches = String.matches or error() 7 local matches = String.matches or error()
9 local Http = require "luan:http/Http.luan" 8 local Http = require "luan:http/Http.luan"
18 Http.request.path = path 17 Http.request.path = path
19 if Http_test.welcome_file ~= nil and matches(path,"/$") then 18 if Http_test.welcome_file ~= nil and matches(path,"/$") then
20 path = path .. Http_test.welcome_file 19 path = path .. Http_test.welcome_file
21 end 20 end
22 local old_out = Io.stdout 21 local old_out = Io.stdout
23 try { 22 try
24 function() 23 local mod = Package.load("site:"..path..".luan") or error(path.." not found")
25 local mod = Package.load("site:"..path..".luan") or error(path.." not found") 24 mod()
26 mod() 25 Http_test.text_writer.close()
27 Http_test.text_writer.close() 26 finally
28 end 27 Io.stdout = old_out
29 finally = function() 28 end
30 Io.stdout = old_out
31 end
32 }
33 return Http_test.result.read_text() 29 return Http_test.result.read_text()
34 end 30 end
35 31
36 function Http_test.run_page(page_fn) 32 function Http_test.run_page(page_fn)
37 local old_out = Io.stdout 33 local old_out = Io.stdout
38 try { 34 try
39 function() 35 page_fn()
40 page_fn() 36 Http_test.text_writer.close()
41 Http_test.text_writer.close() 37 finally
42 end 38 Io.stdout = old_out
43 finally = function() 39 end
44 Io.stdout = old_out
45 end
46 }
47 return Http_test.result.read_text() 40 return Http_test.result.read_text()
48 end 41 end
49 42
50 function Http_test.init() 43 function Http_test.init()
51 function Http.reset_luan() end 44 function Http.reset_luan() end