tests: test nitcorn
authorAlexis Laferrière <alexis.laf@xymus.net>
Wed, 20 Apr 2016 17:48:02 +0000 (13:48 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 22 Apr 2016 18:41:17 +0000 (14:41 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/nitcorn/examples/www/hello_world/dir/binary_file.png [new file with mode: 0644]
tests/sav/test_nitcorn.res [new file with mode: 0644]
tests/test_nitcorn.nit [new file with mode: 0644]

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 (file)
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 (file)
index 0000000..4d2fb2c
--- /dev/null
@@ -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\r
+Content-Length: 0\r
+Server: nitcorn\r
+Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT\r
+\r
+
+[Client] curl -s localhost:*****/file_server/
+<!DOCTYPE html>
+<head>
+       <meta charset="utf-8">
+       <meta http-equiv="X-UA-Compatible" content="IE=edge">
+       <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
+       <script>
+               
+       </script>
+       <title>/</title>
+</head>
+<body>
+       
+       <div class="container">
+               <h1>/</h1>
+               <ul>
+                       <li><a href="b.txt">b.txt</a></li>
+                       <li><a href="binary_file.png">binary_file.png</a></li>
+                       <li><a href="a.txt">a.txt</a></li>
+               </ul>
+       </div>
+</body>
+</html>
+[Client] curl -s localhost:*****/file_server/ --head
+HTTP/1.0 200 OK\r
+Content-Type: text/html\r
+Content-Length: 467\r
+Server: nitcorn\r
+Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT\r
+\r
+
+[Client] curl -s localhost:*****/file_server --head
+HTTP/1.0 303 See Other\r
+Location: /file_server/\r
+Content-Length: 0\r
+Server: nitcorn\r
+Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT\r
+\r
+
+[Client] curl -s localhost:*****/file_server/a.txt
+aaaAAAAAaaaa
+
+[Client] curl -s localhost:*****/file_server/a.txt --head
+HTTP/1.0 200 OK\r
+Content-Type: text/plain\r
+cache-control: public, max-age=360\r
+Content-Length: 13\r
+Server: nitcorn\r
+Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT\r
+\r
+
+[Client] curl -s localhost:*****/file_server/binary_file.png --head
+HTTP/1.0 200 OK\r
+Content-Type: image/png\r
+cache-control: public, max-age=360\r
+Content-Length: 2503\r
+Server: nitcorn\r
+Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT\r
+\r
+
+[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\r
+Content-Length: 329\r
+Server: nitcorn\r
+Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT\r
+\r
+
+[Client] curl -s localhost:*****/invalid_route --head
+HTTP/1.0 405 Method Not Allowed\r
+Content-Length: 0\r
+Server: nitcorn\r
+Set-Cookie: nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT\r
+\r
diff --git a/tests/test_nitcorn.nit b/tests/test_nitcorn.nit
new file mode 100644 (file)
index 0000000..e4088a2
--- /dev/null
@@ -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