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;
45 std::cout << "global setup\n";
46 yaz_log_init_level(YLOG_ALL);
50 std::cout << "global teardown\n";
54 BOOST_GLOBAL_FIXTURE( TestConfig );
56 BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
60 std::cout << "Running non-xml config test case" << std::endl;
61 mp::RouterChain router;
62 mp::filter::HttpRewrite fhr;
65 "<?xml version='1.0'?>\n"
66 "<filter xmlns='http://indexdata.com/metaproxy'\n"
67 " id='rewrite1' type='http_rewrite'>\n"
70 "(?<proto>https?://)(?<pxhost>[^ /?#]+)/(?<pxpath>[^ /]+)"
71 "/(?<host>[^ /]+)(?<path>[^ ]*)'\n"
72 " to='${proto}${host}${path}' />\n"
73 " <rewrite from='(?:Host: )(.*)'\n"
74 " to='Host: ${host}' />\n"
78 "(?<proto>https?://)(?<host>[^/?# "'>]+)/(?<path>[^ "'>]+)'\n"
79 " to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
86 // reading and parsing XML conf
87 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
89 xmlNode *root_element = xmlDocGetRootElement(doc);
90 fhr.configure(root_element, true, "");
95 // create an http request
99 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
100 "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
102 pack.request() = gdu_req;
104 //create the http response
106 const char *resp_buf =
107 "HTTP/1.1 200 OK\r\n"
108 "Content-Length: 441\r\n"
109 "Content-Type: text/html\r\n"
110 "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
111 "Link: </dir/file.xml>; rel=relative\r\n"
113 "<html><head><title>Hello proxy!</title>"
116 " background-image:url('http://targetsite/images/bg.png');"
120 "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
122 "<p>Welcome to our website. It doesn't make it easy to get pro"
124 "<a href=\"http://targetsite/page2.html\">"
125 " An absolute link</a>"
126 "<a target=_blank href='http://targetsite/page3.html\">"
127 " Another abs link</a>"
128 "<a href=\"/docs/page4.html\" />"
131 const char *resp_expected =
132 "HTTP/1.1 200 OK\r\n"
133 "Content-Length: 521\r\n"
134 "Content-Type: text/html\r\n"
135 "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
136 "Link: </dir/file.xml>; rel=relative\r\n"
138 "<html><head><title>Hello proxy!</title>"
141 " background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
145 "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
147 "<p>Welcome to our website. It doesn't make it easy to get pro"
149 "<a href=\"http://proxyhost/proxypath/targetsite/page2.html\">"
150 " An absolute link</a>"
151 "<a target=_blank href='http://proxyhost/proxypath/targetsite/page3.html\">"
152 " Another abs link</a>"
153 "<a href=\"/docs/page4.html\" />"
158 ODR dec = odr_createmem(ODR_DECODE);
159 odr_setbuf(dec, (char *) resp_buf, strlen(resp_buf), 0);
160 r = z_GDU(dec, &gdu_res, 0, 0);
165 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
168 pack.response() = gdu_res;
171 pack.router(router).move();
173 //analyze the response
174 Z_GDU *gdu_res_rew = pack.response().get();
175 BOOST_CHECK(gdu_res_rew);
176 BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
178 Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
182 std::cout << "Expected result:\n" << resp_expected << std::endl;
184 ODR enc = odr_createmem(ODR_ENCODE);
185 z_GDU(enc, &gdu_res_rew, 0, 0);
188 resp_result = odr_getbuf(enc, &resp_result_len, 0);
190 BOOST_CHECK(resp_result);
191 BOOST_CHECK_EQUAL(resp_result_len, strlen(resp_expected));
193 std::cout << "Rewriten result:\n" << resp_result << std::endl;
194 std::cout << "Rewriten result buf len: " << resp_result_len
197 BOOST_CHECK(memcmp(resp_result, resp_expected, resp_result_len) == 0);
202 catch (std::exception & e) {
203 std::cout << e.what();
204 std::cout << std::endl;
210 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
214 std::cout << "Running xml config test case" << std::endl;
215 mp::RouterChain router;
216 mp::filter::HttpRewrite fhr;
218 std::string xmlconf =
219 "<?xml version='1.0'?>\n"
220 "<filter xmlns='http://indexdata.com/metaproxy'\n"
221 " id='rewrite1' type='http_rewrite'>\n"
224 "(?<proto>https?://)(?<pxhost>[^ /?#]+)/(?<pxpath>[^ /]+)"
225 "/(?<host>[^ /]+)(?<path>[^ ]*)'\n"
226 " to='${proto}${host}${path}' />\n"
227 " <rewrite from='(?:Host: )(.*)'\n"
228 " to='Host: ${host}' />\n"
232 "(?<proto>https?://)(?<host>[^/?# "'>]+)/(?<path>[^ "'>]+)'\n"
233 " to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
238 std::cout << xmlconf;
240 // reading and parsing XML conf
241 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
243 xmlNode *root_element = xmlDocGetRootElement(doc);
244 fhr.configure(root_element, true, "");
249 // create an http request
253 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
254 "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
256 pack.request() = gdu_req;
258 //create the http response
260 const char *resp_buf =
261 "HTTP/1.1 200 OK\r\n"
262 "Content-Length: 50\r\n"
263 "Content-Type: text/html\r\n"
264 "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
265 "Link: </dir/file.xml>; rel=relative\r\n"
267 "<html><head><title>Hello proxy!</title>"
270 " background-image:url('http://targetsite/images/bg.png');"
274 "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
276 "<p>Welcome to our website. It doesn't make it easy to get pro"
278 "<a href=\"http://targetsite/page2.html\">"
279 " An absolute link</a>"
280 "<a target=_blank href='http://targetsite/page3.html\">"
281 " Another abs link</a>"
282 "<a href=\"/docs/page4.html\" />"
285 const char *resp_buf_rew =
286 "HTTP/1.1 200 OK\r\n"
287 "Content-Length: 50\r\n"
288 "Content-Type: text/html\r\n"
289 "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
290 "Link: </dir/file.xml>; rel=relative\r\n"
292 "<html><head><title>Hello proxy!</title>"
295 " background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
299 "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
301 "<p>Welcome to our website. It doesn't make it easy to get pro"
303 "<a href=\"http://proxyhost/proxypath/targetsite/page.html\">"
304 " An absolute link</a>"
305 "<a target=_blank href='http://proxyhost/proxypath/targetsite/anotherpage.html\">"
306 " Another abs link</a>"
307 "<a href=\"/docs/page2.html\" />"
312 ODR odr2 = odr_createmem(ODR_DECODE);
313 odr_setbuf(odr2, (char *) resp_buf, strlen(resp_buf), 0);
314 r = z_GDU(odr2, &gdu_res, 0, 0);
319 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
322 pack.response() = gdu_res;
325 pack.router(router).move();
327 //analyze the response
328 Z_GDU *gdu_res_rew = pack.response().get();
329 BOOST_CHECK(gdu_res_rew);
330 BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
332 Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
335 //how to compare the buffers:
339 catch (std::exception & e) {
340 std::cout << e.what();
341 std::cout << std::endl;
350 * c-file-style: "Stroustrup"
351 * indent-tabs-mode: nil
353 * vim: shiftwidth=4 tabstop=8 expandtab