http_rewrite using HTML parser
[metaproxy-moved-to-github.git] / src / test_html_parser.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20 #include <iostream>
21 #include <stdexcept>
22
23 #include "html_parser.hpp"
24 #include <metaproxy/util.hpp>
25
26 #include <boost/lexical_cast.hpp>
27
28 #include <yaz/log.h>
29
30 #define BOOST_AUTO_TEST_MAIN
31 #define BOOST_TEST_DYN_LINK
32
33 #include <boost/test/auto_unit_test.hpp>
34
35 using namespace boost::unit_test;
36 namespace mp = metaproxy_1;
37
38 class MyEvent : public mp::HTMLParserEvent {
39     public:
40         std::string out;
41         void openTagStart(const char *name)
42         {
43             out += "<";
44             out += name;
45         } 
46         
47         void attribute(const char *tagName, 
48                 const char *name, const char *value, int val_len)
49         {
50             out += " ";
51             out += name;
52             out += "=\"";
53             out.append(value, val_len);
54             out += "\"";
55         }
56
57         void anyTagEnd(const char *name, int close_it)
58         {
59             if (close_it)
60                 out += "/";
61             out += ">";
62         }
63         
64         void closeTag(const char *name)
65         {
66             out += "</";
67             out += name;
68         }
69         
70         void text(const char *value, int len)
71         {
72             out.append(value, len);
73         }
74 };
75
76
77 BOOST_AUTO_TEST_CASE( test_html_parser_1 )
78 {
79     try
80     {
81         mp::HTMLParser hp;
82         const char* html = 
83             "<html><body><a t1=v1 t2='v2' t3=\"v3\">some text</a>"
84             "<hr><table ></table  ><a href=\"x\"/></body></html>";
85         const char* expected = 
86             "<html><body><a t1=\"v1\" t2=\"v2\" t3=\"v3\">some text</a>"
87             "<hr><table></table><a href=\"x\"/></body></html>";
88         MyEvent e;
89         hp.parse(e, html);
90
91         std::cout << expected << std::endl;
92         std::cout << e.out << std::endl;
93         BOOST_CHECK_EQUAL(std::string(expected), e.out);
94     }
95     catch (std::exception & e) 
96     {
97         std::cout << e.what();
98         std::cout << std::endl;
99         BOOST_CHECK (false);
100     }
101 }
102
103
104 /*
105  * Local variables:
106  * c-basic-offset: 4
107  * c-file-style: "Stroustrup"
108  * indent-tabs-mode: nil
109  * End:
110  * vim: shiftwidth=4 tabstop=8 expandtab
111  */
112