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 module test_restful_annot
is example
17 import nitcorn
::restful_annot
22 var iface
: String is lazy
do
23 var testid
= "NIT_TESTING_ID".environ
24 if testid
.is_empty
then testid
= "1"
25 return "localhost:{10000+testid.to_i}"
28 var host_re
: Regex is lazy
do return "localhost:\[0-9\]+".to_re
36 # Hide testing concept to force nitcorn to actually run
37 "NIT_TESTING".setenv
("false")
40 var vh
= new VirtualHost(iface
)
41 vh
.routes
.add
new Route("rest_path", new MyAction)
44 var factory
= new HttpFactory.and_libevent
45 factory
.config
.virtual_hosts
.add vh
52 class ClientParallelThread
59 system
"curl -s '{iface}/rest_path/async_service?str=thread_{i}'"
66 print
"[Client] {cmd.replace(host_re, "localhost:*****")}"
72 # First, launch a server in the background
73 var server
= new ServerThread
77 # Launch 4 parallel requests to the async method
79 var client
= new ClientParallelThread(i
)
85 system
"curl -s '{iface}/rest_path/foo?s=s&i=12&b=true'"
86 system
"curl -s '{iface}/rest_path/foo?s=s&i=-4&b=false'"
87 system
"curl -s '{iface}/rest_path/foo' --data 's=s&i=12&b=true'"
89 system
"curl -s '{iface}/rest_path/api_name?s=s'"
90 system
"curl -s '{iface}/rest_path/alt_name?s=s'"
91 system
"curl -s '{iface}/rest_path/api_name?i=4&b=false'"
93 system
"""curl -s '{{{iface}}}/rest_path/complex_args?array=["a","b"]&data={"str":"asdf"}' -g"""
94 system
"""curl -s '{{{iface}}}/rest_path/complex_args?array=["a","b"]&data={"__class":"MyOtherData","str":"asdf","i":1234}' -g"""
96 # Errors falling back to `answer`
97 system
"curl -s '{iface}/rest_path/non_existent_service'"
98 system
"curl -s '{iface}/rest_path/foo'" # Missing args
99 system
"curl -s '{iface}/rest_path/foo?b=WrongDataType'"
100 system
"curl -s '{iface}/rest_path/bar?s=s'" # Renamed
101 system
"curl -s '{iface}/rest_path/api_name' --data s=DoesNotAcceptPOST"
102 system
"""curl -s '{{{iface}}}/rest_path/complex_args?array=["a","b"]&data={"str":"as' -g""" # Truncated JSON