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