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