Mercurial Hosting > luan
annotate src/luan/modules/http/Http_test.luan @ 1340:b3c4fcf29a53
fix values() and minor Http_test
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 18 Feb 2019 05:11:50 -0700 |
parents | d410747a671a |
children | d9a5405a3102 |
rev | line source |
---|---|
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
1 local Luan = require "luan:Luan.luan" |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
2 local error = Luan.error |
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
3 local set_metatable = Luan.set_metatable or error() |
702 | 4 local try = Luan.try or error() |
5 local Package = require "luan:Package.luan" | |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
6 local Io = require "luan:Io.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
7 local String = require "luan:String.luan" |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
8 local matches = String.matches or error() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
9 local Http = require "luan:http/Http.luan" |
503 | 10 |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
571
diff
changeset
|
11 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
12 local Http_test = {} |
503 | 13 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
14 Http_test.welcome_file = "index.html" |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
15 Http_test.cookies = {} |
503 | 16 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
17 function Http_test.get_page(path) |
702 | 18 Http.request.path = path |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
19 if Http_test.welcome_file ~= nil and matches(path,"/$") then |
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
20 path = path .. Http_test.welcome_file |
503 | 21 end |
22 local old_out = Io.stdout | |
702 | 23 try { |
24 function() | |
1340
b3c4fcf29a53
fix values() and minor Http_test
Franklin Schmidt <fschmidt@gmail.com>
parents:
1270
diff
changeset
|
25 local mod = Package.load("site:"..path..".luan") or error(path.." not found") |
b3c4fcf29a53
fix values() and minor Http_test
Franklin Schmidt <fschmidt@gmail.com>
parents:
1270
diff
changeset
|
26 mod() |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
27 Http_test.text_writer.close() |
702 | 28 end |
29 finally = function() | |
30 Io.stdout = old_out | |
31 end | |
32 } | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
33 return Http_test.result.read_text() |
503 | 34 end |
35 | |
1270
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
36 function Http_test.run_page(page_fn) |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
37 local old_out = Io.stdout |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
38 try { |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
39 function() |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
40 page_fn() |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
41 Http_test.text_writer.close() |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
42 end |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
43 finally = function() |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
44 Io.stdout = old_out |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
45 end |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
46 } |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
47 return Http_test.result.read_text() |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
48 end |
d410747a671a
fix scripts/test.luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
1228
diff
changeset
|
49 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
50 function Http_test.init() |
1228 | 51 function Http.reset_luan() end |
1160 | 52 Http.request = Http.new_request() |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
53 Http.request.cookies = Http_test.cookies |
503 | 54 |
1160 | 55 Http.response = { |
56 | |
57 headers = {} | |
58 | |
59 status = Http.STATUS.OK | |
503 | 60 |
61 text_writer = function() | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
62 Http_test.result = Io.uri "string:" |
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
63 Http_test.text_writer = Http_test.result.text_writer() |
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
64 return Http_test.text_writer |
702 | 65 end |
503 | 66 |
67 set_cookie = function(name,value) | |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
68 Http_test.cookies[name] = value |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
69 end |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
70 |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
71 set_persistent_cookie = function(name,value) |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
72 Http_test.cookies[name] = value |
702 | 73 end |
503 | 74 |
75 remove_cookie = function(name) | |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
76 Http_test.cookies[name] = nil |
702 | 77 end |
503 | 78 |
79 send_redirect = function(url) | |
80 Http.response.redirect = url | |
702 | 81 end |
82 | |
83 send_error = function(code) | |
84 error("sent error "..code) | |
85 end | |
503 | 86 |
87 } | |
88 | |
89 end | |
90 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
91 return Http_test |