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 * The global testconfig is commented out, as it won't even compile
43 * on old Centos5 machines
47 std::cout << "global setup\n";
48 yaz_log_init_level(YLOG_ALL);
52 std::cout << "global teardown\n";
56 BOOST_GLOBAL_FIXTURE( TestConfig );
59 BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
63 std::cout << "Running non-xml config test case" << std::endl;
64 mp::RouterChain router;
65 mp::filter::HttpRewrite fhr;
68 "<?xml version='1.0'?>\n"
69 "<filter xmlns='http://indexdata.com/metaproxy'\n"
70 " id='rewrite1' type='http_rewrite'>\n"
73 "(?<proto>https?://)(?<pxhost>[^ /?#]+)/(?<pxpath>[^ /]+)"
74 "/(?<host>[^ /]+)(?<path>[^ ]*)'\n"
75 " to='${proto}${host}${path}' />\n"
76 " <rewrite from='(?:Host: )(.*)'\n"
77 " to='Host: ${host}' />\n"
81 "(?<proto>https?://)(?<host>[^/?# "'>]+)/(?<path>[^ "'>]+)'\n"
82 " to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
89 // reading and parsing XML conf
90 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
92 xmlNode *root_element = xmlDocGetRootElement(doc);
93 fhr.configure(root_element, true, "");
98 // create an http request
102 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
103 "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
105 pack.request() = gdu_req;
107 //create the http response
109 const char *resp_buf =
110 "HTTP/1.1 200 OK\r\n"
111 "Content-Length: 441\r\n"
112 "Content-Type: text/html\r\n"
113 "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
114 "Link: </dir/file.xml>; rel=relative\r\n"
116 "<html><head><title>Hello proxy!</title>"
119 " background-image:url('http://targetsite/images/bg.png');"
123 "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
125 "<p>Welcome to our website. It doesn't make it easy to get pro"
127 "<a href=\"http://targetsite/page2.html\">"
128 " An absolute link</a>"
129 "<a target=_blank href='http://targetsite/page3.html\">"
130 " Another abs link</a>"
131 "<a href=\"/docs/page4.html\" />"
134 const char *resp_expected =
135 "HTTP/1.1 200 OK\r\n"
136 "Content-Length: 521\r\n"
137 "Content-Type: text/html\r\n"
138 "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
139 "Link: </dir/file.xml>; rel=relative\r\n"
141 "<html><head><title>Hello proxy!</title>"
144 " background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
148 "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
150 "<p>Welcome to our website. It doesn't make it easy to get pro"
152 "<a href=\"http://proxyhost/proxypath/targetsite/page2.html\">"
153 " An absolute link</a>"
154 "<a target=_blank href='http://proxyhost/proxypath/targetsite/page3.html\">"
155 " Another abs link</a>"
156 "<a href=\"/docs/page4.html\" />"
161 ODR dec = odr_createmem(ODR_DECODE);
162 odr_setbuf(dec, (char *) resp_buf, strlen(resp_buf), 0);
163 r = z_GDU(dec, &gdu_res, 0, 0);
168 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
171 pack.response() = gdu_res;
174 pack.router(router).move();
176 //analyze the response
177 Z_GDU *gdu_res_rew = pack.response().get();
178 BOOST_CHECK(gdu_res_rew);
179 BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
181 Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
185 std::cout << "Expected result:\n" << resp_expected << std::endl;
187 ODR enc = odr_createmem(ODR_ENCODE);
188 z_GDU(enc, &gdu_res_rew, 0, 0);
191 resp_result = odr_getbuf(enc, &resp_result_len, 0);
193 BOOST_CHECK(resp_result);
194 BOOST_CHECK_EQUAL(resp_result_len, strlen(resp_expected));
196 std::cout << "Rewriten result:\n" << resp_result << std::endl;
197 std::cout << "Rewriten result buf len: " << resp_result_len
200 BOOST_CHECK(memcmp(resp_result, resp_expected, resp_result_len) == 0);
205 catch (std::exception & e) {
206 std::cout << e.what();
207 std::cout << std::endl;
213 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
217 std::cout << "Running xml config test case" << std::endl;
218 mp::RouterChain router;
219 mp::filter::HttpRewrite fhr;
221 std::string xmlconf =
222 "<?xml version='1.0'?>\n"
223 "<filter xmlns='http://indexdata.com/metaproxy'\n"
224 " id='rewrite1' type='http_rewrite'>\n"
227 "(?<proto>https?://)(?<pxhost>[^ /?#]+)/(?<pxpath>[^ /]+)"
228 "/(?<host>[^ /]+)(?<path>[^ ]*)'\n"
229 " to='${proto}${host}${path}' />\n"
230 " <rewrite from='(?:Host: )(.*)'\n"
231 " to='Host: ${host}' />\n"
235 "(?<proto>https?://)(?<host>[^/?# "'>]+)/(?<path>[^ "'>]+)'\n"
236 " to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
241 std::cout << xmlconf;
243 // reading and parsing XML conf
244 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
246 xmlNode *root_element = xmlDocGetRootElement(doc);
247 fhr.configure(root_element, true, "");
252 // create an http request
256 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
257 "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
259 pack.request() = gdu_req;
261 //create the http response
263 const char *resp_buf =
264 "HTTP/1.1 200 OK\r\n"
265 "Content-Length: 50\r\n"
266 "Content-Type: text/html\r\n"
267 "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
268 "Link: </dir/file.xml>; rel=relative\r\n"
270 "<html><head><title>Hello proxy!</title>"
273 " background-image:url('http://targetsite/images/bg.png');"
277 "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
279 "<p>Welcome to our website. It doesn't make it easy to get pro"
281 "<a href=\"http://targetsite/page2.html\">"
282 " An absolute link</a>"
283 "<a target=_blank href='http://targetsite/page3.html\">"
284 " Another abs link</a>"
285 "<a href=\"/docs/page4.html\" />"
288 const char *resp_buf_rew =
289 "HTTP/1.1 200 OK\r\n"
290 "Content-Length: 50\r\n"
291 "Content-Type: text/html\r\n"
292 "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
293 "Link: </dir/file.xml>; rel=relative\r\n"
295 "<html><head><title>Hello proxy!</title>"
298 " background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
302 "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
304 "<p>Welcome to our website. It doesn't make it easy to get pro"
306 "<a href=\"http://proxyhost/proxypath/targetsite/page.html\">"
307 " An absolute link</a>"
308 "<a target=_blank href='http://proxyhost/proxypath/targetsite/anotherpage.html\">"
309 " Another abs link</a>"
310 "<a href=\"/docs/page2.html\" />"
315 ODR odr2 = odr_createmem(ODR_DECODE);
316 odr_setbuf(odr2, (char *) resp_buf, strlen(resp_buf), 0);
317 r = z_GDU(odr2, &gdu_res, 0, 0);
322 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
325 pack.response() = gdu_res;
328 pack.router(router).move();
330 //analyze the response
331 Z_GDU *gdu_res_rew = pack.response().get();
332 BOOST_CHECK(gdu_res_rew);
333 BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
335 Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
338 //how to compare the buffers:
342 catch (std::exception & e) {
343 std::cout << e.what();
344 std::cout << std::endl;
353 * c-file-style: "Stroustrup"
354 * indent-tabs-mode: nil
356 * vim: shiftwidth=4 tabstop=8 expandtab