Remove unused code, fix printing
[metaproxy-moved-to-github.git] / src / test_filter_rewrite.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 "filter_http_client.hpp"
24 #include "filter_http_rewrite.hpp"
25 #include <metaproxy/util.hpp>
26 #include <metaproxy/router_chain.hpp>
27 #include <metaproxy/package.hpp>
28
29 #include <boost/regex.hpp>
30 #include <boost/lexical_cast.hpp>
31
32 #include <yaz/log.h>
33
34 #define BOOST_AUTO_TEST_MAIN
35 #define BOOST_TEST_DYN_LINK
36
37 #include <boost/test/auto_unit_test.hpp>
38
39 using namespace boost::unit_test;
40 namespace mp = metaproxy_1;
41 /*
42  * The global testconfig is commented out, as it won't even compile
43  * on old Centos5 machines
44 struct TestConfig {
45     TestConfig()   
46     {
47         std::cout << "global setup\n"; 
48         yaz_log_init_level(YLOG_ALL);
49     }
50     ~TestConfig() 
51     { 
52         std::cout << "global teardown\n"; 
53     }
54 };
55
56 BOOST_GLOBAL_FIXTURE( TestConfig );
57 */
58
59 BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
60 {
61     try
62     {
63         std::cout << "Running non-xml config test case" << std::endl;
64         mp::RouterChain router;
65         mp::filter::HttpRewrite fhr;
66          
67         std::string xmlconf =
68             "<?xml version='1.0'?>\n"
69             "<filter xmlns='http://indexdata.com/metaproxy'\n"
70             "        id='rewrite1' type='http_rewrite'>\n"
71             " <request>\n"
72             "   <rule name=\"url\">\n"
73             "     <rewrite from='"
74     "(?&lt;proto>https?://)(?&lt;pxhost>[^ /?#]+)/(?&lt;pxpath>[^ /]+)"
75     "/(?&lt;host>[^ /]+)(?&lt;path>[^ ]*)'\n"
76             "            to='${proto}${host}${path}' />\n"
77             "     <rewrite from='(?:Host: )(.*)'\n"
78             "            to='Host: ${host}' />\n"
79             "   </rule>\n"
80             "   <within rule=\"url\"/>\n"
81             " </request>\n"
82             " <response>\n"
83             "   <rule name=\"url\">\n"
84             "     <rewrite from='"
85     "(?&lt;proto>https?://)(?&lt;host>[^/?# &quot;&apos;>]+)/(?&lt;path>[^  &quot;&apos;>]+)'\n"
86             "            to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n" 
87             "  </rule>\n"
88             "  <within rule=\"url\"/>\n"
89             "  <within tag=\"html\" rule=\"url\"/>\n"
90             " </response>\n"
91             "</filter>\n"
92         ;
93
94         std::cout << xmlconf;
95
96         // reading and parsing XML conf
97         xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
98         BOOST_CHECK(doc);
99         xmlNode *root_element = xmlDocGetRootElement(doc);
100         fhr.configure(root_element, true, "");
101         xmlFreeDoc(doc);
102        
103         router.append(fhr);
104
105         // create an http request
106         mp::Package pack;
107
108         mp::odr odr;
109         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, 
110         "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
111
112         pack.request() = gdu_req;
113
114         //create the http response
115
116         const char *resp_buf =
117             "HTTP/1.1 200 OK\r\n"
118             "Content-Length: 441\r\n"
119             "Content-Type: text/html\r\n"
120             "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
121             "Link: </dir/file.xml>; rel=relative\r\n"
122             "\r\n"
123             "<html><head><title>Hello proxy!</title>"
124             "<style>"
125             "body {"
126             "  background-image:url('http://targetsite/images/bg.png');"
127             "}"
128             "</style>"
129             "</head>"
130             "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
131             "<body>"
132             "<p>Welcome to our website. It doesn't make it easy to get pro"
133             "xified"
134             "<a href=\"http://targetsite/page2.html\">"
135             "  An absolute link</a>"
136             "<a target=_blank href='http://targetsite/page3.html\">"
137             "  Another abs link</a>"
138             "<a href=\"/docs/page4.html\" />"
139             "</body></html>";
140
141         const char *resp_expected =
142             "HTTP/1.1 200 OK\r\n"
143             "Content-Length: 521\r\n"
144             "Content-Type: text/html\r\n"
145             "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
146             "Link: </dir/file.xml>; rel=relative\r\n"
147             "\r\n"
148             "<html><head><title>Hello proxy!</title>"
149             "<style>"
150             "body {"
151             "  background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
152             "}"
153             "</style>"
154             "</head>"
155             "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
156             "<body>"
157             "<p>Welcome to our website. It doesn't make it easy to get pro"
158             "xified"
159             "<a href=\"http://proxyhost/proxypath/targetsite/page2.html\">"
160             "  An absolute link</a>"
161             "<a target=_blank href='http://proxyhost/proxypath/targetsite/page3.html\">"
162             "  Another abs link</a>"
163             "<a href=\"/docs/page4.html\" />"
164             "</body></html>";
165
166         int r;
167         Z_GDU *gdu_res;
168         ODR dec = odr_createmem(ODR_DECODE);
169         odr_setbuf(dec, (char *) resp_buf, strlen(resp_buf), 0);
170         r = z_GDU(dec, &gdu_res, 0, 0);
171
172         BOOST_CHECK(r);
173         if (r)
174         {
175             BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
176         }
177
178         pack.response() = gdu_res;
179
180         //feed to the router
181         pack.router(router).move();
182
183         //analyze the response
184         Z_GDU *gdu_res_rew = pack.response().get();
185         BOOST_CHECK(gdu_res_rew);
186         BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
187         
188         Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
189         BOOST_CHECK(hres);
190
191         //compare buffers
192         std::cout << "Expected result:\n" << resp_expected << std::endl;
193
194         ODR enc = odr_createmem(ODR_ENCODE);
195         z_GDU(enc, &gdu_res_rew, 0, 0);
196         char *resp_result;
197         int resp_result_len;
198         resp_result = odr_getbuf(enc, &resp_result_len, 0);
199         
200         BOOST_CHECK(resp_result);
201         BOOST_CHECK_EQUAL((size_t) resp_result_len, strlen(resp_expected));
202
203         std::cout << "Rewritten result:\n" << std::endl;
204         fwrite(resp_result, 1, resp_result_len, stdout);
205         std::cout << "\nRewritten result buf len: " << resp_result_len 
206             << std::endl;
207
208         BOOST_CHECK(memcmp(resp_result, resp_expected, resp_result_len) == 0);
209
210         odr_destroy(dec);
211         odr_destroy(enc);
212     }
213     catch (std::exception & e) {
214         std::cout << e.what();
215         std::cout << std::endl;
216         BOOST_CHECK (false);
217     }
218 }
219
220
221 /*
222  * Local variables:
223  * c-basic-offset: 4
224  * c-file-style: "Stroustrup"
225  * indent-tabs-mode: nil
226  * End:
227  * vim: shiftwidth=4 tabstop=8 expandtab
228  */
229