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