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
21 return "localhost:{10000+20000.rand}"
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 rep
.body
+= "\nGET args: {request.get_args.join(", ", ":")}"
40 if request
.post_args
.not_empty
41 then rep
.body
+= "\nPOST args: {request.post_args.join(", ", ":")}"
43 if request
.uri_params
.not_empty
44 then rep
.body
+= "\nParams args: {request.uri_params.join(", ", ":")}"
46 if request
.cookie
.not_empty
47 then rep
.body
+= "\nCookie: {request.cookie.join(", ", ":")}"
59 # Hide testing concept to force nitcorn to actually run
60 "NIT_TESTING".setenv
("false")
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)
69 var factory
= new HttpFactory.and_libevent
70 factory
.config
.virtual_hosts
.add vh
82 system
"curl -s {iface}/simple_answer"
83 system
"curl -s {iface}/simple_answer/"
84 system
"curl -s {iface}/simple_answer/trailing/path"
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'"
90 system
"curl -s {iface}/params_answer/0123/asdf"
91 system
"curl -s {iface}/params_answer/0123/"
92 system
"curl -s {iface}/params_answer/0123/asdf/trailing/path"
93 system
"curl -s {iface}/params_answer/0123 --head"
95 system
"curl -s {iface}/file_server/"
96 system
"curl -s {iface}/file_server/ --head"
97 system
"curl -s {iface}/file_server --head"
98 system
"curl -s {iface}/file_server/a.txt"
99 system
"curl -s {iface}/file_server/a.txt --head"
100 system
"curl -s {iface}/file_server/binary_file.png --head"
101 system
("curl -s {iface}/file_server/binary_file.png | diff - {fs_path.escape_to_sh}/binary_file.png",
102 "curl -s {iface}/file_server/binary_file.png | diff - .../binary_file.png")
103 system
"curl -s {iface}/file_server/unknown_file.txt --head"
105 system
"curl -s {iface}/invalid_route --head"
109 # Regex to catch and hide the port from the output to get consistent results
110 var host_re
: Regex = "localhost:\[0-9\]+".to_re
112 fun system
(cmd
: String, title
: nullable String)
114 title
= title
or else cmd
115 title
= title
.replace
(host_re
, "localhost:*****")
116 print
"\n[Client] {title}"
121 # First, launch a server in the background
122 var server
= new ServerThread
126 # Then, launch a client running test requests
127 var client
= new ClientThread
132 # Force quit the server