[libgdata: 19/22] tests: Add support for the new SoupServer API



commit d5938f7c4c778ec42f1d0dd9af96dbb0065be1c7
Author: Philip Withnall <philip tecnocode co uk>
Date:   Wed Sep 17 22:59:06 2014 +0100

    tests: Add support for the new SoupServer API
    
    This increments the maximum libsoup dependency to ≤ 2.47.3, but does not
    increment the minimum dependency.

 configure.ac          |   10 +++-
 gdata/tests/streams.c |  181 +++++++++++++++++++++++++++++++++++++------------
 2 files changed, 147 insertions(+), 44 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 2c72cae..9312664 100644
--- a/configure.ac
+++ b/configure.ac
@@ -39,7 +39,7 @@ GLIB_MAX_ALLOWED='(G_ENCODE_VERSION(2, 38))'
 GIO_REQS=2.17.3
 SOUP_REQS=2.42.0
 SOUP_MIN_REQUIRED=SOUP_VERSION_2_42
-SOUP_MAX_ALLOWED=SOUP_VERSION_2_42
+SOUP_MAX_ALLOWED=SOUP_VERSION_2_48
 OAUTH_REQS=0.9.4
 GTK_REQS=2.91.2
 GOA_REQS=3.8
@@ -91,6 +91,14 @@ GDATA_CFLAGS="$GDATA_CFLAGS -DJSON_VERSION_MIN_REQUIRED=$JSON_GLIB_MIN_REQUIRED
 AC_SUBST(GDATA_CFLAGS)
 AC_SUBST(GDATA_LIBS)
 
+# libsoup 2.47.3 is needed for the new SoupServer API.
+PKG_CHECK_MODULES([LIBSOUP], [libsoup-2.4 >= 2.47.3],
+                  [have_libsoup_2_47_3=yes], [have_libsoup_2_47_3=no])
+AS_IF([test "x$have_libsoup_2_47_3" = "xyes"], [
+       AC_DEFINE([HAVE_LIBSOUP_2_47_3], [1],
+                 [Define if the new SoupServer API is available])
+])
+
 # Optional dependencies
 PKG_CHECK_MODULES(GDK_PIXBUF, gdk-pixbuf-2.0 >= $GDK_PIXBUF_REQS, have_gdk_pixbuf=yes, have_gdk_pixbuf=no)
 if test "x$have_gdk_pixbuf" = "xyes"; then
diff --git a/gdata/tests/streams.c b/gdata/tests/streams.c
index 92554d4..eeb4950 100644
--- a/gdata/tests/streams.c
+++ b/gdata/tests/streams.c
@@ -17,6 +17,8 @@
  * License along with GData Client.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "config.h"
+
 #include <glib.h>
 #include <locale.h>
 #include <string.h>
@@ -28,6 +30,17 @@
 #include "gdata.h"
 #include "common.h"
 
+#ifdef HAVE_LIBSOUP_2_47_3
+static gpointer
+run_server_thread (GMainLoop *loop)
+{
+       g_main_context_push_thread_default (g_main_loop_get_context (loop));
+       g_main_loop_run (loop);
+       g_main_context_pop_thread_default (g_main_loop_get_context (loop));
+
+       return NULL;
+}
+#else /* if !HAVE_LIBSOUP_2_47_3 */
 static gpointer
 run_server_thread (SoupServer *server)
 {
@@ -35,26 +48,62 @@ run_server_thread (SoupServer *server)
 
        return NULL;
 }
+#endif /* !HAVE_LIBSOUP_2_47_3 */
 
 static GThread *
-run_server (SoupServer *server)
+run_server (SoupServer *server, GMainLoop *loop)
 {
        GThread *thread;
        gchar *port_string;
        GError *error = NULL;
+       guint16 port;
 
+#ifdef HAVE_LIBSOUP_2_47_3
+       thread = g_thread_try_new ("server-thread", (GThreadFunc) run_server_thread, loop, &error);
+#else /* if !HAVE_LIBSOUP_2_47_3 */
        thread = g_thread_try_new ("server-thread", (GThreadFunc) run_server_thread, server, &error);
+#endif /* !HAVE_LIBSOUP_2_47_3 */
        g_assert_no_error (error);
        g_assert (thread != NULL);
 
        /* Set the port so that libgdata doesn't override it. */
-       port_string = g_strdup_printf ("%u", soup_server_get_port (server));
+#ifdef HAVE_LIBSOUP_2_47_3
+{
+       GSList *uris;  /* owned */
+
+       uris = soup_server_get_uris (server);
+       g_assert (uris != NULL);
+       port = soup_uri_get_port (uris->data);
+
+       g_slist_free_full (uris, (GDestroyNotify) soup_uri_free);
+}
+#else /* if !HAVE_LIBSOUP_2_47_3 */
+       port = soup_server_get_port (server);
+#endif /* !HAVE_LIBSOUP_2_47_3 */
+
+       port_string = g_strdup_printf ("%u", port);
        g_setenv ("LIBGDATA_HTTPS_PORT", port_string, TRUE);
        g_free (port_string);
 
        return thread;
 }
 
+#ifdef HAVE_LIBSOUP_2_47_3
+static gboolean
+quit_server_cb (GMainLoop *loop)
+{
+       g_main_loop_quit (loop);
+
+       return FALSE;
+}
+
+static void
+stop_server (SoupServer *server, GMainLoop *loop)
+{
+       soup_add_completion (g_main_loop_get_context (loop),
+                            (GSourceFunc) quit_server_cb, loop);
+}
+#else /* if !HAVE_LIBSOUP_2_47_3 */
 static gboolean
 quit_server_cb (SoupServer *server)
 {
@@ -63,6 +112,14 @@ quit_server_cb (SoupServer *server)
        return FALSE;
 }
 
+static void
+stop_server (SoupServer *server, GMainLoop *loop)
+{
+       soup_add_completion (g_main_loop_get_context (loop),
+                            (GSourceFunc) quit_server_cb, server);
+}
+#endif /* !HAVE_LIBSOUP_2_47_3 */
+
 static gchar *
 get_test_string (guint start_num, guint end_num)
 {
@@ -97,18 +154,38 @@ test_download_stream_download_server_content_length_handler_cb (SoupServer *serv
 }
 
 static SoupServer *
-create_server (SoupServerCallback callback, gpointer user_data, GMainContext **async_context)
+create_server (SoupServerCallback callback, gpointer user_data, GMainLoop **main_loop)
 {
+       GMainContext *context;
+       SoupServer *server;
+#ifdef HAVE_LIBSOUP_2_47_3
+       GError *error = NULL;
+#else /* if !HAVE_LIBSOUP_2_47_3 */
        union {
                struct sockaddr_in in;
                struct sockaddr norm;
        } sock;
        SoupAddress *addr;
-       SoupServer *server;
-
-       g_assert (async_context != NULL);
+#endif /* HAVE_LIBSOUP_2_47_3 */
 
        /* Create the server */
+       g_assert (main_loop != NULL);
+       context = g_main_context_new ();
+       *main_loop = g_main_loop_new (context, FALSE);
+
+#ifdef HAVE_LIBSOUP_2_47_3
+       server = soup_server_new (NULL, NULL);
+
+       soup_server_add_handler (server, NULL, callback, user_data, NULL);
+
+       g_main_context_push_thread_default (context);
+
+       soup_server_listen_local (server, 0  /* random port */,
+                                 0  /* no options */, &error);
+       g_assert_no_error (error);
+
+       g_main_context_pop_thread_default (context);
+#else /* if !HAVE_LIBSOUP_2_47_3 */
        memset (&sock, 0, sizeof (sock));
        sock.in.sin_family = AF_INET;
        sock.in.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
@@ -117,16 +194,17 @@ create_server (SoupServerCallback callback, gpointer user_data, GMainContext **a
        addr = soup_address_new_from_sockaddr (&sock.norm, sizeof (sock.norm));
        g_assert (addr != NULL);
 
-       *async_context = g_main_context_new ();
-
        server = soup_server_new (SOUP_SERVER_INTERFACE, addr,
-                                 SOUP_SERVER_ASYNC_CONTEXT, *async_context,
+                                 SOUP_SERVER_ASYNC_CONTEXT, context,
                                  NULL);
+
        soup_server_add_handler (server, NULL, callback, user_data, NULL);
 
        g_object_unref (addr);
+#endif /* !HAVE_LIBSOUP_2_47_3 */
 
        g_assert (server != NULL);
+       g_main_context_unref (context);
 
        return server;
 }
@@ -134,16 +212,32 @@ create_server (SoupServerCallback callback, gpointer user_data, GMainContext **a
 static gchar *
 build_server_uri (SoupServer *server)
 {
+#ifdef HAVE_LIBSOUP_2_47_3
+       GSList *uris;  /* owned */
+       gchar *retval = NULL;  /* owned */
+
+       uris = soup_server_get_uris (server);
+       if (uris == NULL) {
+               return NULL;
+       }
+
+       retval = soup_uri_to_string (uris->data, FALSE);
+
+       g_slist_free_full (uris, (GDestroyNotify) soup_uri_free);
+
+       return retval;
+#else /* if !HAVE_LIBSOUP_2_47_3 */
        return g_strdup_printf ("http://%s:%u/";,
                                soup_address_get_physical (soup_socket_get_local_address 
(soup_server_get_listener (server))),
                                soup_server_get_port (server));
+#endif /* !HAVE_LIBSOUP_2_47_3 */
 }
 
 static void
 test_download_stream_download_content_length (void)
 {
        SoupServer *server;
-       GMainContext *async_context;
+       GMainLoop *main_loop;
        GThread *thread;
        gchar *download_uri, *test_string;
        GDataService *service;
@@ -155,8 +249,8 @@ test_download_stream_download_content_length (void)
        GError *error = NULL;
 
        /* Create and run the server */
-       server = create_server ((SoupServerCallback) 
test_download_stream_download_server_content_length_handler_cb, NULL, &async_context);
-       thread = run_server (server);
+       server = create_server ((SoupServerCallback) 
test_download_stream_download_server_content_length_handler_cb, NULL, &main_loop);
+       thread = run_server (server, main_loop);
 
        /* Create a new download stream connected to the server */
        download_uri = build_server_uri (server);
@@ -193,12 +287,12 @@ test_download_stream_download_content_length (void)
        g_string_free (contents, TRUE);
 
        /* Kill the server and wait for it to die */
-       soup_add_completion (async_context, (GSourceFunc) quit_server_cb, server);
+       stop_server (server, main_loop);
        g_thread_join (thread);
 
        g_object_unref (download_stream);
        g_object_unref (server);
-       g_main_context_unref (async_context);
+       g_main_loop_unref (main_loop);
 }
 
 static void
@@ -221,7 +315,7 @@ static void
 test_download_stream_download_seek_before_start (void)
 {
        SoupServer *server;
-       GMainContext *async_context;
+       GMainLoop *main_loop;
        GThread *thread;
        gchar *download_uri, *test_string;
        goffset test_string_offset = 0;
@@ -234,8 +328,8 @@ test_download_stream_download_seek_before_start (void)
        GError *error = NULL;
 
        /* Create and run the server */
-       server = create_server ((SoupServerCallback) test_download_stream_download_server_seek_handler_cb, 
NULL, &async_context);
-       thread = run_server (server);
+       server = create_server ((SoupServerCallback) test_download_stream_download_server_seek_handler_cb, 
NULL, &main_loop);
+       thread = run_server (server, main_loop);
 
        /* Create a new download stream connected to the server */
        download_uri = build_server_uri (server);
@@ -297,12 +391,12 @@ test_download_stream_download_seek_before_start (void)
        g_assert (success == TRUE);
 
        /* Kill the server and wait for it to die */
-       soup_add_completion (async_context, (GSourceFunc) quit_server_cb, server);
+       stop_server (server, main_loop);
        g_thread_join (thread);
 
        g_object_unref (download_stream);
        g_object_unref (server);
-       g_main_context_unref (async_context);
+       g_main_loop_unref (main_loop);
 }
 
 /* Test seeking forwards after the first read */
@@ -310,7 +404,7 @@ static void
 test_download_stream_download_seek_after_start_forwards (void)
 {
        SoupServer *server;
-       GMainContext *async_context;
+       GMainLoop *main_loop;
        GThread *thread;
        gchar *download_uri, *test_string;
        goffset test_string_offset = 0;
@@ -323,8 +417,8 @@ test_download_stream_download_seek_after_start_forwards (void)
        GError *error = NULL;
 
        /* Create and run the server */
-       server = create_server ((SoupServerCallback) test_download_stream_download_server_seek_handler_cb, 
NULL, &async_context);
-       thread = run_server (server);
+       server = create_server ((SoupServerCallback) test_download_stream_download_server_seek_handler_cb, 
NULL, &main_loop);
+       thread = run_server (server, main_loop);
 
        /* Create a new download stream connected to the server */
        download_uri = build_server_uri (server);
@@ -386,12 +480,12 @@ test_download_stream_download_seek_after_start_forwards (void)
        g_assert (success == TRUE);
 
        /* Kill the server and wait for it to die */
-       soup_add_completion (async_context, (GSourceFunc) quit_server_cb, server);
+       stop_server (server, main_loop);
        g_thread_join (thread);
 
        g_object_unref (download_stream);
        g_object_unref (server);
-       g_main_context_unref (async_context);
+       g_main_loop_unref (main_loop);
 }
 
 /* Test seeking backwards after the first read */
@@ -399,7 +493,7 @@ static void
 test_download_stream_download_seek_after_start_backwards (void)
 {
        SoupServer *server;
-       GMainContext *async_context;
+       GMainLoop *main_loop;
        GThread *thread;
        gchar *download_uri, *test_string;
        goffset test_string_offset = 0;
@@ -412,8 +506,8 @@ test_download_stream_download_seek_after_start_backwards (void)
        GError *error = NULL;
 
        /* Create and run the server */
-       server = create_server ((SoupServerCallback) test_download_stream_download_server_seek_handler_cb, 
NULL, &async_context);
-       thread = run_server (server);
+       server = create_server ((SoupServerCallback) test_download_stream_download_server_seek_handler_cb, 
NULL, &main_loop);
+       thread = run_server (server, main_loop);
 
        /* Create a new download stream connected to the server */
        download_uri = build_server_uri (server);
@@ -470,12 +564,12 @@ test_download_stream_download_seek_after_start_backwards (void)
        g_assert (success == TRUE);
 
        /* Kill the server and wait for it to die */
-       soup_add_completion (async_context, (GSourceFunc) quit_server_cb, server);
+       stop_server (server, main_loop);
        g_thread_join (thread);
 
        g_object_unref (download_stream);
        g_object_unref (server);
-       g_main_context_unref (async_context);
+       g_main_loop_unref (main_loop);
 }
 
 static void
@@ -510,7 +604,7 @@ static void
 test_upload_stream_upload_no_entry_content_length (void)
 {
        SoupServer *server;
-       GMainContext *async_context;
+       GMainLoop *main_loop;
        GThread *thread;
        gchar *upload_uri, *test_string;
        GDataService *service;
@@ -522,8 +616,8 @@ test_upload_stream_upload_no_entry_content_length (void)
        GError *error = NULL;
 
        /* Create and run the server */
-       server = create_server ((SoupServerCallback) 
test_upload_stream_upload_no_entry_content_length_server_handler_cb, NULL, &async_context);
-       thread = run_server (server);
+       server = create_server ((SoupServerCallback) 
test_upload_stream_upload_no_entry_content_length_server_handler_cb, NULL, &main_loop);
+       thread = run_server (server, main_loop);
 
        /* Create a new upload stream uploading to the server */
        upload_uri = build_server_uri (server);
@@ -556,12 +650,12 @@ test_upload_stream_upload_no_entry_content_length (void)
        g_assert (success == TRUE);
 
        /* Kill the server and wait for it to die */
-       soup_add_completion (async_context, (GSourceFunc) quit_server_cb, server);
+       stop_server (server, main_loop);
        g_thread_join (thread);
 
        g_object_unref (upload_stream);
        g_object_unref (server);
-       g_main_context_unref (async_context);
+       g_main_loop_unref (main_loop);
 }
 
 /* Test parameters for a run of test_upload_stream_resumable(). */
@@ -772,7 +866,7 @@ error: {
        return;
 
 continuation: {
-               gchar *upload_uri;
+               gchar *upload_uri, *server_uri;
 
                /* Continuation. */
                if (server_data->next_path_index == 0) {
@@ -781,12 +875,13 @@ continuation: {
                        soup_message_set_status (message, 308);
                }
 
-               upload_uri = g_strdup_printf ("http://%s:%u/%u";,
-                                             soup_address_get_physical (soup_socket_get_local_address 
(soup_server_get_listener (server))),
-                                             soup_server_get_port (server),
+               server_uri = build_server_uri (server);
+               g_assert_cmpstr (server_uri + strlen (server_uri) - 1, ==, "/");
+               upload_uri = g_strdup_printf ("%s%u", server_uri,
                                              ++server_data->next_path_index);
                soup_message_headers_replace (message->response_headers, "Location", upload_uri);
                g_free (upload_uri);
+               g_free (server_uri);
        }
 
        return;
@@ -836,7 +931,7 @@ test_upload_stream_resumable (gconstpointer user_data)
        UploadStreamResumableTestParams *test_params;
        UploadStreamResumableServerData server_data;
        SoupServer *server;
-       GMainContext *async_context;
+       GMainLoop *main_loop;
        GThread *thread;
        gchar *upload_uri;
        GDataService *service;
@@ -869,8 +964,8 @@ test_upload_stream_resumable (gconstpointer user_data)
        server_data.next_path_index = 0;
        server_data.test_string = test_string;
 
-       server = create_server ((SoupServerCallback) test_upload_stream_resumable_server_handler_cb, 
&server_data, &async_context);
-       thread = run_server (server);
+       server = create_server ((SoupServerCallback) test_upload_stream_resumable_server_handler_cb, 
&server_data, &main_loop);
+       thread = run_server (server, main_loop);
 
        /* Create a new upload stream uploading to the server */
        if (test_params->content_type == CONTENT_AND_METADATA || test_params->content_type == METADATA_ONLY) {
@@ -936,13 +1031,13 @@ test_upload_stream_resumable (gconstpointer user_data)
        }
 
        /* Kill the server and wait for it to die */
-       soup_add_completion (async_context, (GSourceFunc) quit_server_cb, server);
+       stop_server (server, main_loop);
        g_thread_join (thread);
 
        g_free (test_string);
        g_object_unref (upload_stream);
        g_object_unref (server);
-       g_main_context_unref (async_context);
+       g_main_loop_unref (main_loop);
 }
 
 int


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