Factor out impl from test
[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 #define BOOST_AUTO_TEST_MAIN
33 #define BOOST_TEST_DYN_LINK
34
35 #include <boost/test/auto_unit_test.hpp>
36
37 using namespace boost::unit_test;
38 namespace mp = metaproxy_1;
39
40
41 BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
42 {
43     try
44     {
45         mp::filter::HttpRewrite fhr;
46     }
47     catch ( ... ) {
48         BOOST_CHECK (false);
49     }
50 }
51
52 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
53 {
54     try
55     {
56         mp::RouterChain router;
57
58         mp::filter::HttpRewrite fhr;
59         
60         mp::filter::HttpRewrite::spair_vec vec_req;
61         vec_req.push_back(std::make_pair(
62         "(?<proto>http\\:\\/\\/s?)(?<pxhost>[^\\/?#]+)\\/(?<pxpath>[^\\/]+)"
63         "\\/(?<host>[^\\/]+)(?<path>.*)",
64         "${proto}${host}${path}"
65         ));
66         vec_req.push_back(std::make_pair(
67         "(?:Host\\: )(.*)",
68         "Host: localhost"
69         ));
70
71         mp::filter::HttpRewrite::spair_vec vec_res;
72         vec_res.push_back(std::make_pair(
73         "(?<proto>http\\:\\/\\/s?)(?<host>[^\\/?#]+)\\/(?<path>[^ >]+)",
74         "http://${pxhost}/${pxpath}/${host}/${path}"
75         ));
76         
77         fhr.configure(vec_req, vec_res);
78
79         mp::filter::HTTPClient hc;
80         
81         router.append(fhr);
82         router.append(hc);
83
84         // create an http request
85         mp::Package pack;
86
87         mp::odr odr;
88         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, 
89         "http://proxyhost/proxypath/localhost:80/~jakub/targetsite.php", 0, 1);
90
91         pack.request() = gdu_req;
92
93         //feed to the router
94         pack.router(router).move();
95
96         //analyze the response
97         Z_GDU *gdu_res = pack.response().get();
98         BOOST_CHECK(gdu_res);
99         BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
100         
101         Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
102         BOOST_CHECK(hres);
103
104     }
105     catch (std::exception & e) {
106         std::cout << e.what();
107         BOOST_CHECK (false);
108     }
109 }
110
111 /*
112  * Local variables:
113  * c-basic-offset: 4
114  * c-file-style: "Stroustrup"
115  * indent-tabs-mode: nil
116  * End:
117  * vim: shiftwidth=4 tabstop=8 expandtab
118  */
119