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