Fix Metaproxy stops logging after check config failed MP-590
[metaproxy-moved-to-github.git] / src / test_html_parser.cpp
index 5659152..5ba7e5a 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Metaproxy.
-   Copyright (C) 2005-2013 Index Data
+   Copyright (C) Index Data
 
 Metaproxy is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -42,16 +42,20 @@ public:
     void openTagStart(const char *tag, int tag_len) {
         out += "<";
         out.append(tag, tag_len);
-    } 
-    
+    }
+
     void attribute(const char *tag, int tag_len,
                    const char *attr, int attr_len,
-                   const char *value, int val_len) {
+                   const char *value, int val_len, const char *sep) {
         out += " ";
         out.append(attr, attr_len);
-        out += "=\"";
-        out.append(value, val_len);
-        out += "\"";
+        if (value)
+        {
+            out += "=";
+            out += sep;
+            out.append(value, val_len);
+            out += sep;
+        }
     }
     void anyTagEnd(const char *tag, int tag_len, int close_it) {
         if (close_it)
@@ -67,29 +71,31 @@ public:
     }
 };
 
-
 BOOST_AUTO_TEST_CASE( test_html_parser_1 )
 {
     try
     {
         mp::HTMLParser hp;
-        const char* html = 
+        const char* html =
             "<html><body><a t1=v1 t2='v2' t3=\"v3\">some text</a>"
             "<hr><table ></table  ><a href=\"x\"/></body></html>";
-        const char* expected = 
-            "<html><body><a t1=\"v1\" t2=\"v2\" t3=\"v3\">some text</a>"
-            "<hr><table></table><a href=\"x\"/></body></html>";
+        const char* expected =
+            "<html><body><a t1=v1 t2='v2' t3=\"v3\">some text</a>"
+            "<hr><table></table  ><a href=\"x\"/></body></html>";
         MyEvent e;
-        hp.set_verbose(1);
+        hp.set_verbose(0);
         hp.parse(e, html);
 
-        std::cout << "Expected" << std::endl;
-        std::cout << expected << std::endl;
-        std::cout << "Got" << std::endl;
-        std::cout << e.out << std::endl;
         BOOST_CHECK_EQUAL(std::string(expected), e.out);
+        if (std::string(expected) != e.out)
+        {
+            std::cout << "Expected" << std::endl;
+            std::cout << expected << std::endl;
+            std::cout << "Got" << std::endl;
+            std::cout << e.out << std::endl;
+        }
     }
-    catch (std::exception & e) 
+    catch (std::exception & e)
     {
         std::cout << e.what();
         std::cout << std::endl;
@@ -102,7 +108,7 @@ BOOST_AUTO_TEST_CASE( test_html_parser_2 )
     try
     {
         mp::HTMLParser hp;
-        const char* html = 
+        const char* html =
             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"
             "<HTML>\n"
             " <HEAD>\n"
@@ -117,17 +123,19 @@ BOOST_AUTO_TEST_CASE( test_html_parser_2 )
 
         const char* expected = html;
         MyEvent e;
-        hp.set_verbose(1);
+        hp.set_verbose(0);
         hp.parse(e, html);
 
-        std::cout << "Expected" << std::endl;
-        std::cout << expected << std::endl;
-        std::cout << "Got" << std::endl;
-        std::cout << e.out << std::endl;
-
         BOOST_CHECK_EQUAL(std::string(expected), e.out);
+        if (std::string(expected) != e.out)
+        {
+            std::cout << "Expected" << std::endl;
+            std::cout << expected << std::endl;
+            std::cout << "Got" << std::endl;
+            std::cout << e.out << std::endl;
+        }
     }
-    catch (std::exception & e) 
+    catch (std::exception & e)
     {
         std::cout << e.what();
         std::cout << std::endl;
@@ -152,17 +160,19 @@ BOOST_AUTO_TEST_CASE( test_html_parser_3 )
 
         const char* expected = html;
         MyEvent e;
-        hp.set_verbose(1);
+        hp.set_verbose(0);
         hp.parse(e, html);
 
-        std::cout << "Expected" << std::endl;
-        std::cout << expected << std::endl;
-        std::cout << "Got" << std::endl;
-        std::cout << e.out << std::endl;
-
         BOOST_CHECK_EQUAL(std::string(expected), e.out);
+        if (std::string(expected) != e.out)
+        {
+            std::cout << "Expected" << std::endl;
+            std::cout << expected << std::endl;
+            std::cout << "Got" << std::endl;
+            std::cout << e.out << std::endl;
+        }
     }
-    catch (std::exception & e) 
+    catch (std::exception & e)
     {
         std::cout << e.what();
         std::cout << std::endl;
@@ -170,37 +180,129 @@ BOOST_AUTO_TEST_CASE( test_html_parser_3 )
     }
 }
 
-#if 0
-// null ptr exception
 BOOST_AUTO_TEST_CASE( test_html_parser_4 )
 {
     try
     {
         mp::HTMLParser hp;
         const char* html =
-            "<\"?xml version=\"1.0\" strandalone=\"no\"?>\n"
-            "<book></book>";
+            "<?xml version=\"1.0\" strandalone=\"no\"?  ax>\n"
+            "<book><x ? href/><!-- hello > --></book>";
+
+        const char* expected = html;
+        MyEvent e;
+        hp.set_verbose(0);
+        hp.parse(e, html);
+
+        BOOST_CHECK_EQUAL(std::string(expected), e.out);
+        if (std::string(expected) != e.out)
+        {
+            std::cout << "Expected" << std::endl;
+            std::cout << expected << std::endl;
+            std::cout << "Got" << std::endl;
+            std::cout << e.out << std::endl;
+        }
+    }
+    catch (std::exception & e)
+    {
+        std::cout << e.what();
+        std::cout << std::endl;
+        BOOST_CHECK (false);
+    }
+}
+
+BOOST_AUTO_TEST_CASE( test_html_parser_5 )
+{
+    try
+    {
+        mp::HTMLParser hp;
+        const char* html =
+            "<x link/>";
+
+        const char* expected = html;
+        MyEvent e;
+        hp.set_verbose(0);
+        hp.parse(e, html);
+
+        BOOST_CHECK_EQUAL(std::string(expected), e.out);
+        if (std::string(expected) != e.out)
+        {
+            std::cout << "Expected" << std::endl;
+            std::cout << expected << std::endl;
+            std::cout << "Got" << std::endl;
+            std::cout << e.out << std::endl;
+        }
+    }
+    catch (std::exception & e)
+    {
+        std::cout << e.what();
+        std::cout << std::endl;
+        BOOST_CHECK (false);
+    }
+}
+
+BOOST_AUTO_TEST_CASE( test_html_parser_6 )
+{
+    try
+    {
+        mp::HTMLParser hp;
+        const char* html =
+            "<html><script><x;</script></html>";
 
         const char* expected = html;
         MyEvent e;
-        hp.set_verbose(1);
+        hp.set_verbose(0);
         hp.parse(e, html);
 
-        std::cout << "Expected" << std::endl;
-        std::cout << expected << std::endl;
-        std::cout << "Got" << std::endl;
-        std::cout << e.out << std::endl;
+        BOOST_CHECK_EQUAL(std::string(expected), e.out);
+        if (std::string(expected) != e.out)
+        {
+            std::cout << "Expected" << std::endl;
+            std::cout << expected << std::endl;
+            std::cout << "Got" << std::endl;
+            std::cout << e.out << std::endl;
+        }
+    }
+    catch (std::exception & e)
+    {
+        std::cout << e.what();
+        std::cout << std::endl;
+        BOOST_CHECK (false);
+    }
+}
+
+BOOST_AUTO_TEST_CASE( test_html_parser_7 )
+{
+    try
+    {
+        mp::HTMLParser hp;
+        const char* html =
+            "<html><Script>x=1; for (i=0;i<x;i++) </y>;"
+            "</SCRIPT ;>"
+            "</1>\nx=2;\n</Script></html>";
+
+        const char* expected = html;
+        MyEvent e;
+        hp.set_verbose(0);
+        hp.parse(e, html);
 
         BOOST_CHECK_EQUAL(std::string(expected), e.out);
+        if (std::string(expected) != e.out)
+        {
+            std::cout << "Expected" << std::endl;
+            std::cout << expected << std::endl;
+            std::cout << "Got" << std::endl;
+            std::cout << e.out << std::endl;
+        }
     }
-    catch (std::exception & e) 
+    catch (std::exception & e)
     {
         std::cout << e.what();
         std::cout << std::endl;
         BOOST_CHECK (false);
     }
 }
-#endif
+
 
 /*
  * Local variables: