Use yaz_log
[metaproxy-moved-to-github.git] / src / test_filter_rewrite.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "config.hpp"
20 #include <iostream>
21 #include <stdexcept>
22
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>
28
29 #include <boost/regex.hpp>
30 #include <boost/lexical_cast.hpp>
31
32 #include <yaz/log.h>
33
34 #define BOOST_AUTO_TEST_MAIN
35 #define BOOST_TEST_DYN_LINK
36
37 #include <boost/test/auto_unit_test.hpp>
38
39 using namespace boost::unit_test;
40 namespace mp = metaproxy_1;
41
42 struct TestConfig {
43     TestConfig()   
44     {
45         std::cout << "global setup\n"; 
46         yaz_log_init_level(YLOG_ALL);
47     }
48     ~TestConfig() 
49     { 
50         std::cout << "global teardown\n"; 
51     }
52 };
53
54 BOOST_GLOBAL_FIXTURE( TestConfig );
55
56 BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
57 {
58     try
59     {
60         mp::filter::HttpRewrite fhr;
61     }
62     catch ( ... ) {
63         BOOST_CHECK (false);
64     }
65 }
66
67 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
68 {
69     try
70     {
71         mp::RouterChain router;
72
73         mp::filter::HttpRewrite fhr;
74         
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}"
80         ));
81         vec_req.push_back(std::make_pair(
82         "(?:Host\\: )(.*)",
83         "Host: localhost"
84         ));
85
86         mp::filter::HttpRewrite::spair_vec vec_res;
87         vec_res.push_back(std::make_pair(
88         "(?<proto>http\\:\\/\\/s?)(?<host>[^\\/?#]+)\\/(?<path>[^ >]+)",
89         "http://${pxhost}/${pxpath}/${host}/${path}"
90         ));
91         
92         fhr.configure(vec_req, vec_res);
93
94         mp::filter::HTTPClient hc;
95         
96         router.append(fhr);
97         router.append(hc);
98
99         // create an http request
100         mp::Package pack;
101
102         mp::odr odr;
103         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, 
104         "http://proxyhost/proxypath/localhost:80/~jakub/targetsite.php", 0, 1);
105
106         pack.request() = gdu_req;
107
108         //feed to the router
109         pack.router(router).move();
110
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);
115         
116         Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
117         BOOST_CHECK(hres);
118
119     }
120     catch (std::exception & e) {
121         std::cout << e.what();
122         std::cout << std::endl;
123         BOOST_CHECK (false);
124     }
125 }
126
127 BOOST_AUTO_TEST_CASE( test_filter_rewrite_3 )
128 {
129     try
130     {
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"
135             " <request>\n"
136             "   <rewrite from='"
137     "(?&lt;proto>http\\:\\/\\/s?)(?&lt;pxhost>[^\\/?#]+)\\/(?&lt;pxpath>[^\\/]+)"
138     "\\/(?&lt;host>[^\\/]+)(?&lt;path>.*)'\n"
139             "            to='${proto}${host}${path}' />\n"
140             "   <rewrite from='(?:Host\\: )(.*)'\n"
141             "            to='Host: localhost' />\n" 
142             " </request>\n"
143             " <response>\n"
144             "   <rewrite from='"
145     "(?&lt;proto>http\\:\\/\\/s?)(?&lt;host>[^\\/?#]+)\\/(?&lt;path>[^ >]+)'\n"
146             "            to='http://${pxhost}/${pxpath}/${host}/${path}' />\n" 
147             " </response>\n"
148             "</filter>\n"
149             ;
150
151         std::cout << xmlconf;
152
153         // reading and parsing XML conf
154         xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
155         BOOST_CHECK(doc);
156         xmlNode *root_element = xmlDocGetRootElement(doc);
157         mp::filter::HttpRewrite fhr; 
158         fhr.configure(root_element, true, "");
159         xmlFreeDoc(doc);
160
161         mp::filter::HTTPClient hc;
162         
163         mp::RouterChain router;
164         router.append(fhr);
165         router.append(hc);
166
167         // create an http request
168         mp::Package pack;
169
170         mp::odr odr;
171         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, 
172         "http://proxyhost/proxypath/localhost:80/~jakub/targetsite.php", 0, 1);
173
174         pack.request() = gdu_req;
175
176         //feed to the router
177         pack.router(router).move();
178
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);
183         
184         Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
185         BOOST_CHECK(hres);
186
187     }
188     catch (std::exception & e) {
189         std::cout << e.what();
190         std::cout << std::endl;
191         BOOST_CHECK (false);
192     }
193 }
194
195 /*
196  * Local variables:
197  * c-basic-offset: 4
198  * c-file-style: "Stroustrup"
199  * indent-tabs-mode: nil
200  * End:
201  * vim: shiftwidth=4 tabstop=8 expandtab
202  */
203