Fix Metaproxy stops logging after check config failed MP-590
[metaproxy-moved-to-github.git] / src / test_html_parser.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 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 {
40 public:
41     std::string out;
42     void openTagStart(const char *tag, int tag_len) {
43         out += "<";
44         out.append(tag, tag_len);
45     }
46
47     void attribute(const char *tag, int tag_len,
48                    const char *attr, int attr_len,
49                    const char *value, int val_len, const char *sep) {
50         out += " ";
51         out.append(attr, attr_len);
52         if (value)
53         {
54             out += "=";
55             out += sep;
56             out.append(value, val_len);
57             out += sep;
58         }
59     }
60     void anyTagEnd(const char *tag, int tag_len, int close_it) {
61         if (close_it)
62             out += "/";
63         out += ">";
64     }
65     void closeTag(const char *tag, int tag_len) {
66         out += "</";
67         out.append(tag, tag_len);
68     }
69     void text(const char *value, int len) {
70         out.append(value, len);
71     }
72 };
73
74 BOOST_AUTO_TEST_CASE( test_html_parser_1 )
75 {
76     try
77     {
78         mp::HTMLParser hp;
79         const char* html =
80             "<html><body><a t1=v1 t2='v2' t3=\"v3\">some text</a>"
81             "<hr><table ></table  ><a href=\"x\"/></body></html>";
82         const char* expected =
83             "<html><body><a t1=v1 t2='v2' t3=\"v3\">some text</a>"
84             "<hr><table></table  ><a href=\"x\"/></body></html>";
85         MyEvent e;
86         hp.set_verbose(0);
87         hp.parse(e, html);
88
89         BOOST_CHECK_EQUAL(std::string(expected), e.out);
90         if (std::string(expected) != e.out)
91         {
92             std::cout << "Expected" << std::endl;
93             std::cout << expected << std::endl;
94             std::cout << "Got" << std::endl;
95             std::cout << e.out << std::endl;
96         }
97     }
98     catch (std::exception & e)
99     {
100         std::cout << e.what();
101         std::cout << std::endl;
102         BOOST_CHECK (false);
103     }
104 }
105
106 BOOST_AUTO_TEST_CASE( test_html_parser_2 )
107 {
108     try
109     {
110         mp::HTMLParser hp;
111         const char* html =
112             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"
113             "<HTML>\n"
114             " <HEAD>\n"
115             "  <TITLE>YAZ 4.2.60</TITLE>\n"
116             " </HEAD>\n"
117             " <BODY>\n"
118             "  <P><A HREF=\"http://www.indexdata.com/yaz/\">YAZ</A> 4.2.60</P>\n"
119             "  <P>Error: 404</P>\n"
120             "  <P>Description: Not Found</P>\n"
121             " </BODY>\n"
122             "</HTML>";
123
124         const char* expected = html;
125         MyEvent e;
126         hp.set_verbose(0);
127         hp.parse(e, html);
128
129         BOOST_CHECK_EQUAL(std::string(expected), e.out);
130         if (std::string(expected) != e.out)
131         {
132             std::cout << "Expected" << std::endl;
133             std::cout << expected << std::endl;
134             std::cout << "Got" << std::endl;
135             std::cout << e.out << std::endl;
136         }
137     }
138     catch (std::exception & e)
139     {
140         std::cout << e.what();
141         std::cout << std::endl;
142         BOOST_CHECK (false);
143     }
144 }
145
146 BOOST_AUTO_TEST_CASE( test_html_parser_3 )
147 {
148     try
149     {
150         mp::HTMLParser hp;
151         const char* html =
152             "<?xml version=\"1.0\" strandalone=\"no\"?>\n"
153             "<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.4//EN\"\n"
154             "  \"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd\"\n"
155             "[\n"
156             " <!ENTITY % local SYSTEM \"local.ent\">\n"
157             " %local;\n"
158             "]>\n"
159             "<book></book>";
160
161         const char* expected = html;
162         MyEvent e;
163         hp.set_verbose(0);
164         hp.parse(e, html);
165
166         BOOST_CHECK_EQUAL(std::string(expected), e.out);
167         if (std::string(expected) != e.out)
168         {
169             std::cout << "Expected" << std::endl;
170             std::cout << expected << std::endl;
171             std::cout << "Got" << std::endl;
172             std::cout << e.out << std::endl;
173         }
174     }
175     catch (std::exception & e)
176     {
177         std::cout << e.what();
178         std::cout << std::endl;
179         BOOST_CHECK (false);
180     }
181 }
182
183 BOOST_AUTO_TEST_CASE( test_html_parser_4 )
184 {
185     try
186     {
187         mp::HTMLParser hp;
188         const char* html =
189             "<?xml version=\"1.0\" strandalone=\"no\"?  ax>\n"
190             "<book><x ? href/><!-- hello > --></book>";
191
192         const char* expected = html;
193         MyEvent e;
194         hp.set_verbose(0);
195         hp.parse(e, html);
196
197         BOOST_CHECK_EQUAL(std::string(expected), e.out);
198         if (std::string(expected) != e.out)
199         {
200             std::cout << "Expected" << std::endl;
201             std::cout << expected << std::endl;
202             std::cout << "Got" << std::endl;
203             std::cout << e.out << std::endl;
204         }
205     }
206     catch (std::exception & e)
207     {
208         std::cout << e.what();
209         std::cout << std::endl;
210         BOOST_CHECK (false);
211     }
212 }
213
214 BOOST_AUTO_TEST_CASE( test_html_parser_5 )
215 {
216     try
217     {
218         mp::HTMLParser hp;
219         const char* html =
220             "<x link/>";
221
222         const char* expected = html;
223         MyEvent e;
224         hp.set_verbose(0);
225         hp.parse(e, html);
226
227         BOOST_CHECK_EQUAL(std::string(expected), e.out);
228         if (std::string(expected) != e.out)
229         {
230             std::cout << "Expected" << std::endl;
231             std::cout << expected << std::endl;
232             std::cout << "Got" << std::endl;
233             std::cout << e.out << std::endl;
234         }
235     }
236     catch (std::exception & e)
237     {
238         std::cout << e.what();
239         std::cout << std::endl;
240         BOOST_CHECK (false);
241     }
242 }
243
244 BOOST_AUTO_TEST_CASE( test_html_parser_6 )
245 {
246     try
247     {
248         mp::HTMLParser hp;
249         const char* html =
250             "<html><script><x;</script></html>";
251
252         const char* expected = html;
253         MyEvent e;
254         hp.set_verbose(0);
255         hp.parse(e, html);
256
257         BOOST_CHECK_EQUAL(std::string(expected), e.out);
258         if (std::string(expected) != e.out)
259         {
260             std::cout << "Expected" << std::endl;
261             std::cout << expected << std::endl;
262             std::cout << "Got" << std::endl;
263             std::cout << e.out << std::endl;
264         }
265     }
266     catch (std::exception & e)
267     {
268         std::cout << e.what();
269         std::cout << std::endl;
270         BOOST_CHECK (false);
271     }
272 }
273
274 BOOST_AUTO_TEST_CASE( test_html_parser_7 )
275 {
276     try
277     {
278         mp::HTMLParser hp;
279         const char* html =
280             "<html><Script>x=1; for (i=0;i<x;i++) </y>;"
281             "</SCRIPT ;>"
282             "</1>\nx=2;\n</Script></html>";
283
284         const char* expected = html;
285         MyEvent e;
286         hp.set_verbose(0);
287         hp.parse(e, html);
288
289         BOOST_CHECK_EQUAL(std::string(expected), e.out);
290         if (std::string(expected) != e.out)
291         {
292             std::cout << "Expected" << std::endl;
293             std::cout << expected << std::endl;
294             std::cout << "Got" << std::endl;
295             std::cout << e.out << std::endl;
296         }
297     }
298     catch (std::exception & e)
299     {
300         std::cout << e.what();
301         std::cout << std::endl;
302         BOOST_CHECK (false);
303     }
304 }
305
306
307 /*
308  * Local variables:
309  * c-basic-offset: 4
310  * c-file-style: "Stroustrup"
311  * indent-tabs-mode: nil
312  * End:
313  * vim: shiftwidth=4 tabstop=8 expandtab
314  */
315