nitcorn: add test for nitrestful
[nit.git] / lib / nitcorn / examples / src / test_restful_annot.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::restful_annot
16
17 import pthreads
18
19 redef class Sys
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}"
24 end
25
26 var host_re: Regex is lazy do return "localhost:\[0-9\]+".to_re
27 end
28
29 class ServerThread
30 super Thread
31
32 redef fun main
33 do
34 # Hide testing concept to force nitcorn to actually run
35 "NIT_TESTING".setenv("false")
36
37 # Setup
38 var vh = new VirtualHost(iface)
39 vh.routes.add new Route("rest_path", new MyAction)
40
41 # Launch
42 var factory = new HttpFactory.and_libevent
43 factory.config.virtual_hosts.add vh
44 factory.run
45
46 return null
47 end
48 end
49
50 class ClientParallelThread
51 super Thread
52
53 var i: Int
54
55 redef fun main
56 do
57 system "curl -s '{iface}/rest_path/async_service?str=thread_{i}'"
58 return null
59 end
60 end
61
62 redef fun system(cmd)
63 do
64 print "[Client] {cmd.replace(host_re, "localhost:*****")}"
65 var r = super(cmd)
66 print ""
67 return r
68 end
69
70 # First, launch a server in the background
71 var server = new ServerThread
72 server.start
73 0.1.sleep
74
75 # Launch 4 parallel requests to the async method
76 for i in 4.times do
77 var client = new ClientParallelThread(i)
78 client.start
79 0.1.sleep
80 end
81
82 # Successes
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'"
86
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'"
90
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"""
93
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
101
102 2.5.sleep
103 exit 0