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"
72 " <rule name=\"null\"/>\n"
73 " <rule name=\"url\">\n"
75 "(?<proto>https?://)(?<pxhost>[^ /?#]+)/(?<pxpath>[^ /]+)"
76 "/(?<host>[^ /]+)(?<path>[^ ]*)'\n"
77 " to='${proto}${host}${path}' />\n"
78 " <rewrite from='(?:Host: )(.*)'\n"
79 " to='Host: ${host}' />\n"
81 " <within header=\"link\" rule=\"null\"/>\n"
82 " <within reqline=\"1\" rule=\"url\"/>\n"
85 " <rule name=\"null\"/>\n"
86 " <rule name=\"url\">\n"
88 "(?<proto>https?://)(?<host>[^/?# "'>]+)/(?<path>[^ "'>]+)'\n"
89 " to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
91 " <within header=\"link\" rule=\"url\"/>\n"
92 " <within tag=\"script\" attr=\"src,#text\" rule=\"url\"/>\n"
93 " <within tag=\"style\" rule=\"url\"/>\n"
94 " <within attr=\"href,src\" rule=\"url\"/>\n"
101 // reading and parsing XML conf
102 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
104 xmlNode *root_element = xmlDocGetRootElement(doc);
105 fhr.configure(root_element, true, "");
110 // create an http request
114 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
115 "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
117 pack.request() = gdu_req;
119 //create the http response
121 const char *resp_buf =
122 "HTTP/1.1 200 OK\r\n"
123 "Content-Length: 441\r\n"
124 "Content-Type: text/html\r\n"
125 "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
126 "Link: </dir/file.xml>; rel=relative\r\n"
128 "<html><head><title>Hello proxy!</title>"
131 " background-image:url('http://targetsite/images/bg.png');"
135 "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
137 "<p>Welcome to our website. It doesn't make it easy to get pro"
139 "<a href=\"http://targetsite/page2.html\">"
140 " An absolute link</a>"
141 "<a target=_blank href=\"http://targetsite/page3.html\">"
142 " Another abs link</a>"
143 "<a href=\"/docs/page4.html\" />"
146 const char *resp_expected =
147 "HTTP/1.1 200 OK\r\n"
148 "Content-Length: 522\r\n"
149 "Content-Type: text/html\r\n"
150 "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
151 "Link: </dir/file.xml>; rel=relative\r\n"
153 "<html><head><title>Hello proxy!</title>"
156 " background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
160 "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
162 "<p>Welcome to our website. It doesn't make it easy to get pro"
164 "<a href=\"http://proxyhost/proxypath/targetsite/page2.html\">"
165 " An absolute link</a>"
166 "<a target=\"_blank\" href=\"http://proxyhost/proxypath/targetsite/page3.html\">"
167 " Another abs link</a>"
168 "<a href=\"/docs/page4.html\"/>"
173 ODR dec = odr_createmem(ODR_DECODE);
174 odr_setbuf(dec, (char *) resp_buf, strlen(resp_buf), 0);
175 r = z_GDU(dec, &gdu_res, 0, 0);
180 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
183 pack.response() = gdu_res;
186 pack.router(router).move();
188 //analyze the response
189 Z_GDU *gdu_res_rew = pack.response().get();
190 BOOST_CHECK(gdu_res_rew);
191 BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
193 Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
197 std::cout << "Expected result:\n" << resp_expected << std::endl;
199 ODR enc = odr_createmem(ODR_ENCODE);
200 z_GDU(enc, &gdu_res_rew, 0, 0);
203 resp_result = odr_getbuf(enc, &resp_result_len, 0);
205 BOOST_CHECK(resp_result);
206 BOOST_CHECK_EQUAL((size_t) resp_result_len, strlen(resp_expected));
208 std::cout << "Rewritten result:\n" << std::endl;
210 fwrite(resp_result, 1, resp_result_len, stdout);
212 std::cout << "\nRewritten result buf len: " << resp_result_len
215 BOOST_CHECK(memcmp(resp_result, resp_expected, resp_result_len) == 0);
220 catch (std::exception & e) {
221 std::cout << e.what();
222 std::cout << std::endl;
231 * c-file-style: "Stroustrup"
232 * indent-tabs-mode: nil
234 * vim: shiftwidth=4 tabstop=8 expandtab