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 "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;
64 //configure the filter
65 mp::filter::HttpRewrite::spair_vec vec_req;
66 vec_req.push_back(std::make_pair(
67 "(?<proto>http\\:\\/\\/s?)(?<pxhost>[^\\/?#]+)\\/(?<pxpath>[^\\/]+)"
68 "\\/(?<host>[^\\/]+)(?<path>.*)",
69 "${proto}${host}${path}"
71 vec_req.push_back(std::make_pair(
76 mp::filter::HttpRewrite::spair_vec vec_res;
77 vec_res.push_back(std::make_pair(
78 "(?<proto>http\\:\\/\\/s?)(?<host>[^\\/?# \"'>]+)\\/(?<path>[^ \"'>]+)",
79 "${proto}${pxhost}/${pxpath}/${host}/${path}"
82 fhr.configure(vec_req, vec_res);
86 // create an http request
90 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
91 "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
93 pack.request() = gdu_req;
95 //create the http response
97 /* response, content */
98 const char *resp_buf =
99 /*123456789012345678 */
100 "HTTP/1.1 200 OK\r\n"
101 "Content-Length: 3000\r\n"
102 "Content-Type: text/html\r\n"
103 "Link: <http://targetsite/file.xml>; rel=absolute\r\r"
104 "Link: </dir/file.xml>; rel=relative\r\r"
106 "<html><head><title>Hello proxy!</title>"
109 " background-image:url('http://targetsite/images/bg.png');"
113 "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
115 "<p>Welcome to our website. It doesn't make it easy to get pro"
117 "<a href=\"http://targetsite/page2.html\">"
118 " An absolute link</a>"
119 "<a target=_blank href='http://targetsite/page3.html\">"
120 " Another abs link</a>"
121 "<a href=\"/docs/page4.html\" />"
124 /* response, content */
125 const char *resp_buf_rew =
126 /*123456789012345678 */
127 "HTTP/1.1 200 OK\r\n"
128 "Content-Length: 3000\r\n"
129 "Content-Type: text/html\r\n"
130 "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\r"
131 "Link: </dir/file.xml>; rel=relative\r\r"
133 "<html><head><title>Hello proxy!</title>"
136 " background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
140 "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
142 "<p>Welcome to our website. It doesn't make it easy to get pro"
144 "<a href=\"http://proxyhost/proxypath/targetsite/page.html\">"
145 " An absolute link</a>"
146 "<a target=_blank href='http://proxyhost/proxypath/targetsite/anotherpage.html\">"
147 " Another abs link</a>"
148 "<a href=\"/docs/page2.html\" />"
153 ODR odr2 = odr_createmem(ODR_DECODE);
154 odr_setbuf(odr2, (char *) resp_buf, strlen(resp_buf), 0);
155 r = z_GDU(odr2, &gdu_res, 0, 0);
160 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
163 pack.response() = gdu_res;
166 pack.router(router).move();
168 //analyze the response
169 Z_GDU *gdu_res_rew = pack.response().get();
170 BOOST_CHECK(gdu_res_rew);
171 BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
173 Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
176 //how to compare the buffers:
180 catch (std::exception & e) {
181 std::cout << e.what();
182 std::cout << std::endl;
187 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
191 std::cout << "Running xml config test case" << std::endl;
192 mp::RouterChain router;
193 mp::filter::HttpRewrite fhr;
195 std::string xmlconf =
196 "<?xml version='1.0'?>\n"
197 "<filter xmlns='http://indexdata.com/metaproxy'\n"
198 " id='rewrite1' type='http_rewrite'>\n"
201 "(?<proto>https?://)(?<pxhost>[^ /?#]+)/(?<pxpath>[^ /]+)"
202 "/(?<host>[^ /]+)(?<path>[^ ]*)'\n"
203 " to='${proto}${host}${path}' />\n"
204 " <rewrite from='(?:Host: )(.*)'\n"
205 " to='Host: ${host}' />\n"
209 "(?<proto>https?://)(?<host>[^/?# "'>]+)/(?<path>[^ "'>]+)'\n"
210 " to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
215 std::cout << xmlconf;
217 // reading and parsing XML conf
218 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
220 xmlNode *root_element = xmlDocGetRootElement(doc);
221 fhr.configure(root_element, true, "");
226 // create an http request
230 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
231 "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
233 pack.request() = gdu_req;
235 //create the http response
237 /* response, content */
238 const char *resp_buf =
239 /*123456789012345678 */
240 "HTTP/1.1 200 OK\r\n"
241 "Content-Length: 3000\r\n"
242 "Content-Type: text/html\r\n"
243 "Link: <http://targetsite/file.xml>; rel=absolute\r\r"
244 "Link: </dir/file.xml>; rel=relative\r\r"
246 "<html><head><title>Hello proxy!</title>"
249 " background-image:url('http://targetsite/images/bg.png');"
253 "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
255 "<p>Welcome to our website. It doesn't make it easy to get pro"
257 "<a href=\"http://targetsite/page2.html\">"
258 " An absolute link</a>"
259 "<a target=_blank href='http://targetsite/page3.html\">"
260 " Another abs link</a>"
261 "<a href=\"/docs/page4.html\" />"
264 /* response, content */
265 const char *resp_buf_rew =
266 /*123456789012345678 */
267 "HTTP/1.1 200 OK\r\n"
268 "Content-Length: 3000\r\n"
269 "Content-Type: text/html\r\n"
270 "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\r"
271 "Link: </dir/file.xml>; rel=relative\r\r"
273 "<html><head><title>Hello proxy!</title>"
276 " background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
280 "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
282 "<p>Welcome to our website. It doesn't make it easy to get pro"
284 "<a href=\"http://proxyhost/proxypath/targetsite/page.html\">"
285 " An absolute link</a>"
286 "<a target=_blank href='http://proxyhost/proxypath/targetsite/anotherpage.html\">"
287 " Another abs link</a>"
288 "<a href=\"/docs/page2.html\" />"
293 ODR odr2 = odr_createmem(ODR_DECODE);
294 odr_setbuf(odr2, (char *) resp_buf, strlen(resp_buf), 0);
295 r = z_GDU(odr2, &gdu_res, 0, 0);
300 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
303 pack.response() = gdu_res;
306 pack.router(router).move();
308 //analyze the response
309 Z_GDU *gdu_res_rew = pack.response().get();
310 BOOST_CHECK(gdu_res_rew);
311 BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
313 Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
316 //how to compare the buffers:
320 catch (std::exception & e) {
321 std::cout << e.what();
322 std::cout << std::endl;
331 * c-file-style: "Stroustrup"
332 * indent-tabs-mode: nil
334 * vim: shiftwidth=4 tabstop=8 expandtab