summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDan Winship <danw@gnome.org>2012-07-09 13:44:42 -0400
committerDan Winship <danw@gnome.org>2012-07-13 16:26:55 -0400
commitce65958b63582548675aa598d5126be798e1e866 (patch)
tree83fc61403411b8d5e5084c6e909b9176d7f7e836 /tests
parentda4f5b9e2b83771b23dbcfde62499425fe066e93 (diff)
downloadlibsoup-ce65958b63582548675aa598d5126be798e1e866.tar.gz
libsoup-ce65958b63582548675aa598d5126be798e1e866.tar.bz2
libsoup-ce65958b63582548675aa598d5126be798e1e866.zip
Add some test-utils helpers for async SoupRequest usage
Diffstat (limited to 'tests')
-rw-r--r--tests/coding-test.c33
-rw-r--r--tests/misc-test.c46
-rw-r--r--tests/proxy-test.c55
-rw-r--r--tests/redirect-test.c34
-rw-r--r--tests/test-utils.c59
-rw-r--r--tests/test-utils.h7
6 files changed, 115 insertions, 119 deletions
diff --git a/tests/coding-test.c b/tests/coding-test.c
index 47f5d085..8c68a35b 100644
--- a/tests/coding-test.c
+++ b/tests/coding-test.c
@@ -304,22 +304,6 @@ do_coding_test (void)
}
static void
-req_sent (GObject *req, GAsyncResult *result, gpointer user_data)
-{
- GInputStream **stream = user_data;
- GError *error = NULL;
-
- *stream = soup_request_send_finish (SOUP_REQUEST (req),
- result, &error);
- if (error) {
- debug_printf (1, " Error sending request: %s\n",
- error->message);
- g_error_free (error);
- errors++;
- }
-}
-
-static void
read_finished (GObject *stream, GAsyncResult *result, gpointer user_data)
{
gssize *nread = user_data;
@@ -348,14 +332,19 @@ do_single_coding_req_test (SoupRequest *req,
gssize nread;
GError *error = NULL;
+ data = g_byte_array_new ();
+
msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (req));
- stream = NULL;
- soup_request_send_async (req, NULL, req_sent, &stream);
- while (!stream)
- g_main_context_iteration (NULL, TRUE);
+ stream = soup_test_request_send_async_as_sync (req, NULL, &error);
+ if (error) {
+ debug_printf (1, " Error sending request: %s\n",
+ error->message);
+ g_error_free (error);
+ errors++;
+ return data;
+ }
- data = g_byte_array_new ();
do {
nread = -2;
g_input_stream_read_async (stream, buf, sizeof (buf),
@@ -368,7 +357,7 @@ do_single_coding_req_test (SoupRequest *req,
g_byte_array_append (data, buf, nread);
} while (nread > 0);
- g_input_stream_close (stream, NULL, &error);
+ soup_test_stream_close_async_as_sync (stream, NULL, &error);
if (error) {
debug_printf (1, " error closing stream: %s\n",
error->message);
diff --git a/tests/misc-test.c b/tests/misc-test.c
index 9d75a9c9..605fa216 100644
--- a/tests/misc-test.c
+++ b/tests/misc-test.c
@@ -932,29 +932,12 @@ cancel_request_thread (gpointer cancellable)
}
static void
-cancel_request_finished (GObject *source, GAsyncResult *result, gpointer loop)
-{
- GError *error = NULL;
-
- if (soup_request_send_finish (SOUP_REQUEST (source), result, &error)) {
- debug_printf (1, " Request succeeded?\n");
- errors++;
- } else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
- debug_printf (1, " Unexpected error: %s\n",
- error->message);
- errors++;
- }
- g_clear_error (&error);
-
- g_main_loop_quit (loop);
-}
-
-static void
do_cancel_while_reading_req_test_for_session (SoupRequester *requester)
{
SoupRequest *req;
SoupURI *uri;
GCancellable *cancellable;
+ GError *error = NULL;
uri = soup_uri_new_with_base (base_uri, "/slow");
req = soup_requester_request_uri (requester, uri, NULL);
@@ -963,31 +946,26 @@ do_cancel_while_reading_req_test_for_session (SoupRequester *requester)
cancellable = g_cancellable_new ();
if (SOUP_IS_SESSION_ASYNC (soup_request_get_session (req))) {
- GMainLoop *loop;
-
- loop = g_main_loop_new (NULL, FALSE);
g_timeout_add (100, cancel_request_timeout, cancellable);
- soup_request_send_async (req, cancellable,
- cancel_request_finished, loop);
- g_main_loop_run (loop);
- g_main_loop_unref (loop);
+ soup_test_request_send_async_as_sync (req, cancellable, &error);
} else {
GThread *thread;
- GError *error = NULL;
thread = g_thread_new ("cancel_request_thread", cancel_request_thread, cancellable);
soup_request_send (req, cancellable, &error);
- if (!error) {
- debug_printf (1, " Request succeeded?\n");
- errors++;
- } else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
- debug_printf (1, " Unexpected error: %s\n",
- error->message);
- errors++;
- }
g_thread_unref (thread);
}
+ if (!error) {
+ debug_printf (1, " Request succeeded?\n");
+ errors++;
+ } else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
+ debug_printf (1, " Unexpected error: %s\n",
+ error->message);
+ errors++;
+ }
+ g_clear_error (&error);
+
g_object_unref (req);
g_object_unref (cancellable);
}
diff --git a/tests/proxy-test.c b/tests/proxy-test.c
index cebc8390..85aac9f5 100644
--- a/tests/proxy-test.c
+++ b/tests/proxy-test.c
@@ -124,27 +124,6 @@ test_url (const char *url, int proxy, guint expected,
soup_test_session_abort_unref (session);
}
-static GMainLoop *loop;
-
-static void
-request_completed (GObject *source, GAsyncResult *result, gpointer user_data)
-{
- SoupRequest *req = SOUP_REQUEST (source);
- GInputStream **stream_p = user_data;
- GError *error = NULL;
-
- debug_printf (2, " Request completed\n");
- *stream_p = soup_request_send_finish (req, result, &error);
- if (!*stream_p) {
- debug_printf (1, " Unexpected error on Request: %s\n",
- error->message);
- errors++;
- g_error_free (error);
- }
-
- g_main_loop_quit (loop);
-}
-
static void
test_url_new_api (const char *url, int proxy, guint expected,
gboolean sync, gboolean close)
@@ -155,6 +134,7 @@ test_url_new_api (const char *url, int proxy, guint expected,
SoupRequester *requester;
SoupRequest *request;
GInputStream *stream;
+ GError *error = NULL;
if (!tls_available && g_str_has_prefix (url, "https:"))
return;
@@ -186,26 +166,31 @@ test_url_new_api (const char *url, int proxy, guint expected,
request = soup_requester_request (requester, url, NULL);
msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));
- if (sync) {
- GError *error = NULL;
-
+ if (sync)
stream = soup_request_send (request, NULL, &error);
- if (!stream) {
- debug_printf (1, " Unexpected error on Request: %s\n",
+ else
+ stream = soup_test_request_send_async_as_sync (request, NULL, &error);
+
+ if (!stream) {
+ debug_printf (1, " Unexpected error on Request: %s\n",
+ error->message);
+ errors++;
+ g_clear_error (&error);
+ }
+
+ if (stream) {
+ if (sync)
+ g_input_stream_close (stream, NULL, NULL);
+ else
+ soup_test_stream_close_async_as_sync (stream, NULL, NULL);
+ if (error) {
+ debug_printf (1, " Unexpected error on close: %s\n",
error->message);
errors++;
- g_error_free (error);
+ g_clear_error (&error);
}
- } else {
- loop = g_main_loop_new (NULL, TRUE);
- soup_request_send_async (request, NULL, request_completed, &stream);
- g_main_loop_run (loop);
- g_main_loop_unref (loop);
}
- if (stream)
- g_input_stream_close (stream, NULL, NULL);
-
debug_printf (1, " %d %s\n", msg->status_code, msg->reason_phrase);
if (msg->status_code != expected) {
debug_printf (1, " EXPECTED %d!\n", expected);
diff --git a/tests/redirect-test.c b/tests/redirect-test.c
index 45166e69..59a2f077 100644
--- a/tests/redirect-test.c
+++ b/tests/redirect-test.c
@@ -206,14 +206,6 @@ do_message_api_test (SoupSession *session, SoupURI *base_uri, int n)
}
static void
-async_callback (GObject *source, GAsyncResult *result, gpointer user_data)
-{
- GAsyncResult **result_p = user_data;
-
- *result_p = g_object_ref (result);
-}
-
-static void
do_request_api_test (SoupSession *session, SoupURI *base_uri, int n)
{
SoupRequester *requester = (SoupRequester *)soup_session_get_feature (session, SOUP_TYPE_REQUESTER);
@@ -222,7 +214,6 @@ do_request_api_test (SoupSession *session, SoupURI *base_uri, int n)
SoupMessage *msg;
TestRequest *treq;
GInputStream *stream;
- GAsyncResult *result;
GError *error = NULL;
guint final_status;
@@ -264,16 +255,10 @@ do_request_api_test (SoupSession *session, SoupURI *base_uri, int n)
g_signal_connect (msg, "restarted",
G_CALLBACK (restarted), &treq);
- if (SOUP_IS_SESSION_SYNC (session)) {
+ if (SOUP_IS_SESSION_SYNC (session))
stream = soup_request_send (req, NULL, &error);
- } else {
- result = NULL;
- soup_request_send_async (req, NULL, async_callback, &result);
- while (!result)
- g_main_context_iteration (NULL, TRUE);
- stream = soup_request_send_finish (req, result, &error);
- g_object_unref (result);
- }
+ else
+ stream = soup_test_request_send_async_as_sync (req, NULL, &error);
if (SOUP_STATUS_IS_TRANSPORT_ERROR (final_status)) {
if (stream) {
@@ -304,17 +289,10 @@ do_request_api_test (SoupSession *session, SoupURI *base_uri, int n)
return;
}
- if (SOUP_IS_SESSION_SYNC (session)) {
+ if (SOUP_IS_SESSION_SYNC (session))
g_input_stream_close (stream, NULL, &error);
- } else {
- result = NULL;
- g_input_stream_close_async (stream, G_PRIORITY_DEFAULT,
- NULL, async_callback, &result);
- while (!result)
- g_main_context_iteration (NULL, TRUE);
- g_input_stream_close_finish (stream, result, &error);
- g_object_unref (result);
- }
+ else
+ soup_test_stream_close_async_as_sync (stream, NULL, &error);
if (error) {
debug_printf (1, " could not close stream: %s\n",
error->message);
diff --git a/tests/test-utils.c b/tests/test-utils.c
index f2a3bd69..f040b738 100644
--- a/tests/test-utils.c
+++ b/tests/test-utils.c
@@ -353,3 +353,62 @@ soup_test_server_quit_unref (SoupServer *server)
(gpointer *)&server);
}
}
+
+typedef struct {
+ GMainLoop *loop;
+ GAsyncResult *result;
+} AsyncAsSyncData;
+
+static void
+async_as_sync_callback (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ AsyncAsSyncData *data = user_data;
+
+ data->result = g_object_ref (result);
+ g_main_loop_quit (data->loop);
+}
+
+GInputStream *
+soup_test_request_send_async_as_sync (SoupRequest *req,
+ GCancellable *cancellable,
+ GError **error)
+{
+ AsyncAsSyncData data;
+ GInputStream *stream;
+
+ data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
+
+ soup_request_send_async (req, cancellable, async_as_sync_callback, &data);
+ g_main_loop_run (data.loop);
+
+ stream = soup_request_send_finish (req, data.result, error);
+
+ g_main_loop_unref (data.loop);
+ g_object_unref (data.result);
+
+ return stream;
+}
+
+gboolean
+soup_test_stream_close_async_as_sync (GInputStream *stream,
+ GCancellable *cancellable,
+ GError **error)
+{
+ AsyncAsSyncData data;
+ gboolean ok;
+
+ data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
+
+ g_input_stream_close_async (stream, G_PRIORITY_DEFAULT, cancellable,
+ async_as_sync_callback, &data);
+ g_main_loop_run (data.loop);
+
+ ok = g_input_stream_close_finish (stream, data.result, error);
+
+ g_main_loop_unref (data.loop);
+ g_object_unref (data.result);
+
+ return ok;
+}
diff --git a/tests/test-utils.h b/tests/test-utils.h
index 78f7e487..9e969038 100644
--- a/tests/test-utils.h
+++ b/tests/test-utils.h
@@ -31,3 +31,10 @@ void soup_test_session_abort_unref (SoupSession *session);
SoupServer *soup_test_server_new (gboolean in_own_thread);
SoupServer *soup_test_server_new_ssl (gboolean in_own_thread);
void soup_test_server_quit_unref (SoupServer *server);
+
+GInputStream *soup_test_request_send_async_as_sync (SoupRequest *req,
+ GCancellable *cancellable,
+ GError **error);
+gboolean soup_test_stream_close_async_as_sync (GInputStream *stream,
+ GCancellable *cancellable,
+ GError **error);