[libsoup/carlosgc/remove-old-api: 2/4] tests: stop using soup_session_queue_message in tests




commit 5b416ef85d307442793bbf92cf0ff294df46df85
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Tue Sep 15 16:20:15 2020 +0200

    tests: stop using soup_session_queue_message in tests
    
    Except for a few tests that seem to be testing the old API that we will
    remove once the old api is gone.

 tests/auth-test.c       | 51 +++++++++++++++++++------------
 tests/connection-test.c | 24 +++++++--------
 tests/context-test.c    | 11 +++++--
 tests/continue-test.c   |  4 ++-
 tests/misc-test.c       | 79 ++++++++++++++++++++++++++++++++++---------------
 tests/pull-api-test.c   | 42 +++++++++++++-------------
 tests/range-test.c      | 32 +++++++++++++-------
 tests/server-test.c     | 34 ++++++++++++++-------
 tests/session-test.c    | 28 ++++++++++++------
 tests/sniffing-test.c   |  8 +++--
 tests/ssl-test.c        |  7 +++--
 tests/streaming-test.c  | 10 ++++---
 tests/test-utils.c      | 69 +++++++++++++++++++++++++++++++++++++-----
 tests/test-utils.h      |  9 ++++--
 14 files changed, 279 insertions(+), 129 deletions(-)
---
diff --git a/tests/auth-test.c b/tests/auth-test.c
index d44e8664..75e70510 100644
--- a/tests/auth-test.c
+++ b/tests/auth-test.c
@@ -272,7 +272,7 @@ bug271540_authenticate (SoupSession *session, SoupMessage *msg,
 }
 
 static void
-bug271540_finished (SoupSession *session, SoupMessage *msg, gpointer data)
+bug271540_finished (SoupMessage *msg, gpointer data)
 {
        int *left = data;
 
@@ -309,8 +309,10 @@ do_pipelined_auth_test (void)
                g_signal_connect (msg, "wrote_headers",
                                  G_CALLBACK (bug271540_sent), &authenticated);
 
-               soup_session_queue_message (session, msg,
-                                           bug271540_finished, &i);
+               g_signal_connect (msg, "finished",
+                                 G_CALLBACK (bug271540_finished), &i);
+               soup_session_send_async (session, msg, NULL, NULL, NULL);
+               g_object_unref (msg);
        }
        g_free (uri);
 
@@ -509,7 +511,8 @@ async_authenticate (SoupSession *session, SoupMessage *msg,
 }
 
 static void
-async_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
+async_finished (SoupMessage *msg,
+               gpointer     user_data)
 {
        int *remaining = user_data;
        int id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (msg), "id"));
@@ -571,9 +574,10 @@ do_async_auth_good_password_test (void)
        g_object_set_data (G_OBJECT (msg1), "id", GINT_TO_POINTER (1));
        auth_id = g_signal_connect (session, "authenticate",
                                    G_CALLBACK (async_authenticate), &auth);
-       g_object_ref (msg1);
        remaining++;
-       soup_session_queue_message (session, msg1, async_finished, &remaining);
+       g_signal_connect (msg1, "finished",
+                         G_CALLBACK (async_finished), &remaining);
+       soup_session_send_async (session, msg1, NULL, NULL, NULL);
        g_main_loop_run (loop);
        g_signal_handler_disconnect (session, auth_id);
 
@@ -596,9 +600,10 @@ do_async_auth_good_password_test (void)
        g_object_set_data (G_OBJECT (msg3), "id", GINT_TO_POINTER (3));
        auth_id = g_signal_connect (session, "authenticate",
                                    G_CALLBACK (async_authenticate), NULL);
-       g_object_ref (msg3);
        remaining++;
-       soup_session_queue_message (session, msg3, async_finished, &remaining);
+       g_signal_connect (msg3, "finished",
+                         G_CALLBACK (async_finished), &remaining);
+       soup_session_send_async (session, msg3, NULL, NULL, NULL);
        g_main_loop_run (loop);
        g_signal_handler_disconnect (session, auth_id);
 
@@ -659,9 +664,10 @@ do_async_auth_bad_password_test (void)
        g_object_set_data (G_OBJECT (msg), "id", GINT_TO_POINTER (1));
        auth_id = g_signal_connect (session, "authenticate",
                                    G_CALLBACK (async_authenticate), &auth);
-       g_object_ref (msg);
        remaining++;
-       soup_session_queue_message (session, msg, async_finished, &remaining);
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (async_finished), &remaining);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
        g_main_loop_run (loop);
        g_signal_handler_disconnect (session, auth_id);
        soup_auth_authenticate (auth, "user1", "wrong");
@@ -713,9 +719,10 @@ do_async_auth_no_password_test (void)
        g_object_set_data (G_OBJECT (msg), "id", GINT_TO_POINTER (1));
        auth_id = g_signal_connect (session, "authenticate",
                                    G_CALLBACK (async_authenticate), NULL);
-       g_object_ref (msg);
        remaining++;
-       soup_session_queue_message (session, msg, async_finished, &remaining);
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (async_finished), &remaining);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
        g_main_loop_run (loop);
        g_signal_handler_disconnect (session, auth_id);
        soup_session_unpause_message (session, msg);
@@ -725,13 +732,14 @@ do_async_auth_no_password_test (void)
        /* Now send a second message */
        msg = soup_message_new ("GET", uri);
        g_object_set_data (G_OBJECT (msg), "id", GINT_TO_POINTER (2));
-       g_object_ref (msg);
        been_there = FALSE;
        auth_id = g_signal_connect (session, "authenticate",
                                    G_CALLBACK (async_authenticate_assert_once_and_stop),
                                    &been_there);
        remaining++;
-       soup_session_queue_message (session, msg, async_finished, &remaining);
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (async_finished), &remaining);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
        g_main_loop_run (loop);
        soup_session_unpause_message (session, msg);
 
@@ -1020,6 +1028,7 @@ do_auth_close_test (void)
        SoupAuthDomain *basic_auth_domain;
        SoupURI *uri;
        AuthCloseData acd;
+       GBytes *body;
 
        server = soup_test_server_new (SOUP_TEST_SERVER_DEFAULT);
        soup_server_add_handler (server, NULL,
@@ -1045,10 +1054,11 @@ do_auth_close_test (void)
 
        acd.msg = soup_message_new_from_uri ("GET", uri);
        soup_uri_free (uri);
-       soup_test_session_async_send_message (acd.session, acd.msg);
+       body = soup_test_session_async_send (acd.session, acd.msg);
 
        soup_test_assert_message_status (acd.msg, SOUP_STATUS_OK);
 
+       g_bytes_unref (body);
        g_object_unref (acd.msg);
        soup_test_session_abort_unref (acd.session);
        soup_test_server_quit_unref (server);
@@ -1131,6 +1141,7 @@ do_disappearing_auth_test (void)
        SoupMessage *msg;
        SoupSession *session;
        int counter;
+       GBytes *body;
 
        g_test_bug_base ("https://bugzilla.redhat.com/";);
        g_test_bug ("916224");
@@ -1156,12 +1167,13 @@ do_disappearing_auth_test (void)
                          G_CALLBACK (disappear_authenticate), &counter);
 
        msg = soup_message_new_from_uri ("GET", uri);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
 
        soup_test_assert (counter <= 2,
                          "Got stuck in loop");
        soup_test_assert_message_status (msg, SOUP_STATUS_UNAUTHORIZED);
 
+       g_bytes_unref (body);
        g_object_unref (msg);
        soup_test_session_abort_unref (session);
 
@@ -1369,7 +1381,7 @@ async_no_auth_cache_authenticate (SoupSession *session, SoupMessage *msg,
 }
 
 static void
-async_no_auth_cache_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
+async_no_auth_cache_finished (SoupMessage *msg, gpointer user_data)
 {
        debug_printf (1, "  async_no_auth_cache_finished\n");
 
@@ -1397,8 +1409,9 @@ do_async_message_do_not_use_auth_cache_test (void)
                          G_CALLBACK (async_no_auth_cache_authenticate), &auth);
        flags = soup_message_get_flags (msg);
        soup_message_set_flags (msg, flags | SOUP_MESSAGE_DO_NOT_USE_AUTH_CACHE);
-       g_object_ref (msg);
-       soup_session_queue_message (session, msg, async_no_auth_cache_finished, NULL);
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (async_no_auth_cache_finished), NULL);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
        g_main_loop_run (loop);
 
        soup_test_assert_message_status (msg, SOUP_STATUS_UNAUTHORIZED);
diff --git a/tests/connection-test.c b/tests/connection-test.c
index 9095d84f..61012e0f 100644
--- a/tests/connection-test.c
+++ b/tests/connection-test.c
@@ -512,7 +512,7 @@ max_conns_request_started (SoupSession *session, SoupMessage *msg, gpointer user
 }
 
 static void
-max_conns_message_complete (SoupSession *session, SoupMessage *msg, gpointer user_data)
+max_conns_message_complete (SoupMessage *msg, gpointer user_data)
 {
        if (++msgs_done == TEST_CONNS)
                g_main_loop_quit (max_conns_loop);
@@ -534,9 +534,9 @@ do_max_conns_test_for_session (SoupSession *session)
        msgs_done = 0;
        for (i = 0; i < TEST_CONNS - 1; i++) {
                msgs[i] = soup_message_new_from_uri ("GET", base_uri);
-               g_object_ref (msgs[i]);
-               soup_session_queue_message (session, msgs[i],
-                                           max_conns_message_complete, NULL);
+               g_signal_connect (msgs[i], "finished",
+                                 G_CALLBACK (max_conns_message_complete), NULL);
+               soup_session_send_async (session, msgs[i], NULL, NULL, NULL);
        }
 
        g_main_loop_run (max_conns_loop);
@@ -550,9 +550,9 @@ do_max_conns_test_for_session (SoupSession *session)
        msgs[i] = soup_message_new_from_uri ("GET", base_uri);
        flags = soup_message_get_flags (msgs[i]);
        soup_message_set_flags (msgs[i], flags | SOUP_MESSAGE_IGNORE_CONNECTION_LIMITS);
-       g_object_ref (msgs[i]);
-       soup_session_queue_message (session, msgs[i],
-                                   max_conns_message_complete, NULL);
+       g_signal_connect (msgs[i], "finished",
+                         G_CALLBACK (max_conns_message_complete), NULL);
+       soup_session_send_async (session, msgs[i], NULL, NULL, NULL);
 
        g_main_loop_run (max_conns_loop);
        g_assert_cmpint (msgs_done, ==, MAX_CONNS + 1);
@@ -621,8 +621,8 @@ np_request_unqueued (SoupSession *session, SoupMessage *msg,
 }
 
 static void
-np_request_finished (SoupSession *session, SoupMessage *msg,
-                    gpointer user_data)
+np_request_finished (SoupMessage *msg,
+                    gpointer     user_data)
 {
        GMainLoop *loop = user_data;
 
@@ -647,9 +647,9 @@ do_non_persistent_test_for_session (SoupSession *session)
 
        msg = soup_message_new_from_uri ("GET", base_uri);
        soup_message_headers_append (msg->request_headers, "Connection", "close");
-       g_object_ref (msg);
-       soup_session_queue_message (session, msg,
-                                   np_request_finished, loop);
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (np_request_finished), loop);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
        g_main_loop_run (loop);
        g_main_loop_unref (loop);
 
diff --git a/tests/context-test.c b/tests/context-test.c
index 406c4560..c81b5131 100644
--- a/tests/context-test.c
+++ b/tests/context-test.c
@@ -117,7 +117,8 @@ idle_start_test1_thread (gpointer user_data)
 }
 
 static void
-test1_finished (SoupSession *session, SoupMessage *msg, gpointer loop)
+test1_finished (SoupMessage *msg,
+               GMainLoop   *loop)
 {
        g_main_loop_quit (loop);
 }
@@ -152,9 +153,13 @@ test1_thread (gpointer user_data)
        debug_printf (1, "  queue_message\n");
        msg = soup_message_new ("GET", uri);
        loop = g_main_loop_new (async_context, FALSE);
-       g_object_ref (msg);
-       soup_session_queue_message (session, msg, test1_finished, loop);
+       g_signal_connect (msg, "finished", G_CALLBACK (test1_finished), loop);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
        g_main_loop_run (loop);
+       /* We need one more iteration, because SoupMessage::finished is emitted
+         * right before the message is unqueued.
+         */
+        g_main_context_iteration (async_context, TRUE);
        g_main_loop_unref (loop);
        soup_test_assert_message_status (msg, SOUP_STATUS_OK);
        g_object_unref (msg);
diff --git a/tests/continue-test.c b/tests/continue-test.c
index 1fd90904..0c891b77 100644
--- a/tests/continue-test.c
+++ b/tests/continue-test.c
@@ -59,6 +59,7 @@ do_message (const char *path, gboolean long_body,
        const char *expected_event;
        char *actual_event;
        int expected_status, actual_status;
+       GBytes *response_body;
 
        uri = soup_uri_copy (base_uri);
        if (auth) {
@@ -96,7 +97,7 @@ do_message (const char *path, gboolean long_body,
        events = NULL;
        session = soup_test_session_new (SOUP_TYPE_SESSION, NULL);
         g_assert (SOUP_IS_MESSAGE (msg));
-       soup_test_session_async_send_message (session, msg);
+       response_body = soup_test_session_async_send (session, msg);
         g_assert (SOUP_IS_MESSAGE (msg));
        soup_test_session_abort_unref (session);
         g_assert (SOUP_IS_MESSAGE (msg));
@@ -148,6 +149,7 @@ do_message (const char *path, gboolean long_body,
                    !strcmp (actual_event, "server-wrote_informational"))
                        events = g_slist_delete_link (events, events);
        }
+       g_bytes_unref (response_body);
        g_object_unref (msg);
 }
 
diff --git a/tests/misc-test.c b/tests/misc-test.c
index 69ba727d..62184e35 100644
--- a/tests/misc-test.c
+++ b/tests/misc-test.c
@@ -159,7 +159,8 @@ do_host_big_header (void)
  * (This test will crash if it fails.)
  */
 static void
-cu_one_completed (SoupSession *session, SoupMessage *msg, gpointer loop)
+cu_one_completed (SoupMessage *msg,
+                 SoupSession *session)
 {
        debug_printf (2, "  Message 1 completed\n");
        soup_test_assert_message_status (msg, SOUP_STATUS_CANT_CONNECT);
@@ -174,7 +175,8 @@ cu_idle_quit (gpointer loop)
 }
 
 static void
-cu_two_completed (SoupSession *session, SoupMessage *msg, gpointer loop)
+cu_two_completed (SoupMessage *msg,
+                 GMainLoop   *loop)
 {
        debug_printf (2, "  Message 2 completed\n");
        soup_test_assert_message_status (msg, SOUP_STATUS_CANT_CONNECT);
@@ -203,13 +205,19 @@ do_callback_unref_test (void)
        loop = g_main_loop_new (NULL, TRUE);
 
        one = soup_message_new_from_uri ("GET", bad_uri);
+       g_signal_connect (one, "finished",
+                         G_CALLBACK (cu_one_completed), session);
        g_object_add_weak_pointer (G_OBJECT (one), (gpointer *)&one);
        two = soup_message_new_from_uri ("GET", bad_uri);
+       g_signal_connect (two, "finished",
+                         G_CALLBACK (cu_two_completed), loop);
        g_object_add_weak_pointer (G_OBJECT (two), (gpointer *)&two);
        soup_uri_free (bad_uri);
 
-       soup_session_queue_message (session, one, cu_one_completed, loop);
-       soup_session_queue_message (session, two, cu_two_completed, loop);
+       soup_session_send_async (session, one, NULL, NULL, NULL);
+       soup_session_send_async (session, two, NULL, NULL, NULL);
+       g_object_unref (one);
+       g_object_unref (two);
 
        g_main_loop_run (loop);
        g_main_loop_unref (loop);
@@ -400,7 +408,8 @@ do_msg_reuse_test (void)
 
 /* Handle unexpectedly-early aborts. */
 static void
-ea_msg_completed_one (SoupSession *session, SoupMessage *msg, gpointer loop)
+ea_msg_completed_one (SoupMessage *msg,
+                     GMainLoop   *loop)
 {
        debug_printf (2, "  Message 1 completed\n");
        soup_test_assert_message_status (msg, SOUP_STATUS_CANCELLED);
@@ -447,6 +456,7 @@ do_early_abort_test (void)
 {
        SoupSession *session;
        SoupMessage *msg;
+       GBytes *body;
        GMainContext *context;
        GMainLoop *loop;
 
@@ -458,7 +468,10 @@ do_early_abort_test (void)
 
        context = g_main_context_default ();
        loop = g_main_loop_new (context, TRUE);
-       soup_session_queue_message (session, msg, ea_msg_completed_one, loop);
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (ea_msg_completed_one), loop);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
+       g_object_unref (msg);
        g_main_context_iteration (context, FALSE);
 
        soup_session_abort (session);
@@ -472,10 +485,11 @@ do_early_abort_test (void)
 
        g_signal_connect (session, "connection-created",
                          G_CALLBACK (ea_connection_created), NULL);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        debug_printf (2, "  Message 2 completed\n");
 
        soup_test_assert_message_status (msg, SOUP_STATUS_CANCELLED);
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        while (g_main_context_pending (context))
@@ -490,10 +504,11 @@ do_early_abort_test (void)
 
        g_signal_connect (msg, "starting",
                          G_CALLBACK (ea_message_starting), session);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        debug_printf (2, "  Message 3 completed\n");
 
        soup_test_assert_message_status (msg, SOUP_STATUS_CANCELLED);
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        while (g_main_context_pending (context))
@@ -666,10 +681,9 @@ cancel_message_timeout (gpointer msg)
 }
 
 static void
-set_done (SoupSession *session, SoupMessage *msg, gpointer user_data)
+set_done (SoupMessage *msg,
+         gboolean    *done)
 {
-       gboolean *done = user_data;
-
        *done = TRUE;
 }
 
@@ -693,10 +707,15 @@ do_cancel_while_reading_test_for_session (SoupSession *session)
         * because it holds an extra ref on the SoupMessageQueueItem
         * relative to soup_session_queue_message().
         */
-       g_object_ref (msg);
-       soup_session_queue_message (session, msg, set_done, &done);
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (set_done), &done);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
        while (!done)
                g_main_context_iteration (NULL, TRUE);
+       /* We need one more iteration, because SoupMessage::finished is emitted
+        * right before the message is unqueued.
+        */
+       g_main_context_iteration (NULL, TRUE);
 
        soup_test_assert_message_status (msg, SOUP_STATUS_CANCELLED);
        g_object_unref (msg);
@@ -847,10 +866,11 @@ do_pause_abort_test (void)
        session = soup_test_session_new (SOUP_TYPE_SESSION, NULL);
 
        msg = soup_message_new_from_uri ("GET", base_uri);
-       soup_session_queue_message (session, msg, NULL, NULL);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
        soup_session_pause_message (session, msg);
 
        g_object_add_weak_pointer (G_OBJECT (msg), &ptr);
+       g_object_unref (msg);
        soup_test_session_abort_unref (session);
 
        g_assert_null (ptr);
@@ -867,13 +887,22 @@ pause_cancel_got_headers (SoupMessage *msg, gpointer user_data)
        g_main_loop_quit (pause_cancel_loop);
 }
 
-static void
-pause_cancel_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
+static gboolean
+idle_quit (gpointer loop)
 {
-       gboolean *finished = user_data;
+       g_main_loop_quit (loop);
+       return FALSE;
+}
 
+static void
+pause_cancel_finished (SoupMessage *msg,
+                      gboolean    *finished)
+{
        *finished = TRUE;
-       g_main_loop_quit (pause_cancel_loop);
+       /* Quit the main loop in the next iteration, because finished is emitted
+        * right before the item is unqueued.
+        */
+       g_idle_add (idle_quit, pause_cancel_loop);
 }
 
 static gboolean
@@ -902,11 +931,11 @@ do_pause_cancel_test (void)
        timeout_id = g_timeout_add_seconds (5, pause_cancel_timeout, &timed_out);
 
        msg = soup_message_new_from_uri ("GET", base_uri);
-       g_object_ref (msg);
        g_signal_connect (msg, "got-headers",
                          G_CALLBACK (pause_cancel_got_headers), session);
-
-       soup_session_queue_message (session, msg, pause_cancel_finished, &finished);
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (pause_cancel_finished), &finished);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
        g_main_loop_run (pause_cancel_loop);
        g_assert_false (finished);
 
@@ -1003,7 +1032,8 @@ upgrade_server_callback (SoupServer *server, SoupMessage *msg,
 }
 
 static void
-callback_not_reached (SoupSession *session, SoupMessage *msg, gpointer user_data)
+callback_not_reached (SoupMessage *msg,
+                     gpointer     user_data)
 {
        g_assert_not_reached ();
 }
@@ -1055,8 +1085,9 @@ do_stealing_test (gconstpointer data)
                soup_session_send_message (session, msg);
                soup_test_assert_message_status (msg, SOUP_STATUS_SWITCHING_PROTOCOLS);
        } else {
-               g_object_ref (msg);
-               soup_session_queue_message (session, msg, callback_not_reached, NULL);
+               g_signal_connect (msg, "finished",
+                                 G_CALLBACK (callback_not_reached), NULL);
+               soup_session_send_async (session, msg, NULL, NULL, NULL);
                while (iostream == NULL)
                        g_main_context_iteration (NULL, TRUE);
        }
diff --git a/tests/pull-api-test.c b/tests/pull-api-test.c
index 5b9bd5ea..1024fea1 100644
--- a/tests/pull-api-test.c
+++ b/tests/pull-api-test.c
@@ -56,10 +56,24 @@ typedef struct {
 static void fully_async_got_headers (SoupMessage *msg, gpointer user_data);
 static void fully_async_got_chunk   (SoupMessage *msg, GBytes *chunk,
                                     gpointer user_data);
-static void fully_async_finished    (SoupSession *session, SoupMessage *msg,
-                                    gpointer user_data);
 static gboolean fully_async_request_chunk (gpointer user_data);
 
+static void
+fully_async_finished (SoupMessage    *msg,
+                     FullyAsyncData *ad)
+{
+       soup_test_assert_message_status (msg, ad->expected_status);
+
+       if (ad->timeout != 0)
+               g_source_remove (ad->timeout);
+
+       /* Since our test program is only running the loop for the
+        * purpose of this one test, we quit the loop once the
+        * test is done.
+        */
+       g_main_loop_quit (ad->loop);
+}
+
 static void
 do_fully_async_test (SoupSession *session,
                     const char *base_uri, const char *sub_uri,
@@ -99,8 +113,10 @@ do_fully_async_test (SoupSession *session,
        g_signal_connect (msg, "got_headers",
                          G_CALLBACK (fully_async_got_headers), &ad);
 
-       /* Queue the request */
-       soup_session_queue_message (session, msg, fully_async_finished, &ad);
+       /* Send the request */
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (fully_async_finished), &ad);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
 
        /* In a real program, we'd probably just return at this point.
         * Eventually the caller would return all the way to the main
@@ -211,24 +227,6 @@ fully_async_got_chunk (SoupMessage *msg, GBytes *chunk, gpointer user_data)
        ad->timeout = g_timeout_add (10, fully_async_request_chunk, ad);
 }
 
-static void
-fully_async_finished (SoupSession *session, SoupMessage *msg,
-                     gpointer user_data)
-{
-       FullyAsyncData *ad = user_data;
-
-       soup_test_assert_message_status (msg, ad->expected_status);
-
-       if (ad->timeout != 0)
-               g_source_remove (ad->timeout);
-
-       /* Since our test program is only running the loop for the
-        * purpose of this one test, we quit the loop once the
-        * test is done.
-        */
-       g_main_loop_quit (ad->loop);
-}
-
 static void
 do_fast_async_test (gconstpointer data)
 {
diff --git a/tests/range-test.c b/tests/range-test.c
index 83b59694..3641198c 100644
--- a/tests/range-test.c
+++ b/tests/range-test.c
@@ -7,8 +7,11 @@ int total_length;
 char *test_response;
 
 static void
-check_part (SoupMessageHeaders *headers, const char *body, gsize body_len,
-           gboolean check_start_end, int expected_start, int expected_end)
+check_part (SoupMessageHeaders *headers,
+           GBytes             *body,
+           gboolean            check_start_end,
+           int                 expected_start,
+           int                 expected_end)
 {
        goffset start, end, total_length;
         gsize full_response_length = g_bytes_get_size (full_response);
@@ -46,14 +49,14 @@ check_part (SoupMessageHeaders *headers, const char *body, gsize body_len,
                }
        }
 
-       if (end - start + 1 != body_len) {
+       if (end - start + 1 != g_bytes_get_size (body)) {
                soup_test_assert (FALSE, "Range length (%d) does not match body length (%d)\n",
                                  (int)(end - start) + 1,
-                                 (int)body_len);
+                                 (int)g_bytes_get_size (body));
                return;
        }
 
-       memcpy (test_response + start, body, body_len);
+       memcpy (test_response + start, g_bytes_get_data (body, NULL), g_bytes_get_size (body));
 }
 
 static void
@@ -61,11 +64,12 @@ do_single_range (SoupSession *session, SoupMessage *msg,
                 int start, int end, gboolean succeed)
 {
        const char *content_type;
+       GBytes *body;
 
        debug_printf (1, "    Range: %s\n",
                      soup_message_headers_get_one (msg->request_headers, "Range"));
 
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
 
        if (!succeed) {
                soup_test_assert_message_status (msg, SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE);
@@ -88,8 +92,8 @@ do_single_range (SoupSession *session, SoupMessage *msg,
                msg->response_headers, NULL);
        g_assert_cmpstr (content_type, !=, "multipart/byteranges");
 
-       check_part (msg->response_headers, msg->response_body->data,
-                   msg->response_body->length, TRUE, start, end);
+       check_part (msg->response_headers, body, TRUE, start, end);
+       g_bytes_unref (body);
        g_object_unref (msg);
 }
 
@@ -111,11 +115,16 @@ do_multi_range (SoupSession *session, SoupMessage *msg,
        SoupMultipart *multipart;
        const char *content_type;
        int i, length;
+       GBytes *body;
+       SoupMessageBody *message_body;
 
        debug_printf (1, "    Range: %s\n",
                      soup_message_headers_get_one (msg->request_headers, "Range"));
 
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
+       message_body = soup_message_body_new ();
+       soup_message_body_append_bytes (message_body, body);
+       g_bytes_unref (body);
 
        soup_test_assert_message_status (msg, SOUP_STATUS_PARTIAL_CONTENT);
 
@@ -123,7 +132,8 @@ do_multi_range (SoupSession *session, SoupMessage *msg,
        g_assert_cmpstr (content_type, ==, "multipart/byteranges");
 
        multipart = soup_multipart_new_from_message (msg->response_headers,
-                                                    msg->response_body);
+                                                    message_body);
+       soup_message_body_free (message_body);
        if (!multipart) {
                soup_test_assert (FALSE, "Could not parse multipart");
                g_object_unref (msg);
@@ -139,7 +149,7 @@ do_multi_range (SoupSession *session, SoupMessage *msg,
 
                debug_printf (1, "  Part %d\n", i + 1);
                soup_multipart_get_part (multipart, i, &headers, &body);
-               check_part (headers, g_bytes_get_data (body, NULL), g_bytes_get_size (body), FALSE, 0, 0);
+               check_part (headers, body, FALSE, 0, 0);
        }
 
        soup_multipart_free (multipart);
diff --git a/tests/server-test.c b/tests/server-test.c
index e6ef4d15..dfebb85f 100644
--- a/tests/server-test.c
+++ b/tests/server-test.c
@@ -372,6 +372,7 @@ do_ipv6_test (ServerData *sd, gconstpointer test_data)
 {
        SoupSession *session;
        SoupMessage *msg;
+       GBytes *body;
        GError *error = NULL;
 
        g_test_bug ("666399");
@@ -395,15 +396,17 @@ do_ipv6_test (ServerData *sd, gconstpointer test_data)
 
        debug_printf (1, "  HTTP/1.1\n");
        msg = soup_message_new_from_uri ("GET", sd->base_uri);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        soup_test_assert_message_status (msg, SOUP_STATUS_OK);
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        debug_printf (1, "  HTTP/1.0\n");
        msg = soup_message_new_from_uri ("GET", sd->base_uri);
        soup_message_set_http_version (msg, SOUP_HTTP_1_0);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        soup_test_assert_message_status (msg, SOUP_STATUS_OK);
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        soup_test_session_abort_unref (session);
@@ -444,6 +447,7 @@ do_multi_test (ServerData *sd, SoupURI *uri1, SoupURI *uri2)
        char *uristr;
        SoupSession *session;
        SoupMessage *msg;
+       GBytes *body;
 
        server_add_handler (sd, NULL, multi_server_callback, NULL, NULL);
 
@@ -451,17 +455,19 @@ do_multi_test (ServerData *sd, SoupURI *uri1, SoupURI *uri2)
 
        uristr = soup_uri_to_string (uri1, FALSE);
        msg = soup_message_new ("GET", uristr);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        soup_test_assert_message_status (msg, SOUP_STATUS_OK);
-       g_assert_cmpstr (msg->response_body->data, ==, uristr);
+       g_assert_cmpmem (uristr, strlen (uristr), g_bytes_get_data (body, NULL), g_bytes_get_size (body));
+       g_bytes_unref (body);
        g_object_unref (msg);
        g_free (uristr);
 
        uristr = soup_uri_to_string (uri2, FALSE);
        msg = soup_message_new ("GET", uristr);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        soup_test_assert_message_status (msg, SOUP_STATUS_OK);
-       g_assert_cmpstr (msg->response_body->data, ==, uristr);
+       g_assert_cmpmem (uristr, strlen (uristr), g_bytes_get_data (body, NULL), g_bytes_get_size (body));
+       g_bytes_unref (body);
        g_object_unref (msg);
        g_free (uristr);
 
@@ -588,6 +594,7 @@ do_gsocket_import_test (void)
        SoupURI *uri;
        SoupSession *session;
        SoupMessage *msg;
+       GBytes *body;
        GError *error = NULL;
 
        gsock = g_socket_new (G_SOCKET_FAMILY_IPV4,
@@ -628,8 +635,9 @@ do_gsocket_import_test (void)
 
        session = soup_test_session_new (SOUP_TYPE_SESSION, NULL);
        msg = soup_message_new_from_uri ("GET", uri);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        soup_test_assert_message_status (msg, SOUP_STATUS_OK);
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        soup_test_session_abort_unref (session);
@@ -651,6 +659,7 @@ do_fd_import_test (void)
        SoupURI *uri;
        SoupSession *session;
        SoupMessage *msg;
+       GBytes *body;
        int type;
        GError *error = NULL;
 
@@ -692,8 +701,9 @@ do_fd_import_test (void)
 
        session = soup_test_session_new (SOUP_TYPE_SESSION, NULL);
        msg = soup_message_new_from_uri ("GET", uri);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        soup_test_assert_message_status (msg, SOUP_STATUS_OK);
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        soup_test_session_abort_unref (session);
@@ -887,6 +897,7 @@ do_fail_404_test (ServerData *sd, gconstpointer test_data)
 {
        SoupSession *session;
        SoupMessage *msg;
+       GBytes *body;
        UnhandledServerData usd;
 
        usd.handler_called = usd.paused = FALSE;
@@ -895,8 +906,9 @@ do_fail_404_test (ServerData *sd, gconstpointer test_data)
 
        session = soup_test_session_new (SOUP_TYPE_SESSION, NULL);
        msg = soup_message_new_from_uri ("GET", sd->base_uri);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        soup_test_assert_message_status (msg, SOUP_STATUS_NOT_FOUND);
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        g_assert_false (usd.handler_called);
@@ -910,6 +922,7 @@ do_fail_500_test (ServerData *sd, gconstpointer pause)
 {
        SoupSession *session;
        SoupMessage *msg;
+       GBytes *body;
        UnhandledServerData usd;
 
        usd.handler_called = usd.paused = FALSE;
@@ -920,8 +933,9 @@ do_fail_500_test (ServerData *sd, gconstpointer pause)
        msg = soup_message_new_from_uri ("GET", sd->base_uri);
        if (pause)
                soup_message_headers_append (msg->request_headers, "X-Test-Server-Pause", "true");
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        soup_test_assert_message_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        g_assert_true (usd.handler_called);
diff --git a/tests/session-test.c b/tests/session-test.c
index bea655f6..f26ee3b6 100644
--- a/tests/session-test.c
+++ b/tests/session-test.c
@@ -42,10 +42,9 @@ server_handler (SoupServer        *server,
 }
 
 static void
-finished_cb (SoupSession *session, SoupMessage *msg, gpointer user_data)
+finished_cb (SoupMessage *msg,
+            gboolean    *finished)
 {
-       gboolean *finished = user_data;
-
        *finished = TRUE;
 }
 
@@ -77,7 +76,10 @@ do_test_for_session (SoupSession *session, SoupURI *uri,
 
        msg = soup_message_new_from_uri ("GET", uri);
        server_processed_message = timeout = finished = FALSE;
-       soup_session_queue_message (session, msg, finished_cb, &finished);
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (finished_cb), &finished);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
+       g_object_unref (msg);
        while (!timeout)
                g_usleep (100);
        debug_printf (2, "    got timeout\n");
@@ -121,9 +123,10 @@ do_test_for_session (SoupSession *session, SoupURI *uri,
 
        debug_printf (1, "  cancel_message\n");
        msg = soup_message_new_from_uri ("GET", uri);
-       g_object_ref (msg);
        finished = FALSE;
-       soup_session_queue_message (session, msg, finished_cb, &finished);
+       g_signal_connect (msg, "finished",
+                         G_CALLBACK (finished_cb), &finished);
+       soup_session_send_async (session, msg, NULL, NULL, NULL);
        g_signal_connect (msg, "wrote-headers",
                          G_CALLBACK (cancel_message_cb), session);
 
@@ -139,6 +142,10 @@ do_test_for_session (SoupSession *session, SoupURI *uri,
                debug_printf (2, "    waiting for finished\n");
                while (!finished)
                        g_main_context_iteration (NULL, TRUE);
+               /* We need one iteration more because finished is emitted
+                * right before the item is unqueued.
+                */
+               g_main_context_iteration (NULL, TRUE);
        }
        g_main_loop_unref (loop);
 
@@ -158,9 +165,9 @@ do_plain_tests (gconstpointer data)
 }
 
 static void
-priority_test_finished_cb (SoupSession *session, SoupMessage *msg, gpointer user_data)
+priority_test_finished_cb (SoupMessage *msg,
+                          guint       *finished_count)
 {
-       guint *finished_count = user_data;
        SoupMessagePriority priority = soup_message_get_priority (msg);
 
        debug_printf (1, "  received message %d with priority %d\n",
@@ -204,7 +211,10 @@ do_priority_tests (gconstpointer data)
                soup_uri_free (msg_uri);
 
                soup_message_set_priority (msg, priorities[i]);
-               soup_session_queue_message (session, msg, priority_test_finished_cb, &finished_count);
+               g_signal_connect (msg, "finished",
+                                 G_CALLBACK (priority_test_finished_cb), &finished_count);
+               soup_session_send_async (session, msg, NULL, NULL, NULL);
+               g_object_unref (msg);
        }
 
        debug_printf (2, "    waiting for finished\n");
diff --git a/tests/sniffing-test.c b/tests/sniffing-test.c
index 6b85cc01..f71a1112 100644
--- a/tests/sniffing-test.c
+++ b/tests/sniffing-test.c
@@ -336,6 +336,7 @@ test_sniffing (const char *path, const char *expected_type)
 {
        SoupURI *uri;
        SoupMessage *msg;
+       GBytes *body;
        SoupRequest *req;
        GInputStream *stream;
        char *sniffed_type = NULL;
@@ -348,9 +349,10 @@ test_sniffing (const char *path, const char *expected_type)
        g_signal_connect (msg, "content-sniffed",
                          G_CALLBACK (sniffing_content_sniffed), &sniffed_type);
 
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        g_assert_cmpstr (sniffed_type, ==, expected_type);
        g_free (sniffed_type);
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        req = soup_session_request_uri (session, uri, NULL);
@@ -388,6 +390,7 @@ test_disabled (gconstpointer data)
        const char *path = data;
        SoupURI *uri;
        SoupMessage *msg;
+       GBytes *body;
        SoupRequest *req;
        GInputStream *stream;
        char *sniffed_type = NULL;
@@ -406,9 +409,10 @@ test_disabled (gconstpointer data)
        g_signal_connect (msg, "content-sniffed",
                          G_CALLBACK (sniffing_content_sniffed), &sniffed_type);
 
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
 
        g_assert_null (sniffed_type);
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        req = soup_session_request_uri (session, uri, NULL);
diff --git a/tests/ssl-test.c b/tests/ssl-test.c
index ac33d2c9..4e0f10e8 100644
--- a/tests/ssl-test.c
+++ b/tests/ssl-test.c
@@ -184,6 +184,7 @@ do_tls_interaction_test (void)
        GSocketAddress *address, *bound_address;
        SoupSession *session;
        SoupMessage *msg;
+       GBytes *body;
        GTlsInteraction *interaction;
        SoupURI *test_uri;
        GError *error = NULL;
@@ -209,8 +210,9 @@ do_tls_interaction_test (void)
 
        /* Without a GTlsInteraction */
        msg = soup_message_new_from_uri ("GET", test_uri);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        soup_test_assert_message_status (msg, SOUP_STATUS_SSL_FAILED);
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        interaction = g_object_new (test_tls_interaction_get_type (), NULL);
@@ -221,9 +223,10 @@ do_tls_interaction_test (void)
 
        /* With a GTlsInteraction */
        msg = soup_message_new_from_uri ("GET", test_uri);
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
        soup_test_assert_message_status (msg, SOUP_STATUS_OK);
        g_assert_true (soup_message_get_https_status (msg, NULL, NULL));
+       g_bytes_unref (body);
        g_object_unref (msg);
 
        soup_uri_free (test_uri);
diff --git a/tests/streaming-test.c b/tests/streaming-test.c
index b4091938..b1a10d54 100644
--- a/tests/streaming-test.c
+++ b/tests/streaming-test.c
@@ -77,23 +77,25 @@ do_request (SoupSession *session, SoupURI *base_uri, char *path)
 {
        SoupURI *uri;
        SoupMessage *msg;
+       GBytes *body;
        char *md5;
 
        uri = soup_uri_new_with_base (base_uri, path);
        msg = soup_message_new_from_uri ("GET", uri);
        soup_uri_free (uri);
 
-       soup_test_session_async_send_message (session, msg);
+       body = soup_test_session_async_send (session, msg);
 
        soup_test_assert_message_status (msg, SOUP_STATUS_OK);
-       g_assert_cmpint (msg->response_body->length, ==, g_bytes_get_size (full_response));
+       g_assert_cmpint (g_bytes_get_size (body), ==, g_bytes_get_size (full_response));
 
        md5 = g_compute_checksum_for_data (G_CHECKSUM_MD5,
-                                          (guchar *)msg->response_body->data,
-                                          msg->response_body->length);
+                                          (guchar *)g_bytes_get_data (body, NULL),
+                                          g_bytes_get_size (body));
        g_assert_cmpstr (md5, ==, full_response_md5);
        g_free (md5);
 
+       g_bytes_unref (body);
        g_object_unref (msg);
 }
 
diff --git a/tests/test-utils.c b/tests/test-utils.c
index 6b1b10f9..92560982 100644
--- a/tests/test-utils.c
+++ b/tests/test-utils.c
@@ -304,26 +304,81 @@ soup_test_session_abort_unref (SoupSession *session)
 }
 
 static void
-on_message_finished (SoupSession *session, SoupMessage *msg, gpointer user_data)
+send_async_ready_cb (SoupSession  *session,
+                    GAsyncResult *result,
+                    GBytes      **body)
+{
+       GInputStream *istream;
+       GOutputStream *ostream;
+
+       istream = soup_session_send_finish (session, result, NULL);
+       if (!istream)
+               return;
+
+       ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
+       g_output_stream_splice (ostream,
+                               istream,
+                               G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
+                               G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
+                               NULL, NULL);
+       *body = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (ostream));
+       g_object_unref (ostream);
+       g_object_unref (istream);
+}
+
+static void
+on_message_finished (SoupMessage *msg,
+                    gboolean    *message_finished)
 {
-        gboolean *message_finished = user_data;
         *message_finished = TRUE;
 }
 
-guint
-soup_test_session_async_send_message (SoupSession *session, SoupMessage *msg)
+GBytes *
+soup_test_session_async_send (SoupSession *session,
+                             SoupMessage *msg)
 {
         gboolean message_finished = FALSE;
        GMainContext *async_context = g_main_context_ref_thread_default ();
+       gulong signal_id;
+       GBytes *body = NULL;
 
-        g_object_ref (msg); // We want to keep it alive rather than the session consuming it.
-        soup_session_queue_message (session, msg, on_message_finished, &message_finished);
+       signal_id = g_signal_connect (msg, "finished",
+                                     G_CALLBACK (on_message_finished), &message_finished);
+       soup_session_send_async (session, msg, NULL, (GAsyncReadyCallback)send_async_ready_cb, &body);
 
        while (!message_finished)
                g_main_context_iteration (async_context, TRUE);
 
+       g_signal_handler_disconnect (msg, signal_id);
+
         g_main_context_unref (async_context);
-       return msg->status_code;
+       return body;
+}
+
+static void
+on_send_message_finished (SoupSession *session,
+                         SoupMessage *msg,
+                         gpointer     user_data)
+{
+       gboolean *message_finished = user_data;
+       *message_finished = TRUE;
+}
+
+guint
+soup_test_session_async_send_message (SoupSession *session,
+                                     SoupMessage *msg)
+{
+       gboolean message_finished = FALSE;
+        GMainContext *async_context = g_main_context_ref_thread_default ();
+
+       g_object_ref (msg); // We want to keep it alive rather than the session consuming it.
+        soup_session_queue_message (session, msg, on_send_message_finished, &message_finished);
+
+       while (!message_finished)
+                g_main_context_iteration (async_context, TRUE);
+
+       g_main_context_unref (async_context);
+        return msg->status_code;
 }
 
 static void
diff --git a/tests/test-utils.h b/tests/test-utils.h
index 058e153a..67392056 100644
--- a/tests/test-utils.h
+++ b/tests/test-utils.h
@@ -51,9 +51,12 @@ typedef enum {
        SOUP_TEST_REQUEST_CANCEL_AFTER_SEND_FINISH = (1 << 5),
 } SoupTestRequestFlags;
 
-SoupSession *soup_test_session_new         (GType type, ...);
-void         soup_test_session_abort_unref (SoupSession *session);
-guint        soup_test_session_async_send_message (SoupSession *session, SoupMessage *msg);
+SoupSession *soup_test_session_new                (GType type, ...);
+void         soup_test_session_abort_unref        (SoupSession *session);
+GBytes      *soup_test_session_async_send         (SoupSession *session,
+                                                  SoupMessage *msg);
+guint        soup_test_session_async_send_message (SoupSession *session,
+                                                  SoupMessage *msg);
 
 typedef enum {
        SOUP_TEST_SERVER_DEFAULT             = 0,



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