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