1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
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
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
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
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>
29 #include <boost/regex.hpp>
30 #include <boost/lexical_cast.hpp>
34 #define BOOST_AUTO_TEST_MAIN
35 #define BOOST_TEST_DYN_LINK
37 #include <boost/test/auto_unit_test.hpp>
39 using namespace boost::unit_test;
40 namespace mp = metaproxy_1;
42 BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
46 std::cout << "Running non-xml config test case" << std::endl;
47 mp::RouterChain router;
48 mp::filter::HttpRewrite fhr;
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"
58 "(?<proto>https?://)(?<pxhost>[^ /?#]+)/(?<pxpath>[^ /]+)"
59 "/(?<host>[^ /]+)(?<path>[^ ]*)'\n"
60 " to='${proto}${host}${path}' />\n"
61 " <rewrite from='(?:Host: )(.*)'\n"
62 " to='Host: ${host}' />\n"
64 " <content type=\"headers\">\n"
65 " <within header=\"link\" rule=\"null\"/>\n"
66 " <within reqline=\"1\" rule=\"url\"/>\n"
69 " <response verbose=\"1\">\n"
70 " <rule name=\"null\"/>\n"
71 " <rule name=\"url\">\n"
72 " <rewrite from='foo' to='bar'/>\n"
73 " <rewrite from='^cx' to='cy'/>\n"
75 "(?<proto>https?://)(?<host>[^/?# "'>]+)/(?<path>[^ "'>]+)'\n"
76 " to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
78 " <content type=\"headers\">\n"
79 " <within header=\"link\" rule=\"url\"/>\n"
81 " <content type=\"html\" mime=\"text/xml|text/html\">\n"
82 " <within tag=\"body\" attr=\"background\" rule=\"null\"/>\n"
83 " <within tag=\"script\" attr=\"#text\" rule=\"url\"/>\n"
84 " <within tag=\"style\" attr=\"#text\" rule=\"url\"/>\n"
85 " <within attr=\"href,src\" rule=\"url\"/>\n"
93 // reading and parsing XML conf
94 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
96 xmlNode *root_element = xmlDocGetRootElement(doc);
97 fhr.configure(root_element, true, "");
102 // create an http request
106 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
107 "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
109 pack.request() = gdu_req;
111 //create the http response
113 const char *resp_buf =
114 "HTTP/1.1 200 OK\r\n"
115 "Content-Length: 441\r\n"
116 "Content-Type: text/html\r\n"
117 "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
118 "Link: </dir/file.xml>; rel=relative\r\n"
120 "<html><head><title>Hello proxy!</title>"
123 " background-image:url('http://targetsite/images/bg.png');"
127 "<script>var jslink=\"http://targetsite/webservice.xml\";"
131 "<p>Welcome to our website. It doesn't make it easy to get pro"
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 "<a href=\"cxcx\" />"
142 const char *resp_expected =
143 "HTTP/1.1 200 OK\r\n"
144 "Content-Length: 564\r\n"
145 "Content-Type: text/html\r\n"
146 "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
147 "Link: </dir/file.xml>; rel=relative\r\n"
149 "<html><head><title>Hello proxy!</title>"
152 " background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
156 "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";"
160 "<p>Welcome to our website. It doesn't make it easy to get pro"
162 "<a href=\"http://proxyhost/proxypath/targetsite/page2.html\">"
163 " An absolute link</a>"
164 "<a target=_blank href=\"http://proxyhost/proxypath/targetsite/page3.html\">"
165 " Another abs link</a>"
166 "<a href=\"/docs/page4.html\"/>"
172 mp::odr dec(ODR_DECODE);
173 mp::odr enc(ODR_ENCODE);
174 odr_setbuf(dec, (char *) resp_buf, strlen(resp_buf), 0);
175 int r = z_GDU(dec, &gdu_res, 0, 0);
180 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
182 pack.response() = gdu_res;
185 pack.router(router).move();
187 //analyze the response
188 Z_GDU *gdu_res_rew = pack.response().get();
189 BOOST_CHECK(gdu_res_rew);
190 BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
192 Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
195 z_GDU(enc, &gdu_res_rew, 0, 0);
198 resp_result = odr_getbuf(enc, &resp_result_len, 0);
200 int equal = ((size_t) resp_result_len == strlen(resp_expected))
201 && !memcmp(resp_result, resp_expected, resp_result_len);
207 std::cout << "Expected result:\n" << resp_expected << "\n";
208 std::cout << "Got result:\n" << "\n";
210 fwrite(resp_result, 1, resp_result_len, stdout);
212 std::cout << "\nGot result buf len: " << resp_result_len
217 catch (std::exception & e) {
218 std::cout << e.what();
219 std::cout << std::endl;
225 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
229 std::cout << "Running non-xml config test case" << std::endl;
230 mp::RouterChain router;
231 mp::filter::HttpRewrite fhr;
233 std::string xmlconf =
234 "<?xml version='1.0'?>\n"
235 "<filter xmlns='http://indexdata.com/metaproxy'\n"
236 " id='rewrite1' type='http_rewrite'>\n"
237 " <request verbose=\"1\">\n"
238 " <rule name=\"null\"/>\n"
239 " <rule name=\"url\">\n"
241 "(?<proto>https?://)(?<pxhost>[^ /?#]+)/(?<pxpath>[^ /]+)"
242 "/(?<host>[^ /]+)(?<path>[^ ]*)'\n"
243 " to='${proto}${host}${path}' />\n"
244 " <rewrite from='(?:Host: )(.*)'\n"
245 " to='Host: ${host}' />\n"
247 " <content type=\"headers\">\n"
248 " <within header=\"link\" rule=\"null\"/>\n"
249 " <within reqline=\"1\" rule=\"url\"/>\n"
252 " <response verbose=\"1\">\n"
253 " <rule name=\"null\"/>\n"
254 " <rule name=\"url\">\n"
255 " <rewrite from='foo' to='bar'/>\n"
256 " <rewrite from='^cx' to='cy'/>\n"
258 "(?<proto>https?://)(?<host>[^/?# "'>]+)/(?<path>[^ "'>]+)'\n"
259 " to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
261 " <content type=\"headers\">\n"
262 " <within header=\"link\" rule=\"url\"/>\n"
264 " <content type=\"html\" mime=\"text/xml|text/html\">\n"
265 " <within tag=\"body\" attr=\"background\" rule=\"null\"/>\n"
266 " <within tag=\"script\" attr=\"#text\" rule=\"url\"/>\n"
267 " <within tag=\"style\" attr=\"#text\" rule=\"url\"/>\n"
268 " <within attr=\"href,src\" rule=\"url\"/>\n"
270 " <content type=\"quoted-literal\" mime=\".*javascript\">\n"
271 " <within rule=\"url\"/>\n"
277 std::cout << xmlconf;
279 // reading and parsing XML conf
280 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
282 xmlNode *root_element = xmlDocGetRootElement(doc);
283 fhr.configure(root_element, true, "");
288 // create an http request
292 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
293 "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
295 pack.request() = gdu_req;
297 //create the http response
299 const char *resp_buf =
300 "HTTP/1.1 200 OK\r\n"
301 "Content-Length: 140\r\n"
302 "Content-Type: application/javascript\r\n"
303 "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
304 "Link: </dir/file.xml>; rel=relative\r\n"
307 "my.location = 'http://targetsite/images/bg.png';\n"
308 "my.other = \"http://targetsite/images/fg.png\";\n"
309 "my.thrd = \"other\";\n"
310 "// http://targetsite/images/bg.png\n";
312 const char *resp_expected =
313 "HTTP/1.1 200 OK\r\n"
314 "Content-Length: 194\r\n"
315 "Content-Type: application/javascript\r\n"
316 "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
317 "Link: </dir/file.xml>; rel=relative\r\n"
320 "my.location = 'http://proxyhost/proxypath/targetsite/images/bg.png';\n"
321 "my.other = \"http://proxyhost/proxypath/targetsite/images/fg.png\";\n"
322 "my.thrd = \"other\";\n"
323 "// http://targetsite/images/bg.png\n";
326 mp::odr dec(ODR_DECODE);
327 mp::odr enc(ODR_ENCODE);
328 odr_setbuf(dec, (char *) resp_buf, strlen(resp_buf), 0);
329 int r = z_GDU(dec, &gdu_res, 0, 0);
334 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
336 pack.response() = gdu_res;
339 pack.router(router).move();
341 //analyze the response
342 Z_GDU *gdu_res_rew = pack.response().get();
343 BOOST_CHECK(gdu_res_rew);
344 BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
346 Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
349 z_GDU(enc, &gdu_res_rew, 0, 0);
352 resp_result = odr_getbuf(enc, &resp_result_len, 0);
354 int equal = ((size_t) resp_result_len == strlen(resp_expected))
355 && !memcmp(resp_result, resp_expected, resp_result_len);
361 std::cout << "Expected result:\n" << resp_expected << "\n";
362 std::cout << "Got result:\n" << "\n";
364 fwrite(resp_result, 1, resp_result_len, stdout);
366 std::cout << "\nGot result buf len: " << resp_result_len
371 catch (std::exception & e) {
372 std::cout << e.what();
373 std::cout << std::endl;
381 * c-file-style: "Stroustrup"
382 * indent-tabs-mode: nil
384 * vim: shiftwidth=4 tabstop=8 expandtab