From b359a6219abee546c373017756d37bf266c8adca Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Wed, 20 Apr 2016 13:48:02 -0400 Subject: [PATCH] tests: test nitcorn MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- .../examples/www/hello_world/dir/binary_file.png | Bin 0 -> 2503 bytes tests/sav/test_nitcorn.res | 125 ++++++++++++++++++ tests/test_nitcorn.nit | 133 ++++++++++++++++++++ 3 files changed, 258 insertions(+) create mode 100644 lib/nitcorn/examples/www/hello_world/dir/binary_file.png create mode 100644 tests/sav/test_nitcorn.res create mode 100644 tests/test_nitcorn.nit 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 0000000000000000000000000000000000000000..d522fcc4b05f02ef6d2e04ef0c647c2b662e84bd GIT binary patch literal 2503 zcmV;&2{`tNP)z?oSXm|4vr8}BEx zomI9$l68{5A;>ZTW`G1iqF9Qh$PZ>nTcRaCL{aP}n|-_Q``(&`OvBE2VkT3o0t$7` zS6_YSJ4fRTYi4Fv1VI46&6_vBK07=6!NS5qu)V$gCcvw&zWQ(7Zud7UD=WX*+S>XM zU~XpA>(>FeckdpCVH$>E{zsB#UmZsgj_bY#yaphpBn%_cH2W9F ztt>&0_K*M`{M0>3P+jn4&0=Nt#vuUjVw@bDvOw z9%+U{vo*=Y&~=l^&K!G> z-~X1b8($PsejNy2e)(kpZr{HBxn+1tmpV+M!GvEgQ(^(G^86d)qA)evMmG#} zT}KK*UZk8128>1nP~bP(_`XgYg$z%Q2?hsPhG3%IGCQ5ge>61X=eK@$Yws^IqOd5w zV`$oUY|EnNH)%B{s8oG42~lv!TYq?$cQ-#osgf^VUg4E5U8N{;5G96D(p{XRDAn}# z_RdenAE2vKi(#eWB+qSc=_^W0M^&npC1@TXxlbZok=X)5lI*j4SNiR$GrXE z3A@g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: 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 -- 1.7.9.5