[libsoup/carlosgc/session-has-request: 3/3] soup-session: soup_session_has_feature doesn't work for requests



commit 3c7c666c26bbd9fefca1fb6fb48a51c9db68efd2
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Sat Jul 4 15:21:31 2020 +0200

    soup-session: soup_session_has_feature doesn't work for requests
    
    We are currently using the hash table value as a key in the lookup. So,
    we compare the feature type with the registewred schemes. We should get
    the list of schemes and find one in the table whose value matches the
    given type.

 libsoup/soup-session.c | 17 +++++++++++++++--
 tests/session-test.c   | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 2 deletions(-)
---
diff --git a/libsoup/soup-session.c b/libsoup/soup-session.c
index f45d80e7..b8608538 100644
--- a/libsoup/soup-session.c
+++ b/libsoup/soup-session.c
@@ -2878,8 +2878,21 @@ soup_session_has_feature (SoupSession *session,
                                return TRUE;
                }
        } else if (g_type_is_a (feature_type, SOUP_TYPE_REQUEST)) {
-               return g_hash_table_lookup (priv->request_types,
-                                           GSIZE_TO_POINTER (feature_type)) != NULL;
+               SoupRequestClass *request_class;
+               int i;
+
+               request_class = g_type_class_peek (feature_type);
+               if (!request_class)
+                       return FALSE;
+
+               for (i = 0; request_class->schemes[i]; i++) {
+                       gpointer type;
+
+                       type = g_hash_table_lookup (priv->request_types,
+                                                   request_class->schemes[i]);
+                       if (type && g_type_is_a (GPOINTER_TO_SIZE (type), feature_type))
+                               return TRUE;
+               }
        } else {
                for (f = priv->features; f; f = f->next) {
                        if (soup_session_feature_has_feature (f->data, feature_type))
diff --git a/tests/session-test.c b/tests/session-test.c
index 8957a258..eb8cbc13 100644
--- a/tests/session-test.c
+++ b/tests/session-test.c
@@ -370,6 +370,46 @@ do_property_tests (void)
        g_object_unref (session);
 }
 
+static gint
+compare_by_gtype (gconstpointer a,
+                 gconstpointer b)
+{
+       return G_TYPE_CHECK_INSTANCE_TYPE (a, GPOINTER_TO_SIZE (b)) ? 0 : 1;
+}
+
+static void
+do_features_test (void)
+{
+       SoupSession *session;
+       GSList *features;
+       SoupSessionFeature *feature;
+
+       session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
+
+       features = soup_session_get_features (session, SOUP_TYPE_SESSION_FEATURE);
+       /* SoupAuthManager is always added */
+       g_assert_cmpuint (g_slist_length (features), >=, 1);
+       g_assert_nonnull (g_slist_find_custom (features, GSIZE_TO_POINTER (SOUP_TYPE_AUTH_MANAGER), 
compare_by_gtype));
+       g_assert_true (soup_session_has_feature (session, SOUP_TYPE_AUTH_MANAGER));
+       feature = soup_session_get_feature (session, SOUP_TYPE_AUTH_MANAGER);
+       g_assert_true (SOUP_IS_AUTH_MANAGER (feature));
+       soup_session_remove_feature (session, feature);
+       g_assert_false (soup_session_has_feature (session, SOUP_TYPE_AUTH_MANAGER));
+       g_assert_null (soup_session_get_feature (session, SOUP_TYPE_AUTH_MANAGER));
+       g_slist_free (features);
+
+       /* HTTP, File and Data requests are always added */
+       g_assert_true (soup_session_has_feature (session, SOUP_TYPE_REQUEST_HTTP));
+       g_assert_true (soup_session_has_feature (session, SOUP_TYPE_REQUEST_FILE));
+       g_assert_true (soup_session_has_feature (session, SOUP_TYPE_REQUEST_DATA));
+       soup_session_remove_feature_by_type (session, SOUP_TYPE_REQUEST_FILE);
+       g_assert_false (soup_session_has_feature (session, SOUP_TYPE_REQUEST_FILE));
+       g_assert_true (soup_session_has_feature (session, SOUP_TYPE_REQUEST_HTTP));
+       g_assert_true (soup_session_has_feature (session, SOUP_TYPE_REQUEST_DATA));
+
+       soup_test_session_abort_unref (session);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -388,6 +428,7 @@ main (int argc, char **argv)
        g_test_add_data_func ("/session/SoupSessionSync", uri, do_sync_tests);
        g_test_add_data_func ("/session/priority", uri, do_priority_tests);
        g_test_add_func ("/session/property", do_property_tests);
+       g_test_add_func ("/session/features", do_features_test);
 
        ret = g_test_run ();
 


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