Merge: doc: fixed some typos and other misc. corrections
[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 var body = """
34 [Response] Simple answer
35 Method: {{{request.method}}}, URI: {{{request.uri}}}, trailing: {{{turi}}}"""
36
37 if request.get_args.not_empty
38 then body += "\nGET args: {request.get_args.join(", ", ":")}"
39
40 if request.post_args.not_empty
41 then body += "\nPOST args: {request.post_args.join(", ", ":")}"
42
43 if request.uri_params.not_empty
44 then body += "\nParams args: {request.uri_params.join(", ", ":")}"
45
46 if request.cookie.not_empty
47 then body += "\nCookie: {request.cookie.join(", ", ":")}"
48
49 body += "\n"
50 rep.body = body
51 return rep
52 end
53 end
54
55 class MyBinAction
56 super Action
57
58 redef fun answer(request, turi)
59 do
60 var rep = new HttpResponse(200)
61 rep.body = b"\xF1\xF2\xF3\n"
62 return rep
63 end
64 end
65
66 class MyTmplAction
67 super Action
68
69 redef fun answer(request, turi)
70 do
71 var rep = new HttpResponse(200)
72 var body = new Template
73 body.add "Hello\n"
74 body.add b"\xF1\xF2\xF3\n"
75 rep.body = body
76 return rep
77 end
78 end
79
80 class ServerThread
81 super Thread
82
83 redef fun main
84 do
85 # Hide testing concept to force nitcorn to actually run
86 "NIT_TESTING".setenv("false")
87
88 # Setup
89 var vh = new VirtualHost(iface)
90 vh.routes.add new Route("file_server", new FileServer(fs_path.simplify_path))
91 vh.routes.add new Route("simple_answer", new MyAction)
92 vh.routes.add new Route("params_answer/:i/:s", new MyAction)
93 vh.routes.add new Route("simple_binary", new MyBinAction)
94 vh.routes.add new Route("simple_template", new MyTmplAction)
95
96 # Launch
97 var factory = new HttpFactory.and_libevent
98 factory.config.virtual_hosts.add vh
99 factory.run
100
101 return null
102 end
103 end
104
105 class ClientThread
106 super Thread
107
108 redef fun main
109 do
110 system "curl -s {iface}/simple_answer"
111 system "curl -s {iface}/simple_answer/"
112 system "curl -s {iface}/simple_answer/trailing/path"
113
114 system "curl -s '{iface}/simple_answer?i=0123&s=asdf'"
115 system "curl -s {iface}/simple_answer --data 'i=0123&s=asdf'"
116 system "curl -s {iface}/simple_answer --cookie 'i=0123; s=asdf'"
117 system "curl -s {iface}/simple_answer --get --data-urlencode 's=b b'"
118
119 system "curl -s {iface}/params_answer/0123/asdf"
120 system "curl -s {iface}/params_answer/0123/"
121 system "curl -s {iface}/params_answer/0123/asdf/trailing/path"
122 system "curl -s {iface}/params_answer/0123 --head"
123
124 system "curl -s {iface}/file_server/"
125 system "curl -s {iface}/file_server/ --head"
126 system "curl -s {iface}/file_server --head"
127 system "curl -s {iface}/file_server/a.txt"
128 system "curl -s {iface}/file_server/a.txt --head"
129 system "curl -s {iface}/file_server/binary_file.png --head"
130 system("curl -s {iface}/file_server/binary_file.png | diff - {fs_path.escape_to_sh}/binary_file.png",
131 "curl -s {iface}/file_server/binary_file.png | diff - .../binary_file.png")
132 system "curl -s {iface}/file_server/unknown_file.txt --head"
133
134 system "curl -s {iface}/invalid_route --head"
135
136 system "curl -s {iface}/simple_binary/"
137 system "curl -s {iface}/simple_template/"
138 return null
139 end
140
141 # Regex to catch and hide the port from the output to get consistent results
142 var host_re: Regex = "localhost:\[0-9\]+".to_re
143
144 fun system(cmd: String, title: nullable String)
145 do
146 title = title or else cmd
147 title = title.replace(host_re, "localhost:*****")
148 print "\n[Client] {title}"
149 sys.system cmd
150 end
151 end
152
153 # First, launch a server in the background
154 var server = new ServerThread
155 server.start
156 0.1.sleep
157
158 # Then, launch a client running test requests
159 var client = new ClientThread
160 client.start
161 client.join
162 0.1.sleep
163
164 # Force quit the server
165 exit 0