libsoup r1030 - in branches/libsoup-2.4: . tests



Author: danw
Date: Mon Jan 14 14:19:32 2008
New Revision: 1030
URL: http://svn.gnome.org/viewvc/libsoup?rev=1030&view=rev

Log:
	* tests/test-utils.c: renamed from apache-wrappers and expanded.
	(test_init): do option parsing and general setup
	(test_cleanup): print error count and do cleanup
	(debug_printf): define here rather than in each test, and rename
	from dprintf to avoid a conflict on cygwin [#501631]
	(soup_test_server_new): create a SoupServer, optionally in its own
	thread, and clean it up when exiting.
	(soup_test_session_new): create a SoupSession, optionally with
	an attached SoupLogger (if requested via command line)
	
	* tests/*.c: use test-utils


Added:
   branches/libsoup-2.4/tests/test-utils.c
   branches/libsoup-2.4/tests/test-utils.h
Removed:
   branches/libsoup-2.4/tests/apache-wrapper.c
   branches/libsoup-2.4/tests/apache-wrapper.h
Modified:
   branches/libsoup-2.4/ChangeLog
   branches/libsoup-2.4/tests/Makefile.am
   branches/libsoup-2.4/tests/auth-test.c
   branches/libsoup-2.4/tests/context-test.c
   branches/libsoup-2.4/tests/continue-test.c
   branches/libsoup-2.4/tests/date.c
   branches/libsoup-2.4/tests/header-parsing.c
   branches/libsoup-2.4/tests/ntlm-test.c
   branches/libsoup-2.4/tests/proxy-test.c
   branches/libsoup-2.4/tests/pull-api.c
   branches/libsoup-2.4/tests/query-test.c
   branches/libsoup-2.4/tests/server-auth-test.c
   branches/libsoup-2.4/tests/simple-httpd.c
   branches/libsoup-2.4/tests/uri-parsing.c
   branches/libsoup-2.4/tests/xmlrpc-test.c

Modified: branches/libsoup-2.4/tests/Makefile.am
==============================================================================
--- branches/libsoup-2.4/tests/Makefile.am	(original)
+++ branches/libsoup-2.4/tests/Makefile.am	Mon Jan 14 14:19:32 2008
@@ -25,25 +25,27 @@
 	$(SSL_TESTS)	\
 	$(XMLRPC_TESTS)
 
-auth_test_SOURCES = auth-test.c apache-wrapper.c apache-wrapper.h
-context_test_SOURCES = context-test.c
-continue_test_SOURCES = continue-test.c
-date_SOURCES = date.c
+TEST_SRCS = test-utils.c test-utils.h
+
+auth_test_SOURCES = auth-test.c $(TEST_SRCS)
+context_test_SOURCES = context-test.c $(TEST_SRCS)
+continue_test_SOURCES = continue-test.c $(TEST_SRCS)
+date_SOURCES = date.c $(TEST_SRCS)
 dns_SOURCES = dns.c
 get_SOURCES = get.c
 getbug_SOURCES = getbug.c
-header_parsing_SOURCES = header-parsing.c
-ntlm_test_SOURCES = ntlm-test.c
-proxy_test_SOURCES = proxy-test.c apache-wrapper.c apache-wrapper.h
-pull_api_SOURCES = pull-api.c apache-wrapper.c apache-wrapper.h
-query_test_SOURCES = query-test.c
+header_parsing_SOURCES = header-parsing.c $(TEST_SRCS)
+ntlm_test_SOURCES = ntlm-test.c $(TEST_SRCS)
+proxy_test_SOURCES = proxy-test.c $(TEST_SRCS)
+pull_api_SOURCES = pull-api.c $(TEST_SRCS)
+query_test_SOURCES = query-test.c $(TEST_SRCS)
 revserver_SOURCES = revserver.c
-server_auth_test_SOURCES = server-auth-test.c
+server_auth_test_SOURCES = server-auth-test.c $(TEST_SRCS)
 simple_httpd_SOURCES = simple-httpd.c
 simple_proxy_SOURCES = simple-proxy.c
-ssl_test_SOURCES = ssl-test.c
-uri_parsing_SOURCES = uri-parsing.c
-xmlrpc_test_SOURCES = xmlrpc-test.c apache-wrapper.c apache-wrapper.h
+ssl_test_SOURCES = ssl-test.c $(TEST_SRCS)
+uri_parsing_SOURCES = uri-parsing.c $(TEST_SRCS)
+xmlrpc_test_SOURCES = xmlrpc-test.c $(TEST_SRCS)
 
 if HAVE_APACHE
 APACHE_TESTS = auth-test proxy-test pull-api

Modified: branches/libsoup-2.4/tests/auth-test.c
==============================================================================
--- branches/libsoup-2.4/tests/auth-test.c	(original)
+++ branches/libsoup-2.4/tests/auth-test.c	Mon Jan 14 14:19:32 2008
@@ -12,24 +12,9 @@
 #include "libsoup/soup-auth.h"
 #include "libsoup/soup-session.h"
 
-#include "apache-wrapper.h"
+#include "test-utils.h"
 
 GMainLoop *loop;
-int errors = 0;
-gboolean debug = FALSE;
-
-static void
-dprintf (const char *format, ...)
-{
-	va_list args;
-
-	if (!debug)
-		return;
-
-	va_start (args, format);
-	vprintf (format, args);
-	va_end (args);
-}
 
 typedef struct {
 	/* Explanation of what you should see */
@@ -216,18 +201,19 @@
 
 	auth = identify_auth (msg);
 
-	dprintf ("  %d %s (using %s)\n", msg->status_code, msg->reason_phrase,
-		 auths[auth]);
+	debug_printf (1, "  %d %s (using %s)\n",
+		      msg->status_code, msg->reason_phrase,
+		      auths[auth]);
 
 	if (*expected) {
 		exp = *expected - '0';
 		if (auth != exp) {
-			dprintf ("    expected %s!\n", auths[exp]);
+			debug_printf (1, "    expected %s!\n", auths[exp]);
 			errors++;
 		}
 		memmove (expected, expected + 1, strlen (expected));
 	} else {
-		dprintf ("    expected to be finished\n");
+		debug_printf (1, "    expected to be finished\n");
 		errors++;
 	}
 }
@@ -264,10 +250,10 @@
 	int auth = identify_auth (msg);
 
 	if (!*authenticated && auth) {
-		dprintf ("    using auth on message %d before authenticating!!??\n", n);
+		debug_printf (1, "    using auth on message %d before authenticating!!??\n", n);
 		errors++;
 	} else if (*authenticated && !auth) {
-		dprintf ("    sent unauthenticated message %d after authenticating!\n", n);
+		debug_printf (1, "    sent unauthenticated message %d after authenticating!\n", n);
 		errors++;
 	}
 }
@@ -284,11 +270,11 @@
 		return;
 
 	if (!*authenticated) {
-		dprintf ("    authenticating message %d\n", n);
+		debug_printf (1, "    authenticating message %d\n", n);
 		soup_auth_authenticate (auth, "user1", "realm1");
 		*authenticated = TRUE;
 	} else {
-		dprintf ("    asked to authenticate message %d after authenticating!\n", n);
+		debug_printf (1, "    asked to authenticate message %d after authenticating!\n", n);
 		errors++;
 	}
 }
@@ -300,8 +286,8 @@
 	int n = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (msg), "#"));
 
 	if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
-		dprintf ("      got status '%d %s' on message %d!\n",
-			msg->status_code, msg->reason_phrase, n);
+		debug_printf (1, "      got status '%d %s' on message %d!\n",
+			      msg->status_code, msg->reason_phrase, n);
 		errors++;
 	}
 
@@ -331,14 +317,13 @@
 	*got_401 = TRUE;
 }
 
-static int
+static void
 do_digest_nonce_test (SoupSession *session,
 		      const char *nth, const char *uri,
 		      gboolean expect_401, gboolean expect_signal)
 {
 	SoupMessage *msg;
 	gboolean got_401;
-	int errors = 0;
 
 	msg = soup_message_new (SOUP_METHOD_GET, uri);
 	if (expect_signal) {
@@ -353,20 +338,18 @@
 	got_401 = FALSE;
 	soup_session_send_message (session, msg);
 	if (got_401 != expect_401) {
-		dprintf ("  %s request %s a 401 Unauthorized!\n", nth,
-			 got_401 ? "got" : "did not get");
+		debug_printf (1, "  %s request %s a 401 Unauthorized!\n", nth,
+			      got_401 ? "got" : "did not get");
 		errors++;
 	}
 	if (msg->status_code != SOUP_STATUS_OK) {
-		dprintf ("  %s request got status %d %s!\n", nth,
-			 msg->status_code, msg->reason_phrase);
+		debug_printf (1, "  %s request got status %d %s!\n", nth,
+			      msg->status_code, msg->reason_phrase);
 		errors++;
 	}
 	if (errors == 0)
-		dprintf ("  %s request succeeded\n", nth);
+		debug_printf (1, "  %s request succeeded\n", nth);
 	g_object_unref (msg);
-
-	return errors;
 }
 
 int
@@ -376,37 +359,22 @@
 	SoupMessage *msg;
 	char *base_uri, *uri, *expected;
 	gboolean authenticated;
-	int i, opt;
-
-	g_type_init ();
-	g_thread_init (NULL);
+	int i;
 
-	while ((opt = getopt (argc, argv, "d")) != -1) {
-		switch (opt) {
-		case 'd':
-			debug = TRUE;
-			break;
-		default:
-			fprintf (stderr, "Usage: %s [-d]\n", argv[0]);
-			return 1;
-		}
-	}
+	test_init (argc, argv, NULL);
+	apache_init ();
 
-	if (!apache_init ()) {
-		fprintf (stderr, "Could not start apache\n");
-		return 1;
-	}
 	base_uri = "http://localhost:47524/";;
 
-	session = soup_session_async_new ();
+	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
 	g_signal_connect (session, "authenticate",
 			  G_CALLBACK (authenticate), &i);
 
 	for (i = 0; i < ntests; i++) {
-		dprintf ("Test %d: %s\n", i + 1, tests[i].explanation);
+		debug_printf (1, "Test %d: %s\n", i + 1, tests[i].explanation);
 
 		uri = g_strconcat (base_uri, tests[i].url, NULL);
-		dprintf ("  GET %s\n", uri);
+		debug_printf (1, "  GET %s\n", uri);
 
 		msg = soup_message_new (SOUP_METHOD_GET, uri);
 		g_free (uri);
@@ -425,21 +393,23 @@
 		soup_session_send_message (session, msg);
 		if (msg->status_code != SOUP_STATUS_UNAUTHORIZED &&
 		    msg->status_code != SOUP_STATUS_OK) {
-			dprintf ("  %d %s !\n", msg->status_code,
-				msg->reason_phrase);
+			debug_printf (1, "  %d %s !\n", msg->status_code,
+				      msg->reason_phrase);
 			errors++;
 		}
 		if (*expected) {
-			dprintf ("  expected %d more round(s)\n",
-				(int)strlen (expected));
+			debug_printf (1, "  expected %d more round(s)\n",
+				      (int)strlen (expected));
 			errors++;
 		}
 		g_free (expected);
 
-		if (msg->status_code != tests[i].final_status)
-			dprintf ("  expected %d\n", tests[i].final_status);
+		if (msg->status_code != tests[i].final_status) {
+			debug_printf (1, "  expected %d\n",
+				      tests[i].final_status);
+		}
 
-		dprintf ("\n");
+		debug_printf (1, "\n");
 
 		g_object_unref (msg);
 	}
@@ -448,8 +418,8 @@
 
 	/* And now for some regression tests */
 
-	dprintf ("Testing pipelined auth (bug 271540):\n");
-	session = soup_session_async_new ();
+	debug_printf (1, "Testing pipelined auth (bug 271540):\n");
+	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
 
 	authenticated = FALSE;
 	g_signal_connect (session, "authenticate",
@@ -470,7 +440,7 @@
 	loop = g_main_loop_new (NULL, TRUE);
 	g_main_loop_run (loop);
 
-	dprintf ("\nTesting digest nonce expiration:\n");
+	debug_printf (1, "\nTesting digest nonce expiration:\n");
 
 	/* We test two different things here:
 	 *
@@ -547,30 +517,22 @@
 	 */
 
 	uri = g_strconcat (base_uri, "Digest/realm1/", NULL);
-	errors += do_digest_nonce_test (session, "First", uri, TRUE, TRUE);
+	do_digest_nonce_test (session, "First", uri, TRUE, TRUE);
 	g_free (uri);
 	sleep (2);
 	uri = g_strconcat (base_uri, "Digest/realm1/expire/", NULL);
-	errors += do_digest_nonce_test (session, "Second", uri, TRUE, FALSE);
+	do_digest_nonce_test (session, "Second", uri, TRUE, FALSE);
 	sleep (1);
-	errors += do_digest_nonce_test (session, "Third", uri, FALSE, FALSE);
+	do_digest_nonce_test (session, "Third", uri, FALSE, FALSE);
 	sleep (1);
-	errors += do_digest_nonce_test (session, "Fourth", uri, FALSE, FALSE);
+	do_digest_nonce_test (session, "Fourth", uri, FALSE, FALSE);
 	g_free (uri);
 
 	g_main_loop_unref (loop);
-	g_main_context_unref (g_main_context_default ());
 
 	soup_session_abort (session);
 	g_object_unref (session);
 
-	apache_cleanup ();
-
-	dprintf ("\n");
-	if (errors) {
-		printf ("auth-test: %d error(s). Run with '-d' for details\n",
-			errors);
-	} else
-		printf ("auth-test: OK\n");
-	return errors;
+	test_cleanup ();
+	return errors != 0;
 }

Modified: branches/libsoup-2.4/tests/context-test.c
==============================================================================
--- branches/libsoup-2.4/tests/context-test.c	(original)
+++ branches/libsoup-2.4/tests/context-test.c	Mon Jan 14 14:19:32 2008
@@ -21,23 +21,9 @@
 #include <libsoup/soup-session-async.h>
 #include <libsoup/soup-session-sync.h>
 
-gboolean debug = FALSE;
-int errors = 0;
-GThread *server_thread;
-char *base_uri;
-
-static void
-dprintf (const char *format, ...)
-{
-	va_list args;
+#include "test-utils.h"
 
-	if (!debug)
-		return;
-
-	va_start (args, format);
-	vprintf (format, args);
-	va_end (args);
-}
+char *base_uri;
 
 typedef struct {
 	SoupServer *server;
@@ -81,11 +67,6 @@
 		return;
 	}
 
-	if (!strcmp (path, "/shutdown")) {
-		soup_server_quit (server);
-		return;
-	}
-
 	soup_message_set_status (msg, SOUP_STATUS_OK);
 	if (!strcmp (path, "/fast")) {
 		soup_message_set_response (msg, "text/plain",
@@ -108,63 +89,6 @@
 			  G_CALLBACK (request_failed), sd);
 }
 
-static gpointer
-run_server_thread (gpointer user_data)
-{
-	SoupServer *server = user_data;
-
-	soup_server_add_handler (server, NULL,
-				 server_callback, NULL, NULL);
-	soup_server_run (server);
-	g_object_unref (server);
-
-	return NULL;
-}
-
-static guint
-create_server (void)
-{
-	SoupServer *server;
-	GMainContext *async_context;
-	guint port;
-
-	async_context = g_main_context_new ();
-	server = soup_server_new (SOUP_SERVER_PORT, 0,
-				  SOUP_SERVER_ASYNC_CONTEXT, async_context,
-				  NULL);
-	g_main_context_unref (async_context);
-
-	if (!server) {
-		fprintf (stderr, "Unable to bind server\n");
-		exit (1);
-	}
-
-	port = soup_server_get_port (server);
-	server_thread = g_thread_create (run_server_thread, server, TRUE, NULL);
-
-	return port;
-}
-
-static void
-shutdown_server (void)
-{
-	SoupSession *session;
-	char *uri;
-	SoupMessage *msg;
-
-	session = soup_session_sync_new ();
-	uri = g_build_filename (base_uri, "shutdown", NULL);
-	msg = soup_message_new ("GET", uri);
-	soup_session_send_message (session, msg);
-	g_object_unref (msg);
-	g_free (uri);
-
-	soup_session_abort (session);
-	g_object_unref (session);
-
-	g_thread_join (server_thread);
-}
-
 /* Test 1: An async session in another thread with its own
  * async_context can complete a request while the main thread's main
  * loop is stopped.
@@ -181,7 +105,7 @@
 {
 	GMainLoop *loop;
 
-	dprintf ("Test 1: blocking the main thread does not block other thread\n");
+	debug_printf (1, "Test 1: blocking the main thread does not block other thread\n");
 
 	test1_cond = g_cond_new ();
 	test1_mutex = g_mutex_new ();
@@ -209,7 +133,7 @@
 	if (g_cond_timed_wait (test1_cond, test1_mutex, &time))
 		g_thread_join (thread);
 	else {
-		dprintf ("  timeout!\n");
+		debug_printf (1, "  timeout!\n");
 		errors++;
 	}
 
@@ -238,24 +162,25 @@
 	g_mutex_unlock (test1_mutex);
 
 	async_context = g_main_context_new ();
-	session = soup_session_async_new_with_options (
+	session = soup_test_session_new (
+		SOUP_TYPE_SESSION_ASYNC,
 		SOUP_SESSION_ASYNC_CONTEXT, async_context,
 		NULL);
 	g_main_context_unref (async_context);
 
 	uri = g_build_filename (base_uri, "slow", NULL);
 
-	dprintf ("  send_message\n");
+	debug_printf (1, "  send_message\n");
 	msg = soup_message_new ("GET", uri);
 	soup_session_send_message (session, msg);
 	if (msg->status_code != SOUP_STATUS_OK) {
-		dprintf ("    unexpected status: %d %s\n",
-			 msg->status_code, msg->reason_phrase);
+		debug_printf (1, "    unexpected status: %d %s\n",
+			      msg->status_code, msg->reason_phrase);
 		errors++;
 	}
 	g_object_unref (msg);
 
-	dprintf ("  queue_message\n");
+	debug_printf (1, "  queue_message\n");
 	msg = soup_message_new ("GET", uri);
 	loop = g_main_loop_new (async_context, FALSE);
 	g_object_ref (msg);
@@ -263,8 +188,8 @@
 	g_main_loop_run (loop);
 	g_main_loop_unref (loop);
 	if (msg->status_code != SOUP_STATUS_OK) {
-		dprintf ("    unexpected status: %d %s\n",
-			 msg->status_code, msg->reason_phrase);
+		debug_printf (1, "    unexpected status: %d %s\n",
+			      msg->status_code, msg->reason_phrase);
 		errors++;
 	}
 	g_object_unref (msg);
@@ -292,24 +217,25 @@
 	char *uri;
 	SoupMessage *msg;
 
-	dprintf ("Test 2: a session with its own context is independent of the main loop.\n");
+	debug_printf (1, "Test 2: a session with its own context is independent of the main loop.\n");
 
 	idle = g_idle_add_full (G_PRIORITY_HIGH, idle_test2_fail, NULL, NULL);
 
 	async_context = g_main_context_new ();
-	session = soup_session_async_new_with_options (
+	session = soup_test_session_new (
+		SOUP_TYPE_SESSION_ASYNC,
 		SOUP_SESSION_ASYNC_CONTEXT, async_context,
 		NULL);
 	g_main_context_unref (async_context);
 
 	uri = g_build_filename (base_uri, "slow", NULL);
 
-	dprintf ("  send_message\n");
+	debug_printf (1, "  send_message\n");
 	msg = soup_message_new ("GET", uri);
 	soup_session_send_message (session, msg);
 	if (msg->status_code != SOUP_STATUS_OK) {
-		dprintf ("    unexpected status: %d %s\n",
-			 msg->status_code, msg->reason_phrase);
+		debug_printf (1, "    unexpected status: %d %s\n",
+			      msg->status_code, msg->reason_phrase);
 		errors++;
 	}
 	g_object_unref (msg);
@@ -324,56 +250,29 @@
 static gboolean
 idle_test2_fail (gpointer user_data)
 {
-	dprintf ("  idle ran!\n");
+	debug_printf (1, "  idle ran!\n");
 	errors++;
 	return FALSE;
 }
 
 
-static void
-quit (int sig)
-{
-	/* Exit cleanly on ^C in case we're valgrinding. */
-	exit (0);
-}
-
 int
 main (int argc, char **argv)
 {
-	int opt;
-	guint port;
+	SoupServer *server;
 
-	g_type_init ();
-	g_thread_init (NULL);
-	signal (SIGINT, quit);
-
-	while ((opt = getopt (argc, argv, "d")) != -1) {
-		switch (opt) {
-		case 'd':
-			debug = TRUE;
-			break;
-		default:
-			fprintf (stderr, "Usage: %s [-d]\n",
-				 argv[0]);
-			exit (1);
-		}
-	}
+	test_init (argc, argv, NULL);
 
-	port = create_server ();
-	base_uri = g_strdup_printf ("http://localhost:%u/";, port);
+	server = soup_test_server_new (TRUE);
+	soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
+	base_uri = g_strdup_printf ("http://localhost:%u/";,
+				    soup_server_get_port (server));
 
 	do_test1 ();
 	do_test2 ();
 
-	shutdown_server ();
 	g_free (base_uri);
-	g_main_context_unref (g_main_context_default ());
 
-	dprintf ("\n");
-	if (errors) {
-		printf ("context-test: %d error(s). Run with '-d' for details\n",
-			errors);
-	} else
-		printf ("context-test: OK\n");
+	test_cleanup ();
 	return errors != 0;
 }

Modified: branches/libsoup-2.4/tests/continue-test.c
==============================================================================
--- branches/libsoup-2.4/tests/continue-test.c	(original)
+++ branches/libsoup-2.4/tests/continue-test.c	Mon Jan 14 14:19:32 2008
@@ -13,28 +13,17 @@
 #include <libsoup/soup-auth-domain-basic.h>
 #include <libsoup/soup-server.h>
 
+#include "test-utils.h"
+
 #define SHORT_BODY "This is a test.\r\n"
 #define LONG_BODY (SHORT_BODY SHORT_BODY)
 
 #define MAX_POST_LENGTH (sizeof (SHORT_BODY))
 
-int debug, port;
+int port;
 GSList *events;
 
 static void
-dprintf (int level, const char *format, ...)
-{
-	va_list args;
-
-	if (debug < level)
-		return;
-
-	va_start (args, format);
-	vprintf (format, args);
-	va_end (args);
-}
-
-static void
 event (SoupMessage *msg, char *side, char *message)
 {
 	char *data = g_strdup_printf ("%s-%s", side, message);
@@ -42,10 +31,10 @@
 		(!strcmp (data, "server-wrote_headers") ||
 		 !strcmp (data, "server-wrote_informational"));
 
-	dprintf (2, "  %s", data);
+	debug_printf (2, "  %s", data);
 	if (record_status)
-		dprintf (2, " (%s)", msg->reason_phrase);
-	dprintf (2, "\n");
+		debug_printf (2, " (%s)", msg->reason_phrase);
+	debug_printf (2, "\n");
 
 	events = g_slist_append (events, data);
 	if (record_status)
@@ -67,7 +56,7 @@
 EVENT_HANDLER (wrote_body);
 EVENT_HANDLER (finished);
 
-static int
+static void
 do_message (const char *path, gboolean long_body,
 	    gboolean expect_continue, gboolean auth,
 	    ...)
@@ -79,14 +68,13 @@
 	const char *expected_event;
 	char *actual_event;
 	int expected_status, actual_status;
-	int errors = 0;
 	static int count = 1;
 
-	dprintf (1, "%d. /%s, %s body, %sExpect, %s password\n",
-		 count++, path,
-		 long_body ? "long" : "short",
-		 expect_continue ? "" : "no ",
-		 auth ? "with" : "without");
+	debug_printf (1, "%d. /%s, %s body, %sExpect, %s password\n",
+		      count++, path,
+		      long_body ? "long" : "short",
+		      expect_continue ? "" : "no ",
+		      auth ? "with" : "without");
 
 	uri = g_strdup_printf ("http://%slocalhost:%d/%s";,
 			       auth ? "user:pass@" : "",
@@ -119,7 +107,7 @@
 			  G_CALLBACK (finished), "client");
 
 	events = NULL;
-	session = soup_session_async_new ();
+	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
 	soup_session_send_message (session, msg);
 	soup_session_abort (session);
 	g_object_unref (session);
@@ -129,14 +117,14 @@
 
 		if (!events) {
 			actual_event = "";
-			dprintf (1, "  Expected '%s', got end of list\n",
-				 expected_event);
+			debug_printf (1, "  Expected '%s', got end of list\n",
+				      expected_event);
 			errors++;
 		} else {
 			actual_event = events->data;
 			if (strcmp (expected_event, actual_event) != 0) {
-				dprintf (1, "  Expected '%s', got '%s'\n",
-					 expected_event, actual_event);
+				debug_printf (1, "  Expected '%s', got '%s'\n",
+					      expected_event, actual_event);
 				errors++;
 			}
 			events = g_slist_delete_link (events, events);
@@ -156,9 +144,9 @@
 
 		if (expected_status != -1 && actual_status != -1 &&
 		    expected_status != actual_status) {
-			dprintf (1, "  Expected status '%s', got '%s'\n",
-				 soup_status_get_phrase (expected_status),
-				 soup_status_get_phrase (actual_status));
+			debug_printf (1, "  Expected status '%s', got '%s'\n",
+				      soup_status_get_phrase (expected_status),
+				      soup_status_get_phrase (actual_status));
 			errors++;
 		}
 
@@ -167,7 +155,7 @@
 	va_end (ap);
 	while (events) {
 		actual_event = events->data;
-		dprintf (1, "  Expected to be done, got '%s'\n", actual_event);
+		debug_printf (1, "  Expected to be done, got '%s'\n", actual_event);
 		errors++;
 		events = g_slist_delete_link (events, events);
 
@@ -176,191 +164,185 @@
 			events = g_slist_delete_link (events, events);
 	}
 	g_object_unref (msg);
-
-	return errors;
 }
 
-static int
+static void
 run_tests (void)
 {
-	int errors = 0;
-
-	errors += do_message ("unauth", FALSE, FALSE, FALSE,
-			      "client-wrote_headers",
-			      "client-wrote_body",
-			      "server-got_headers",
-			      "server-got_body",
-			      "server-wrote_headers", SOUP_STATUS_CREATED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-	errors += do_message ("unauth", TRUE, FALSE, FALSE,
-			      "client-wrote_headers",
-			      "client-wrote_body",
-			      "server-got_headers",
-			      "server-got_body",
-			      "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-	errors += do_message ("unauth", FALSE, TRUE, FALSE,
-			      "client-wrote_headers",
-			      "server-got_headers",
-			      "server-wrote_informational", SOUP_STATUS_CONTINUE,
-			      "client-got_informational",
-			      "client-wrote_body",
-			      "server-got_body",
-			      "server-wrote_headers", SOUP_STATUS_CREATED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-	errors += do_message ("unauth", TRUE, TRUE, FALSE,
-			      "client-wrote_headers",
-			      "server-got_headers",
-			      "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-
-	errors += do_message ("auth", FALSE, FALSE, FALSE,
-			      "client-wrote_headers",
-			      "client-wrote_body",
-			      "server-got_headers",
-			      "server-got_body",
-			      "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-	errors += do_message ("auth", TRUE, FALSE, FALSE,
-			      "client-wrote_headers",
-			      "client-wrote_body",
-			      "server-got_headers",
-			      "server-got_body",
-			      "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-	errors += do_message ("auth", FALSE, TRUE, FALSE,
-			      "client-wrote_headers",
-			      "server-got_headers",
-			      "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-	errors += do_message ("auth", TRUE, TRUE, FALSE,
-			      "client-wrote_headers",
-			      "server-got_headers",
-			      "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-
-	errors += do_message ("auth", FALSE, FALSE, TRUE,
-			      "client-wrote_headers",
-			      "client-wrote_body",
-			      "server-got_headers",
-			      "server-got_body",
-			      "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-wrote_headers",
-			      "client-wrote_body",
-			      "server-got_headers",
-			      "server-got_body",
-			      "server-wrote_headers", SOUP_STATUS_CREATED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-	errors += do_message ("auth", TRUE, FALSE, TRUE,
-			      "client-wrote_headers",
-			      "client-wrote_body",
-			      "server-got_headers",
-			      "server-got_body",
-			      "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-wrote_headers",
-			      "client-wrote_body",
-			      "server-got_headers",
-			      "server-got_body",
-			      "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-	errors += do_message ("auth", FALSE, TRUE, TRUE,
-			      "client-wrote_headers",
-			      "server-got_headers",
-			      "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-wrote_headers",
-			      "server-got_headers",
-			      "server-wrote_informational", SOUP_STATUS_CONTINUE,
-			      "client-got_informational",
-			      "client-wrote_body",
-			      "server-got_body",
-			      "server-wrote_headers", SOUP_STATUS_CREATED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-	errors += do_message ("auth", TRUE, TRUE, TRUE,
-			      "client-wrote_headers",
-			      "server-got_headers",
-			      "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-wrote_headers",
-			      "server-got_headers",
-			      "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
-			      "server-wrote_body",
-			      "server-finished",
-			      "client-got_headers",
-			      "client-got_body",
-			      "client-finished",
-			      NULL);
-
-	return errors;
+	do_message ("unauth", FALSE, FALSE, FALSE,
+		    "client-wrote_headers",
+		    "client-wrote_body",
+		    "server-got_headers",
+		    "server-got_body",
+		    "server-wrote_headers", SOUP_STATUS_CREATED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
+	do_message ("unauth", TRUE, FALSE, FALSE,
+		    "client-wrote_headers",
+		    "client-wrote_body",
+		    "server-got_headers",
+		    "server-got_body",
+		    "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
+	do_message ("unauth", FALSE, TRUE, FALSE,
+		    "client-wrote_headers",
+		    "server-got_headers",
+		    "server-wrote_informational", SOUP_STATUS_CONTINUE,
+		    "client-got_informational",
+		    "client-wrote_body",
+		    "server-got_body",
+		    "server-wrote_headers", SOUP_STATUS_CREATED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
+	do_message ("unauth", TRUE, TRUE, FALSE,
+		    "client-wrote_headers",
+		    "server-got_headers",
+		    "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
+
+	do_message ("auth", FALSE, FALSE, FALSE,
+		    "client-wrote_headers",
+		    "client-wrote_body",
+		    "server-got_headers",
+		    "server-got_body",
+		    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
+	do_message ("auth", TRUE, FALSE, FALSE,
+		    "client-wrote_headers",
+		    "client-wrote_body",
+		    "server-got_headers",
+		    "server-got_body",
+		    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
+	do_message ("auth", FALSE, TRUE, FALSE,
+		    "client-wrote_headers",
+		    "server-got_headers",
+		    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
+	do_message ("auth", TRUE, TRUE, FALSE,
+		    "client-wrote_headers",
+		    "server-got_headers",
+		    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
+
+	do_message ("auth", FALSE, FALSE, TRUE,
+		    "client-wrote_headers",
+		    "client-wrote_body",
+		    "server-got_headers",
+		    "server-got_body",
+		    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-wrote_headers",
+		    "client-wrote_body",
+		    "server-got_headers",
+		    "server-got_body",
+		    "server-wrote_headers", SOUP_STATUS_CREATED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
+	do_message ("auth", TRUE, FALSE, TRUE,
+		    "client-wrote_headers",
+		    "client-wrote_body",
+		    "server-got_headers",
+		    "server-got_body",
+		    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-wrote_headers",
+		    "client-wrote_body",
+		    "server-got_headers",
+		    "server-got_body",
+		    "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
+	do_message ("auth", FALSE, TRUE, TRUE,
+		    "client-wrote_headers",
+		    "server-got_headers",
+		    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-wrote_headers",
+		    "server-got_headers",
+		    "server-wrote_informational", SOUP_STATUS_CONTINUE,
+		    "client-got_informational",
+		    "client-wrote_body",
+		    "server-got_body",
+		    "server-wrote_headers", SOUP_STATUS_CREATED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
+	do_message ("auth", TRUE, TRUE, TRUE,
+		    "client-wrote_headers",
+		    "server-got_headers",
+		    "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-wrote_headers",
+		    "server-got_headers",
+		    "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
+		    "server-wrote_body",
+		    "server-finished",
+		    "client-got_headers",
+		    "client-got_body",
+		    "client-finished",
+		    NULL);
 }
 
 
@@ -438,8 +420,7 @@
 	SoupServer *server;
 	SoupAuthDomain *auth_domain;
 
-	server = soup_server_new (SOUP_SERVER_PORT, 0,
-				  NULL);
+	server = soup_test_server_new (FALSE);
 
 	g_signal_connect (server, "request-started",
 			  G_CALLBACK (request_started), NULL);
@@ -458,47 +439,18 @@
 
 /* MAIN */
 
-static void
-usage (void)
-{
-	fprintf (stderr, "Usage: continue-test [-d] [-d]\n");
-	exit (1);
-}
-
 int
 main (int argc, char **argv)
 {
-	int opt, errors;
 	SoupServer *server;
 
-	g_type_init ();
-	g_thread_init (NULL);
-
-	while ((opt = getopt (argc, argv, "d")) != -1) {
-		switch (opt) {
-		case 'd':
-			debug++;
-			break;
-
-		case '?':
-			usage ();
-			break;
-		}
-	}
+	test_init (argc, argv, NULL);
 
 	server = setup_server ();
 	port = soup_server_get_port (server);
-	soup_server_run_async (server);
 
-	errors = run_tests ();
-	soup_server_quit (server);
-
-	dprintf (1, "\n");
-	if (errors) {
-		printf ("continue-test: %d error(s). Run with '-d' for details\n",
-			errors);
-	} else
-		printf ("continue-test: OK\n");
+	run_tests ();
 
-	return errors > 0;
+	test_cleanup ();
+	return errors != 0;
 }

Modified: branches/libsoup-2.4/tests/date.c
==============================================================================
--- branches/libsoup-2.4/tests/date.c	(original)
+++ branches/libsoup-2.4/tests/date.c	Mon Jan 14 14:19:32 2008
@@ -9,7 +9,7 @@
 #include <libsoup/soup-date.h>
 #include <glib.h>
 
-static int errors = 0;
+#include "test-utils.h"
 
 const char *date_tests[] = {
 	/* rfc1123-date, and broken variants */
@@ -90,14 +90,13 @@
 {
 	int i;
 
+	test_init (argc, argv, NULL);
+
 	for (i = 0; i < G_N_ELEMENTS (date_tests); i++) {
 		check (date_tests[i], soup_date_new_from_string (date_tests[i]));
 	}
 	check (TIME_T_STRING, soup_date_new_from_time_t (TIME_T));
 
-	if (errors == 0)
-		printf ("date: OK\n");
-	else
-		fprintf (stderr, "date: %d errors\n", errors);
-	return errors;
+	test_cleanup ();
+	return errors != 0;
 }

Modified: branches/libsoup-2.4/tests/header-parsing.c
==============================================================================
--- branches/libsoup-2.4/tests/header-parsing.c	(original)
+++ branches/libsoup-2.4/tests/header-parsing.c	Mon Jan 14 14:19:32 2008
@@ -7,20 +7,7 @@
 #include "libsoup/soup-message.h"
 #include "libsoup/soup-headers.h"
 
-gboolean debug = FALSE;
-
-static void
-dprintf (const char *format, ...)
-{
-	va_list args;
-
-	if (!debug)
-		return;
-
-	va_start (args, format);
-	vprintf (format, args);
-	va_end (args);
-}
+#include "test-utils.h"
 
 typedef struct {
 	char *name, *value;
@@ -554,7 +541,7 @@
 static void
 print_header (const char *name, const char *value, gpointer data)
 {
-	dprintf ("              '%s': '%s'\n", name, value);
+	debug_printf (1, "              '%s': '%s'\n", name, value);
 }
 
 typedef struct {
@@ -577,21 +564,21 @@
 	return TRUE;
 }
 
-static int
+static void
 do_request_tests (void)
 {
-	int i, len, h, errors = 0;
+	int i, len, h;
 	char *method, *path;
 	SoupHTTPVersion version;
 	SoupMessageHeaders *headers;
 	guint status;
 
-	dprintf ("Request tests\n");
+	debug_printf (1, "Request tests\n");
 	for (i = 0; i < num_reqtests; i++) {
 		gboolean ok = TRUE;
 
-		dprintf ("%2d. %s (%s): ", i + 1, reqtests[i].description,
-			 soup_status_get_phrase (reqtests[i].status));
+		debug_printf (1, "%2d. %s (%s): ", i + 1, reqtests[i].description,
+			      soup_status_get_phrase (reqtests[i].status));
 
 		headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_REQUEST);
 		method = path = NULL;
@@ -619,30 +606,31 @@
 		}
 
 		if (ok)
-			dprintf ("OK!\n");
+			debug_printf (1, "OK!\n");
 		else {
-			dprintf ("BAD!\n");
+			debug_printf (1, "BAD!\n");
 			errors++;
 			if (reqtests[i].method) {
-				dprintf ("    expected: '%s' '%s' 'HTTP/1.%d'\n",
-					 reqtests[i].method, reqtests[i].path,
-					 reqtests[i].version);
+				debug_printf (1, "    expected: '%s' '%s' 'HTTP/1.%d'\n",
+					      reqtests[i].method,
+					      reqtests[i].path,
+					      reqtests[i].version);
 				for (h = 0; reqtests[i].headers[h].name; h++) {
-					dprintf ("              '%s': '%s'\n",
-						 reqtests[i].headers[h].name,
-						 reqtests[i].headers[h].value);
+					debug_printf (1, "              '%s': '%s'\n",
+						      reqtests[i].headers[h].name,
+						      reqtests[i].headers[h].value);
 				}
 			} else {
-				dprintf ("    expected: %s\n",
-					 soup_status_get_phrase (reqtests[i].status));
+				debug_printf (1, "    expected: %s\n",
+					      soup_status_get_phrase (reqtests[i].status));
 			}
 			if (method) {
-				dprintf ("         got: '%s' '%s' 'HTTP/1.%d'\n",
-					method, path, version);
+				debug_printf (1, "         got: '%s' '%s' 'HTTP/1.%d'\n",
+					      method, path, version);
 				soup_message_headers_foreach (headers, print_header, NULL);
 			} else {
-				dprintf ("         got: %s\n",
-					 soup_status_get_phrase (status));
+				debug_printf (1, "         got: %s\n",
+					      soup_status_get_phrase (status));
 			}
 		}
 
@@ -650,26 +638,24 @@
 		g_free (path);
 		soup_message_headers_free (headers);
 	}
-	dprintf ("\n");
-
-	return errors;
+	debug_printf (1, "\n");
 }
 
-static int
+static void
 do_response_tests (void)
 {
-	int i, len, h, errors = 0;
+	int i, len, h;
 	guint status_code;
 	char *reason_phrase;
 	SoupHTTPVersion version;
 	SoupMessageHeaders *headers;
 
-	dprintf ("Response tests\n");
+	debug_printf (1, "Response tests\n");
 	for (i = 0; i < num_resptests; i++) {
 		gboolean ok = TRUE;
 
-		dprintf ("%2d. %s (%s): ", i + 1, resptests[i].description,
-			 resptests[i].reason_phrase ? "should parse" : "should NOT parse");
+		debug_printf (1, "%2d. %s (%s): ", i + 1, resptests[i].description,
+			      resptests[i].reason_phrase ? "should parse" : "should NOT parse");
 
 		headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_RESPONSE);
 		reason_phrase = NULL;
@@ -696,124 +682,104 @@
 		}
 
 		if (ok)
-			dprintf ("OK!\n");
+			debug_printf (1, "OK!\n");
 		else {
-			dprintf ("BAD!\n");
+			debug_printf (1, "BAD!\n");
 			errors++;
 			if (resptests[i].reason_phrase) {
-				dprintf ("    expected: 'HTTP/1.%d' '%03d' '%s'\n",
-					 resptests[i].version,
-					 resptests[i].status_code,
-					 resptests[i].reason_phrase);
+				debug_printf (1, "    expected: 'HTTP/1.%d' '%03d' '%s'\n",
+					      resptests[i].version,
+					      resptests[i].status_code,
+					      resptests[i].reason_phrase);
 				for (h = 0; resptests[i].headers[h].name; h++) {
-					dprintf ("              '%s': '%s'\n",
-						 resptests[i].headers[h].name,
-						 resptests[i].headers[h].value);
+					debug_printf (1, "              '%s': '%s'\n",
+						      resptests[i].headers[h].name,
+						      resptests[i].headers[h].value);
 				}
 			} else
-				dprintf ("    expected: parse error\n");
+				debug_printf (1, "    expected: parse error\n");
 			if (reason_phrase) {
-				dprintf ("         got: 'HTTP/1.%d' '%03d' '%s'\n",
-					 version, status_code, reason_phrase);
+				debug_printf (1, "         got: 'HTTP/1.%d' '%03d' '%s'\n",
+					      version, status_code, reason_phrase);
 				soup_message_headers_foreach (headers, print_header, NULL);
 			} else
-				dprintf ("         got: parse error\n");
+				debug_printf (1, "         got: parse error\n");
 		}
 
 		g_free (reason_phrase);
 		soup_message_headers_free (headers);
 	}
-	dprintf ("\n");
-
-	return errors;
+	debug_printf (1, "\n");
 }
 
-static int
+static void
 do_qvalue_tests (void)
 {
-	int i, j, errors = 0;
+	int i, j;
 	GSList *acceptable, *unacceptable, *iter;
 	gboolean wrong;
 
-	dprintf ("qvalue tests\n");
+	debug_printf (1, "qvalue tests\n");
 	for (i = 0; i < num_qvaluetests; i++) {
-		dprintf ("%2d. %s:\n", i + 1, qvaluetests[i].header_value);
+		debug_printf (1, "%2d. %s:\n", i + 1, qvaluetests[i].header_value);
 
 		unacceptable = NULL;
 		acceptable = soup_header_parse_quality_list (qvaluetests[i].header_value,
 							     &unacceptable);
 
-		dprintf ("    acceptable: ");
+		debug_printf (1, "    acceptable: ");
 		wrong = FALSE;
 		if (acceptable) {
 			for (iter = acceptable, j = 0; iter; iter = iter->next, j++) {
-				dprintf ("%s ", iter->data);
+				debug_printf (1, "%s ", iter->data);
 				if (!qvaluetests[i].acceptable[j] ||
 				    strcmp (iter->data, qvaluetests[i].acceptable[j]) != 0)
 					wrong = TRUE;
 			}
-			dprintf ("\n");
+			debug_printf (1, "\n");
 		} else
-			dprintf ("(none)\n");
+			debug_printf (1, "(none)\n");
 		if (wrong) {
-			dprintf ("    WRONG! expected: ");
+			debug_printf (1, "    WRONG! expected: ");
 			for (j = 0; qvaluetests[i].acceptable[j]; j++)
-				dprintf ("%s ", qvaluetests[i].acceptable[j]);
-			dprintf ("\n");
+				debug_printf (1, "%s ", qvaluetests[i].acceptable[j]);
+			debug_printf (1, "\n");
 			errors++;
 		}
 
-		dprintf ("  unacceptable: ");
+		debug_printf (1, "  unacceptable: ");
 		wrong = FALSE;
 		if (unacceptable) {
 			for (iter = unacceptable, j = 0; iter; iter = iter->next, j++) {
-				dprintf ("%s ", iter->data);
+				debug_printf (1, "%s ", iter->data);
 				if (!qvaluetests[i].unacceptable[j] ||
 				    strcmp (iter->data, qvaluetests[i].unacceptable[j]) != 0)
 					wrong = TRUE;
 			}
-			dprintf ("\n");
+			debug_printf (1, "\n");
 		} else
-			dprintf ("(none)\n");
+			debug_printf (1, "(none)\n");
 		if (wrong) {
-			dprintf ("    WRONG! expected: ");
+			debug_printf (1, "    WRONG! expected: ");
 			for (j = 0; qvaluetests[i].unacceptable[j]; j++)
-				dprintf ("%s ", qvaluetests[i].unacceptable[j]);
-			dprintf ("\n");
+				debug_printf (1, "%s ", qvaluetests[i].unacceptable[j]);
+			debug_printf (1, "\n");
 			errors++;
 		}
 
-		dprintf ("\n");
+		debug_printf (1, "\n");
 	}
-
-	return errors;
 }
 
 int
 main (int argc, char **argv)
 {
-	int opt, errors;
+	test_init (argc, argv, NULL);
 
-	while ((opt = getopt (argc, argv, "d")) != -1) {
-		switch (opt) {
-		case 'd':
-			debug = TRUE;
-			break;
-		default:
-			fprintf (stderr, "Usage: %s [-d]\n", argv[0]);
-			return 1;
-		}
-	}
+	do_request_tests ();
+	do_response_tests ();
+	do_qvalue_tests ();
 
-	errors = do_request_tests ();
-	errors += do_response_tests ();
-	errors += do_qvalue_tests ();
-
-	dprintf ("\n");
-	if (errors) {
-		printf ("header-parsing: %d error(s). Run with '-d' for details\n",
-			errors);
-	} else
-		printf ("header-parsing: OK\n");
-	return errors;
+	test_cleanup ();
+	return errors != 0;
 }

Modified: branches/libsoup-2.4/tests/ntlm-test.c
==============================================================================
--- branches/libsoup-2.4/tests/ntlm-test.c	(original)
+++ branches/libsoup-2.4/tests/ntlm-test.c	Mon Jan 14 14:19:32 2008
@@ -26,20 +26,9 @@
 #include <libsoup/soup-server.h>
 #include <libsoup/soup-session-async.h>
 
-gboolean debug = FALSE;
+#include "test-utils.h"
 
-static void
-dprintf (const char *format, ...)
-{
-	va_list args;
-
-	if (!debug)
-		return;
-
-	va_start (args, format);
-	vprintf (format, args);
-	va_end (args);
-}
+GHashTable *connections;
 
 typedef enum {
 	NTLM_UNAUTHENTICATED,
@@ -188,14 +177,13 @@
 		state->sent_response = TRUE;
 }
 
-static int
+static void
 do_message (SoupSession *session, SoupURI *base_uri, const char *path,
 	    gboolean get_prompt, gboolean do_ntlm, guint status_code)
 {
 	SoupURI *uri;
 	SoupMessage *msg;
 	NTLMState state = { FALSE, FALSE, FALSE, FALSE };
-	int errors = 0;
 
 	uri = soup_uri_copy (base_uri);
 	g_free (uri->path);
@@ -213,129 +201,115 @@
 			  G_CALLBACK (ntlm_response_check), &state);
 
 	soup_session_send_message (session, msg);
-	dprintf ("  %-10s -> ", path);
+	debug_printf (1, "  %-10s -> ", path);
 
 	if (state.got_prompt) {
-		dprintf (" PROMPT");
+		debug_printf (1, " PROMPT");
 		if (!get_prompt) {
-			dprintf ("???");
+			debug_printf (1, "???");
 			errors++;
 		}
 	} else if (get_prompt) {
-		dprintf (" no-prompt???");
+		debug_printf (1, " no-prompt???");
 		errors++;
 	}
 
 	if (state.sent_request) {
-		dprintf (" REQUEST");
+		debug_printf (1, " REQUEST");
 		if (!do_ntlm) {
-			dprintf ("???");
+			debug_printf (1, "???");
 			errors++;
 		}
 	} else if (do_ntlm) {
-		dprintf (" no-request???");
+		debug_printf (1, " no-request???");
 		errors++;
 	}
 
 	if (state.got_challenge) {
-		dprintf (" CHALLENGE");
+		debug_printf (1, " CHALLENGE");
 		if (!do_ntlm) {
-			dprintf ("???");
+			debug_printf (1, "???");
 			errors++;
 		}
 	} else if (do_ntlm) {
-		dprintf (" no-challenge???");
+		debug_printf (1, " no-challenge???");
 		errors++;
 	}
 
 	if (state.sent_response) {
-		dprintf (" RESPONSE");
+		debug_printf (1, " RESPONSE");
 		if (!do_ntlm) {
-			dprintf ("???");
+			debug_printf (1, "???");
 			errors++;
 		}
 	} else if (do_ntlm) {
-		dprintf (" no-response???");
+		debug_printf (1, " no-response???");
 		errors++;
 	}
 
-	dprintf (" -> %s", msg->reason_phrase);
+	debug_printf (1, " -> %s", msg->reason_phrase);
 	if (msg->status_code != status_code) {
-		dprintf ("???");
+		debug_printf (1, "???");
 		errors++;
 	}
-	dprintf ("\n");
+	debug_printf (1, "\n");
 
 	g_object_unref (msg);
-	return errors;
 }
 
-static int
+static void
 do_ntlm_round (SoupURI *base_uri, const char *user)
 {
 	SoupSession *session;
-	int errors = 0;
 	gboolean use_ntlm = user != NULL;
 	gboolean alice = use_ntlm && !strcmp (user, "alice");
 	gboolean bob = use_ntlm && !strcmp (user, "bob");
 
-	g_return_val_if_fail (use_ntlm || !alice, 0);
+	g_return_if_fail (use_ntlm || !alice);
 
-	session = soup_session_async_new_with_options (
+	session = soup_test_session_new (
+		SOUP_TYPE_SESSION_ASYNC,
 		SOUP_SESSION_USE_NTLM, use_ntlm,
 		NULL);
 	g_signal_connect (session, "authenticate",
 			  G_CALLBACK (authenticate), (char *)user);
 
-	errors += do_message (session, base_uri, "/noauth",
-			      FALSE, use_ntlm, SOUP_STATUS_OK);
-	errors += do_message (session, base_uri, "/alice",
-			      !use_ntlm || bob, FALSE,
-			      alice ? SOUP_STATUS_OK :
-			      SOUP_STATUS_UNAUTHORIZED);
-	errors += do_message (session, base_uri, "/alice/404",
-			      !use_ntlm, bob,
-			      alice ? SOUP_STATUS_NOT_FOUND :
-			      SOUP_STATUS_UNAUTHORIZED);
-	errors += do_message (session, base_uri, "/alice",
-			      !use_ntlm, bob,
-			      alice ? SOUP_STATUS_OK :
-			      SOUP_STATUS_UNAUTHORIZED);
-	errors += do_message (session, base_uri, "/bob",
-			      !use_ntlm || alice, bob,
-			      bob ? SOUP_STATUS_OK :
-			      SOUP_STATUS_UNAUTHORIZED);
-	errors += do_message (session, base_uri, "/alice",
-			      !use_ntlm || bob, alice,
-			      alice ? SOUP_STATUS_OK :
-			      SOUP_STATUS_UNAUTHORIZED);
+	do_message (session, base_uri, "/noauth",
+		    FALSE, use_ntlm, SOUP_STATUS_OK);
+	do_message (session, base_uri, "/alice",
+		    !use_ntlm || bob, FALSE,
+		    alice ? SOUP_STATUS_OK :
+		    SOUP_STATUS_UNAUTHORIZED);
+	do_message (session, base_uri, "/alice/404",
+		    !use_ntlm, bob,
+		    alice ? SOUP_STATUS_NOT_FOUND :
+		    SOUP_STATUS_UNAUTHORIZED);
+	do_message (session, base_uri, "/alice",
+		    !use_ntlm, bob,
+		    alice ? SOUP_STATUS_OK :
+		    SOUP_STATUS_UNAUTHORIZED);
+	do_message (session, base_uri, "/bob",
+		    !use_ntlm || alice, bob,
+		    bob ? SOUP_STATUS_OK :
+		    SOUP_STATUS_UNAUTHORIZED);
+	do_message (session, base_uri, "/alice",
+		    !use_ntlm || bob, alice,
+		    alice ? SOUP_STATUS_OK :
+		    SOUP_STATUS_UNAUTHORIZED);
 
 	soup_session_abort (session);
 	g_object_unref (session);
-
-	return errors;
-}
-
-static int
-do_ntlm_tests (SoupURI *base_uri)
-{
-	int errors = 0;
-
-	dprintf ("Round 1: Non-NTLM Connection\n");
-	errors += do_ntlm_round (base_uri, NULL);
-	dprintf ("Round 2: NTLM Connection, user=alice\n");
-	errors += do_ntlm_round (base_uri, "alice");
-	dprintf ("Round 3: NTLM Connection, user=bob\n");
-	errors += do_ntlm_round (base_uri, "bob");
-
-	return errors;
 }
 
 static void
-quit (int sig)
+do_ntlm_tests (SoupURI *base_uri)
 {
-	/* Exit cleanly on ^C in case we're valgrinding. */
-	exit (0);
+	debug_printf (1, "Round 1: Non-NTLM Connection\n");
+	do_ntlm_round (base_uri, NULL);
+	debug_printf (1, "Round 2: NTLM Connection, user=alice\n");
+	do_ntlm_round (base_uri, "alice");
+	debug_printf (1, "Round 3: NTLM Connection, user=bob\n");
+	do_ntlm_round (base_uri, "bob");
 }
 
 int
@@ -343,57 +317,26 @@
 {
 	GMainLoop *loop;
 	SoupServer *server;
-	int opt;
 	GHashTable *connections;
 	SoupURI *uri;
-	int errors;
 
-	g_type_init ();
-	g_thread_init (NULL);
-	signal (SIGINT, quit);
-
-	while ((opt = getopt (argc, argv, "d")) != -1) {
-		switch (opt) {
-		case 'd':
-			debug = TRUE;
-			break;
-		default:
-			fprintf (stderr, "Usage: %s [-d]\n",
-				 argv[0]);
-			exit (1);
-		}
-	}
+	test_init (argc, argv, NULL);
 
+	server = soup_test_server_new (FALSE);
 	connections = g_hash_table_new (NULL, NULL);
-
-	server = soup_server_new (SOUP_SERVER_PORT, 0,
-				  NULL);
-	if (!server) {
-		fprintf (stderr, "Unable to bind server\n");
-		exit (1);
-	}
 	soup_server_add_handler (server, NULL,
 				 server_callback, connections, NULL);
-	soup_server_run_async (server);
 
 	loop = g_main_loop_new (NULL, TRUE);
 
 	uri = soup_uri_new ("http://localhost/";);
 	uri->port = soup_server_get_port (server);
-	errors = do_ntlm_tests (uri);
+	do_ntlm_tests (uri);
 	soup_uri_free (uri);
 
-	soup_server_quit (server);
-	g_object_unref (server);
 	g_main_loop_unref (loop);
 	g_hash_table_destroy (connections);
-	g_main_context_unref (g_main_context_default ());
 
-	dprintf ("\n");
-	if (errors) {
-		printf ("ntlm-test: %d error(s). Run with '-d' for details\n",
-			errors);
-	} else
-		printf ("ntlm-test: OK\n");
+	test_cleanup ();
 	return errors != 0;
 }

Modified: branches/libsoup-2.4/tests/proxy-test.c
==============================================================================
--- branches/libsoup-2.4/tests/proxy-test.c	(original)
+++ branches/libsoup-2.4/tests/proxy-test.c	Mon Jan 14 14:19:32 2008
@@ -8,23 +8,7 @@
 #include <unistd.h>
 
 #include "libsoup/soup.h"
-#include "apache-wrapper.h"
-
-int errors = 0;
-gboolean debug = FALSE;
-
-static void
-dprintf (const char *format, ...)
-{
-	va_list args;
-
-	if (!debug)
-		return;
-
-	va_start (args, format);
-	vprintf (format, args);
-	va_end (args);
-}
+#include "test-utils.h"
 
 typedef struct {
 	const char *explanation;
@@ -75,7 +59,7 @@
 	SoupURI *proxy_uri;
 	SoupMessage *msg;
 
-	dprintf ("  GET %s via %s\n", url, proxy_names[proxy]);
+	debug_printf (1, "  GET %s via %s\n", url, proxy_names[proxy]);
 	if (proxy == UNAUTH_PROXY && expected != SOUP_STATUS_FORBIDDEN)
 		expected = SOUP_STATUS_PROXY_UNAUTHORIZED;
 
@@ -98,9 +82,9 @@
 
 	soup_session_send_message (session, msg);
 
-	dprintf ("  %d %s\n", msg->status_code, msg->reason_phrase);
+	debug_printf (1, "  %d %s\n", msg->status_code, msg->reason_phrase);
 	if (msg->status_code != expected) {
-		dprintf ("  EXPECTED %d!\n", expected);
+		debug_printf (1, "  EXPECTED %d!\n", expected);
 		errors++;
 	}
 
@@ -114,8 +98,8 @@
 {
 	char *http_url, *https_url;
 
-	dprintf ("Test %d: %s (%s)\n", i + 1, tests[i].explanation,
-		 sync ? "sync" : "async");
+	debug_printf (1, "Test %d: %s (%s)\n", i + 1, tests[i].explanation,
+		      sync ? "sync" : "async");
 
 	if (!strncmp (tests[i].url, "http", 4)) {
 		http_url = g_strdup (tests[i].url);
@@ -140,46 +124,22 @@
 	g_free (http_url);
 	g_free (https_url);
 
-	dprintf ("\n");
+	debug_printf (1, "\n");
 }
 
 int
 main (int argc, char **argv)
 {
-	int i, opt;
-
-	g_type_init ();
-	g_thread_init (NULL);
-
-	while ((opt = getopt (argc, argv, "d")) != -1) {
-		switch (opt) {
-		case 'd':
-			debug = TRUE;
-			break;
-		default:
-			fprintf (stderr, "Usage: %s [-d]\n", argv[0]);
-			return 1;
-		}
-	}
+	int i;
 
-	if (!apache_init ()) {
-		fprintf (stderr, "Could not start apache\n");
-		return 1;
-	}
+	test_init (argc, argv, NULL);
+	apache_init ();
 
 	for (i = 0; i < ntests; i++) {
 		run_test (i, FALSE);
 		run_test (i, TRUE);
 	}
 
-	apache_cleanup ();
-	g_main_context_unref (g_main_context_default ());
-
-	dprintf ("\n");
-	if (errors) {
-		printf ("proxy-test: %d error(s). Run with '-d' for details\n",
-			errors);
-	} else
-		printf ("proxy-test: OK\n");
-	return errors;
+	test_cleanup ();
+	return errors != 0;
 }

Modified: branches/libsoup-2.4/tests/pull-api.c
==============================================================================
--- branches/libsoup-2.4/tests/pull-api.c	(original)
+++ branches/libsoup-2.4/tests/pull-api.c	Mon Jan 14 14:19:32 2008
@@ -11,26 +11,11 @@
 #include "libsoup/soup.h"
 #include "libsoup/soup-session.h"
 
-#include "apache-wrapper.h"
+#include "test-utils.h"
 
-int errors = 0;
-int debug = 0;
 SoupBuffer *correct_response;
 
 static void
-dprintf (int level, const char *format, ...)
-{
-	va_list args;
-
-	if (debug < level)
-		return;
-
-	va_start (args, format);
-	vprintf (format, args);
-	va_end (args);
-}
-
-static void
 authenticate (SoupSession *session, SoupMessage *msg,
 	      SoupAuth *auth, gboolean retrying, gpointer data)
 {
@@ -44,7 +29,7 @@
 	SoupSession *session;
 	SoupMessage *msg;
 
-	session = soup_session_async_new ();
+	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
 	msg = soup_message_new (SOUP_METHOD_GET, uri);
 	soup_session_send_message (session, msg);
 	if (msg->status_code != SOUP_STATUS_OK) {
@@ -98,7 +83,7 @@
 	loop = g_main_loop_new (NULL, FALSE);
 
 	uri = g_build_filename (base_uri, sub_uri, NULL);
-	dprintf (1, "GET %s\n", uri);
+	debug_printf (1, "GET %s\n", uri);
 
 	msg = soup_message_new (SOUP_METHOD_GET, uri);
 	g_free (uri);
@@ -153,10 +138,10 @@
 	FullyAsyncData *ad = user_data;
 
 	if (!ad->did_first_timeout) {
-		dprintf (1, "  first timeout\n");
+		debug_printf (1, "  first timeout\n");
 		ad->did_first_timeout = TRUE;
 	} else
-		dprintf (2, "  timeout\n");
+		debug_printf (2, "  timeout\n");
 	ad->timeout = 0;
 
 	/* ad->chunks_ready and ad->chunk_wanted are used because
@@ -181,15 +166,15 @@
 {
 	FullyAsyncData *ad = user_data;
 
-	dprintf (1, "  %d %s\n", msg->status_code, msg->reason_phrase);
+	debug_printf (1, "  %d %s\n", msg->status_code, msg->reason_phrase);
 	if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
 		/* Let soup handle this one; this got_headers handler
 		 * will get called again next time around.
 		 */
 		return;
 	} else if (msg->status_code != SOUP_STATUS_OK) {
-		dprintf (1, "  unexpected status: %d %s\n",
-			 msg->status_code, msg->reason_phrase);
+		debug_printf (1, "  unexpected status: %d %s\n",
+			      msg->status_code, msg->reason_phrase);
 		errors++;
 		return;
 	}
@@ -211,24 +196,24 @@
 {
 	FullyAsyncData *ad = user_data;
 
-	dprintf (2, "  got chunk from %lu - %lu\n",
-		 (unsigned long) ad->read_so_far,
-		 (unsigned long) ad->read_so_far + chunk->length);
+	debug_printf (2, "  got chunk from %lu - %lu\n",
+		      (unsigned long) ad->read_so_far,
+		      (unsigned long) ad->read_so_far + chunk->length);
 
 	/* We've got a chunk, let's process it. In the case of the
 	 * test program, that means comparing it against
 	 * correct_response to make sure that we got the right data.
 	 */
 	if (ad->read_so_far + chunk->length > correct_response->length) {
-		dprintf (1, "  read too far! (%lu > %lu)\n",
-			 (unsigned long) (ad->read_so_far + chunk->length),
-			 (unsigned long) correct_response->length);
+		debug_printf (1, "  read too far! (%lu > %lu)\n",
+			      (unsigned long) (ad->read_so_far + chunk->length),
+			      (unsigned long) correct_response->length);
 		errors++;
 	} else if (memcmp (chunk->data,
 			   correct_response->data + ad->read_so_far,
 			   chunk->length) != 0) {
-		dprintf (1, "  data mismatch in block starting at %lu\n",
-			 (unsigned long) ad->read_so_far);
+		debug_printf (1, "  data mismatch in block starting at %lu\n",
+			      (unsigned long) ad->read_so_far);
 		errors++;
 	}
 	ad->read_so_far += chunk->length;
@@ -252,8 +237,8 @@
 	FullyAsyncData *ad = user_data;
 
 	if (msg->status_code != ad->expected_status) {
-		dprintf (1, "  unexpected final status: %d %s !\n",
-			 msg->status_code, msg->reason_phrase);
+		debug_printf (1, "  unexpected final status: %d %s !\n",
+			      msg->status_code, msg->reason_phrase);
 		errors++;
 	}
 
@@ -299,7 +284,7 @@
 	SoupBuffer *chunk;
 
 	uri = g_build_filename (base_uri, sub_uri, NULL);
-	dprintf (1, "GET %s\n", uri);
+	debug_printf (1, "GET %s\n", uri);
 
 	msg = soup_message_new (SOUP_METHOD_GET, uri);
 	g_free (uri);
@@ -313,11 +298,11 @@
 	sync_async_send (session, msg);
 	if (sync_async_is_finished (msg) &&
 	    expected_status == SOUP_STATUS_OK) {
-		dprintf (1, "  finished without reading response!\n");
+		debug_printf (1, "  finished without reading response!\n");
 		errors++;
 	} else if (!sync_async_is_finished (msg) &&
 		   expected_status != SOUP_STATUS_OK) {
-		dprintf (1, "  request failed to fail!\n");
+		debug_printf (1, "  request failed to fail!\n");
 		errors++;
 	}
 
@@ -326,20 +311,20 @@
 	 */
 	read_so_far = 0;
 	while ((chunk = sync_async_read_chunk (msg))) {
-		dprintf (2, "  read chunk from %lu - %lu\n",
-			 (unsigned long) read_so_far,
-			 (unsigned long) read_so_far + chunk->length);
+		debug_printf (2, "  read chunk from %lu - %lu\n",
+			      (unsigned long) read_so_far,
+			      (unsigned long) read_so_far + chunk->length);
 
 		if (read_so_far + chunk->length > correct_response->length) {
-			dprintf (1, "  read too far! (%lu > %lu)\n",
-				 (unsigned long) read_so_far + chunk->length,
-				 (unsigned long) correct_response->length);
+			debug_printf (1, "  read too far! (%lu > %lu)\n",
+				      (unsigned long) read_so_far + chunk->length,
+				      (unsigned long) correct_response->length);
 			errors++;
 		} else if (memcmp (chunk->data,
 				   correct_response->data + read_so_far,
 				   chunk->length) != 0) {
-			dprintf (1, "  data mismatch in block starting at %lu\n",
-				 (unsigned long) read_so_far);
+			debug_printf (1, "  data mismatch in block starting at %lu\n",
+				      (unsigned long) read_so_far);
 			errors++;
 		}
 		read_so_far += chunk->length;
@@ -349,11 +334,11 @@
 	if (!sync_async_is_finished (msg) ||
 	    (msg->status_code == SOUP_STATUS_OK &&
 	     read_so_far != correct_response->length)) {
-		dprintf (1, "  loop ended before message was fully read!\n");
+		debug_printf (1, "  loop ended before message was fully read!\n");
 		errors++;
 	} else if (msg->status_code != expected_status) {
-		dprintf (1, "  unexpected final status: %d %s !\n",
-			 msg->status_code, msg->reason_phrase);
+		debug_printf (1, "  unexpected final status: %d %s !\n",
+			      msg->status_code, msg->reason_phrase);
 		errors++;
 	}
 
@@ -413,15 +398,15 @@
 {
 	SyncAsyncData *ad = user_data;
 
-	dprintf (1, "  %d %s\n", msg->status_code, msg->reason_phrase);
+	debug_printf (1, "  %d %s\n", msg->status_code, msg->reason_phrase);
 	if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
 		/* Let soup handle this one; this got_headers handler
 		 * will get called again next time around.
 		 */
 		return;
 	} else if (msg->status_code != SOUP_STATUS_OK) {
-		dprintf (1, "  unexpected status: %d %s\n",
-			 msg->status_code, msg->reason_phrase);
+		debug_printf (1, "  unexpected status: %d %s\n",
+			      msg->status_code, msg->reason_phrase);
 		errors++;
 		return;
 	}
@@ -506,31 +491,15 @@
 {
 	SoupSession *session;
 	char *base_uri;
-	int opt;
 
-	g_type_init ();
-	g_thread_init (NULL);
-
-	while ((opt = getopt (argc, argv, "d")) != -1) {
-		switch (opt) {
-		case 'd':
-			debug++;
-			break;
-		default:
-			fprintf (stderr, "Usage: %s [-d [-d]]\n", argv[0]);
-			return 1;
-		}
-	}
+	test_init (argc, argv, NULL);
+	apache_init ();
 
-	if (!apache_init ()) {
-		fprintf (stderr, "Could not start apache\n");
-		return 1;
-	}
 	base_uri = "http://localhost:47524/";;
 	get_correct_response (base_uri);
 
-	dprintf (1, "\nFully async, fast requests\n");
-	session = soup_session_async_new ();
+	debug_printf (1, "\nFully async, fast requests\n");
+	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
 	g_signal_connect (session, "authenticate",
 			  G_CALLBACK (authenticate), NULL);
 	do_fully_async_test (session, base_uri, "/",
@@ -542,8 +511,8 @@
 	soup_session_abort (session);
 	g_object_unref (session);
 
-	dprintf (1, "\nFully async, slow requests\n");
-	session = soup_session_async_new ();
+	debug_printf (1, "\nFully async, slow requests\n");
+	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
 	g_signal_connect (session, "authenticate",
 			  G_CALLBACK (authenticate), NULL);
 	do_fully_async_test (session, base_uri, "/",
@@ -555,8 +524,8 @@
 	soup_session_abort (session);
 	g_object_unref (session);
 
-	dprintf (1, "\nSynchronously async\n");
-	session = soup_session_async_new ();
+	debug_printf (1, "\nSynchronously async\n");
+	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
 	g_signal_connect (session, "authenticate",
 			  G_CALLBACK (authenticate), NULL);
 	do_synchronously_async_test (session, base_uri, "/",
@@ -571,14 +540,6 @@
 
 	soup_buffer_free (correct_response);
 
-	apache_cleanup ();
-	g_main_context_unref (g_main_context_default ());
-
-	dprintf (1, "\n");
-	if (errors) {
-		printf ("pull-api: %d error(s). Run with '-d' for details\n",
-			errors);
-	} else
-		printf ("pull-api: OK\n");
-	return errors;
+	test_cleanup ();
+	return errors != 0;
 }

Modified: branches/libsoup-2.4/tests/query-test.c
==============================================================================
--- branches/libsoup-2.4/tests/query-test.c	(original)
+++ branches/libsoup-2.4/tests/query-test.c	Mon Jan 14 14:19:32 2008
@@ -22,21 +22,9 @@
 #include <libsoup/soup-server.h>
 #include <libsoup/soup-session-sync.h>
 
-GMainLoop *loop;
-gboolean debug = FALSE;
-
-static void
-dprintf (const char *format, ...)
-{
-	va_list args;
-
-	if (!debug)
-		return;
+#include "test-utils.h"
 
-	va_start (args, format);
-	vprintf (format, args);
-	va_end (args);
-}
+GMainLoop *loop;
 
 struct {
 	char *title, *name;
@@ -68,19 +56,18 @@
 	{ "Mr.", "Foo %2f Bar", "Hello, MR. Foo %2f Bar" },
 };
 
-static int
+static void
 do_test (int n, gboolean extra, const char *uri)
 {
 	GPtrArray *args;
-	int errors = 0;
 	GHashTable *form_data_set;
 	char *title_arg = NULL, *name_arg = NULL;
 	char *stdout = NULL;
 
-	dprintf ("%2d. '%s' '%s'%s: ", n * 2 + (extra ? 2 : 1),
-		 tests[n].title ? tests[n].title : "(null)",
-		 tests[n].name  ? tests[n].name  : "(null)",
-		 extra ? " + extra" : "");
+	debug_printf (1, "%2d. '%s' '%s'%s: ", n * 2 + (extra ? 2 : 1),
+		      tests[n].title ? tests[n].title : "(null)",
+		      tests[n].name  ? tests[n].name  : "(null)",
+		      extra ? " + extra" : "");
 
 	form_data_set = g_hash_table_new (g_str_hash, g_str_equal);
 
@@ -115,37 +102,34 @@
 			  NULL, NULL,
 			  &stdout, NULL, NULL, NULL)) {
 		if (stdout && !strcmp (stdout, tests[n].result))
-			dprintf ("OK!\n");
+			debug_printf (1, "OK!\n");
 		else {
-			dprintf ("WRONG!\n");
-			dprintf ("  expected '%s', got '%s'\n",
-				 tests[n].result, stdout ? stdout : "(error)");
+			debug_printf (1, "WRONG!\n");
+			debug_printf (1, "  expected '%s', got '%s'\n",
+				      tests[n].result,
+				      stdout ? stdout : "(error)");
 			errors++;
 		}
 		g_free (stdout);
 	} else {
-		dprintf ("ERROR!\n");
+		debug_printf (1, "ERROR!\n");
 		errors++;
 	}
 	g_ptr_array_free (args, TRUE);
 	g_hash_table_destroy (form_data_set);
 	g_free (title_arg);
 	g_free (name_arg);
-
-	return errors;
 }
 
-static int
+static void
 do_query_tests (const char *uri)
 {
-	int n, errors = 0;
+	int n;
 
 	for (n = 0; n < G_N_ELEMENTS (tests); n++) {
-		errors += do_test (n, FALSE, uri);
-		errors += do_test (n, TRUE, uri);
+		do_test (n, FALSE, uri);
+		do_test (n, TRUE, uri);
 	}
-
-	return errors;
 }
 
 GThread *server_thread;
@@ -164,11 +148,6 @@
 		return;
 	}
 
-	if (!strcmp (path, "/shutdown")) {
-		soup_server_quit (server);
-		return;
-	}
-
 	if (query) {
 		title = g_hash_table_lookup (query, "title");
 		name = g_hash_table_lookup (query, "name");
@@ -209,93 +188,31 @@
 	soup_message_set_status (msg, SOUP_STATUS_OK);
 }
 
-static gpointer
-run_server_thread (gpointer user_data)
-{
-	SoupServer *server = user_data;
-
-	soup_server_add_handler (server, NULL,
-				 server_callback, NULL, NULL);
-	soup_server_run (server);
-	g_object_unref (server);
-
-	return NULL;
-}
-
-static guint
-create_server (void)
-{
-	SoupServer *server;
-	GMainContext *async_context;
-	guint port;
-
-	async_context = g_main_context_new ();
-	server = soup_server_new (SOUP_SERVER_PORT, 0,
-				  SOUP_SERVER_ASYNC_CONTEXT, async_context,
-				  NULL);
-	g_main_context_unref (async_context);
-
-	if (!server) {
-		fprintf (stderr, "Unable to bind server\n");
-		exit (1);
-	}
+gboolean run_tests = TRUE;
 
-	port = soup_server_get_port (server);
-	server_thread = g_thread_create (run_server_thread, server, TRUE, NULL);
-
-	return port;
-}
-
-static void
-shutdown_server (const char *base_uri)
-{
-	SoupSession *session;
-	char *uri;
-	SoupMessage *msg;
-
-	session = soup_session_sync_new ();
-	uri = g_build_filename (base_uri, "shutdown", NULL);
-	msg = soup_message_new ("GET", uri);
-	soup_session_send_message (session, msg);
-	g_object_unref (msg);
-	g_free (uri);
-
-	soup_session_abort (session);
-	g_object_unref (session);
-
-	g_thread_join (server_thread);
-}
+static GOptionEntry no_test_entry[] = {
+        { "no-tests", 'n', G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_REVERSE,
+          G_OPTION_ARG_NONE, &run_tests,
+          "Don't run tests, just run the test server", NULL },
+        { NULL }
+};
 
 int
 main (int argc, char **argv)
 {
 	GMainLoop *loop;
+	SoupServer *server;
 	guint port;
-	int opt;
-	int errors;
-	gboolean run_tests = TRUE;
 	SoupURI *uri;
 	char *uri_str;
 
-	g_type_init ();
-	g_thread_init (NULL);
+	test_init (argc, argv, no_test_entry);
 
-	while ((opt = getopt (argc, argv, "dn")) != -1) {
-		switch (opt) {
-		case 'd':
-			debug = TRUE;
-			break;
-		case 'n':
-			run_tests = FALSE;
-			break;
-		default:
-			fprintf (stderr, "Usage: %s [-d]\n",
-				 argv[0]);
-			exit (1);
-		}
-	}
+	server = soup_test_server_new (TRUE);
+	soup_server_add_handler (server, NULL,
+				 server_callback, NULL, NULL);
+	port = 	soup_server_get_port (server);
 
-	port = create_server ();
 	loop = g_main_loop_new (NULL, TRUE);
 
 	if (run_tests) {
@@ -304,8 +221,7 @@
 		uri_str = soup_uri_to_string (uri, FALSE);
 		soup_uri_free (uri);
 
-		errors = do_query_tests (uri_str);
-		shutdown_server (uri_str);
+		do_query_tests (uri_str);
 		g_free (uri_str);
 	} else {
 		printf ("Listening on port %d\n", port);
@@ -314,11 +230,7 @@
 
 	g_main_loop_unref (loop);
 
-	dprintf ("\n");
-	if (errors) {
-		printf ("query-test: %d error(s). Run with '-d' for details\n",
-			errors);
-	} else
-		printf ("query-test: OK\n");
+	if (run_tests)
+		test_cleanup ();
 	return errors != 0;
 }

Modified: branches/libsoup-2.4/tests/server-auth-test.c
==============================================================================
--- branches/libsoup-2.4/tests/server-auth-test.c	(original)
+++ branches/libsoup-2.4/tests/server-auth-test.c	Mon Jan 14 14:19:32 2008
@@ -23,8 +23,9 @@
 #include <libsoup/soup-message.h>
 #include <libsoup/soup-server.h>
 
+#include "test-utils.h"
+
 GMainLoop *loop;
-gboolean debug = FALSE;
 
 struct {
 	gboolean client_sent_basic, client_sent_digest;
@@ -33,19 +34,6 @@
 } test_data;
 
 static void
-dprintf (const char *format, ...)
-{
-	va_list args;
-
-	if (!debug)
-		return;
-
-	va_start (args, format);
-	vprintf (format, args);
-	va_end (args);
-}
-
-static void
 curl_exited (GPid pid, int status, gpointer data)
 {
 	gboolean *done = data;
@@ -54,7 +42,7 @@
 	test_data.succeeded = (status == 0);
 }
 
-static int
+static void
 do_test (int n, SoupURI *base_uri, const char *path, gboolean good_password,
 	 gboolean offer_basic, gboolean offer_digest,
 	 gboolean client_sends_basic, gboolean client_sends_digest,
@@ -66,12 +54,11 @@
 	GPtrArray *args;
 	GPid pid;
 	gboolean done;
-	int errors = 0;
 
-	dprintf ("%2d. %s, %soffer Basic, %soffer Digest, %s password\n",
-		 n, path, offer_basic ? "" : "don't ",
-		 offer_digest ? "" : "don't ",
-		 good_password ? "good" : "bad");
+	debug_printf (1, "%2d. %s, %soffer Basic, %soffer Digest, %s password\n",
+		      n, path, offer_basic ? "" : "don't ",
+		      offer_digest ? "" : "don't ",
+		      good_password ? "good" : "bad");
 
 	uri = soup_uri_new_with_base (base_uri, path);
 	uri_str = soup_uri_to_string (uri, FALSE);
@@ -115,46 +102,44 @@
 	if (server_requests_basic != test_data.server_requested_basic) {
 		errors++;
 		if (test_data.server_requested_basic)
-			dprintf ("  Server sent WWW-Authenticate: Basic, but shouldn't have!\n");
+			debug_printf (1, "  Server sent WWW-Authenticate: Basic, but shouldn't have!\n");
 		else
-			dprintf ("  Server didn't send WWW-Authenticate: Basic, but should have!\n");
+			debug_printf (1, "  Server didn't send WWW-Authenticate: Basic, but should have!\n");
 	}
 	if (server_requests_digest != test_data.server_requested_digest) {
 		errors++;
 		if (test_data.server_requested_digest)
-			dprintf ("  Server sent WWW-Authenticate: Digest, but shouldn't have!\n");
+			debug_printf (1, "  Server sent WWW-Authenticate: Digest, but shouldn't have!\n");
 		else
-			dprintf ("  Server didn't send WWW-Authenticate: Digest, but should have!\n");
+			debug_printf (1, "  Server didn't send WWW-Authenticate: Digest, but should have!\n");
 	}
 	if (client_sends_basic != test_data.client_sent_basic) {
 		errors++;
 		if (test_data.client_sent_basic)
-			dprintf ("  Client sent Authorization: Basic, but shouldn't have!\n");
+			debug_printf (1, "  Client sent Authorization: Basic, but shouldn't have!\n");
 		else
-			dprintf ("  Client didn't send Authorization: Basic, but should have!\n");
+			debug_printf (1, "  Client didn't send Authorization: Basic, but should have!\n");
 	}
 	if (client_sends_digest != test_data.client_sent_digest) {
 		errors++;
 		if (test_data.client_sent_digest)
-			dprintf ("  Client sent Authorization: Digest, but shouldn't have!\n");
+			debug_printf (1, "  Client sent Authorization: Digest, but shouldn't have!\n");
 		else
-			dprintf ("  Client didn't send Authorization: Digest, but should have!\n");
+			debug_printf (1, "  Client didn't send Authorization: Digest, but should have!\n");
 	}
 	if (success && !test_data.succeeded) {
 		errors++;
-		dprintf ("  Should have succeeded, but didn't!\n");
+		debug_printf (1, "  Should have succeeded, but didn't!\n");
 	} else if (!success && test_data.succeeded) {
 		errors++;
-		dprintf ("  Should not have succeeded, but did!\n");
+		debug_printf (1, "  Should not have succeeded, but did!\n");
 	}
-
-	return errors;
 }
 
-static int
+static void
 do_auth_tests (SoupURI *base_uri)
 {
-	int i, n = 1, errors = 0;
+	int i, n = 1;
 	gboolean use_basic, use_digest, good_password;
 	gboolean preemptive_basic;
 
@@ -172,58 +157,56 @@
 		 * Authorization headers completely, and the request
 		 * will always succeed.
 		 */
-		errors += do_test (n++, base_uri, "/foo", good_password,
-				   /* request */
-				   use_basic, use_digest,
-				   /* expected from client */
-				   preemptive_basic, FALSE,
-				   /* expected from server */
-				   FALSE, FALSE,
-				   /* success? */
-				   TRUE);
+		do_test (n++, base_uri, "/foo", good_password,
+			 /* request */
+			 use_basic, use_digest,
+			 /* expected from client */
+			 preemptive_basic, FALSE,
+			 /* expected from server */
+			 FALSE, FALSE,
+			 /* success? */
+			 TRUE);
 
 		/* 2. Basic auth required. The server will send
 		 * "WWW-Authenticate: Basic" if the client fails to
 		 * send an Authorization: Basic on the first request,
 		 * or if it sends a bad password.
 		 */
-		errors += do_test (n++, base_uri, "/Basic/foo", good_password,
-				   /* request */
-				   use_basic, use_digest,
-				   /* expected from client */
-				   use_basic, FALSE,
-				   /* expected from server */
-				   !preemptive_basic || !good_password, FALSE,
-				   /* success? */
-				   use_basic && good_password);
+		do_test (n++, base_uri, "/Basic/foo", good_password,
+			 /* request */
+			 use_basic, use_digest,
+			 /* expected from client */
+			 use_basic, FALSE,
+			 /* expected from server */
+			 !preemptive_basic || !good_password, FALSE,
+			 /* success? */
+			 use_basic && good_password);
 
 		/* 3. Digest auth required. Simpler than the basic
 		 * case because the client can't send Digest auth
 		 * premptively.
 		 */
-		errors += do_test (n++, base_uri, "/Digest/foo", good_password,
-				   /* request */
-				   use_basic, use_digest,
-				   /* expected from client */
-				   preemptive_basic, use_digest,
-				   /* expected from server */
-				   FALSE, TRUE,
-				   /* success? */
-				   use_digest && good_password);
+		do_test (n++, base_uri, "/Digest/foo", good_password,
+			 /* request */
+			 use_basic, use_digest,
+			 /* expected from client */
+			 preemptive_basic, use_digest,
+			 /* expected from server */
+			 FALSE, TRUE,
+			 /* success? */
+			 use_digest && good_password);
 
 		/* 4. Any auth required. */
-		errors += do_test (n++, base_uri, "/Any/foo", good_password,
-				   /* request */
-				   use_basic, use_digest,
-				   /* expected from client */
-				   preemptive_basic, use_digest,
-				   /* expected from server */
-				   !preemptive_basic || !good_password, !preemptive_basic || !good_password,
-				   /* success? */
-				   (use_basic || use_digest) && good_password);
+		do_test (n++, base_uri, "/Any/foo", good_password,
+			 /* request */
+			 use_basic, use_digest,
+			 /* expected from client */
+			 preemptive_basic, use_digest,
+			 /* expected from server */
+			 !preemptive_basic || !good_password, !preemptive_basic || !good_password,
+			 /* success? */
+			 (use_basic || use_digest) && good_password);
 	}
-
-	return errors;
 }
 
 static gboolean
@@ -306,41 +289,27 @@
 			  G_CALLBACK (wrote_headers_callback), NULL);
 }
 
+gboolean run_tests = TRUE;
+
+static GOptionEntry no_test_entry[] = {
+        { "no-tests", 'n', G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_REVERSE,
+          G_OPTION_ARG_NONE, &run_tests,
+          "Don't run tests, just run the test server", NULL },
+        { NULL }
+};
+
 int
 main (int argc, char **argv)
 {
 	GMainLoop *loop;
 	SoupServer *server;
-	int opt;
 	SoupURI *uri;
-	int errors;
 	SoupAuthDomain *auth_domain;
 	gboolean run_tests = TRUE;
 
-	g_type_init ();
-	g_thread_init (NULL);
+	test_init (argc, argv, no_test_entry);
 
-	while ((opt = getopt (argc, argv, "dn")) != -1) {
-		switch (opt) {
-		case 'd':
-			debug = TRUE;
-			break;
-		case 'n':
-			run_tests = FALSE;
-			break;
-		default:
-			fprintf (stderr, "Usage: %s [-d]\n",
-				 argv[0]);
-			exit (1);
-		}
-	}
-
-	server = soup_server_new (SOUP_SERVER_PORT, 0,
-				  NULL);
-	if (!server) {
-		fprintf (stderr, "Unable to bind server\n");
-		exit (1);
-	}
+	server = soup_test_server_new (FALSE);
 	g_signal_connect (server, "request_started",
 			  G_CALLBACK (request_started_callback), NULL);
 	soup_server_add_handler (server, NULL,
@@ -362,14 +331,12 @@
 		NULL);
 	soup_server_add_auth_domain (server, auth_domain);
 
-	soup_server_run_async (server);
-
 	loop = g_main_loop_new (NULL, TRUE);
 
 	if (run_tests) {
 		uri = soup_uri_new ("http://localhost";);
 		uri->port = soup_server_get_port (server);
-		errors = do_auth_tests (uri);
+		do_auth_tests (uri);
 		soup_uri_free (uri);
 	} else {
 		printf ("Listening on port %d\n", soup_server_get_port (server));
@@ -378,11 +345,7 @@
 
 	g_main_loop_unref (loop);
 
-	dprintf ("\n");
-	if (errors) {
-		printf ("server-auth-test: %d error(s). Run with '-d' for details\n",
-			errors);
-	} else
-		printf ("server-auth-test: OK\n");
+	if (run_tests)
+		test_cleanup ();
 	return errors != 0;
 }

Modified: branches/libsoup-2.4/tests/simple-httpd.c
==============================================================================
--- branches/libsoup-2.4/tests/simple-httpd.c	(original)
+++ branches/libsoup-2.4/tests/simple-httpd.c	Mon Jan 14 14:19:32 2008
@@ -19,47 +19,26 @@
 #include <libsoup/soup-server.h>
 
 static void
-print_header (const char *name, const char *value, gpointer data)
+do_get (SoupServer *server, SoupMessage *msg, const char *path)
 {
-	printf ("%s: %s\n", name, value);
-}
-
-static void
-server_callback (SoupServer *server, SoupMessage *msg,
-		 const char *path, GHashTable *query,
-		 SoupClientContext *context, gpointer data)
-{
-	char *path_to_open, *slash;
+	char *slash;
 	struct stat st;
 	int fd;
 
-	printf ("%s %s HTTP/1.%d\n", msg->method, path,
-		soup_message_get_http_version (msg));
-	soup_message_headers_foreach (msg->request_headers, print_header, NULL);
-	if (msg->request_body->length)
-		printf ("%s\n", msg->request_body->data);
-
-	if (msg->method != SOUP_METHOD_GET && msg->method != SOUP_METHOD_HEAD) {
-		soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
-		goto DONE;
-	}
-
-	path_to_open = g_strdup_printf (".%s", path);
-
- AGAIN:
-	if (stat (path_to_open, &st) == -1) {
-		g_free (path_to_open);
+	if (stat (path, &st) == -1) {
 		if (errno == EPERM)
 			soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
 		else if (errno == ENOENT)
 			soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
 		else
 			soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
-		goto DONE;
+		return;
 	}
 
 	if (S_ISDIR (st.st_mode)) {
-		slash = strrchr (path_to_open, '/');
+		char *index_path;
+
+		slash = strrchr (path, '/');
 		if (!slash || slash[1]) {
 			char *uri, *redir_uri;
 
@@ -70,20 +49,19 @@
 			soup_message_set_status (msg, SOUP_STATUS_MOVED_PERMANENTLY);
 			g_free (redir_uri);
 			g_free (uri);
-			g_free (path_to_open);
-			goto DONE;
+			return;
 		}
 
-		g_free (path_to_open);
-		path_to_open = g_strdup_printf (".%s/index.html", path);
-		goto AGAIN;
+		index_path = g_strdup_printf ("%s/index.html", path);
+		do_get (server, msg, index_path);
+		g_free (index_path);
+		return;
 	}
 
-	fd = open (path_to_open, O_RDONLY);
-	g_free (path_to_open);
+	fd = open (path, O_RDONLY);
 	if (fd == -1) {
 		soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
-		goto DONE;
+		return;
 	}
 
 	if (msg->method == SOUP_METHOD_GET) {
@@ -108,8 +86,71 @@
 	}
 
 	soup_message_set_status (msg, SOUP_STATUS_OK);
+}
+
+static void
+do_put (SoupServer *server, SoupMessage *msg, const char *path)
+{
+	struct stat st;
+	FILE *f;
+	gboolean created = TRUE;
+
+	if (stat (path, &st) != -1) {
+		const char *match = soup_message_headers_get (msg->request_headers, "If-None-Match");
+		if (match && !strcmp (match, "*")) {
+			soup_message_set_status (msg, SOUP_STATUS_CONFLICT);
+			return;
+		}
+
+		if (!S_ISREG (st.st_mode)) {
+			soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
+			return;
+		}
+
+		created = FALSE;
+	}
+
+	f = fopen (path, "w");
+	if (!f) {
+		soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
+		return;
+	}
+
+	fwrite (msg->request_body->data, 1, msg->request_body->length, f);
+	fclose (f);
+
+	soup_message_set_status (msg, created ? SOUP_STATUS_CREATED : SOUP_STATUS_OK);
+}
+
+static void
+print_header (const char *name, const char *value, gpointer data)
+{
+	printf ("%s: %s\n", name, value);
+}
+
+static void
+server_callback (SoupServer *server, SoupMessage *msg,
+		 const char *path, GHashTable *query,
+		 SoupClientContext *context, gpointer data)
+{
+	char *file_path;
+
+	printf ("%s %s HTTP/1.%d\n", msg->method, path,
+		soup_message_get_http_version (msg));
+	soup_message_headers_foreach (msg->request_headers, print_header, NULL);
+	if (msg->request_body->length)
+		printf ("%s\n", msg->request_body->data);
+
+	file_path = g_strdup_printf (".%s", path);
+
+	if (msg->method == SOUP_METHOD_GET || msg->method == SOUP_METHOD_HEAD)
+		do_get (server, msg, file_path);
+	else if (msg->method == SOUP_METHOD_PUT)
+		do_put (server, msg, file_path);
+	else
+		soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
 
- DONE:
+	g_free (file_path);
 	printf ("  -> %d %s\n\n", msg->status_code, msg->reason_phrase);
 }
 

Added: branches/libsoup-2.4/tests/test-utils.c
==============================================================================
--- (empty file)
+++ branches/libsoup-2.4/tests/test-utils.c	Mon Jan 14 14:19:32 2008
@@ -0,0 +1,284 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "test-utils.h"
+#include "libsoup/soup-logger.h"
+#include "libsoup/soup-misc.h"
+#include "libsoup/soup-server.h"
+
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifdef HAVE_APACHE
+static gboolean apache_running;
+#endif
+static SoupServer *test_server;
+GThread *server_thread;
+static void test_server_shutdown (void);
+
+static SoupLogger *logger;
+
+int debug_level, http_debug_level, errors;
+
+static gboolean
+increment_debug_level (const char *option_name, const char *value,
+		       gpointer data, GError **error)
+{
+	debug_level++;
+	return TRUE;
+}
+
+static gboolean
+increment_http_debug_level (const char *option_name, const char *value,
+			    gpointer data, GError **error)
+{
+	http_debug_level++;
+	return TRUE;
+}
+
+static GOptionEntry debug_entry[] = {
+	{ "debug", 'd', G_OPTION_FLAG_NO_ARG,
+	  G_OPTION_ARG_CALLBACK, increment_debug_level,
+	  "Enable (or increase) test-specific debugging", NULL },
+	{ "http-debug", 'h', G_OPTION_FLAG_NO_ARG,
+	  G_OPTION_ARG_CALLBACK, increment_http_debug_level,
+	  "Enable (or increase) HTTP-level debugging", NULL },
+	{ NULL }
+};
+
+static void
+quit (int sig)
+{
+#ifdef HAVE_APACHE
+	if (apache_running)
+		apache_cleanup ();
+#endif
+	if (test_server)
+		test_server_shutdown ();
+
+	exit (1);
+}
+
+void
+test_init (int argc, char **argv, GOptionEntry *entries)
+{
+	GOptionContext *opts;
+	char *name;
+	GError *error = NULL;
+
+	g_type_init ();
+	g_thread_init (NULL);
+
+	name = strrchr (argv[0], '/');
+	if (!name++)
+		name = argv[0];
+	if (!strncmp (name, "lt-", 3))
+		name += 3;
+	g_set_prgname (name);
+
+	opts = g_option_context_new (NULL);
+	g_option_context_add_main_entries (opts, debug_entry, NULL);
+	if (entries)
+		g_option_context_add_main_entries (opts, entries, NULL);
+
+	if (!g_option_context_parse (opts, &argc, &argv, &error)) {
+		fprintf (stderr, "Could not parse arguments: %s\n",
+			 error->message);
+		fprintf (stderr, "%s",
+			 g_option_context_get_help (opts, TRUE, NULL));
+		exit (1);
+	}
+	g_option_context_free (opts);
+
+	/* Exit cleanly on ^C in case we're valgrinding. */
+	signal (SIGINT, quit);
+}
+
+void
+test_cleanup (void)
+{
+	debug_printf (1, "\n");
+	if (errors) {
+		printf ("%s: %d error(s).%s\n",
+			g_get_prgname (), errors,
+			debug_level == 0 ? " Run with '-d' for details" : "");
+	} else
+		printf ("%s: OK\n", g_get_prgname ());
+
+#ifdef HAVE_APACHE
+	if (apache_running)
+		apache_cleanup ();
+#endif
+	if (test_server)
+		test_server_shutdown ();
+
+	if (logger)
+		g_object_unref (logger);
+
+	g_main_context_unref (g_main_context_default ());
+}
+
+void
+debug_printf (int level, const char *format, ...)
+{
+	va_list args;
+
+	if (debug_level < level)
+		return;
+
+	va_start (args, format);
+	vprintf (format, args);
+	va_end (args);
+}
+
+#ifdef HAVE_APACHE
+
+static gboolean
+apache_cmd (char *cmd)
+{
+	char *argv[8];
+	char *cwd, *conf;
+	int status;
+	gboolean ok;
+
+	cwd = g_get_current_dir ();
+	conf = g_build_filename (cwd, "httpd.conf", NULL);
+
+	argv[0] = APACHE_HTTPD;
+	argv[1] = "-d";
+	argv[2] = cwd;
+	argv[3] = "-f";
+	argv[4] = conf;
+	argv[5] = "-k";
+	argv[6] = cmd;
+	argv[7] = NULL;
+
+	ok = g_spawn_sync (cwd, argv, NULL, 0, NULL, NULL,
+			   NULL, NULL, &status, NULL);
+	if (ok)
+		ok = (status == 0);
+
+	g_free (cwd);
+	g_free (conf);
+
+	return ok;
+}
+
+void
+apache_init (void)
+{
+	if (!apache_cmd ("start")) {
+		fprintf (stderr, "Could not start apache\n");
+		exit (1);
+	}
+	apache_running = TRUE;
+}
+
+void
+apache_cleanup (void)
+{
+	pid_t pid;
+	char *contents;
+
+	if (g_file_get_contents ("httpd.pid", &contents, NULL, NULL)) {
+		pid = strtoul (contents, NULL, 10);
+		g_free (contents);
+	} else
+		pid = 0;
+
+	if (!apache_cmd ("graceful-stop"))
+		return;
+	apache_running = FALSE;
+
+	if (pid) {
+		while (kill (pid, 0) == 0)
+			g_usleep (100);
+	}
+}
+
+#endif /* HAVE_APACHE */
+
+SoupSession *
+soup_test_session_new (GType type, ...)
+{
+	va_list args;
+	const char *propname;
+	SoupSession *session;
+
+	va_start (args, type);
+	propname = va_arg (args, const char *);
+	session = (SoupSession *)g_object_new_valist (type, propname, args);
+	va_end (args);
+
+	if (http_debug_level && !logger) {
+		SoupLoggerLogLevel level = MIN ((SoupLoggerLogLevel)http_debug_level, SOUP_LOGGER_LOG_BODY);
+
+		logger = soup_logger_new (level, -1);
+	}
+
+	if (logger)
+		soup_logger_attach (logger, session);
+
+	return session;
+}
+
+static gpointer run_server_thread (gpointer user_data);
+
+SoupServer *
+soup_test_server_new (gboolean in_own_thread)
+{
+	GMainContext *async_context;
+
+	async_context = in_own_thread ? g_main_context_new () : NULL;
+	test_server = soup_server_new (SOUP_SERVER_ASYNC_CONTEXT, async_context,
+				       NULL);
+	if (async_context)
+		g_main_context_unref (async_context);
+
+	if (!test_server) {
+		fprintf (stderr, "Unable to create server\n");
+		exit (1);
+	}
+
+	if (in_own_thread) {
+		server_thread = g_thread_create (run_server_thread, test_server,
+						 TRUE, NULL);
+	} else
+		soup_server_run_async (test_server);
+
+	return test_server;
+}
+
+static gpointer
+run_server_thread (gpointer user_data)
+{
+	SoupServer *server = user_data;
+
+	soup_server_run (server);
+	return NULL;
+}
+
+static gboolean
+idle_quit_server (gpointer server)
+{
+	soup_server_quit (server);
+	return FALSE;
+}
+
+static void
+test_server_shutdown (void)
+{
+	if (server_thread) {
+		soup_add_idle (soup_server_get_async_context (test_server),
+			       idle_quit_server, test_server);
+		g_thread_join (server_thread);
+	} else
+		soup_server_quit (test_server);
+	g_object_unref (test_server);
+}
+
+

Added: branches/libsoup-2.4/tests/test-utils.h
==============================================================================
--- (empty file)
+++ branches/libsoup-2.4/tests/test-utils.h	Mon Jan 14 14:19:32 2008
@@ -0,0 +1,19 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "libsoup/soup-types.h"
+
+void test_init    (int argc, char **argv, GOptionEntry *entries);
+void test_cleanup (void);
+
+extern int debug_level, errors;
+void debug_printf (int level, const char *format, ...);
+
+#ifdef HAVE_APACHE
+void apache_init    (void);
+void apache_cleanup (void);
+#endif
+
+SoupSession *soup_test_session_new (GType type, ...);
+SoupServer  *soup_test_server_new  (gboolean in_own_thread);

Modified: branches/libsoup-2.4/tests/uri-parsing.c
==============================================================================
--- branches/libsoup-2.4/tests/uri-parsing.c	(original)
+++ branches/libsoup-2.4/tests/uri-parsing.c	Mon Jan 14 14:19:32 2008
@@ -5,22 +5,10 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include "libsoup/soup-uri.h"
-
-gboolean debug = FALSE;
-
-static void
-dprintf (const char *format, ...)
-{
-	va_list args;
 
-	if (!debug)
-		return;
+#include "libsoup/soup-uri.h"
 
-	va_start (args, format);
-	vprintf (format, args);
-	va_end (args);
-}
+#include "test-utils.h"
 
 struct {
 	const char *uri_string, *result;
@@ -150,21 +138,21 @@
 	char *uri_string;
 
 	if (base_uri) {
-		dprintf ("<%s> + <%s> = <%s>? ", base_str, in_uri,
-			 out_uri ? out_uri : "ERR");
+		debug_printf (1, "<%s> + <%s> = <%s>? ", base_str, in_uri,
+			      out_uri ? out_uri : "ERR");
 		uri = soup_uri_new_with_base (base_uri, in_uri);
 	} else {
-		dprintf ("<%s> => <%s>? ", in_uri,
-			 out_uri ? out_uri : "ERR");
+		debug_printf (1, "<%s> => <%s>? ", in_uri,
+			      out_uri ? out_uri : "ERR");
 		uri = soup_uri_new (in_uri);
 	}
 
 	if (!uri) {
 		if (out_uri) {
-			dprintf ("ERR\n  Could not parse %s\n", in_uri);
+			debug_printf (1, "ERR\n  Could not parse %s\n", in_uri);
 			return FALSE;
 		} else {
-			dprintf ("OK\n");
+			debug_printf (1, "OK\n");
 			return TRUE;
 		}
 	}
@@ -173,18 +161,18 @@
 	soup_uri_free (uri);
 
 	if (!out_uri) {
-		dprintf ("ERR\n  Got %s\n", uri_string);
+		debug_printf (1, "ERR\n  Got %s\n", uri_string);
 		return FALSE;
 	}
 
 	if (strcmp (uri_string, out_uri) != 0) {
-		dprintf ("NO\n  Unparses to <%s>\n", uri_string);
+		debug_printf (1, "NO\n  Unparses to <%s>\n", uri_string);
 		g_free (uri_string);
 		return FALSE;
 	}
 	g_free (uri_string);
 
-	dprintf ("OK\n");
+	debug_printf (1, "OK\n");
 	return TRUE;
 }
 
@@ -193,27 +181,18 @@
 {
 	SoupURI *base_uri, *uri1, *uri2;
 	char *uri_string;
-	int i, errs = 0, opt;
+	int i;
 
-	while ((opt = getopt (argc, argv, "d")) != -1) {
-		switch (opt) {
-		case 'd':
-			debug = TRUE;
-			break;
-		default:
-			fprintf (stderr, "Usage: %s [-d]\n", argv[0]);
-			return 1;
-		}
-	}
+	test_init (argc, argv, NULL);
 
-	dprintf ("Absolute URI parsing\n");
+	debug_printf (1, "Absolute URI parsing\n");
 	for (i = 0; i < num_abs_tests; i++) {
 		if (!do_uri (NULL, NULL, abs_tests[i].uri_string,
 			     abs_tests[i].result))
-			errs++;
+			errors++;
 	}
 
-	dprintf ("\nRelative URI parsing\n");
+	debug_printf (1, "\nRelative URI parsing\n");
 	base_uri = soup_uri_new (base);
 	if (!base_uri) {
 		fprintf (stderr, "Could not parse %s!\n", base);
@@ -224,38 +203,33 @@
 	if (strcmp (uri_string, base) != 0) {
 		fprintf (stderr, "URI <%s> unparses to <%s>\n",
 			 base, uri_string);
-		errs++;
+		errors++;
 	}
 	g_free (uri_string);
 
 	for (i = 0; i < num_rel_tests; i++) {
 		if (!do_uri (base_uri, base, rel_tests[i].uri_string,
 			     rel_tests[i].result))
-			errs++;
+			errors++;
 	}
 	soup_uri_free (base_uri);
 
-	dprintf ("\nURI equality testing\n");
+	debug_printf (1, "\nURI equality testing\n");
 	for (i = 0; i < num_eq_tests; i++) {
 		uri1 = soup_uri_new (eq_tests[i].one);
 		uri2 = soup_uri_new (eq_tests[i].two);
-		dprintf ("<%s> == <%s>? ", eq_tests[i].one, eq_tests[i].two);
+		debug_printf (1, "<%s> == <%s>? ", eq_tests[i].one, eq_tests[i].two);
 		if (soup_uri_equal (uri1, uri2))
-			dprintf ("OK\n");
+			debug_printf (1, "OK\n");
 		else {
-			dprintf ("NO\n");
-			dprintf ("%s : %s : %s\n%s : %s : %s\n",
-				 uri1->scheme, uri1->host, uri1->path,
-				 uri2->scheme, uri2->host, uri2->path);
-			errs++;
+			debug_printf (1, "NO\n");
+			debug_printf (1, "%s : %s : %s\n%s : %s : %s\n",
+				      uri1->scheme, uri1->host, uri1->path,
+				      uri2->scheme, uri2->host, uri2->path);
+			errors++;
 		}
 	}
 
-	dprintf ("\n");
-	if (errs) {
-		printf ("uri-parsing: %d error(s). Run with '-d' for details\n",
-			errs);
-	} else
-		printf ("uri-parsing: OK\n");
-	return errs;
+	test_cleanup ();
+	return errors != 0;
 }

Modified: branches/libsoup-2.4/tests/xmlrpc-test.c
==============================================================================
--- branches/libsoup-2.4/tests/xmlrpc-test.c	(original)
+++ branches/libsoup-2.4/tests/xmlrpc-test.c	Mon Jan 14 14:19:32 2008
@@ -11,24 +11,10 @@
 #include <libsoup/soup.h>
 #include <libsoup/soup-md5-utils.h>
 
-#include "apache-wrapper.h"
+#include "test-utils.h"
 
 SoupSession *session;
 static const char *uri = "http://localhost:47524/xmlrpc-server.php";;
-int debug;
-
-static void
-dprintf (int level, const char *format, ...)
-{
-	va_list args;
-
-	if (debug < level)
-		return;
-
-	va_start (args, format);
-	vprintf (format, args);
-	va_end (args);
-}
 
 static const char *const value_type[] = {
 	"BAD",
@@ -66,16 +52,16 @@
 				  body, strlen (body));
 	soup_session_send_message (session, msg);
 
-	if (debug >= 3) {
-		dprintf (3, "\n%s\n%d %s\n%s\n",
-			 msg->request_body->data,
-			 msg->status_code, msg->reason_phrase,
-			 msg->response_body->data);
+	if (debug_level >= 3) {
+		debug_printf (3, "\n%s\n%d %s\n%s\n",
+			      msg->request_body->data,
+			      msg->status_code, msg->reason_phrase,
+			      msg->response_body->data);
 	}
 
 	if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
-		dprintf (1, "ERROR: %d %s\n", msg->status_code,
-			 msg->reason_phrase);
+		debug_printf (1, "ERROR: %d %s\n", msg->status_code,
+			      msg->reason_phrase);
 		g_object_unref (msg);
 		return FALSE;
 	}
@@ -84,10 +70,10 @@
 						msg->response_body->length,
 						retval, &err)) {
 		if (err) {
-			dprintf (1, "FAULT: %d %s\n", err->code, err->message);
+			debug_printf (1, "FAULT: %d %s\n", err->code, err->message);
 			g_error_free (err);
 		} else
-			dprintf (1, "ERROR: could not parse response\n");
+			debug_printf (1, "ERROR: could not parse response\n");
 		g_object_unref (msg);
 		return FALSE;
 	}
@@ -102,7 +88,7 @@
 	va_list args;
 
 	if (!G_VALUE_HOLDS (value, type)) {
-		dprintf (1, "ERROR: could not parse response\n");
+		debug_printf (1, "ERROR: could not parse response\n");
 		g_value_unset (value);
 		return FALSE;
 	}
@@ -121,16 +107,16 @@
 	GValue retval;
 	gboolean ok;
 
-	dprintf (1, "sum (array of int -> int): ");
+	debug_printf (1, "sum (array of int -> int): ");
 
 	ints = g_value_array_new (10);
 	for (i = sum = 0; i < 10; i++) {
 		val = rand () % 100;
-		dprintf (2, "%s%d", i == 0 ? "[" : ", ", val);
+		debug_printf (2, "%s%d", i == 0 ? "[" : ", ", val);
 		soup_value_array_append (ints, G_TYPE_INT, val);
 		sum += val;
 	}
-	dprintf (2, "] -> ");
+	debug_printf (2, "] -> ");
 
 	ok = (do_xmlrpc ("sum", &retval,
 			G_TYPE_VALUE_ARRAY, ints,
@@ -141,8 +127,8 @@
 	if (!ok)
 		return FALSE;
 
-	dprintf (2, "%d: ", result);
-	dprintf (1, "%s\n", result == sum ? "OK!" : "WRONG!");
+	debug_printf (2, "%d: ", result);
+	debug_printf (1, "%s\n", result == sum ? "OK!" : "WRONG!");
 	return result == sum;
 }
 
@@ -156,19 +142,19 @@
 	gboolean val, ok;
 	GHashTable *result;
 
-	dprintf (1, "countBools (array of boolean -> struct of ints): ");
+	debug_printf (1, "countBools (array of boolean -> struct of ints): ");
 
 	bools = g_value_array_new (10);
 	for (i = trues = falses = 0; i < 10; i++) {
 		val = rand () > (RAND_MAX / 2);
-		dprintf (2, "%s%c", i == 0 ? "[" : ", ", val ? 'T' : 'F');
+		debug_printf (2, "%s%c", i == 0 ? "[" : ", ", val ? 'T' : 'F');
 		soup_value_array_append (bools, G_TYPE_BOOLEAN, val);
 		if (val)
 			trues++;
 		else
 			falses++;
 	}
-	dprintf (2, "] -> ");
+	debug_printf (2, "] -> ");
 
 	ok = (do_xmlrpc ("countBools", &retval,
 			 G_TYPE_VALUE_ARRAY, bools,
@@ -179,18 +165,18 @@
 		return FALSE;
 
 	if (!soup_value_hash_lookup (result, "true", G_TYPE_INT, &ret_trues)) {
-		dprintf (1, "NO 'true' value in response\n");
+		debug_printf (1, "NO 'true' value in response\n");
 		return FALSE;
 	}
 	if (!soup_value_hash_lookup (result, "false", G_TYPE_INT, &ret_falses)) {
-		dprintf (1, "NO 'false' value in response\n");
+		debug_printf (1, "NO 'false' value in response\n");
 		return FALSE;
 	}
 	g_hash_table_destroy (result);
 
-	dprintf (2, "{ true: %d, false: %d } ", ret_trues, ret_falses);
+	debug_printf (2, "{ true: %d, false: %d } ", ret_trues, ret_falses);
 	ok = (trues == ret_trues) && (falses == ret_falses);
-	dprintf (1, "%s\n", ok ? "OK!" : "WRONG!");
+	debug_printf (1, "%s\n", ok ? "OK!" : "WRONG!");
 	return ok;
 }
 
@@ -204,7 +190,7 @@
 	GValue retval;
 	gboolean ok;
 
-	dprintf (1, "md5sum (base64 -> base64): ");
+	debug_printf (1, "md5sum (base64 -> base64): ");
 
 	data = g_byte_array_new ();
 	g_byte_array_set_size (data, 256);
@@ -224,13 +210,13 @@
 		return FALSE;
 
 	if (result->len != 16) {
-		dprintf (1, "result has WRONG length (%d)\n", result->len);
+		debug_printf (1, "result has WRONG length (%d)\n", result->len);
 		g_byte_array_free (result, TRUE);
 		return FALSE;
 	}
 
 	ok = (memcmp (digest, result->data, 16) == 0);
-	dprintf (1, "%s\n", ok ? "OK!" : "WRONG!");
+	debug_printf (1, "%s\n", ok ? "OK!" : "WRONG!");
 	g_byte_array_free (result, TRUE);
 	return ok;
 }
@@ -244,7 +230,7 @@
 	GValue retval;
 	gboolean ok;
 
-	dprintf (1, "dateChange (struct of time and ints -> time): ");
+	debug_printf (1, "dateChange (struct of time and ints -> time): ");
 
 	structval = soup_value_hash_new ();
 
@@ -256,50 +242,50 @@
 			      rand () % 60);
 	soup_value_hash_insert (structval, "date", SOUP_TYPE_DATE, date);
 
-	if (debug >= 2) {
+	if (debug_level >= 2) {
 		timestamp = soup_date_to_string (date, SOUP_DATE_ISO8601_XMLRPC);
-		dprintf (2, "{ date: %s", timestamp);
+		debug_printf (2, "{ date: %s", timestamp);
 		g_free (timestamp);
 	}
 
 	if (rand () % 3) {
 		date->year = 1970 + (rand () % 50);
-		dprintf (2, ", tm_year: %d", date->year - 1900);
+		debug_printf (2, ", tm_year: %d", date->year - 1900);
 		soup_value_hash_insert (structval, "tm_year",
 					G_TYPE_INT, date->year - 1900);
 	}
 	if (rand () % 3) {
 		date->month = 1 + rand () % 12;
-		dprintf (2, ", tm_mon: %d", date->month - 1);
+		debug_printf (2, ", tm_mon: %d", date->month - 1);
 		soup_value_hash_insert (structval, "tm_mon",
 					G_TYPE_INT, date->month - 1);
 	}
 	if (rand () % 3) {
 		date->day = 1 + rand () % 28;
-		dprintf (2, ", tm_mday: %d", date->day);
+		debug_printf (2, ", tm_mday: %d", date->day);
 		soup_value_hash_insert (structval, "tm_mday",
 					G_TYPE_INT, date->day);
 	}
 	if (rand () % 3) {
 		date->hour = rand () % 24;
-		dprintf (2, ", tm_hour: %d", date->hour);
+		debug_printf (2, ", tm_hour: %d", date->hour);
 		soup_value_hash_insert (structval, "tm_hour",
 					G_TYPE_INT, date->hour);
 	}
 	if (rand () % 3) {
 		date->minute = rand () % 60;
-		dprintf (2, ", tm_min: %d", date->minute);
+		debug_printf (2, ", tm_min: %d", date->minute);
 		soup_value_hash_insert (structval, "tm_min",
 					G_TYPE_INT, date->minute);
 	}
 	if (rand () % 3) {
 		date->second = rand () % 60;
-		dprintf (2, ", tm_sec: %d", date->second);
+		debug_printf (2, ", tm_sec: %d", date->second);
 		soup_value_hash_insert (structval, "tm_sec",
 					G_TYPE_INT, date->second);
 	}
 
-	dprintf (2, " } -> ");
+	debug_printf (2, " } -> ");
 
 	ok = (do_xmlrpc ("dateChange", &retval,
 			 G_TYPE_HASH_TABLE, structval,
@@ -311,9 +297,9 @@
 		return FALSE;
 	}
 
-	if (debug >= 2) {
+	if (debug_level >= 2) {
 		timestamp = soup_date_to_string (result, SOUP_DATE_ISO8601_XMLRPC);
-		dprintf (2, "%s: ", timestamp);
+		debug_printf (2, "%s: ", timestamp);
 		g_free (timestamp);
 	}
 
@@ -326,7 +312,7 @@
 	soup_date_free (date);
 	soup_date_free (result);
 
-	dprintf (1, "%s\n", ok ? "OK!" : "WRONG!");
+	debug_printf (1, "%s\n", ok ? "OK!" : "WRONG!");
 	return ok;
 }
 
@@ -345,14 +331,14 @@
 	GValue retval;
 	int i;
 
-	dprintf (1, "echo (array of string -> array of string): ");
+	debug_printf (1, "echo (array of string -> array of string): ");
 
 	originals = g_value_array_new (N_ECHO_STRINGS);
 	for (i = 0; i < N_ECHO_STRINGS; i++) {
 		soup_value_array_append (originals, G_TYPE_STRING, echo_strings[i]);
-		dprintf (2, "%s\"%s\"", i == 0 ? "[" : ", ", echo_strings[i]);
+		debug_printf (2, "%s\"%s\"", i == 0 ? "[" : ", ", echo_strings[i]);
 	}
-	dprintf (2, "] -> ");
+	debug_printf (2, "] -> ");
 
 	if (!(do_xmlrpc ("echo", &retval,
 			 G_TYPE_VALUE_ARRAY, originals,
@@ -363,68 +349,42 @@
 	}
 	g_value_array_free (originals);
 
-	if (debug >= 2) {
+	if (debug_level >= 2) {
 		for (i = 0; i < echoes->n_values; i++) {
-			dprintf (2, "%s\"%s\"", i == 0 ? "[" : ", ",
-				 g_value_get_string (&echoes->values[i]));
+			debug_printf (2, "%s\"%s\"", i == 0 ? "[" : ", ",
+				      g_value_get_string (&echoes->values[i]));
 		}
-		dprintf (2, "] -> ");
+		debug_printf (2, "] -> ");
 	}
 
 	if (echoes->n_values != N_ECHO_STRINGS) {
-		dprintf (1, " WRONG! Wrong number of return strings");
+		debug_printf (1, " WRONG! Wrong number of return strings");
 		g_value_array_free (echoes);
 		return FALSE;
 	}
 
 	for (i = 0; i < echoes->n_values; i++) {
 		if (strcmp (echo_strings[i], g_value_get_string (&echoes->values[i])) != 0) {
-			dprintf (1, " WRONG! Mismatch at %d\n", i + 1);
+			debug_printf (1, " WRONG! Mismatch at %d\n", i + 1);
 			g_value_array_free (echoes);
 			return FALSE;
 		}
 	}
 
-	dprintf (1, "OK!\n");
+	debug_printf (1, "OK!\n");
 	g_value_array_free (echoes);
 	return TRUE;
 }
 
-static void
-usage (void)
-{
-	fprintf (stderr, "Usage: xmlrpc-test [-d] [-d]\n");
-	exit (1);
-}
-
 int
 main (int argc, char **argv)
 {
-	int opt, errors = 0;
-
-	g_type_init ();
-	g_thread_init (NULL);
-
-	while ((opt = getopt (argc, argv, "d")) != -1) {
-		switch (opt) {
-		case 'd':
-			debug++;
-			break;
-
-		case '?':
-			usage ();
-			break;
-		}
-	}
+	test_init (argc, argv, NULL);
+	apache_init ();
 
 	srand (time (NULL));
 
-	if (!apache_init ()) {
-		fprintf (stderr, "Could not start apache\n");
-		return 1;
-	}
-
-	session = soup_session_sync_new ();
+	session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, NULL);
 
 	if (!test_sum ())
 		errors++;
@@ -440,13 +400,6 @@
 	soup_session_abort (session);
 	g_object_unref (session);
 
-	apache_cleanup ();
-
-	dprintf (1, "\n");
-	if (errors) {
-		printf ("xmlrpc-test: %d error(s). Run with '-d' for details\n",
-			errors);
-	} else
-		printf ("xmlrpc-test: OK\n");
-	return errors;
+	test_cleanup ();
+	return errors = 0;
 }



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