lib: intro `prompt`, basic Apache 2.0 service to display a prompt
[nit.git] / tests / test_nitcorn.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import nitcorn
16 import pthreads
17
18 redef class Sys
19 var iface: String is lazy do
20 var testid = "NIT_TESTING_ID".environ.to_i
21 return "localhost:{10000+testid}"
22 end
23
24 var fs_path: String = getcwd / "../lib/nitcorn/examples/www/hello_world/dir" is lazy
25 end
26
27 class MyAction
28 super Action
29
30 redef fun answer(request, turi)
31 do
32 var rep = new HttpResponse(200)
33 rep.body = """
34 [Response] Simple answer
35 Method: {{{request.method}}}, URI: {{{request.uri}}}, trailing: {{{turi}}}"""
36
37 if request.get_args.not_empty
38 then rep.body += "\nGET args: {request.get_args.join(", ", ":")}"
39
40 if request.post_args.not_empty
41 then rep.body += "\nPOST args: {request.post_args.join(", ", ":")}"
42
43 if request.uri_params.not_empty
44 then rep.body += "\nParams args: {request.uri_params.join(", ", ":")}"
45
46 if request.cookie.not_empty
47 then rep.body += "\nCookie: {request.cookie.join(", ", ":")}"
48
49 rep.body += "\n"
50 return rep
51 end
52 end
53
54 class ServerThread
55 super Thread
56
57 redef fun main
58 do
59 # Hide testing concept to force nitcorn to actually run
60 "NIT_TESTING".setenv("false")
61
62 # Setup
63 var vh = new VirtualHost(iface)
64 vh.routes.add new Route("file_server", new FileServer(fs_path.simplify_path))
65 vh.routes.add new Route("simple_answer", new MyAction)
66 vh.routes.add new Route("params_answer/:i/:s", new MyAction)
67
68 # Launch
69 var factory = new HttpFactory.and_libevent
70 factory.config.virtual_hosts.add vh
71 factory.run
72
73 return null
74 end
75 end
76
77 class ClientThread
78 super Thread
79
80 redef fun main
81 do
82 system "curl -s {iface}/simple_answer"
83 system "curl -s {iface}/simple_answer/"
84 system "curl -s {iface}/simple_answer/trailing/path"
85
86 system "curl -s '{iface}/simple_answer?i=0123&s=asdf'"
87 system "curl -s {iface}/simple_answer --data 'i=0123&s=asdf'"
88 system "curl -s {iface}/simple_answer --cookie 'i=0123; s=asdf'"
89 system "curl -s {iface}/simple_answer --get --data-urlencode 's=b b'"
90
91 system "curl -s {iface}/params_answer/0123/asdf"
92 system "curl -s {iface}/params_answer/0123/"
93 system "curl -s {iface}/params_answer/0123/asdf/trailing/path"
94 system "curl -s {iface}/params_answer/0123 --head"
95
96 system "curl -s {iface}/file_server/"
97 system "curl -s {iface}/file_server/ --head"
98 system "curl -s {iface}/file_server --head"
99 system "curl -s {iface}/file_server/a.txt"
100 system "curl -s {iface}/file_server/a.txt --head"
101 system "curl -s {iface}/file_server/binary_file.png --head"
102 system("curl -s {iface}/file_server/binary_file.png | diff - {fs_path.escape_to_sh}/binary_file.png",
103 "curl -s {iface}/file_server/binary_file.png | diff - .../binary_file.png")
104 system "curl -s {iface}/file_server/unknown_file.txt --head"
105
106 system "curl -s {iface}/invalid_route --head"
107 return null
108 end
109
110 # Regex to catch and hide the port from the output to get consistent results
111 var host_re: Regex = "localhost:\[0-9\]+".to_re
112
113 fun system(cmd: String, title: nullable String)
114 do
115 title = title or else cmd
116 title = title.replace(host_re, "localhost:*****")
117 print "\n[Client] {title}"
118 sys.system cmd
119 end
120 end
121
122 # First, launch a server in the background
123 var server = new ServerThread
124 server.start
125 0.1.sleep
126
127 # Then, launch a client running test requests
128 var client = new ClientThread
129 client.start
130 client.join
131 0.1.sleep
132
133 # Force quit the server
134 exit 0