More tests for 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 {
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) {
50         out += " ";
51         out.append(attr, attr_len);
52         out += "=\"";
53         out.append(value, val_len);
54         out += "\"";
55     }
56     void anyTagEnd(const char *tag, int tag_len, int close_it) {
57         if (close_it)
58             out += "/";
59         out += ">";
60     }
61     void closeTag(const char *tag, int tag_len) {
62         out += "</";
63         out.append(tag, tag_len);
64     }
65     void text(const char *value, int len) {
66         out.append(value, len);
67     }
68 };
69
70
71 BOOST_AUTO_TEST_CASE( test_html_parser_1 )
72 {
73     try
74     {
75         mp::HTMLParser hp;
76         const char* html = 
77             "<html><body><a t1=v1 t2='v2' t3=\"v3\">some text</a>"
78             "<hr><table ></table  ><a href=\"x\"/></body></html>";
79         const char* expected = 
80             "<html><body><a t1=\"v1\" t2=\"v2\" t3=\"v3\">some text</a>"
81             "<hr><table></table><a href=\"x\"/></body></html>";
82         MyEvent e;
83         hp.set_verbose(1);
84         hp.parse(e, html);
85
86         std::cout << "Expected" << std::endl;
87         std::cout << expected << std::endl;
88         std::cout << "Got" << std::endl;
89         std::cout << e.out << std::endl;
90         BOOST_CHECK_EQUAL(std::string(expected), e.out);
91     }
92     catch (std::exception & e) 
93     {
94         std::cout << e.what();
95         std::cout << std::endl;
96         BOOST_CHECK (false);
97     }
98 }
99
100 BOOST_AUTO_TEST_CASE( test_html_parser_2 )
101 {
102     try
103     {
104         mp::HTMLParser hp;
105         const char* html = 
106             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"
107             "<HTML>\n"
108             " <HEAD>\n"
109             "  <TITLE>YAZ 4.2.60</TITLE>\n"
110             " </HEAD>\n"
111             " <BODY>\n"
112             "  <P><A HREF=\"http://www.indexdata.com/yaz/\">YAZ</A> 4.2.60</P>\n"
113             "  <P>Error: 404</P>\n"
114             "  <P>Description: Not Found</P>\n"
115             " </BODY>\n"
116             "</HTML>";
117
118         const char* expected = html;
119         MyEvent e;
120         hp.set_verbose(1);
121         hp.parse(e, html);
122
123         std::cout << "Expected" << std::endl;
124         std::cout << expected << std::endl;
125         std::cout << "Got" << std::endl;
126         std::cout << e.out << std::endl;
127
128         BOOST_CHECK_EQUAL(std::string(expected), e.out);
129     }
130     catch (std::exception & e) 
131     {
132         std::cout << e.what();
133         std::cout << std::endl;
134         BOOST_CHECK (false);
135     }
136 }
137
138 BOOST_AUTO_TEST_CASE( test_html_parser_3 )
139 {
140     try
141     {
142         mp::HTMLParser hp;
143         const char* html =
144             "<?xml version=\"1.0\" strandalone=\"no\"?>\n"
145             "<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.4//EN\"\n"
146             "  \"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd\"\n"
147             "[\n"
148             " <!ENTITY % local SYSTEM \"local.ent\">\n"
149             " %local;\n"
150             "]>\n"
151             "<book></book>";
152
153         const char* expected = html;
154         MyEvent e;
155         hp.set_verbose(1);
156         hp.parse(e, html);
157
158         std::cout << "Expected" << std::endl;
159         std::cout << expected << std::endl;
160         std::cout << "Got" << std::endl;
161         std::cout << e.out << std::endl;
162
163         BOOST_CHECK_EQUAL(std::string(expected), e.out);
164     }
165     catch (std::exception & e) 
166     {
167         std::cout << e.what();
168         std::cout << std::endl;
169         BOOST_CHECK (false);
170     }
171 }
172
173 #if 0
174 // null ptr exception
175 BOOST_AUTO_TEST_CASE( test_html_parser_4 )
176 {
177     try
178     {
179         mp::HTMLParser hp;
180         const char* html =
181             "<\"?xml version=\"1.0\" strandalone=\"no\"?>\n"
182             "<book></book>";
183
184         const char* expected = html;
185         MyEvent e;
186         hp.set_verbose(1);
187         hp.parse(e, html);
188
189         std::cout << "Expected" << std::endl;
190         std::cout << expected << std::endl;
191         std::cout << "Got" << std::endl;
192         std::cout << e.out << std::endl;
193
194         BOOST_CHECK_EQUAL(std::string(expected), e.out);
195     }
196     catch (std::exception & e) 
197     {
198         std::cout << e.what();
199         std::cout << std::endl;
200         BOOST_CHECK (false);
201     }
202 }
203 #endif
204
205 /*
206  * Local variables:
207  * c-basic-offset: 4
208  * c-file-style: "Stroustrup"
209  * indent-tabs-mode: nil
210  * End:
211  * vim: shiftwidth=4 tabstop=8 expandtab
212  */
213