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