http_rewrite using HTML parser
[metaproxy-moved-to-github.git] / src / test_filter_rewrite.cpp
index b16768f..65cb4c0 100644 (file)
@@ -42,14 +42,14 @@ namespace mp = metaproxy_1;
  * The global testconfig is commented out, as it won't even compile
  * on old Centos5 machines
 struct TestConfig {
-    TestConfig()   
+    TestConfig()
     {
-        std::cout << "global setup\n"; 
+        std::cout << "global setup\n";
         yaz_log_init_level(YLOG_ALL);
     }
-    ~TestConfig() 
-    { 
-        std::cout << "global teardown\n"; 
+    ~TestConfig()
+    {
+        std::cout << "global teardown\n";
     }
 };
 
@@ -63,12 +63,13 @@ BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
         std::cout << "Running non-xml config test case" << std::endl;
         mp::RouterChain router;
         mp::filter::HttpRewrite fhr;
-         
+
         std::string xmlconf =
             "<?xml version='1.0'?>\n"
             "<filter xmlns='http://indexdata.com/metaproxy'\n"
             "        id='rewrite1' type='http_rewrite'>\n"
             " <request>\n"
+            "   <rule name=\"null\"/>\n"
             "   <rule name=\"url\">\n"
             "     <rewrite from='"
     "(?&lt;proto>https?://)(?&lt;pxhost>[^ /?#]+)/(?&lt;pxpath>[^ /]+)"
@@ -77,16 +78,20 @@ BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
             "     <rewrite from='(?:Host: )(.*)'\n"
             "            to='Host: ${host}' />\n"
             "   </rule>\n"
-            "   <within rule=\"url\"/>\n"
+            "  <within header=\"link\" rule=\"null\"/>\n"
+            "  <within reqline=\"1\" rule=\"url\"/>\n"
             " </request>\n"
             " <response>\n"
+            "   <rule name=\"null\"/>\n"
             "   <rule name=\"url\">\n"
             "     <rewrite from='"
     "(?&lt;proto>https?://)(?&lt;host>[^/?# &quot;&apos;>]+)/(?&lt;path>[^  &quot;&apos;>]+)'\n"
-            "            to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n" 
+            "            to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
             "  </rule>\n"
-            "  <within rule=\"url\"/>\n"
-            "  <within tag=\"html\" rule=\"url\"/>\n"
+            "  <within header=\"link\" rule=\"url\"/>\n"
+            "  <within tag=\"script\" attr=\"src,#text\" rule=\"url\"/>\n"
+            "  <within tag=\"style\" rule=\"url\"/>\n"
+            "  <within attr=\"href,src\" rule=\"url\"/>\n"
             " </response>\n"
             "</filter>\n"
         ;
@@ -99,14 +104,14 @@ BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
         xmlNode *root_element = xmlDocGetRootElement(doc);
         fhr.configure(root_element, true, "");
         xmlFreeDoc(doc);
-       
+
         router.append(fhr);
 
         // create an http request
         mp::Package pack;
 
         mp::odr odr;
-        Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, 
+        Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
         "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
 
         pack.request() = gdu_req;
@@ -133,14 +138,14 @@ BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
             "xified"
             "<a href=\"http://targetsite/page2.html\">"
             "  An absolute link</a>"
-            "<a target=_blank href='http://targetsite/page3.html\">"
+            "<a target=_blank href=\"http://targetsite/page3.html\">"
             "  Another abs link</a>"
             "<a href=\"/docs/page4.html\" />"
             "</body></html>";
 
         const char *resp_expected =
             "HTTP/1.1 200 OK\r\n"
-            "Content-Length: 521\r\n"
+            "Content-Length: 522\r\n"
             "Content-Type: text/html\r\n"
             "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
             "Link: </dir/file.xml>; rel=relative\r\n"
@@ -158,9 +163,9 @@ BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
             "xified"
             "<a href=\"http://proxyhost/proxypath/targetsite/page2.html\">"
             "  An absolute link</a>"
-            "<a target=_blank href='http://proxyhost/proxypath/targetsite/page3.html\">"
+            "<a target=\"_blank\" href=\"http://proxyhost/proxypath/targetsite/page3.html\">"
             "  Another abs link</a>"
-            "<a href=\"/docs/page4.html\" />"
+            "<a href=\"/docs/page4.html\"/>"
             "</body></html>";
 
         int r;
@@ -184,7 +189,7 @@ BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
         Z_GDU *gdu_res_rew = pack.response().get();
         BOOST_CHECK(gdu_res_rew);
         BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
-        
+
         Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
         BOOST_CHECK(hres);
 
@@ -196,13 +201,15 @@ BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
         char *resp_result;
         int resp_result_len;
         resp_result = odr_getbuf(enc, &resp_result_len, 0);
-        
+
         BOOST_CHECK(resp_result);
         BOOST_CHECK_EQUAL((size_t) resp_result_len, strlen(resp_expected));
 
         std::cout << "Rewritten result:\n" << std::endl;
+        fflush(stdout);
         fwrite(resp_result, 1, resp_result_len, stdout);
-        std::cout << "\nRewritten result buf len: " << resp_result_len 
+        fflush(stdout);
+        std::cout << "\nRewritten result buf len: " << resp_result_len
             << std::endl;
 
         BOOST_CHECK(memcmp(resp_result, resp_expected, resp_result_len) == 0);