diff options
author | Pixel <pixel@nobis-crew.org> | 2011-11-17 18:41:23 -0800 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2011-11-17 18:41:51 -0800 |
commit | 1b7a57d7f88cab0a1968e8c886eac3629dc74617 (patch) | |
tree | 3d60366ee3caa2ec3061a42abd2a61c78a1fa5d2 /tests | |
parent | e617d26a9291c69988321a812dc5e1f3578e743b (diff) |
HTTP server's first real test, alongside multiple taskmanager threads.
I'm not really sure I fully like the way I'm designing this, but I guess it could be solved with an HTTP/HTML helper class around the Action class.
However, the HTTP server awfully need reference counting, so it doesn't go away before all of the workers disappear, which means a bit of a redesign of the Listener template.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-Http.cc | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/test-Http.cc b/tests/test-Http.cc new file mode 100644 index 0000000..496d7e6 --- /dev/null +++ b/tests/test-Http.cc @@ -0,0 +1,62 @@ +#include <Main.h> +#include <HttpServer.h> + +BALAU_STARTUP; + +#define DAEMON_NAME "Balau/1.0" + +using namespace Balau; + +class TestAction : public HttpServer::Action { + public: + TestAction() : Action(Regexes::any) { } + virtual bool Do(HttpServer * server, HttpServer::Action::ActionMatches & m, IO<Handle> out, HttpServer::StringMap & vars, HttpServer::StringMap & headers, HttpServer::FileList & files); +}; + +bool TestAction::Do(HttpServer * server, HttpServer::Action::ActionMatches & m, IO<Handle> out, HttpServer::StringMap & vars, HttpServer::StringMap & headers, HttpServer::FileList & files) { + static const char str[] = +"HTTP/1.1 200 Found\r\n" +"Content-Type: text/html; charset=UTF-8\r\n" +"Content-Length: 266\r\n" +"Server: " DAEMON_NAME "\r\n" +"\r\n" +"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" +"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +" <head>\n" +" <title>Test</title>\n" +" </head>\n" +"\n" +" <body>\n" +" This is a test document.\n" +" </body>\n" +"</html>\n"; + + out->forceWrite(str, sizeof(str) - 1); + return true; +} + +void MainTask::Do() { + Printer::log(M_STATUS, "Test::Http running."); + + Thread * tms[4]; + + for (int i = 0; i < 4; i++) + tms[i] = TaskMan::createThreadedTaskMan(); + + HttpServer * s = new HttpServer(); + TestAction * a = new TestAction(); + a->registerMe(s); + s->setPort(8080); + s->setLocal("localhost"); + s->start(); + + yield(); + + s->stop(); + + for (int i = 0; i < 4; i++) + tms[i]->join(); + + Printer::log(M_STATUS, "Test::Http passed."); +} |