Mercurial Hosting > luan
annotate src/luan/modules/http/Http_test.luan @ 1160:4beabb087be6
add http/impl
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 05 Feb 2018 22:33:59 -0700 |
parents | 1f4da56abd4f |
children | f8dd726bd4f5 |
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() | |
25 local mod = Package.load("site:"..path..".luan") | |
799
9c13a15a4002
Package.load() now returns false instead of null, and caches the result. This cleans up the luan.isLocked issues.
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
26 if mod and true then |
702 | 27 mod() |
28 else | |
29 local not_found = Package.load("site:/not_found.luan") | |
30 not_found or error(path.." not found") | |
31 not_found() | |
32 end | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
33 Http_test.text_writer.close() |
702 | 34 end |
35 finally = function() | |
36 Io.stdout = old_out | |
37 end | |
38 } | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
39 return Http_test.result.read_text() |
503 | 40 end |
41 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
42 function Http_test.init() |
1160 | 43 Http.request = Http.new_request() |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
44 Http.request.cookies = Http_test.cookies |
503 | 45 |
1160 | 46 Http.response = { |
47 | |
48 headers = {} | |
49 | |
50 status = Http.STATUS.OK | |
503 | 51 |
52 text_writer = function() | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
53 Http_test.result = Io.uri "string:" |
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
54 Http_test.text_writer = Http_test.result.text_writer() |
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
55 return Http_test.text_writer |
702 | 56 end |
503 | 57 |
58 set_cookie = function(name,value) | |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
59 Http_test.cookies[name] = value |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
60 end |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
61 |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
62 set_persistent_cookie = function(name,value) |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
63 Http_test.cookies[name] = value |
702 | 64 end |
503 | 65 |
66 remove_cookie = function(name) | |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
67 Http_test.cookies[name] = nil |
702 | 68 end |
503 | 69 |
70 send_redirect = function(url) | |
71 Http.response.redirect = url | |
702 | 72 end |
73 | |
74 send_error = function(code) | |
75 error("sent error "..code) | |
76 end | |
503 | 77 |
78 } | |
79 | |
80 end | |
81 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
82 return Http_test |