Try to comapte buffers
[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         std::cout << "Running non-xml config test case" << std::endl;
61         mp::RouterChain router;
62         mp::filter::HttpRewrite fhr;
63         
64         //configure the filter
65         mp::filter::HttpRewrite::spair_vec vec_req;
66         vec_req.push_back(std::make_pair(
67         "(?<proto>http\\:\\/\\/s?)(?<pxhost>[^\\/?#]+)\\/(?<pxpath>[^\\/]+)"
68         "\\/(?<host>[^\\/]+)(?<path>.*)",
69         "${proto}${host}${path}"
70         ));
71         vec_req.push_back(std::make_pair(
72         "(?:Host\\: )(.*)",
73         "Host: ${host}"
74         ));
75
76         mp::filter::HttpRewrite::spair_vec vec_res;
77         vec_res.push_back(std::make_pair(
78         "(?<proto>http\\:\\/\\/s?)(?<host>[^\\/?# \"'>]+)\\/(?<path>[^ \"'>]+)",
79         "${proto}${pxhost}/${pxpath}/${host}/${path}"
80         ));
81         
82         fhr.configure(vec_req, vec_res);
83         
84         router.append(fhr);
85
86         // create an http request
87         mp::Package pack;
88
89         mp::odr odr;
90         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, 
91         "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
92
93         pack.request() = gdu_req;
94
95         //create the http response
96
97         /* response, content  */
98         const char *resp_buf =
99             /*123456789012345678 */
100             "HTTP/1.1 200 OK\r\n"
101             "Content-Length: 50\r\n"
102             "Content-Type: text/html\r\n"
103             "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
104             "Link: </dir/file.xml>; rel=relative\r\n"
105             "\r\n"
106             "<html><head><title>Hello proxy!</title>"
107             "<style>"
108             "body {"
109             "  background-image:url('http://targetsite/images/bg.png');"
110             "}"
111             "</style>"
112             "</head>"
113             "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
114             "<body>"
115             "<p>Welcome to our website. It doesn't make it easy to get pro"
116             "xified"
117             "<a href=\"http://targetsite/page2.html\">"
118             "  An absolute link</a>"
119             "<a target=_blank href='http://targetsite/page3.html\">"
120             "  Another abs link</a>"
121             "<a href=\"/docs/page4.html\" />"
122             "</body></html>";
123
124         /* response, content  */
125         const char *resp_result =
126             /*123456789012345678 */
127             "HTTP/1.1 200 OK\r\n"
128             "Content-Length: 50\r\n"
129             "Content-Type: text/html\r\n"
130             "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
131             "Link: </dir/file.xml>; rel=relative\r\n"
132             "\r\n"
133             "<html><head><title>Hello proxy!</title>"
134             "<style>"
135             "body {"
136             "  background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
137             "}"
138             "</style>"
139             "</head>"
140             "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
141             "<body>"
142             "<p>Welcome to our website. It doesn't make it easy to get pro"
143             "xified"
144             "<a href=\"http://proxyhost/proxypath/targetsite/page.html\">"
145             "  An absolute link</a>"
146             "<a target=_blank href='http://proxyhost/proxypath/targetsite/page3.html\">"
147             "  Another abs link</a>"
148             "<a href=\"/docs/page2.html\" />"
149             "</body></html>";
150
151         int r;
152         Z_GDU *gdu_res;
153         ODR dec = odr_createmem(ODR_DECODE);
154         odr_setbuf(dec, (char *) resp_buf, strlen(resp_buf), 0);
155         r = z_GDU(dec, &gdu_res, 0, 0);
156
157         BOOST_CHECK(r == 0);
158         if (r)
159         {
160             BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
161         }
162
163         pack.response() = gdu_res;
164
165         //feed to the router
166         pack.router(router).move();
167
168         //analyze the response
169         Z_GDU *gdu_res_rew = pack.response().get();
170         BOOST_CHECK(gdu_res_rew);
171         BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
172         
173         Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
174         BOOST_CHECK(hres);
175
176         //how to compare the buffers:
177         ODR enc = odr_createmem(ODR_ENCODE);
178         Z_GDU *zgdu;
179         Z_GDU(enc, &zgdu, 0, 0);
180         char *resp_result;
181         int resp_result_len;
182         resp_result = odr_getbuf(enc, &resp_result_len, 0);
183         
184         BOOST_CHECK(memcmp(resp_result, resp_expected, resp_result_len));
185
186         odr_destroy(dec);
187     }
188     catch (std::exception & e) {
189         std::cout << e.what();
190         std::cout << std::endl;
191         BOOST_CHECK (false);
192     }
193 }
194
195 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
196 {
197     try
198     {
199         std::cout << "Running xml config test case" << std::endl;
200         mp::RouterChain router;
201         mp::filter::HttpRewrite fhr;
202
203         std::string xmlconf =
204             "<?xml version='1.0'?>\n"
205             "<filter xmlns='http://indexdata.com/metaproxy'\n"
206             "        id='rewrite1' type='http_rewrite'>\n"
207             " <request>\n"
208             "   <rewrite from='"
209     "(?&lt;proto>https?://)(?&lt;pxhost>[^ /?#]+)/(?&lt;pxpath>[^ /]+)"
210     "/(?&lt;host>[^ /]+)(?&lt;path>[^ ]*)'\n"
211             "            to='${proto}${host}${path}' />\n"
212             "   <rewrite from='(?:Host: )(.*)'\n"
213             "            to='Host: ${host}' />\n" 
214             " </request>\n"
215             " <response>\n"
216             "   <rewrite from='"
217     "(?&lt;proto>https?://)(?&lt;host>[^/?# &quot;&apos;>]+)/(?&lt;path>[^  &quot;&apos;>]+)'\n"
218             "            to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n" 
219             " </response>\n"
220             "</filter>\n"
221         ;
222
223         std::cout << xmlconf;
224
225         // reading and parsing XML conf
226         xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
227         BOOST_CHECK(doc);
228         xmlNode *root_element = xmlDocGetRootElement(doc);
229         fhr.configure(root_element, true, "");
230         xmlFreeDoc(doc);
231         
232         router.append(fhr);
233
234         // create an http request
235         mp::Package pack;
236
237         mp::odr odr;
238         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, 
239         "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
240
241         pack.request() = gdu_req;
242
243         //create the http response
244
245         /* response, content  */
246         const char *resp_buf =
247             /*123456789012345678 */
248             "HTTP/1.1 200 OK\r\n"
249             "Content-Length: 50\r\n"
250             "Content-Type: text/html\r\n"
251             "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
252             "Link: </dir/file.xml>; rel=relative\r\n"
253             "\r\n"
254             "<html><head><title>Hello proxy!</title>"
255             "<style>"
256             "body {"
257             "  background-image:url('http://targetsite/images/bg.png');"
258             "}"
259             "</style>"
260             "</head>"
261             "<script>var jslink=\"http://targetsite/webservice.xml\";</script>"
262             "<body>"
263             "<p>Welcome to our website. It doesn't make it easy to get pro"
264             "xified"
265             "<a href=\"http://targetsite/page2.html\">"
266             "  An absolute link</a>"
267             "<a target=_blank href='http://targetsite/page3.html\">"
268             "  Another abs link</a>"
269             "<a href=\"/docs/page4.html\" />"
270             "</body></html>";
271
272         /* response, content  */
273         const char *resp_buf_rew =
274             /*123456789012345678 */
275             "HTTP/1.1 200 OK\r\n"
276             "Content-Length: 50\r\n"
277             "Content-Type: text/html\r\n"
278             "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
279             "Link: </dir/file.xml>; rel=relative\r\n"
280             "\r\n"
281             "<html><head><title>Hello proxy!</title>"
282             "<style>"
283             "body {"
284             "  background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
285             "}"
286             "</style>"
287             "</head>"
288             "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";</script>"
289             "<body>"
290             "<p>Welcome to our website. It doesn't make it easy to get pro"
291             "xified"
292             "<a href=\"http://proxyhost/proxypath/targetsite/page.html\">"
293             "  An absolute link</a>"
294             "<a target=_blank href='http://proxyhost/proxypath/targetsite/anotherpage.html\">"
295             "  Another abs link</a>"
296             "<a href=\"/docs/page2.html\" />"
297             "</body></html>";
298
299         int r;
300         Z_GDU *gdu_res;
301         ODR odr2 = odr_createmem(ODR_DECODE);
302         odr_setbuf(odr2, (char *) resp_buf, strlen(resp_buf), 0);
303         r = z_GDU(odr2, &gdu_res, 0, 0);
304
305         BOOST_CHECK(r == 0);
306         if (r)
307         {
308             BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
309         }
310
311         pack.response() = gdu_res;
312
313         //feed to the router
314         pack.router(router).move();
315
316         //analyze the response
317         Z_GDU *gdu_res_rew = pack.response().get();
318         BOOST_CHECK(gdu_res_rew);
319         BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
320         
321         Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
322         BOOST_CHECK(hres);
323
324         //how to compare the buffers:
325
326         odr_destroy(odr2);
327     }
328     catch (std::exception & e) {
329         std::cout << e.what();
330         std::cout << std::endl;
331         BOOST_CHECK (false);
332     }
333 }
334
335
336 /*
337  * Local variables:
338  * c-basic-offset: 4
339  * c-file-style: "Stroustrup"
340  * indent-tabs-mode: nil
341  * End:
342  * vim: shiftwidth=4 tabstop=8 expandtab
343  */
344