c_src/** -diff
tests/sav/**/*.res -whitespace
-lib/popcorn/tests/res/*.res -whitespace
+*.res -whitespace
*.patch -whitespace
NITUNIT=../../bin/nitunit
check:
- $(NITUNIT) README.md
- $(NITUNIT) pop_routes.nit
- $(NITUNIT) pop_handlers.nit
- $(NITUNIT) popcorn.nit
- cd tests; make check
+ $(NITUNIT) .
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_angular is test_suite
+
+import pop_tests
import example_angular
-import base_tests
-class TestClient
- super ClientThread
+class TestExampleAngular
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/counter"
system "curl -s {host}:{port}/counter -X POST"
system "curl -s {host}:{port}/counter"
system "curl -s {host}:{port}/not_found" # handled by angular controller
- return null
end
-end
-
-var app = new App
-app.use("/counter", new CounterAPI)
-app.use("/*", new StaticHandler("../examples/angular/www/", "index.html"))
-
-var host = test_host
-var port = test_port
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_param_route do
+ var app = new App
+ app.use("/counter", new CounterAPI)
+ app.use("/*", new StaticHandler("../examples/angular/www/", "index.html"))
+ run_test(app)
+ end
+end
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_post_handler is test_suite
+
+import pop_tests
import example_post_handler
-import base_tests
-class TestClient
- super ClientThread
+class TestExampleQueryString
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/ -X POST"
system "curl -s {host}:{port}/ --data 'user'"
system "curl -s {host}:{port}/ --data 'user=Morriar'"
system "curl -s {host}:{port}/ --data 'user=\&order=desc'"
system "curl -s {host}:{port}/ --data 'user=Morriar\&order=desc'"
-
system "curl -s {host}:{port}/"
- return null
end
-end
-
-var app = new App
-app.use("/", new PostHandler)
-
-var host = test_host
-var port = test_port
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_glob_route do
+ var app = new App
+ app.use("/", new PostHandler)
+ run_test(app)
+ end
+end
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_query_string is test_suite
+
+import pop_tests
import example_query_string
-import base_tests
-class TestClient
- super ClientThread
+class TestExampleQueryString
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/"
system "curl -s {host}:{port}/?user=Morriar"
system "curl -s {host}:{port}/?reload"
system "curl -s {host}:{port}/?foo\\&bar=baz"
system "curl -s {host}:{port}/?items=10\\&order=asc"
- return null
end
-end
-
-var app = new App
-app.use("/", new QueryStringHandler)
-
-var host = test_host
-var port = test_port
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_glob_route do
+ var app = new App
+ app.use("/", new QueryStringHandler)
+ run_test(app)
+ end
+end
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_hello is test_suite
+
+import pop_tests
import example_hello
-import base_tests
-class TestClient
- super ClientThread
+class TestExampleHello
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}"
system "curl -s {host}:{port}/"
system "curl -s {host}:{port}///////////"
system "curl -s {host}:{port}/not_found"
system "curl -s {host}:{port}/not_found/not_found"
- return null
end
-end
-
-var app = new App
-app.use("/", new HelloHandler)
-
-var host = test_host
-var port = test_port
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_hello do
+ var app = new App
+ app.use("/", new HelloHandler)
+ run_test(app)
+ end
+end
<body>
<h1>404 Not Found</h1>
</body>
- </html>
\ No newline at end of file
+ </html>
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_advanced_logger is test_suite
+
+import pop_tests
import example_advanced_logger
-import base_tests
-class TestClient
- super ClientThread
+class TestExampleAdvancedLogger
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/"
system "curl -s {host}:{port}/about"
- return null
end
-end
-
-var app = new App
-app.use_before("/*", new RequestTimeHandler)
-app.use("/", new HelloHandler)
-app.use_after("/*", new LogHandler)
-
-var host = test_host
-var port = test_port
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_param_route do
+ var app = new App
+ app.use_before("/*", new RequestTimeHandler)
+ app.use("/", new HelloHandler)
+ app.use_after("/*", new LogHandler)
+ run_test(app)
+ end
+end
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_html_error_handler is test_suite
+
+import pop_tests
import example_html_error_handler
-import base_tests
-class TestClient
- super ClientThread
+class TestExampleHtmlErrorHandler
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/"
system "curl -s {host}:{port}/about"
- return null
end
-end
-
-var app = new App
-app.use("/*", new HtmlErrorHandler)
-
-var host = test_host
-var port = test_port
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_param_route do
+ var app = new App
+ app.use("/*", new HtmlErrorHandler)
+ run_test(app)
+ end
+end
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_simple_error_handler is test_suite
+
+import pop_tests
import example_simple_error_handler
-import base_tests
-class TestClient
- super ClientThread
+class TestExampleSimpleErrorHandler
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/"
system "curl -s {host}:{port}/about"
- return null
end
-end
-
-var app = new App
-app.use("/", new HelloHandler)
-app.use("/*", new SimpleErrorHandler)
-
-var host = test_host
-var port = test_port
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_param_route do
+ var app = new App
+ app.use("/", new HelloHandler)
+ app.use("/*", new SimpleErrorHandler)
+ run_test(app)
+ end
+end
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_simple_logger is test_suite
+
+import pop_tests
import example_simple_logger
-import base_tests
-class TestClient
- super ClientThread
+class TestExampleSimpleLogger
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/"
system "curl -s {host}:{port}/about"
- return null
end
-end
-
-var app = new App
-app.use_before("/*", new LogHandler)
-app.use("/", new HelloHandler)
-
-var host = test_host
-var port = test_port
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_param_route do
+ var app = new App
+ app.use_before("/*", new LogHandler)
+ app.use("/", new HelloHandler)
+ run_test(app)
+ end
+end
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_glob_route is test_suite
+
+import pop_tests
import example_glob_route
-import base_tests
-class TestClient
- super ClientThread
+class TestExampleGlobRoute
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/user/Morriar/item/10"
system "curl -s {host}:{port}/user/Morriar/item/10/"
system "curl -s {host}:{port}/user/Morriar/item/10/profile"
system "curl -s {host}:{port}/user/Morriar/item/10/profile/settings"
-
system "curl -s {host}:{port}/"
system "curl -s {host}:{port}/not_found"
system "curl -s {host}:{port}/not_found/not_found"
- return null
end
-end
-
-var app = new App
-app.use("/user/:user/item/:item/*", new UserItem)
-
-var host = test_host
-var port = test_port
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_glob_route do
+ var app = new App
+ app.use("/user/:user/item/:item/*", new UserItem)
+ run_test(app)
+ end
+end
<body>
<h1>404 Not Found</h1>
</body>
- </html>
\ No newline at end of file
+ </html>
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_param_route is test_suite
+
+import pop_tests
import example_param_route
-import base_tests
-class TestClient
- super ClientThread
+class TestExampleParamRoute
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/Morriar"
system "curl -s {host}:{port}//"
-
system "curl -s {host}:{port}/"
system "curl -s {host}:{port}/not_found"
system "curl -s {host}:{port}/not_found/not_found"
- return null
end
-end
-
-var app = new App
-app.use("/:user", new UserHome)
-
-var host = test_host
-var port = test_port
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_param_route do
+ var app = new App
+ app.use("/:user", new UserHome)
+ run_test(app)
+ end
+end
<body>
<h1>404 Not Found</h1>
</body>
- </html>
\ No newline at end of file
+ </html>
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_router is test_suite
+
+import pop_tests
import example_router
-import base_tests
-class HelloClient
- super ClientThread
+class TestExampleRouter
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}"
system "curl -s {host}:{port}/"
system "curl -s {host}:{port}/user"
system "curl -s {host}:{port}/user/"
system "curl -s {host}:{port}/user/profile"
-
system "curl -s {host}:{port}/not_found"
system "curl -s {host}:{port}/user/not_found"
system "curl -s {host}:{port}/products/not_found"
- return null
end
-end
-
-var user_router = new Router
-user_router.use("/*", new UserLogger)
-user_router.use("/", new UserHome)
-user_router.use("/profile", new UserProfile)
-
-var app = new App
-app.use("/", new AppHome)
-app.use("/user", user_router)
-var host = test_host
-var port = test_port
-
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new HelloClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_router do
+ var user_router = new Router
+ user_router.use("/*", new UserLogger)
+ user_router.use("/", new UserHome)
+ user_router.use("/profile", new UserProfile)
+ var app = new App
+ app.use("/", new AppHome)
+ app.use("/user", user_router)
+ run_test(app)
+ end
+end
<body>
<h1>404 Not Found</h1>
</body>
- </html>
\ No newline at end of file
+ </html>
# See the License for the specific language governing permissions and
# limitations under the License.
+module test_example_session is test_suite
+
+import pop_tests
import example_session
-import base_tests
-class HelloClient
- super ClientThread
+class TestExampleSession
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/"
system "curl -s {host}:{port}/ -X POST"
-
system "curl -s {host}:{port}/not_found"
system "curl -s {host}:{port}/user/not_found"
system "curl -s {host}:{port}/products/not_found"
- return null
end
-end
-
-var app = new App
-app.use("/*", new SessionInit)
-app.use("/", new AppLogin)
-
-var host = test_host
-var port = test_port
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new HelloClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+ fun test_example_param_route do
+ var app = new App
+ app.use("/*", new SessionInit)
+ app.use("/", new AppLogin)
+ run_test(app)
+ end
+end
# See the License for the specific language governing permissions and
# limitations under the License.
-import base_tests
+module test_example_static is test_suite
+
+import pop_tests
import example_static
-class TestClient
- super ClientThread
+class TestExampleStatic
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/css/style.css"
system "curl -s {host}:{port}/js/app.js"
system "curl -s {host}:{port}/hello.html"
system "curl -s {host}:{port}/"
-
system "curl -s {host}:{port}/css/not_found.nit"
system "curl -s {host}:{port}/static/css/not_found.nit"
system "curl -s {host}:{port}/not_found.nit"
+ end
- return null
+ fun test_example_param_route do
+ var app = new App
+ app.use("/", new StaticHandler("../examples/static_files/public/"))
+ run_test(app)
end
end
-
-var app = new App
-app.use("/", new StaticHandler("../examples/static_files/public/"))
-
-var host = test_host
-var port = test_port
-
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
# See the License for the specific language governing permissions and
# limitations under the License.
-import base_tests
+module test_example_static_default is test_suite
+
+import pop_tests
import example_static_default
-class TestClient
- super ClientThread
+class TestExampleStaticDefault
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/css/style.css"
system "curl -s {host}:{port}/js/app.js"
system "curl -s {host}:{port}/hello.html"
system "curl -s {host}:{port}/"
-
system "curl -s {host}:{port}/css/not_found.nit"
system "curl -s {host}:{port}/static/css/not_found.nit"
system "curl -s {host}:{port}/not_found.nit"
+ end
- return null
+ fun test_example_param_route do
+ var app = new App
+ app.use("/", new StaticHandler("../examples/static_files/public/", "default.html"))
+ run_test(app)
end
end
-
-var app = new App
-app.use("/", new StaticHandler("../examples/static_files/public/", "default.html"))
-
-var host = test_host
-var port = test_port
-
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
# See the License for the specific language governing permissions and
# limitations under the License.
-import base_tests
+module test_example_static_multiple is test_suite
+
+import pop_tests
import example_static_multiple
-class TestClient
- super ClientThread
+class TestExampleStaticMultiple
+ super TestPopcorn
- redef fun main do
+ redef fun client_test do
system "curl -s {host}:{port}/css/style.css"
system "curl -s {host}:{port}/js/app.js"
system "curl -s {host}:{port}/hello.html"
system "curl -s {host}:{port}/"
-
system "curl -s {host}:{port}/static/css/style.css"
system "curl -s {host}:{port}/static/js/app.js"
system "curl -s {host}:{port}/static/hello.html"
system "curl -s {host}:{port}/static/"
-
system "curl -s {host}:{port}/css/not_found.nit"
system "curl -s {host}:{port}/static/css/not_found.nit"
system "curl -s {host}:{port}/not_found.nit"
+ end
- return null
+ fun test_example_param_route do
+ var app = new App
+ app.use("/", new StaticHandler("../examples/static_files/public/"))
+ app.use("/", new StaticHandler("../examples/static_files/files/"))
+ app.use("/static", new StaticHandler("../examples/static_files/public/"))
+ app.use("/static", new StaticHandler("../examples/static_files/files/"))
+ run_test(app)
end
end
-
-var app = new App
-app.use("/", new StaticHandler("../examples/static_files/public/"))
-app.use("/", new StaticHandler("../examples/static_files/files/"))
-app.use("/static", new StaticHandler("../examples/static_files/public/"))
-app.use("/static", new StaticHandler("../examples/static_files/files/"))
-
-var host = test_host
-var port = test_port
-
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new TestClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
--- /dev/null
+# 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.
+
+# Popcorn testing services
+#
+# ## Blackbox testing
+#
+# Popcorn allows you to test your apps using nitunit blackbox testing.
+#
+# With blackbox testing you compare the output of your program with a result file.
+#
+# To get started with blackbox testing, create a nitunit test suite and imports
+# the `pop_tests` module.
+#
+# You then need to build the app that will be tested by nitunit as shown in the
+# `test_example_hello` method.
+# Calling `run_test` will automatically set the `host` and `port` used for testing.
+#
+# Redefine the `client_test` method to write your scenario.
+# Here we use `curl` to access some URI on the app.
+#
+# ~~~nitish
+# module test_example_hello is test_suite
+#
+# import pop_tests
+# import example_hello
+#
+# class TestExampleHello
+# super TestPopcorn
+#
+# fun test_example_hello do
+# var app = new App
+# app.use("/", new HelloHandler)
+# run_test(app)
+# end
+#
+# redef fun client_test do
+# system "curl -s {host}:{port}"
+# system "curl -s {host}:{port}/"
+# system "curl -s {host}:{port}///////////"
+# system "curl -s {host}:{port}/not_found"
+# system "curl -s {host}:{port}/not_found/not_found"
+# end
+# end
+# ~~~
+#
+# The blackbox testing needs a reference result file against wich the test output
+# will be compared.
+# Create your expected result file in `test_example_hello.sav/test_example_hello.res`.
+#
+# Test your app by running nitunit:
+#
+# ~~~bash
+# nitunit ./example_hello.nit
+# ~~~
+#
+# See `examples/hello_world` for the complete example.
+module pop_tests
+
+import test_suite
+import popcorn
+import pthreads
+
+redef class Sys
+
+ # Use localhost for testing
+ var test_host = "localhost"
+
+ # Return a new port for each instance
+ fun test_port: Int do
+ srand
+ return 10000+20000.rand
+ end
+end
+
+# Thread running the App to test.
+class AppThread
+ super Thread
+
+ # Host used by tested App.
+ var host: String
+
+ # Port used by tested App.
+ var port: Int
+
+ # App to test.
+ var app: App
+
+ redef fun main
+ do
+ # Hide testing concept to force nitcorn to actually run
+ "NIT_TESTING".setenv("false")
+ app.quiet = true
+ app.listen(host, port)
+ return null
+ end
+end
+
+# Thread running the test client.
+class ClientThread
+ super Thread
+
+ # Test suite to execute.
+ var test_suite: TestPopcorn
+
+ redef fun main do
+ test_suite.client_test
+ print ""
+ return null
+ end
+end
+
+# TestSuite for Popcorn blackbox testing.
+class TestPopcorn
+ super TestSuite
+
+ # Host used to run App.
+ var host: String = test_host
+
+ # Port used to run App.
+ var port: Int = test_port
+
+ # Run the test suite on the App.
+ fun run_test(app: App) do
+ var server = new AppThread(host, port, app)
+ server.start
+ 0.1.sleep
+
+ var client = new ClientThread(self)
+ client.start
+ client.join
+ 0.1.sleep
+
+ exit 0
+ end
+
+ # Redefine this method to implement your test scenario.
+ fun client_test do end
+
+ # Regex to catch and hide the port from the output to get consistent results
+ var host_re: Regex = "localhost:\[0-9\]+".to_re
+
+ # Execute a System function.
+ 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
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2016 Alexandre Terrasa <alexandre@moz-code.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.
+
+module test_popcorn is test_suite
+
+import pop_tests
+import popcorn
+
+class TestHandler
+ super Handler
+
+ var marker: String
+
+ redef fun get(req, res) do res.send marker
+end
+
+class TestPopcornRouter
+ super TestPopcorn
+
+ redef fun client_test do
+ system "curl -s {host}:{port}"
+ system "curl -s {host}:{port}/"
+ system "curl -s {host}:{port}/user"
+ system "curl -s {host}:{port}/user/"
+ system "curl -s {host}:{port}/user/settings"
+ system "curl -s {host}:{port}/products"
+ system "curl -s {host}:{port}/products/"
+ system "curl -s {host}:{port}/products/list"
+ system "curl -s {host}:{port}/not_found"
+ system "curl -s {host}:{port}/user/not_found"
+ system "curl -s {host}:{port}/products/not_found"
+ end
+
+ fun test_router do
+ var app = new App
+ app.use("/", new TestHandler("/"))
+ app.use("/about", new TestHandler("/about"))
+
+ var router1 = new App
+ router1.use("/", new TestHandler("/user"))
+ router1.use("/settings", new TestHandler("/user/settings"))
+ app.use("/user", router1)
+
+ var router2 = new App
+ router2.use("/", new TestHandler("/products"))
+ router2.use("/list", new TestHandler("/products/list"))
+ app.use("/products", router2)
+
+ run_test(app)
+ end
+end
+
+class TestPopcornRoutes
+ super TestPopcorn
+
+ redef fun client_test do
+ system "curl -s {host}:{port}"
+ system "curl -s {host}:{port}/"
+ system "curl -s {host}:{port}/misc"
+ system "curl -s {host}:{port}/misc/foo"
+ system "curl -s {host}:{port}/misc/foo/bar"
+ system "curl -s {host}:{port}/misc/foo/baz"
+ system "curl -s {host}:{port}/user"
+ system "curl -s {host}:{port}/user/"
+ system "curl -s {host}:{port}/user/id"
+ system "curl -s {host}:{port}/user/id/profile"
+ system "curl -s {host}:{port}/user/id/misc/foo"
+ system "curl -s {host}:{port}/user/id/misc/foo/bar"
+ system "curl -s {host}:{port}/user/id/misc/foo/bar/baz"
+ system "curl -s {host}:{port}/not_found"
+ system "curl -s {host}:{port}/user/id/not_found"
+ end
+
+ fun test_routes do
+ var app = new App
+ app.use("/", new TestHandler("/"))
+ app.use("/user", new TestHandler("/user"))
+ app.use("/misc/*", new TestHandler("/misc/everything"))
+ app.use("/user/:id", new TestHandler("/user/id"))
+ app.use("/user/:id/profile", new TestHandler("/user/id/profile"))
+ app.use("/user/:id/misc/*", new TestHandler("/user/id/misc/everything"))
+ run_test(app)
+ end
+end
<body>
<h1>404 Not Found</h1>
</body>
- </html>
\ No newline at end of file
+ </html>
<body>
<h1>404 Not Found</h1>
</body>
- </html>
\ No newline at end of file
+ </html>
+++ /dev/null
-# Copyright 2013 Alexandre Terrasa <alexandre@moz-code.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.
-
-all: tests
-
-check: clean
- ./tests.sh
-
-clean:
- rm -rf out/
+++ /dev/null
-# 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 popcorn
-import pthreads
-
-redef class Sys
- var test_host = "localhost"
-
- # Return a new port for each instance
- fun test_port: Int do
- srand
- return 10000+20000.rand
- end
-end
-
-class AppThread
- super Thread
-
- var host: String
- var port: Int
- var app: App
-
- redef fun main
- do
- # Hide testing concept to force nitcorn to actually run
- "NIT_TESTING".setenv("false")
- app.quiet = true
- app.listen(host, port)
- return null
- end
-end
-
-class ClientThread
- super Thread
-
- var host: String
- var port: Int
-
- redef fun main do return null
-
- # 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
+++ /dev/null
-# This file is part of NIT ( http://www.nitlanguage.org ).
-#
-# Copyright 2016 Alexandre Terrasa <alexandre@moz-code.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 base_tests
-
-class TestHandler
- super Handler
-
- var marker: String
-
- redef fun get(req, res) do res.send marker
-end
-
-class HelloClient
- super ClientThread
-
- redef fun main do
- system "curl -s {host}:{port}"
- system "curl -s {host}:{port}/"
- system "curl -s {host}:{port}/user"
- system "curl -s {host}:{port}/user/"
- system "curl -s {host}:{port}/user/settings"
- system "curl -s {host}:{port}/products"
- system "curl -s {host}:{port}/products/"
- system "curl -s {host}:{port}/products/list"
-
- system "curl -s {host}:{port}/not_found"
- system "curl -s {host}:{port}/user/not_found"
- system "curl -s {host}:{port}/products/not_found"
- return null
- end
-end
-
-var app = new App
-app.use("/", new TestHandler("/"))
-app.use("/about", new TestHandler("/about"))
-
-var router1 = new App
-router1.use("/", new TestHandler("/user"))
-router1.use("/settings", new TestHandler("/user/settings"))
-app.use("/user", router1)
-
-var router2 = new App
-router2.use("/", new TestHandler("/products"))
-router2.use("/list", new TestHandler("/products/list"))
-app.use("/products", router2)
-
-var host = test_host
-var port = test_port
-
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-var client = new HelloClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-exit 0
+++ /dev/null
-# This file is part of NIT ( http://www.nitlanguage.org ).
-#
-# Copyright 2016 Alexandre Terrasa <alexandre@moz-code.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 base_tests
-
-class TestHandler
- super Handler
-
- var marker: String
-
- redef fun get(req, res) do res.send marker
-end
-
-class HelloClient
- super ClientThread
-
- redef fun main do
- system "curl -s {host}:{port}"
- system "curl -s {host}:{port}/"
-
- system "curl -s {host}:{port}/misc"
- system "curl -s {host}:{port}/misc/foo"
- system "curl -s {host}:{port}/misc/foo/bar"
- system "curl -s {host}:{port}/misc/foo/baz"
-
- system "curl -s {host}:{port}/user"
- system "curl -s {host}:{port}/user/"
- system "curl -s {host}:{port}/user/id"
- system "curl -s {host}:{port}/user/id/profile"
- system "curl -s {host}:{port}/user/id/misc/foo"
- system "curl -s {host}:{port}/user/id/misc/foo/bar"
- system "curl -s {host}:{port}/user/id/misc/foo/bar/baz"
-
- system "curl -s {host}:{port}/not_found"
- system "curl -s {host}:{port}/user/id/not_found"
- return null
- end
-end
-
-var app = new App
-app.use("/", new TestHandler("/"))
-app.use("/user", new TestHandler("/user"))
-app.use("/misc/*", new TestHandler("/misc/everything"))
-app.use("/user/:id", new TestHandler("/user/id"))
-app.use("/user/:id/profile", new TestHandler("/user/id/profile"))
-app.use("/user/:id/misc/*", new TestHandler("/user/id/misc/everything"))
-
-var host = test_host
-var port = test_port
-
-# First, launch a server in the background
-var server = new AppThread(host, port, app)
-server.start
-0.1.sleep
-
-# Then, launch a client running test requests
-var client = new HelloClient(host, port)
-client.start
-client.join
-0.1.sleep
-
-# Force quit the server
-exit 0
+++ /dev/null
-#!/bin/bash
-
-# This file is part of NIT ( http://www.nitlanguage.org ).
-#
-# Copyright 2016 Alexandre Terrasa <alexandre@moz-code.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.
-
-BIN=../bin
-OUT=./out
-RES=./res
-
-NITC=../../../bin/nitc
-
-compile() {
- local test="$1"
- $NITC $test.nit -o $OUT/$test.bin 1>&2 2> $OUT/$test.cmp_err
-}
-
-test_prog()
-{
- local test="$1"
-
- chmod +x $OUT/$test.bin 2> $OUT/$test.err
- $OUT/$test.bin > $OUT/$test.res 2> $OUT/$test.err
-
- diff $OUT/$test.res $RES/$test.res > $OUT/$test.diff 2> /dev/null
-}
-
-# return
-# 0 if the sav not exists
-# 1 if the file does match
-# 2 if the file does not match
-check_result() {
- local test="$1"
-
- if [ -s "$OUT/$test.cmp_err" ]; then
- return 0
- elif [ -s "$OUT/$test.err" ]; then
- return 1
- elif [ ! -r "$RES/$test.res" ]; then
- return 2
- elif [ -s "$OUT/$test.diff" ]; then
- return 3
- else
- return 4
- fi
-}
-
-echo "Testing..."
-echo ""
-
-rm -rf $OUT 2>/dev/null
-mkdir $OUT 2>/dev/null
-
-all=0
-ok=0
-ko=0
-sk=0
-
-for file in `ls test_*.nit`; do
- ((all++))
- test="${file%.*}"
- echo -n "* $test: "
-
- compile $test
- test_prog $test
- check_result $test
-
- case "$?" in
- 0)
- echo "compile error (cat $OUT/$test.cmp_err)"
- ((ko++))
- ;;
- 1)
- echo "error (cat $OUT/$test.cmp_err)"
- ((ko++))
- ;;
- 2)
- echo "skip ($test.res not found)"
- ((sk++))
- continue;;
- 3)
- echo "error (diff $OUT/$test.res $RES/$test.res)"
- ((ko++))
- ;;
- 4)
- echo "success"
- ((ok++))
- ;;
-
- esac
-done
-echo ""
-echo "==> success $ok/$all ($ko tests failed, $sk skipped)"
-
-# return result
-test "$ok" == "$all"