Humble beginings
[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 <metaproxy/util.hpp>
25 #include "router_chain.hpp"
26 #include <metaproxy/package.hpp>
27
28 #define BOOST_REGEX_MATCH_EXTRA
29
30 #include <boost/regex.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 class FilterHeaderRewrite: public mp::filter::Base {
41 public:
42     void process(mp::Package & package) const {
43         Z_GDU *gdu = package.request().get();
44         //we have an http req
45         if (gdu && gdu->which == Z_GDU_HTTP_Request)
46         {
47           std::cout << "Request headers" << std::endl;
48           Z_HTTP_Request *hreq = gdu->u.HTTP_Request;
49           //dump req headers
50           for (Z_HTTP_Header *header = hreq->headers;
51               header != 0; 
52               header = header->next) 
53           {
54             std::cout << header->name << ": " << header->value << std::endl;
55             rewrite_req_header(header);
56           }
57         }
58         package.move();
59         gdu = package.response().get();
60         if (gdu && gdu->which == Z_GDU_HTTP_Response)
61         {
62           std::cout << "Respose headers" << std::endl;
63           Z_HTTP_Response *hr = gdu->u.HTTP_Response;
64           //dump resp headers
65           for (Z_HTTP_Header *header = hr->headers;
66               header != 0; 
67               header = header->next) 
68           {
69             std::cout << header->name << ": " << header->value << std::endl;
70           }
71         }
72
73     };
74     void configure(const xmlNode* ptr, bool test_only, const char *path) {};
75
76     void rewrite_req_header(Z_HTTP_Header *header) const
77     {
78         //exec regex against value
79         boost::regex e(req_uri_pat, boost::regex::perl);
80         boost::smatch what;
81         std::string hvalue(header->value);
82         if(boost::regex_match(hvalue, what, e, boost::match_extra))
83         {
84             unsigned i, j;
85             std::cout << "** Match found **\n   Sub-Expressions:\n";
86             for(i = 0; i < what.size(); ++i)
87                 std::cout << "      $" << i << " = \"" << what[i] << "\"\n";
88             std::cout << "   Captures:\n";
89             for(i = 0; i < what.size(); ++i)
90             {
91                 std::cout << "      $" << i << " = {";
92                 for(j = 0; j < what.captures(i).size(); ++j)
93                 {
94                     if(j)
95                         std::cout << ", ";
96                     else
97                         std::cout << " ";
98                     std::cout << "\"" << what.captures(i)[j] << "\"";
99                 }
100                 std::cout << " }\n";
101             }
102         }
103         else
104         {
105             std::cout << "** No Match found **\n";
106         }
107         //iteratate over named groups
108         //set the captured values in the map
109         //rewrite the header according to the hardcoded recipe
110     };
111     
112     void configure(const std::string & req_uri_pat, 
113             const std::string & resp_uri_pat) 
114     {
115        this->req_uri_pat = req_uri_pat;
116        this->resp_uri_pat = resp_uri_pat;
117     };
118
119 private:
120     std::map<std::string, std::string> vars;
121     std::string req_uri_pat;
122     std::string resp_uri_pat;
123 };
124
125
126 BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
127 {
128     try
129     {
130        FilterHeaderRewrite fhr;
131     }
132     catch ( ... ) {
133         BOOST_CHECK (false);
134     }
135 }
136
137 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
138 {
139     try
140     {
141         mp::RouterChain router;
142
143         FilterHeaderRewrite fhr;
144         fhr.configure(".*(localhost).*", ".*(localhost).*");
145         mp::filter::HTTPClient hc;
146         
147         router.append(fhr);
148         router.append(hc);
149
150         // create an http request
151         mp::Package pack;
152
153         mp::odr odr;
154         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, 
155             "http://localhost:80/~jakub/targetsite.php", 0, 1);
156
157         pack.request() = gdu_req;
158
159         //feed to the router
160         pack.router(router).move();
161
162         //analyze the response
163         Z_GDU *gdu_res = pack.response().get();
164         BOOST_CHECK(gdu_res);
165         BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
166         
167         Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
168         BOOST_CHECK(hres);
169
170     }
171     catch ( ... ) {
172         BOOST_CHECK (false);
173     }
174 }
175
176 /*
177  * Local variables:
178  * c-basic-offset: 4
179  * c-file-style: "Stroustrup"
180  * indent-tabs-mode: nil
181  * End:
182  * vim: shiftwidth=4 tabstop=8 expandtab
183  */
184