882816224a4fc8e9f5a35e119b177ddd6e2c7fbb
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.
15 import nitcorn
::restful_annot
20 var iface
: String is lazy
do
21 var testid
= "NIT_TESTING_ID".environ
22 if testid
.is_empty
then testid
= "1"
23 return "localhost:{10000+testid.to_i}"
26 var host_re
: Regex is lazy
do return "localhost:\[0-9\]+".to_re
34 # Hide testing concept to force nitcorn to actually run
35 "NIT_TESTING".setenv
("false")
38 var vh
= new VirtualHost(iface
)
39 vh
.routes
.add
new Route("rest_path", new MyAction)
42 var factory
= new HttpFactory.and_libevent
43 factory
.config
.virtual_hosts
.add vh
50 class ClientParallelThread
57 system
"curl -s '{iface}/rest_path/async_service?str=thread_{i}'"
64 print
"[Client] {cmd.replace(host_re, "localhost:*****")}"
70 # First, launch a server in the background
71 var server
= new ServerThread
75 # Launch 4 parallel requests to the async method
77 var client
= new ClientParallelThread(i
)
83 system
"curl -s '{iface}/rest_path/foo?s=s&i=12&b=true'"
84 system
"curl -s '{iface}/rest_path/foo?s=s&i=-4&b=false'"
85 system
"curl -s '{iface}/rest_path/foo' --data 's=s&i=12&b=true'"
87 system
"curl -s '{iface}/rest_path/api_name?s=s'"
88 system
"curl -s '{iface}/rest_path/alt_name?s=s'"
89 system
"curl -s '{iface}/rest_path/api_name?i=4&b=false'"
91 system
"""curl -s '{{{iface}}}/rest_path/complex_args?array=["a","b"]&data={"str":"asdf"}' -g"""
92 system
"""curl -s '{{{iface}}}/rest_path/complex_args?array=["a","b"]&data={"__class":"MyOtherData","str":"asdf","i":1234}' -g"""
94 # Errors falling back to `answer`
95 system
"curl -s '{iface}/rest_path/non_existent_service'"
96 system
"curl -s '{iface}/rest_path/foo'" # Missing args
97 system
"curl -s '{iface}/rest_path/foo?b=WrongDataType'"
98 system
"curl -s '{iface}/rest_path/bar?s=s'" # Renamed
99 system
"curl -s '{iface}/rest_path/api_name' --data s=DoesNotAcceptPOST"
100 system
"""curl -s '{{{iface}}}/rest_path/complex_args?array=["a","b"]&data={"str":"as' -g""" # Truncated JSON