add rewrite_url tests

Sun, 07 Dec 2025 17:37:05 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 07 Dec 2025 17:37:05 +0100
changeset 652
dd90c858eb74
parent 651
ed74879c7041
child 653
c4dae65155e9

add rewrite_url tests

src/server/daemon/location.c file | annotate | diff | comparison | revisions
src/server/daemon/rewrite.c file | annotate | diff | comparison | revisions
src/server/test/main.c file | annotate | diff | comparison | revisions
src/server/test/rewrite.c file | annotate | diff | comparison | revisions
src/server/test/rewrite.h file | annotate | diff | comparison | revisions
--- a/src/server/daemon/location.c	Sun Dec 07 17:06:21 2025 +0100
+++ b/src/server/daemon/location.c	Sun Dec 07 17:37:05 2025 +0100
@@ -224,7 +224,7 @@
     } else if(loc->match == WS_LOCATION_MATCH_PREFIX) {
         return cx_strprefix(uri, loc->match_string);
     } else {
-        return regexec(&loc->regex, uri.ptr, 0, match, WS_LOCATION_NMATCH) == 0;
+        return regexec(&loc->regex, uri.ptr, WS_LOCATION_NMATCH, match, 0) == 0;
     }
     return 0;
 }
--- a/src/server/daemon/rewrite.c	Sun Dec 07 17:06:21 2025 +0100
+++ b/src/server/daemon/rewrite.c	Sun Dec 07 17:37:05 2025 +0100
@@ -96,7 +96,7 @@
     
     regmatch_t rewrite_match[WS_REWRITE_NMATCH];
     if(rule->has_regex) {
-        if(regexec(&rule->regex, url, 0, rewrite_match, WS_REWRITE_NMATCH) != 0) {
+        if(regexec(&rule->regex, url, WS_REWRITE_NMATCH, rewrite_match, 0) != 0) {
             return 0;
         }
         vars.match = rewrite_match;
--- a/src/server/test/main.c	Sun Dec 07 17:06:21 2025 +0100
+++ b/src/server/test/main.c	Sun Dec 07 17:37:05 2025 +0100
@@ -151,6 +151,7 @@
     // rewrite tests
     cx_test_register(suite, test_rewrite_rule_create);
     cx_test_register(suite, test_rewrite_url_no_regex);
+    cx_test_register(suite, test_rewrite_url);
     
     // xml tests
     cx_test_register(suite, test_wsxml_iterator);
--- a/src/server/test/rewrite.c	Sun Dec 07 17:06:21 2025 +0100
+++ b/src/server/test/rewrite.c	Sun Dec 07 17:37:05 2025 +0100
@@ -73,6 +73,10 @@
     memset(&rule, 0, sizeof(RewriteRule));
     rule.url = string_template_compile(a, cx_str("/static/"));
     
+    RewriteRule rule2;
+    memset(&rule2, 0, sizeof(RewriteRule));
+    rule2.url = string_template_compile(a, cx_str("/dynamic/$0"));
+    
     CX_TEST_DO {
         char *new_url = NULL;
         int r = rewrite_url(&rule, NULL, 0, a, "/previous/url", &new_url);
@@ -80,8 +84,62 @@
         CX_TEST_ASSERT(r == 0);
         CX_TEST_ASSERT(new_url != NULL);
         CX_TEST_ASSERT(!strcmp(new_url, "/static/"));
+        
+        new_url = NULL;
+        regmatch_t m;
+        m.rm_so = 6;
+        m.rm_eo = 9;
+        r = rewrite_url(&rule2, &m, 1, a, "/test/url", &new_url);
+        
+        CX_TEST_ASSERT(r == 0);
+        CX_TEST_ASSERT(new_url != NULL);
+        CX_TEST_ASSERT(!strcmp(new_url, "/dynamic/url"));
     }
     
     server_config_destroy(&cfg);
     cxMempoolFree(mp);
 }
+
+CX_TEST(test_rewrite_url) {
+    CxMempool *mp = cxMempoolCreate(100, CX_MEMPOOL_TYPE_ADVANCED);
+    const CxAllocator *a = mp->allocator;
+    
+    ServerConfiguration cfg = { NULL };
+    cfg.a = a;
+    cfg.destr = cxLinkedListCreate(a, NULL, sizeof(ScfgDestr));
+    
+    RewriteRule *rule0 = rewrite_rule_create(&cfg, cx_mutstr("test.*"), cx_mutstr("/rewrite"));
+    RewriteRule *rule1 = rewrite_rule_create(&cfg, cx_mutstr("/abc/(.*)\\.html"), cx_mutstr("/newpath/$1.md"));
+    RewriteRule *rule2 = rewrite_rule_create(&cfg, cx_mutstr("/abc/(.*)/dir/(.*)"), cx_mutstr("/new/$1/files/$2"));
+    
+    CX_TEST_DO {
+        char *new_url = NULL;
+        int r = rewrite_url(rule0, NULL, 0, a, "nomatch", &new_url);
+        
+        CX_TEST_ASSERT(r == 0);
+        CX_TEST_ASSERT(new_url == NULL);
+        
+        r = rewrite_url(rule0, NULL, 0, a, "test_match", &new_url);
+        
+        CX_TEST_ASSERT(r == 0);
+        CX_TEST_ASSERT(new_url != NULL);
+        CX_TEST_ASSERT(!strcmp(new_url, "/rewrite"));
+        
+        new_url = NULL;
+        r = rewrite_url(rule1, NULL, 0, a, "/abc/index.html", &new_url);
+        
+        CX_TEST_ASSERT(r == 0);
+        CX_TEST_ASSERT(new_url != NULL);
+        CX_TEST_ASSERT(!strcmp(new_url, "/newpath/index.md"));
+        
+        new_url = NULL;
+        r = rewrite_url(rule2, NULL, 0, a, "/abc/sub/dir/index.html", &new_url);
+        
+        CX_TEST_ASSERT(r == 0);
+        CX_TEST_ASSERT(new_url != NULL);
+        CX_TEST_ASSERT(!strcmp(new_url, "/new/sub/files/index.html"));
+    }
+    
+    server_config_destroy(&cfg);
+    cxMempoolFree(mp);
+}
--- a/src/server/test/rewrite.h	Sun Dec 07 17:06:21 2025 +0100
+++ b/src/server/test/rewrite.h	Sun Dec 07 17:37:05 2025 +0100
@@ -37,6 +37,7 @@
 
 CX_TEST(test_rewrite_rule_create);
 CX_TEST(test_rewrite_url_no_regex);
+CX_TEST(test_rewrite_url);
 
 
 #ifdef __cplusplus

mercurial