From: Alexis Laferrière Date: Wed, 20 Apr 2016 17:48:02 +0000 (-0400) Subject: tests: test nitcorn X-Git-Url: http://nitlanguage.org tests: test nitcorn Signed-off-by: Alexis Laferrière --- diff --git a/lib/nitcorn/examples/www/hello_world/dir/binary_file.png b/lib/nitcorn/examples/www/hello_world/dir/binary_file.png new file mode 100644 index 0000000..d522fcc Binary files /dev/null and b/lib/nitcorn/examples/www/hello_world/dir/binary_file.png differ diff --git a/tests/sav/test_nitcorn.res b/tests/sav/test_nitcorn.res new file mode 100644 index 0000000..4d2fb2c --- /dev/null +++ b/tests/sav/test_nitcorn.res @@ -0,0 +1,125 @@ + +[Client] curl -s localhost:*****/simple_answer +[Response] Simple answer +Method: GET, URI: /simple_answer, trailing: / + +[Client] curl -s localhost:*****/simple_answer/ +[Response] Simple answer +Method: GET, URI: /simple_answer/, trailing: / + +[Client] curl -s localhost:*****/simple_answer/trailing/path +[Response] Simple answer +Method: GET, URI: /simple_answer/trailing/path, trailing: /trailing/path + +[Client] curl -s 'localhost:*****/simple_answer?i=0123&s=asdf' +[Response] Simple answer +Method: GET, URI: /simple_answer, trailing: / +GET args: i:0123, s:asdf + +[Client] curl -s localhost:*****/simple_answer --data 'i=0123&s=asdf' +[Response] Simple answer +Method: POST, URI: /simple_answer, trailing: / +POST args: i:0123, s:asdf + +[Client] curl -s localhost:*****/simple_answer --cookie 'i=0123; s=asdf' +[Response] Simple answer +Method: GET, URI: /simple_answer, trailing: / +Cookie: i:0123, s:asdf + +[Client] curl -s localhost:*****/params_answer/0123/asdf +[Response] Simple answer +Method: GET, URI: /params_answer/0123/asdf, trailing: / +Params args: i:0123, s:asdf + +[Client] curl -s localhost:*****/params_answer/0123/ +[Response] Simple answer +Method: GET, URI: /params_answer/0123/, trailing: / +Params args: i:0123, s: + +[Client] curl -s localhost:*****/params_answer/0123/asdf/trailing/path +[Response] Simple answer +Method: GET, URI: /params_answer/0123/asdf/trailing/path, trailing: /trailing/path +Params args: i:0123, s:asdf + +[Client] curl -s localhost:*****/params_answer/0123 --head +HTTP/1.0 405 Method Not Allowed +Content-Length: 0 +Server: nitcorn +Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT + + +[Client] curl -s localhost:*****/file_server/ + + + + + + + / + + + +
+

/

+ +
+ + +[Client] curl -s localhost:*****/file_server/ --head +HTTP/1.0 200 OK +Content-Type: text/html +Content-Length: 467 +Server: nitcorn +Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT + + +[Client] curl -s localhost:*****/file_server --head +HTTP/1.0 303 See Other +Location: /file_server/ +Content-Length: 0 +Server: nitcorn +Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT + + +[Client] curl -s localhost:*****/file_server/a.txt +aaaAAAAAaaaa + +[Client] curl -s localhost:*****/file_server/a.txt --head +HTTP/1.0 200 OK +Content-Type: text/plain +cache-control: public, max-age=360 +Content-Length: 13 +Server: nitcorn +Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT + + +[Client] curl -s localhost:*****/file_server/binary_file.png --head +HTTP/1.0 200 OK +Content-Type: image/png +cache-control: public, max-age=360 +Content-Length: 2503 +Server: nitcorn +Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT + + +[Client] curl -s localhost:*****/file_server/binary_file.png | diff - .../binary_file.png + +[Client] curl -s localhost:*****/file_server/unknown_file.txt --head +HTTP/1.0 404 Not Found +Content-Length: 329 +Server: nitcorn +Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT + + +[Client] curl -s localhost:*****/invalid_route --head +HTTP/1.0 405 Method Not Allowed +Content-Length: 0 +Server: nitcorn +Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT + diff --git a/tests/test_nitcorn.nit b/tests/test_nitcorn.nit new file mode 100644 index 0000000..e4088a2 --- /dev/null +++ b/tests/test_nitcorn.nit @@ -0,0 +1,133 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import nitcorn +import pthreads + +redef class Sys + var iface: String is lazy do + srand + return "localhost:{10000+20000.rand}" + end + + var fs_path: String = getcwd / "../lib/nitcorn/examples/www/hello_world/dir" is lazy +end + +class MyAction + super Action + + redef fun answer(request, turi) + do + var rep = new HttpResponse(200) + rep.body = """ +[Response] Simple answer +Method: {{{request.method}}}, URI: {{{request.uri}}}, trailing: {{{turi}}}""" + + if request.get_args.not_empty + then rep.body += "\nGET args: {request.get_args.join(", ", ":")}" + + if request.post_args.not_empty + then rep.body += "\nPOST args: {request.post_args.join(", ", ":")}" + + if request.uri_params.not_empty + then rep.body += "\nParams args: {request.uri_params.join(", ", ":")}" + + if request.cookie.not_empty + then rep.body += "\nCookie: {request.cookie.join(", ", ":")}" + + rep.body += "\n" + return rep + end +end + +class ServerThread + super Thread + + redef fun main + do + # Hide testing concept to force nitcorn to actually run + "NIT_TESTING".setenv("false") + + # Setup + var vh = new VirtualHost(iface) + vh.routes.add new Route("file_server", new FileServer(fs_path.simplify_path)) + vh.routes.add new Route("simple_answer", new MyAction) + vh.routes.add new Route("params_answer/:i/:s", new MyAction) + + # Launch + var factory = new HttpFactory.and_libevent + factory.config.virtual_hosts.add vh + factory.run + + return null + end +end + +class ClientThread + super Thread + + redef fun main + do + system "curl -s {iface}/simple_answer" + system "curl -s {iface}/simple_answer/" + system "curl -s {iface}/simple_answer/trailing/path" + + system "curl -s '{iface}/simple_answer?i=0123&s=asdf'" + system "curl -s {iface}/simple_answer --data 'i=0123&s=asdf'" + system "curl -s {iface}/simple_answer --cookie 'i=0123; s=asdf'" + + system "curl -s {iface}/params_answer/0123/asdf" + system "curl -s {iface}/params_answer/0123/" + system "curl -s {iface}/params_answer/0123/asdf/trailing/path" + system "curl -s {iface}/params_answer/0123 --head" + + system "curl -s {iface}/file_server/" + system "curl -s {iface}/file_server/ --head" + system "curl -s {iface}/file_server --head" + system "curl -s {iface}/file_server/a.txt" + system "curl -s {iface}/file_server/a.txt --head" + system "curl -s {iface}/file_server/binary_file.png --head" + system("curl -s {iface}/file_server/binary_file.png | diff - {fs_path.escape_to_sh}/binary_file.png", + "curl -s {iface}/file_server/binary_file.png | diff - .../binary_file.png") + system "curl -s {iface}/file_server/unknown_file.txt --head" + + system "curl -s {iface}/invalid_route --head" + return null + end + + # Regex to catch and hide the port from the output to get consistent results + var host_re: Regex = "localhost:\[0-9\]+".to_re + + fun system(cmd: String, title: nullable String) + do + title = title or else cmd + title = title.replace(host_re, "localhost:*****") + print "\n[Client] {title}" + sys.system cmd + end +end + +# First, launch a server in the background +var server = new ServerThread +server.start +0.1.sleep + +# Then, launch a client running test requests +var client = new ClientThread +client.start +client.join +0.1.sleep + +# Force quit the server +exit 0