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 mp::filter::HttpRewrite fhr;
67 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
71 mp::RouterChain router;
73 mp::filter::HttpRewrite fhr;
75 mp::filter::HttpRewrite::spair_vec vec_req;
76 vec_req.push_back(std::make_pair(
77 "(?<proto>http\\:\\/\\/s?)(?<pxhost>[^\\/?#]+)\\/(?<pxpath>[^\\/]+)"
78 "\\/(?<host>[^\\/]+)(?<path>.*)",
79 "${proto}${host}${path}"
81 vec_req.push_back(std::make_pair(
86 mp::filter::HttpRewrite::spair_vec vec_res;
87 vec_res.push_back(std::make_pair(
88 "(?<proto>http\\:\\/\\/s?)(?<host>[^\\/?#]+)\\/(?<path>[^ >]+)",
92 fhr.configure(vec_req, vec_res);
94 mp::filter::HTTPClient hc;
99 // create an http request
103 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
104 "http://proxyhost/proxypath/localhost:80/~jakub/targetsite.php", 0, 1);
106 pack.request() = gdu_req;
109 pack.router(router).move();
111 //analyze the response
112 Z_GDU *gdu_res = pack.response().get();
113 BOOST_CHECK(gdu_res);
114 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
116 Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
120 catch (std::exception & e) {
121 std::cout << e.what();
122 std::cout << std::endl;
127 BOOST_AUTO_TEST_CASE( test_filter_rewrite_3 )
131 std::string xmlconf =
132 "<?xml version='1.0'?>\n"
133 "<filter xmlns='http://indexdata.com/metaproxy'\n"
134 " id='rewrite1' type='http_rewrite'>\n"
137 "(?<proto>http\\:\\/\\/s?)(?<pxhost>[^\\/?#]+)\\/(?<pxpath>[^\\/]+)"
138 "\\/(?<host>[^\\/]+)(?<path>.*)'\n"
139 " to='${proto}${host}${path}' />\n"
140 " <rewrite from='(?:Host\\: )(.*)'\n"
141 " to='Host: localhost' />\n"
145 "(?<proto>http\\:\\/\\/s?)(?<host>[^\\/?#]+)\\/(?<path>[^ >]+)'\n"
146 " to='THAT WAS MATCHED' />\n"
151 std::cout << xmlconf;
153 // reading and parsing XML conf
154 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
156 xmlNode *root_element = xmlDocGetRootElement(doc);
157 mp::filter::HttpRewrite fhr;
158 fhr.configure(root_element, true, "");
161 mp::filter::HTTPClient hc;
163 mp::RouterChain router;
167 // create an http request
171 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
172 "http://proxyhost/proxypath/localhost:80/~jakub/targetsite.php", 0, 1);
174 pack.request() = gdu_req;
177 pack.router(router).move();
179 //analyze the response
180 Z_GDU *gdu_res = pack.response().get();
181 BOOST_CHECK(gdu_res);
182 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
184 Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
188 catch (std::exception & e) {
189 std::cout << e.what();
190 std::cout << std::endl;
198 * c-file-style: "Stroustrup"
199 * indent-tabs-mode: nil
201 * vim: shiftwidth=4 tabstop=8 expandtab