summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimm Bäder <mail@baedert.org>2016-07-26 08:19:32 +0200
committerTimm Bäder <mail@baedert.org>2017-02-21 17:21:19 +0100
commitbc9ba9d05734799c5e936604e5058788679b1488 (patch)
tree6cc27278208105b6db56b4e9c4418bb468b7bc3b
parent4b38ffc06b424a914e0bc9f1c133243577a55ae6 (diff)
downloadlibrest-bc9ba9d05734799c5e936604e5058788679b1488.tar.gz
tests: Add TestServer implementation
Add a simple soup server implementation that runs in a different thread.
-rw-r--r--tests/Makefile.am8
-rw-r--r--tests/custom-serialize.c27
-rw-r--r--tests/proxy-continuous.c34
-rw-r--r--tests/proxy.c90
-rw-r--r--tests/test-server.c90
-rw-r--r--tests/test-server.h38
-rw-r--r--tests/threaded.c27
7 files changed, 166 insertions, 148 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5d77f9c..d3fbae9 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -8,13 +8,13 @@ AM_LDFLAGS = $(SOUP_LIBS) $(GCOV_LDFLAGS) \
check_PROGRAMS = $(TESTS)
-proxy_SOURCES = proxy.c
-proxy_continuous_SOURCES = proxy-continuous.c
-threaded_SOURCES = threaded.c
+proxy_SOURCES = proxy.c test-server.c
+proxy_continuous_SOURCES = proxy-continuous.c test-server.c
+threaded_SOURCES = threaded.c test-server.c
oauth_SOURCES = oauth.c
oauth_async_SOURCES = oauth-async.c
oauth2_SOURCES = oauth2.c
flickr_SOURCES = flickr.c
lastfm_SOURCES = lastfm.c
xml_SOURCES = xml.c
-custom_serialize_SOURCES = custom-serialize.c
+custom_serialize_SOURCES = custom-serialize.c test-server.c
diff --git a/tests/custom-serialize.c b/tests/custom-serialize.c
index c3fde93..ff3cecf 100644
--- a/tests/custom-serialize.c
+++ b/tests/custom-serialize.c
@@ -26,8 +26,7 @@
#include <stdlib.h>
#include <libsoup/soup.h>
#include <rest/rest-proxy.h>
-
-#define PORT 8080
+#include "test-server.h"
#define REST_TYPE_CUSTOM_PROXY_CALL custom_proxy_call_get_type()
@@ -107,33 +106,17 @@ server_callback (SoupServer *server, SoupMessage *msg,
}
}
-static void *
-server_func (gpointer data)
-{
- GMainLoop *loop = g_main_loop_new (NULL, FALSE);
- SoupServer *server = soup_server_new (NULL, NULL);
- GSocketAddress *address = g_inet_socket_address_new_from_string ("127.0.0.1", PORT);
-
- soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
- soup_server_listen (server, address, 0, NULL);
-
- g_main_loop_run (loop);
- return NULL;
-}
-
static void
test_custom_serialize ()
{
+ TestServer *server = test_server_create (server_callback);
RestProxy *proxy;
RestProxyCall *call;
- char *url;
GError *error = NULL;
- url = g_strdup_printf ("http://127.0.0.1:%d/", PORT);
-
- g_thread_new ("Server Thread", server_func, NULL);
+ test_server_run (server);
- proxy = rest_proxy_new (url, FALSE);
+ proxy = rest_proxy_new (server->url, FALSE);
call = g_object_new (REST_TYPE_CUSTOM_PROXY_CALL, "proxy", proxy, NULL);
rest_proxy_call_set_function (call, "wrong-function");
@@ -143,9 +126,9 @@ test_custom_serialize ()
g_assert_cmpint (rest_proxy_call_get_status_code (call), ==, SOUP_STATUS_OK);
+ test_server_stop (server);
g_object_unref (call);
g_object_unref (proxy);
- g_free (url);
}
int
diff --git a/tests/proxy-continuous.c b/tests/proxy-continuous.c
index 8f4b7a8..d4c2e3f 100644
--- a/tests/proxy-continuous.c
+++ b/tests/proxy-continuous.c
@@ -27,6 +27,7 @@
#include <stdio.h>
#include <libsoup/soup.h>
#include <rest/rest-proxy.h>
+#include "test-server.h"
static GMainLoop *loop = NULL;
@@ -34,7 +35,7 @@ static GMainLoop *loop = NULL;
#define SIZE_CHUNK 4
static guint8 server_count = 0;
static guint8 client_count = 0;
-static SoupServer *server;
+static TestServer *server;
static gboolean
send_chunks (gpointer user_data)
@@ -50,7 +51,7 @@ send_chunks (gpointer user_data)
}
soup_message_body_append (msg->response_body, SOUP_MEMORY_COPY, data, SIZE_CHUNK);
- soup_server_unpause_message (server, msg);
+ soup_server_unpause_message (server->server, msg);
if (server_count == NUM_CHUNKS * SIZE_CHUNK)
{
@@ -127,29 +128,22 @@ stream_test (RestProxy *proxy)
static void
continuous ()
{
- char *url;
+ server = test_server_create (server_callback);
RestProxy *proxy;
- GError *error = NULL;
- GSList *uris;
-
-
- server = soup_server_new (NULL);
- soup_server_listen_local (server, 0, 0, &error);
- g_assert_no_error (error);
-
- soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
-
- uris = soup_server_get_uris (server);
- g_assert (g_slist_length (uris) > 0);
+ RestProxyCall *call;
- url = soup_uri_to_string (uris->data, FALSE);
+ test_server_run (server);
loop = g_main_loop_new (NULL, FALSE);
- proxy = rest_proxy_new (url, FALSE);
- stream_test (proxy);
- g_slist_free_full (uris, (GDestroyNotify)soup_uri_free);
-
+ proxy = rest_proxy_new (server->url, FALSE);
+ call = rest_proxy_new_call (proxy);
+ rest_proxy_call_set_function (call, "stream");
+ rest_proxy_call_continuous (call,
+ _call_continuous_cb,
+ NULL,
+ call_done_cb,
+ NULL);
g_main_loop_run (loop);
g_free (url);
g_main_loop_unref (loop);
diff --git a/tests/proxy.c b/tests/proxy.c
index 79841a4..e9ad394 100644
--- a/tests/proxy.c
+++ b/tests/proxy.c
@@ -35,42 +35,7 @@
#include <libsoup/soup.h>
#include <rest/rest-proxy.h>
-typedef struct {
- SoupServer *server;
- GMainLoop *main_loop;
- char *url;
- GThread *thread;
-} TestServer;
-
-static void
-test_server_stop (TestServer *server)
-{
- g_assert (server->thread);
- g_main_loop_quit (server->main_loop);
- g_thread_join (server->thread);
- g_object_unref (server->server);
- g_main_loop_unref (server->main_loop);
- g_free (server->url);
- g_slice_free (TestServer, server);
-}
-
-static void *
-test_server_thread_func (gpointer data)
-{
- TestServer *server = data;
-
- g_main_context_push_thread_default (g_main_loop_get_context (server->main_loop));
- g_main_loop_run (server->main_loop);
- g_main_context_pop_thread_default (g_main_loop_get_context (server->main_loop));
-
- return NULL;
-}
-
-static void
-test_server_run (TestServer *server)
-{
- server->thread = g_thread_new ("Server Thread", test_server_thread_func, server);
-}
+#include "test-server.h"
static void
server_callback (SoupServer *server, SoupMessage *msg,
@@ -128,49 +93,10 @@ server_callback (SoupServer *server, SoupMessage *msg,
}
}
-static TestServer *
-create_server ()
-{
- TestServer *test_server;
- GMainLoop *main_loop;
- GMainContext *context;
- SoupServer *server;
- GError *error = NULL;
- GSList *uris;
- char *url;
-
- context = g_main_context_new ();
- g_main_context_push_thread_default (context);
- main_loop = g_main_loop_new (context, FALSE);
- server = soup_server_new (NULL);
-
- soup_server_listen_local (server, 0, 0, &error);
- g_assert_no_error (error);
-
- soup_server_add_handler (server, "/", server_callback,
- NULL, NULL);
-
- uris = soup_server_get_uris (server);
- g_assert (g_slist_length (uris) > 0);
- url = soup_uri_to_string (uris->data, FALSE);
-
- test_server = g_slice_alloc (sizeof (TestServer));
- test_server->server = server;
- test_server->main_loop = main_loop;
- test_server->url = url;
- test_server->thread = NULL;
-
- g_slist_free_full (uris, (GDestroyNotify)soup_uri_free);
-
- g_main_context_pop_thread_default (context);
-
- return test_server;
-}
-
static void
ping ()
{
- TestServer *server = create_server ();
+ TestServer *server = test_server_create (server_callback);
RestProxy *proxy = rest_proxy_new (server->url, FALSE);
RestProxyCall *call = rest_proxy_new_call (proxy);
GError *error = NULL;
@@ -193,7 +119,7 @@ ping ()
static void
echo ()
{
- TestServer *server = create_server ();
+ TestServer *server = test_server_create (server_callback);
RestProxy *proxy;
RestProxyCall *call;
GError *error = NULL;
@@ -220,7 +146,7 @@ echo ()
static void
reverse ()
{
- TestServer *server = create_server ();
+ TestServer *server = test_server_create (server_callback);
RestProxy *proxy = rest_proxy_new (server->url, FALSE);
RestProxyCall *call;
GError *error = NULL;
@@ -246,7 +172,7 @@ reverse ()
static void
params ()
{
- TestServer *server = create_server ();
+ TestServer *server = test_server_create (server_callback);
RestProxy *proxy = rest_proxy_new (server->url, FALSE);
RestProxyCall *call;
GError *error = NULL;
@@ -289,7 +215,7 @@ params ()
static void
fail ()
{
- TestServer *server = create_server ();
+ TestServer *server = test_server_create (server_callback);
RestProxy *proxy = rest_proxy_new (server->url, FALSE);
RestProxyCall *call;
GError *error = NULL;
@@ -317,7 +243,7 @@ fail ()
static void
useragent ()
{
- TestServer *server = create_server ();
+ TestServer *server = test_server_create (server_callback);
RestProxy *proxy = rest_proxy_new (server->url, FALSE);
RestProxyCall *call;
GError *error = NULL;
@@ -372,7 +298,7 @@ cancel_cb (GObject *source_object,
static void
cancel ()
{
- TestServer *server = create_server ();
+ TestServer *server = test_server_create (server_callback);
RestProxy *proxy = rest_proxy_new (server->url, FALSE);
RestProxyCall *call;
GMainLoop *main_loop;
diff --git a/tests/test-server.c b/tests/test-server.c
new file mode 100644
index 0000000..a033aec
--- /dev/null
+++ b/tests/test-server.c
@@ -0,0 +1,90 @@
+/*
+ * librest - RESTful web services access
+ * Copyright (c) 2009 Intel Corporation.
+ *
+ * Authors: Timm Bäder <mail@baedert.org>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#include "test-server.h"
+
+TestServer *
+test_server_create (SoupServerCallback server_cb)
+{
+ TestServer *test_server;
+ GMainLoop *main_loop;
+ GMainContext *context;
+ SoupServer *server;
+ GError *error = NULL;
+ GSList *uris;
+ char *url;
+
+ context = g_main_context_new ();
+ g_main_context_push_thread_default (context);
+ main_loop = g_main_loop_new (context, FALSE);
+ server = soup_server_new (NULL);
+
+ soup_server_listen_local (server, 0, 0, &error);
+ g_assert_no_error (error);
+
+ soup_server_add_handler (server, "/", server_cb,
+ NULL, NULL);
+
+ uris = soup_server_get_uris (server);
+ g_assert (g_slist_length (uris) > 0);
+ url = soup_uri_to_string (uris->data, FALSE);
+
+ test_server = g_slice_alloc (sizeof (TestServer));
+ test_server->server = server;
+ test_server->main_loop = main_loop;
+ test_server->url = url;
+ test_server->thread = NULL;
+
+ g_slist_free_full (uris, (GDestroyNotify)soup_uri_free);
+
+ g_main_context_pop_thread_default (context);
+
+ return test_server;
+}
+
+static void *
+test_server_thread_func (gpointer data)
+{
+ TestServer *server = data;
+
+ g_main_context_push_thread_default (g_main_loop_get_context (server->main_loop));
+ g_main_loop_run (server->main_loop);
+ g_main_context_pop_thread_default (g_main_loop_get_context (server->main_loop));
+
+ return NULL;
+}
+
+void
+test_server_run (TestServer *server)
+{
+ server->thread = g_thread_new ("Server Thread", test_server_thread_func, server);
+}
+
+void
+test_server_stop (TestServer *server)
+{
+ g_assert (server->thread);
+ g_main_loop_quit (server->main_loop);
+ g_thread_join (server->thread);
+ g_object_unref (server->server);
+ g_main_loop_unref (server->main_loop);
+ g_free (server->url);
+ g_slice_free (TestServer, server);
+}
diff --git a/tests/test-server.h b/tests/test-server.h
new file mode 100644
index 0000000..7866edb
--- /dev/null
+++ b/tests/test-server.h
@@ -0,0 +1,38 @@
+/*
+ * librest - RESTful web services access
+ * Copyright (c) 2009 Intel Corporation.
+ *
+ * Authors: Timm Bäder <mail@baedert.org>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#ifndef __TEST_SERVER_H
+#define __TEST_SERVER_H
+
+#include <libsoup/soup.h>
+
+typedef struct {
+ SoupServer *server;
+ GMainLoop *main_loop;
+ char *url;
+ GThread *thread;
+} TestServer;
+
+void test_server_stop (TestServer *server);
+void test_server_run (TestServer *server);
+TestServer *test_server_create (SoupServerCallback server_cb);
+
+#endif
+
diff --git a/tests/threaded.c b/tests/threaded.c
index a251900..39da55e 100644
--- a/tests/threaded.c
+++ b/tests/threaded.c
@@ -26,11 +26,12 @@
#include <stdlib.h>
#include <libsoup/soup.h>
#include <rest/rest-proxy.h>
+#include "test-server.h"
const int N_THREADS = 10;
static volatile int threads_done = 0;
-static const gboolean verbose = FALSE;
+static const gboolean verbose = TRUE;
GMainLoop *main_loop;
SoupServer *server;
@@ -80,37 +81,23 @@ func (gpointer data)
static void ping ()
{
+ TestServer *server = test_server_create (server_callback);
GThread *threads[N_THREADS];
- GError *error = NULL;
- char *url;
int i;
- GSList *uris;
-
- server = soup_server_new (NULL);
- soup_server_listen_local (server, 0, 0, &error);
- g_assert_no_error (error);
-
- soup_server_add_handler (server, "/ping", server_callback,
- NULL, NULL);
-
- uris = soup_server_get_uris (server);
- g_assert (g_slist_length (uris) > 0);
-
- url = soup_uri_to_string (uris->data, FALSE);
main_loop = g_main_loop_new (NULL, TRUE);
+ test_server_run (server);
+
for (i = 0; i < N_THREADS; i++) {
- threads[i] = g_thread_new ("client thread", func, url);
+ threads[i] = g_thread_new ("client thread", func, server->url);
if (verbose)
g_print ("Starting thread %p\n", threads[i]);
}
g_main_loop_run (main_loop);
- g_free (url);
- g_slist_free_full (uris, (GDestroyNotify)soup_uri_free);
- g_object_unref (server);
+ test_server_stop (server);
g_main_loop_unref (main_loop);
}