[libsoup/carlosgc/logger: 4/4] logger: ensure request cookies are shown in the log




commit 176328f698cd28a609de764761b0f84d04c84b0d
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Fri Nov 20 15:01:48 2020 +0100

    logger: ensure request cookies are shown in the log
    
    Cookies feature includes the cookies request information in a
    SoupMessage::starting callback, same callback used by the logger to
    print the request information. If the cookies feature is created before
    the logger, the starting logger callback is called before the cookies
    one, and the cookies header hasn't been added yet. Use
    g_signal_connect_after() in the logger to ensure the callback is called
    after all other features have updated the message in the starting
    signal.

 libsoup/soup-logger.c |  6 +++---
 tests/logger-test.c   | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 3 deletions(-)
---
diff --git a/libsoup/soup-logger.c b/libsoup/soup-logger.c
index 83f138be..38d1627c 100644
--- a/libsoup/soup-logger.c
+++ b/libsoup/soup-logger.c
@@ -651,9 +651,9 @@ soup_logger_request_queued (SoupSessionFeature *logger,
 {
        g_return_if_fail (SOUP_IS_MESSAGE (msg));
 
-       g_signal_connect (msg, "starting",
-                         G_CALLBACK (starting),
-                         logger);
+       g_signal_connect_after (msg, "starting",
+                               G_CALLBACK (starting),
+                               logger);
        g_signal_connect (msg, "got-informational",
                          G_CALLBACK (got_informational),
                          logger);
diff --git a/tests/logger-test.c b/tests/logger-test.c
index 61e67a5a..b8d882ea 100644
--- a/tests/logger-test.c
+++ b/tests/logger-test.c
@@ -201,6 +201,52 @@ do_logger_filters_test (void)
         soup_test_session_abort_unref (session);
 }
 
+static void
+do_logger_cookies_test (void)
+{
+        SoupSession *session;
+        SoupLogger *logger;
+        GUri *uri;
+        SoupMessage *msg;
+        LogData log = { NULL, NULL };
+
+        session = soup_test_session_new (NULL);
+        soup_session_add_feature_by_type (session, SOUP_TYPE_COOKIE_JAR);
+
+        logger = soup_logger_new (SOUP_LOGGER_LOG_HEADERS);
+        soup_logger_set_printer (logger, (SoupLoggerPrinter)printer, &log, NULL);
+        soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger));
+        g_object_unref (logger);
+
+        uri = g_uri_parse_relative (base_uri, "/cookies", SOUP_HTTP_URI_FLAGS, NULL);
+        msg = soup_message_new_from_uri ("GET", uri);
+        soup_test_session_send_message (session, msg);
+        g_uri_unref (uri);
+        g_object_unref (msg);
+
+        g_assert_nonnull (log.request);
+        g_assert_false (g_hash_table_contains (log.request, "Cookie"));
+
+        g_assert_nonnull (log.response);
+        g_assert_true (g_hash_table_contains (log.response, "Set-Cookie"));
+        g_assert_cmpstr (g_hash_table_lookup (log.response, "Set-Cookie"), ==, "foo=bar");
+        log_data_clear (&log);
+
+        msg = soup_message_new_from_uri ("GET", base_uri);
+        soup_test_session_send_message (session, msg);
+        g_object_unref (msg);
+
+        g_assert_nonnull (log.request);
+        g_assert_true (g_hash_table_contains (log.request, "Cookie"));
+        g_assert_cmpstr (g_hash_table_lookup (log.request, "Cookie"), ==, "foo=bar");
+
+        g_assert_nonnull (log.response);
+        g_assert_false (g_hash_table_contains (log.response, "Set-Cookie"));
+        log_data_clear (&log);
+
+        soup_test_session_abort_unref (session);
+}
+
 static void
 server_callback (SoupServer        *server,
                  SoupServerMessage *msg,
@@ -208,6 +254,10 @@ server_callback (SoupServer        *server,
                  GHashTable        *query,
                  gpointer           data)
 {
+        if (g_str_equal (path, "/cookies")) {
+                soup_message_headers_replace (soup_server_message_get_response_headers (msg),
+                                              "Set-Cookie", "foo=bar");
+        }
         soup_server_message_set_status (msg, SOUP_STATUS_OK, NULL);
         soup_server_message_set_response (msg, "text/plain",
                                           SOUP_MEMORY_STATIC, "index", 5);
@@ -228,6 +278,7 @@ main (int argc, char **argv)
         g_test_add_func ("/logger/minimal", do_logger_minimal_test);
         g_test_add_func ("/logger/headers", do_logger_headers_test);
         g_test_add_func ("/logger/filters", do_logger_filters_test);
+        g_test_add_func ("/logger/cookies", do_logger_cookies_test);
 
         ret = g_test_run ();
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]