]> source.dussan.org Git - rspamd.git/commitdiff
[Test] Add unit tests 4814/head
authorVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 12 Feb 2024 15:36:29 +0000 (15:36 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 12 Feb 2024 15:36:29 +0000 (15:36 +0000)
src/libserver/http/http_message.c
src/libserver/http/http_message.h
test/rspamd_cxx_unit_utils.hxx

index 4b68d1ecb86b4158b582fad53cceda115a6e99f5..e02827a76a97ab72e718e4d5d309aa2f875eb9a5 100644 (file)
@@ -736,4 +736,14 @@ bool rspamd_http_message_is_standard_port(struct rspamd_http_message *msg)
        }
 
        return msg->port == 80;
+}
+
+const gchar *rspamd_http_message_get_url(struct rspamd_http_message *msg, gsize *len)
+{
+       if (msg->url) {
+               *len = msg->url->len;
+               return msg->url->str;
+       }
+
+       return NULL;
 }
\ No newline at end of file
index fa8ed04c2d248d9b878298e98e5614bd7110b598..a483b316a2b73112b6d7f2ee3c2b898713d00c00 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2019 Vsevolod Stakhov
+/*
+ * Copyright 2024 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -247,6 +247,8 @@ const gchar *rspamd_http_message_get_http_host(struct rspamd_http_message *msg,
  */
 bool rspamd_http_message_is_standard_port(struct rspamd_http_message *msg);
 
+const gchar *rspamd_http_message_get_url(struct rspamd_http_message *msg, gsize *len);
+
 #ifdef __cplusplus
 }
 #endif
index 126253fd664cae608e3cb607e61abe22974fb661..1dea7af9d7eee9727994b9467d52be6334618a53 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2023 Vsevolod Stakhov
+ * Copyright 2024 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
 #include "libmime/mime_headers.h"
 #include "contrib/libottery/ottery.h"
 #include "libcryptobox/cryptobox.h"
+#include "libserver/http/http_message.h"
 
 #include <vector>
 #include <utility>
@@ -204,6 +205,32 @@ TEST_SUITE("rspamd_utils")
                        }
                }
        }
+
+       TEST_CASE("rspamd_http_message_from_url")
+       {
+               std::vector<std::pair<std::string, std::string>> cases{
+                       {"http://example.com", "/"},
+                       {"http://example.com/", "/"},
+                       {"http://example.com/lol", "/lol"},
+                       {"http://example.com/lol#keke", "/lol"},
+                       {"http://example.com/lol?omg=huh&oh", "/lol?omg=huh&oh"},
+                       {"http://example.com/lol?omg=huh&oh#", "/lol?omg=huh&oh"},
+                       {"http://example.com/lol?omg=huh&oh#keke", "/lol?omg=huh&oh"},
+                       {"http://example.com/lol?", "/lol"},
+                       {"http://example.com/lol?#", "/lol"},
+               };
+
+               for (const auto &c: cases) {
+                       SUBCASE(("rspamd_http_message_from_url: " + c.first).c_str())
+                       {
+                               auto *msg = rspamd_http_message_from_url(c.first.c_str());
+                               std::size_t nlen;
+                               auto *path = rspamd_http_message_get_url(msg, &nlen);
+                               CHECK(std::string{path, nlen} == c.second);
+                               rspamd_http_message_unref(msg);
+                       }
+               }
+       }
 }
 
 #endif