Testing for all Replace rules in use
[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 verbose=\"1\">\n"
72             "   <rule name=\"null\"/>\n"
73             "   <rule name=\"url\">\n"
74             "     <rewrite from='"
75     "(?&lt;proto>https?://)(?&lt;pxhost>[^ /?#]+)/(?&lt;pxpath>[^ /]+)"
76     "/(?&lt;host>[^ /]+)(?&lt;path>[^ ]*)'\n"
77             "            to='${proto}${host}${path}' />\n"
78             "     <rewrite from='(?:Host: )(.*)'\n"
79             "            to='Host: ${host}' />\n"
80             "   </rule>\n"
81             "  <within header=\"link\" rule=\"null\"/>\n"
82             "  <within reqline=\"1\" rule=\"url\"/>\n"
83             " </request>\n"
84             " <response verbose=\"1\">\n"
85             "   <rule name=\"null\"/>\n"
86             "   <rule name=\"url\">\n"
87             "     <rewrite from='foo' to='bar'/>\n"
88             "     <rewrite from='"
89     "(?&lt;proto>https?://)(?&lt;host>[^/?# &quot;&apos;>]+)/(?&lt;path>[^  &quot;&apos;>]+)'\n"
90             "            to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
91             "  </rule>\n"
92             "  <within header=\"link\" rule=\"url\"/>\n"
93             "  <within tag=\"body\" attr=\"background\" rule=\"null\"/>\n"
94             "  <within tag=\"script\" attr=\"#text\" rule=\"url\"/>\n"
95             "  <within tag=\"style\" attr=\"#text\" rule=\"url\"/>\n"
96             "  <within attr=\"href,src\" rule=\"url\"/>\n"
97             " </response>\n"
98             "</filter>\n"
99         ;
100
101         std::cout << xmlconf;
102
103         // reading and parsing XML conf
104         xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
105         BOOST_CHECK(doc);
106         xmlNode *root_element = xmlDocGetRootElement(doc);
107         fhr.configure(root_element, true, "");
108         xmlFreeDoc(doc);
109
110         router.append(fhr);
111
112         // create an http request
113         mp::Package pack;
114
115         mp::odr odr;
116         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
117         "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
118
119         pack.request() = gdu_req;
120
121         //create the http response
122
123         const char *resp_buf =
124             "HTTP/1.1 200 OK\r\n"
125             "Content-Length: 441\r\n"
126             "Content-Type: text/html\r\n"
127             "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
128             "Link: </dir/file.xml>; rel=relative\r\n"
129             "\r\n"
130             "<html><head><title>Hello proxy!</title>"
131             "<style>"
132             "body {"
133             "  background-image:url('http://targetsite/images/bg.png');"
134             "}"
135             "</style>"
136             "</head>"
137             "<script>var jslink=\"http://targetsite/webservice.xml\";"
138             "var some=foo;"
139             "</script>"
140             "<body>"
141             "<p>Welcome to our website. It doesn't make it easy to get pro"
142             "xified"
143             "<a href=\"http://targetsite/page2.html\">"
144             "  An absolute link</a>"
145             "<a target=_blank href=\"http://targetsite/page3.html\">"
146             "  Another abs link</a>"
147             "<a href=\"/docs/page4.html\" />"
148             "</body></html>";
149
150         const char *resp_expected =
151             "HTTP/1.1 200 OK\r\n"
152             "Content-Length: 533\r\n"
153             "Content-Type: text/html\r\n"
154             "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
155             "Link: </dir/file.xml>; rel=relative\r\n"
156             "\r\n"
157             "<html><head><title>Hello proxy!</title>"
158             "<style>"
159             "body {"
160             "  background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
161             "}"
162             "</style>"
163             "</head>"
164             "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";"
165             "var some=bar;"
166             "</script>"
167             "<body>"
168             "<p>Welcome to our website. It doesn't make it easy to get pro"
169             "xified"
170             "<a href=\"http://proxyhost/proxypath/targetsite/page2.html\">"
171             "  An absolute link</a>"
172             "<a target=_blank href=\"http://proxyhost/proxypath/targetsite/page3.html\">"
173             "  Another abs link</a>"
174             "<a href=\"/docs/page4.html\"/>"
175             "</body></html>";
176
177         int r;
178         Z_GDU *gdu_res;
179         ODR dec = odr_createmem(ODR_DECODE);
180         odr_setbuf(dec, (char *) resp_buf, strlen(resp_buf), 0);
181         r = z_GDU(dec, &gdu_res, 0, 0);
182
183         BOOST_CHECK(r);
184         if (r)
185         {
186             BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
187         }
188
189         pack.response() = gdu_res;
190
191         //feed to the router
192         pack.router(router).move();
193
194         //analyze the response
195         Z_GDU *gdu_res_rew = pack.response().get();
196         BOOST_CHECK(gdu_res_rew);
197         BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
198
199         Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
200         BOOST_CHECK(hres);
201
202         //compare buffers
203         std::cout << "Expected result:\n" << resp_expected << std::endl;
204
205         ODR enc = odr_createmem(ODR_ENCODE);
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         BOOST_CHECK(resp_result);
212         BOOST_CHECK_EQUAL((size_t) resp_result_len, strlen(resp_expected));
213
214         std::cout << "Rewritten result:\n" << std::endl;
215         fflush(stdout);
216         fwrite(resp_result, 1, resp_result_len, stdout);
217         fflush(stdout);
218         std::cout << "\nRewritten result buf len: " << resp_result_len
219             << std::endl;
220
221         BOOST_CHECK(memcmp(resp_result, resp_expected, resp_result_len) == 0);
222
223         odr_destroy(dec);
224         odr_destroy(enc);
225     }
226     catch (std::exception & e) {
227         std::cout << e.what();
228         std::cout << std::endl;
229         BOOST_CHECK (false);
230     }
231 }
232
233
234 /*
235  * Local variables:
236  * c-basic-offset: 4
237  * c-file-style: "Stroustrup"
238  * indent-tabs-mode: nil
239  * End:
240  * vim: shiftwidth=4 tabstop=8 expandtab
241  */
242