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