From b7c4ac9269e8321ac21f068c9238b4619ab6be4d Mon Sep 17 00:00:00 2001 From: Pixel Date: Fri, 6 Apr 2012 13:51:00 -0700 Subject: Making use of SimpleMustache templates into the http test. --- tests/test-Http.cc | 67 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 25 deletions(-) (limited to 'tests') diff --git a/tests/test-Http.cc b/tests/test-Http.cc index c387168..d4848e7 100644 --- a/tests/test-Http.cc +++ b/tests/test-Http.cc @@ -2,11 +2,43 @@ #include #include #include +#include #define DAEMON_NAME "Balau/1.0" using namespace Balau; +const char htmlTemplateStr[] = +"\n" +"\n" +" \n" +" \n" +" {{title}}\n" +" \n" +" \n" +"\n" +" \n" +"

{{title}}

\n" +"

{{msg}}

\n" +" \n" +"\n" +; + +class TestHtmlTemplate : public AtStart { + public: + TestHtmlTemplate() : AtStart(10), htmlTemplate(m_template) { } + virtual void doStart() { m_template.setTemplate(htmlTemplateStr); } + + const SimpleMustache & htmlTemplate; + private: + SimpleMustache m_template; +}; + +static TestHtmlTemplate testHtmlTemplate; + static Regex stopURL("/stop$"); class StopAction : public HttpServer::Action { @@ -21,21 +53,13 @@ class StopAction : public HttpServer::Action { bool StopAction::Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO out) throw (GeneralException) { m_stop = true; m_event.trigger(); - + SimpleMustache::Context ctx; HttpServer::Response response(server, req, out); - response->writeString( -"\n" -"\n" -" \n" -" Stop\n" -" \n" -"\n" -" \n" -" Server stopping.\n" -" \n" -"\n"); + ctx["title"] = "Stop"; + ctx["msg"] = "Server stopping"; + + testHtmlTemplate.htmlTemplate.render(response.get(), &ctx); response.Flush(); return true; } @@ -48,20 +72,13 @@ class TestAction : public HttpServer::Action { }; bool TestAction::Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO out) throw (GeneralException) { + SimpleMustache::Context ctx; HttpServer::Response response(server, req, out); - response->writeString( -"\n" -"\n" -" \n" -" Test\n" -" \n" -"\n" -" \n" -" This is a test document.\n" -" \n" -"\n"); + ctx["title"] = "Test"; + ctx["msg"] = "This is a test document."; + + testHtmlTemplate.htmlTemplate.render(response.get(), &ctx); response.Flush(); return true; } -- cgit v1.2.3