1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 var iface
: String is lazy
do
20 var testid
= "NIT_TESTING_ID".environ
.to_i
21 return "localhost:{10000+testid}"
24 var fs_path
: String = getcwd
/ "../lib/nitcorn/examples/www/hello_world/dir" is lazy
30 redef fun answer
(request
, turi
)
32 var rep
= new HttpResponse(200)
34 [Response] Simple answer
35 Method: {{{request.method}}}, URI: {{{request.uri}}}, trailing: {{{turi}}}"""
37 if request
.get_args
.not_empty
38 then body
+= "\nGET args: {request.get_args.join(", ", ":")}"
40 if request
.post_args
.not_empty
41 then body
+= "\nPOST args: {request.post_args.join(", ", ":")}"
43 if request
.uri_params
.not_empty
44 then body
+= "\nParams args: {request.uri_params.join(", ", ":")}"
46 if request
.cookie
.not_empty
47 then body
+= "\nCookie: {request.cookie.join(", ", ":")}"
58 redef fun answer
(request
, turi
)
60 var rep
= new HttpResponse(200)
61 rep
.body
= b
"\xF1\xF2\xF3\n"
69 redef fun answer
(request
, turi
)
71 var rep
= new HttpResponse(200)
72 var body
= new Template
74 body
.add b
"\xF1\xF2\xF3\n"
85 # Hide testing concept to force nitcorn to actually run
86 "NIT_TESTING".setenv
("false")
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)
97 var factory
= new HttpFactory.and_libevent
98 factory
.config
.virtual_hosts
.add vh
110 system
"curl -s {iface}/simple_answer"
111 system
"curl -s {iface}/simple_answer/"
112 system
"curl -s {iface}/simple_answer/trailing/path"
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'"
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"
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"
134 system
"curl -s {iface}/invalid_route --head"
136 system
"curl -s {iface}/simple_binary/"
137 system
"curl -s {iface}/simple_template/"
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
144 fun system
(cmd
: String, title
: nullable String)
146 title
= title
or else cmd
147 title
= title
.replace
(host_re
, "localhost:*****")
148 print
"\n[Client] {title}"
153 # First, launch a server in the background
154 var server
= new ServerThread
158 # Then, launch a client running test requests
159 var client
= new ClientThread
164 # Force quit the server