http_rewrite: parsing of new configuration
[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             " </response>\n"
90             "</filter>\n"
91         ;
92
93         std::cout << xmlconf;
94
95         // reading and parsing XML conf
96         xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
97         BOOST_CHECK(doc);
98         xmlNode *root_element = xmlDocGetRootElement(doc);
99         fhr.configure(root_element, true, "");
100         xmlFreeDoc(doc);
101        
102         router.append(fhr);
103
104         // create an http request
105         mp::Package pack;
106
107         mp::odr odr;
108         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, 
109         "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
110
111         pack.request() = gdu_req;
112
113         //create the http response
114
115         const char *resp_buf =
116             "HTTP/1.1 200 OK\r\n"
117             "Content-Length: 441\r\n"
118             "Content-Type: text/html\r\n"
119             "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
120             "Link: </dir/file.xml>; rel=relative\r\n"
121             "\r\n"
122             "<html><head><title>Hello proxy!</title>"
123             "<style>"
124             "body {"
125             "  background-image:url('http://targetsite/images/bg.png');"
126             "}"
127             "</style>"
128             "</head>"
129             "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
130             "<body>"
131             "<p>Welcome to our website. It doesn't make it easy to get pro"
132             "xified"
133             "<a href=\"http://targetsite/page2.html\">"
134             "  An absolute link</a>"
135             "<a target=_blank href='http://targetsite/page3.html\">"
136             "  Another abs link</a>"
137             "<a href=\"/docs/page4.html\" />"
138             "</body></html>";
139
140         const char *resp_expected =
141             "HTTP/1.1 200 OK\r\n"
142             "Content-Length: 521\r\n"
143             "Content-Type: text/html\r\n"
144             "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
145             "Link: </dir/file.xml>; rel=relative\r\n"
146             "\r\n"
147             "<html><head><title>Hello proxy!</title>"
148             "<style>"
149             "body {"
150             "  background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
151             "}"
152             "</style>"
153             "</head>"
154             "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
155             "<body>"
156             "<p>Welcome to our website. It doesn't make it easy to get pro"
157             "xified"
158             "<a href=\"http://proxyhost/proxypath/targetsite/page2.html\">"
159             "  An absolute link</a>"
160             "<a target=_blank href='http://proxyhost/proxypath/targetsite/page3.html\">"
161             "  Another abs link</a>"
162             "<a href=\"/docs/page4.html\" />"
163             "</body></html>";
164
165         int r;
166         Z_GDU *gdu_res;
167         ODR dec = odr_createmem(ODR_DECODE);
168         odr_setbuf(dec, (char *) resp_buf, strlen(resp_buf), 0);
169         r = z_GDU(dec, &gdu_res, 0, 0);
170
171         BOOST_CHECK(r);
172         if (r)
173         {
174             BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
175         }
176
177         pack.response() = gdu_res;
178
179         //feed to the router
180         pack.router(router).move();
181
182         //analyze the response
183         Z_GDU *gdu_res_rew = pack.response().get();
184         BOOST_CHECK(gdu_res_rew);
185         BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
186         
187         Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
188         BOOST_CHECK(hres);
189
190         //compare buffers
191         std::cout << "Expected result:\n" << resp_expected << std::endl;
192
193         ODR enc = odr_createmem(ODR_ENCODE);
194         z_GDU(enc, &gdu_res_rew, 0, 0);
195         char *resp_result;
196         int resp_result_len;
197         resp_result = odr_getbuf(enc, &resp_result_len, 0);
198         
199         BOOST_CHECK(resp_result);
200         BOOST_CHECK_EQUAL((size_t) resp_result_len, strlen(resp_expected));
201
202         std::cout << "Rewriten result:\n" << resp_result << std::endl;
203         std::cout << "Rewriten result buf len: " << resp_result_len 
204             << std::endl;
205
206         BOOST_CHECK(memcmp(resp_result, resp_expected, resp_result_len) == 0);
207
208         odr_destroy(dec);
209         odr_destroy(enc);
210     }
211     catch (std::exception & e) {
212         std::cout << e.what();
213         std::cout << std::endl;
214         BOOST_CHECK (false);
215     }
216 }
217
218 /*
219 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
220 {
221     try
222     {
223         std::cout << "Running xml config test case" << std::endl;
224         mp::RouterChain router;
225         mp::filter::HttpRewrite fhr;
226
227         std::string xmlconf =
228             "<?xml version='1.0'?>\n"
229             "<filter xmlns='http://indexdata.com/metaproxy'\n"
230             "        id='rewrite1' type='http_rewrite'>\n"
231             " <request>\n"
232             "   <rewrite from='"
233     "(?&lt;proto>https?://)(?&lt;pxhost>[^ /?#]+)/(?&lt;pxpath>[^ /]+)"
234     "/(?&lt;host>[^ /]+)(?&lt;path>[^ ]*)'\n"
235             "            to='${proto}${host}${path}' />\n"
236             "   <rewrite from='(?:Host: )(.*)'\n"
237             "            to='Host: ${host}' />\n" 
238             " </request>\n"
239             " <response>\n"
240             "   <rewrite from='"
241     "(?&lt;proto>https?://)(?&lt;host>[^/?# &quot;&apos;>]+)/(?&lt;path>[^  &quot;&apos;>]+)'\n"
242             "            to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n" 
243             " </response>\n"
244             "</filter>\n"
245         ;
246
247         std::cout << xmlconf;
248
249         // reading and parsing XML conf
250         xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
251         BOOST_CHECK(doc);
252         xmlNode *root_element = xmlDocGetRootElement(doc);
253         fhr.configure(root_element, true, "");
254         xmlFreeDoc(doc);
255         
256         router.append(fhr);
257
258         // create an http request
259         mp::Package pack;
260
261         mp::odr odr;
262         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, 
263         "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
264
265         pack.request() = gdu_req;
266
267         //create the http response
268
269         const char *resp_buf =
270             "HTTP/1.1 200 OK\r\n"
271             "Content-Length: 50\r\n"
272             "Content-Type: text/html\r\n"
273             "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
274             "Link: </dir/file.xml>; rel=relative\r\n"
275             "\r\n"
276             "<html><head><title>Hello proxy!</title>"
277             "<style>"
278             "body {"
279             "  background-image:url('http://targetsite/images/bg.png');"
280             "}"
281             "</style>"
282             "</head>"
283             "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
284             "<body>"
285             "<p>Welcome to our website. It doesn't make it easy to get pro"
286             "xified"
287             "<a href=\"http://targetsite/page2.html\">"
288             "  An absolute link</a>"
289             "<a target=_blank href='http://targetsite/page3.html\">"
290             "  Another abs link</a>"
291             "<a href=\"/docs/page4.html\" />"
292             "</body></html>";
293
294         const char *resp_buf_rew =
295             "HTTP/1.1 200 OK\r\n"
296             "Content-Length: 50\r\n"
297             "Content-Type: text/html\r\n"
298             "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
299             "Link: </dir/file.xml>; rel=relative\r\n"
300             "\r\n"
301             "<html><head><title>Hello proxy!</title>"
302             "<style>"
303             "body {"
304             "  background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
305             "}"
306             "</style>"
307             "</head>"
308             "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
309             "<body>"
310             "<p>Welcome to our website. It doesn't make it easy to get pro"
311             "xified"
312             "<a href=\"http://proxyhost/proxypath/targetsite/page.html\">"
313             "  An absolute link</a>"
314             "<a target=_blank href='http://proxyhost/proxypath/targetsite/anotherpage.html\">"
315             "  Another abs link</a>"
316             "<a href=\"/docs/page2.html\" />"
317             "</body></html>";
318
319         int r;
320         Z_GDU *gdu_res;
321         ODR odr2 = odr_createmem(ODR_DECODE);
322         odr_setbuf(odr2, (char *) resp_buf, strlen(resp_buf), 0);
323         r = z_GDU(odr2, &gdu_res, 0, 0);
324
325         BOOST_CHECK(r == 0);
326         if (r)
327         {
328             BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
329         }
330
331         pack.response() = gdu_res;
332
333         //feed to the router
334         pack.router(router).move();
335
336         //analyze the response
337         Z_GDU *gdu_res_rew = pack.response().get();
338         BOOST_CHECK(gdu_res_rew);
339         BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
340         
341         Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
342         BOOST_CHECK(hres);
343
344         //how to compare the buffers:
345
346         odr_destroy(odr2);
347     }
348     catch (std::exception & e) {
349         std::cout << e.what();
350         std::cout << std::endl;
351         BOOST_CHECK (false);
352     }
353 }
354 */
355
356 /*
357  * Local variables:
358  * c-basic-offset: 4
359  * c-file-style: "Stroustrup"
360  * indent-tabs-mode: nil
361  * End:
362  * vim: shiftwidth=4 tabstop=8 expandtab
363  */
364