Mercurial Hosting > luan
annotate src/luan/modules/http/Http_test.luan @ 1153:1f4da56abd4f
change http cookies interface
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 04 Feb 2018 20:01:23 -0700 |
parents | bae2d0c2576c |
children | 4beabb087be6 |
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() |
503 | 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 |
46 Http.response = Http.new_response{ | |
47 | |
48 text_writer = function() | |
580 | 49 Http.sent_headers(Http.response.headers) |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
50 Http_test.result = Io.uri "string:" |
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
51 Http_test.text_writer = Http_test.result.text_writer() |
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
52 return Http_test.text_writer |
702 | 53 end |
503 | 54 |
55 set_cookie = function(name,value) | |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
56 Http_test.cookies[name] = value |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
57 end |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
58 |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
59 set_persistent_cookie = function(name,value) |
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
60 Http_test.cookies[name] = value |
702 | 61 end |
503 | 62 |
63 remove_cookie = function(name) | |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1088
diff
changeset
|
64 Http_test.cookies[name] = nil |
702 | 65 end |
503 | 66 |
67 send_redirect = function(url) | |
68 Http.response.redirect = url | |
702 | 69 end |
70 | |
71 send_error = function(code) | |
72 error("sent error "..code) | |
73 end | |
503 | 74 |
75 } | |
76 | |
77 end | |
78 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
799
diff
changeset
|
79 return Http_test |