From 1b7a57d7f88cab0a1968e8c886eac3629dc74617 Mon Sep 17 00:00:00 2001 From: Pixel Date: Thu, 17 Nov 2011 18:41:23 -0800 Subject: 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. --- tests/test-Http.cc | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/test-Http.cc (limited to 'tests') 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 +#include + +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 out, HttpServer::StringMap & vars, HttpServer::StringMap & headers, HttpServer::FileList & files); +}; + +bool TestAction::Do(HttpServer * server, HttpServer::Action::ActionMatches & m, IO 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" +"\n" +"\n" +" \n" +" Test\n" +" \n" +"\n" +" \n" +" This is a test document.\n" +" \n" +"\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."); +} -- cgit v1.2.3