5 #include "test-utils.h"
6 #include "libsoup/soup-logger.h"
7 #include "libsoup/soup-misc.h"
8 #include "libsoup/soup-server.h"
17 static gboolean apache_running;
19 static SoupServer *test_server;
20 GThread *server_thread;
21 static void test_server_shutdown (void);
23 static SoupLogger *logger;
25 int debug_level, http_debug_level, errors;
28 increment_debug_level (const char *option_name, const char *value,
29 gpointer data, GError **error)
36 increment_http_debug_level (const char *option_name, const char *value,
37 gpointer data, GError **error)
43 static GOptionEntry debug_entry[] = {
44 { "debug", 'd', G_OPTION_FLAG_NO_ARG,
45 G_OPTION_ARG_CALLBACK, increment_debug_level,
46 "Enable (or increase) test-specific debugging", NULL },
47 { "http-debug", 'h', G_OPTION_FLAG_NO_ARG,
48 G_OPTION_ARG_CALLBACK, increment_http_debug_level,
49 "Enable (or increase) HTTP-level debugging", NULL },
61 test_server_shutdown ();
67 test_init (int argc, char **argv, GOptionEntry *entries)
76 name = strrchr (argv[0], '/');
79 if (!strncmp (name, "lt-", 3))
83 opts = g_option_context_new (NULL);
84 g_option_context_add_main_entries (opts, debug_entry, NULL);
86 g_option_context_add_main_entries (opts, entries, NULL);
88 if (!g_option_context_parse (opts, &argc, &argv, &error)) {
89 fprintf (stderr, "Could not parse arguments: %s\n",
91 fprintf (stderr, "%s",
92 g_option_context_get_help (opts, TRUE, NULL));
95 g_option_context_free (opts);
97 /* Exit cleanly on ^C in case we're valgrinding. */
98 signal (SIGINT, quit);
104 debug_printf (1, "\n");
106 printf ("%s: %d error(s).%s\n",
107 g_get_prgname (), errors,
108 debug_level == 0 ? " Run with '-d' for details" : "");
110 printf ("%s: OK\n", g_get_prgname ());
117 test_server_shutdown ();
120 g_object_unref (logger);
122 g_main_context_unref (g_main_context_default ());
126 debug_printf (int level, const char *format, ...)
130 if (debug_level < level)
133 va_start (args, format);
134 vprintf (format, args);
141 apache_cmd (char *cmd)
148 cwd = g_get_current_dir ();
149 conf = g_build_filename (cwd, "httpd.conf", NULL);
151 argv[0] = APACHE_HTTPD;
160 ok = g_spawn_sync (cwd, argv, NULL, 0, NULL, NULL,
161 NULL, NULL, &status, NULL);
174 if (!apache_cmd ("start")) {
175 fprintf (stderr, "Could not start apache\n");
178 apache_running = TRUE;
182 apache_cleanup (void)
187 if (g_file_get_contents ("httpd.pid", &contents, NULL, NULL)) {
188 pid = strtoul (contents, NULL, 10);
193 if (!apache_cmd ("graceful-stop"))
195 apache_running = FALSE;
198 while (kill (pid, 0) == 0)
203 #endif /* HAVE_APACHE */
206 soup_test_session_new (GType type, ...)
209 const char *propname;
210 SoupSession *session;
212 va_start (args, type);
213 propname = va_arg (args, const char *);
214 session = (SoupSession *)g_object_new_valist (type, propname, args);
217 if (http_debug_level && !logger) {
218 SoupLoggerLogLevel level = MIN ((SoupLoggerLogLevel)http_debug_level, SOUP_LOGGER_LOG_BODY);
220 logger = soup_logger_new (level, -1);
224 soup_logger_attach (logger, session);
229 static gpointer run_server_thread (gpointer user_data);
232 soup_test_server_new (gboolean in_own_thread)
234 GMainContext *async_context;
236 async_context = in_own_thread ? g_main_context_new () : NULL;
237 test_server = soup_server_new (SOUP_SERVER_ASYNC_CONTEXT, async_context,
240 g_main_context_unref (async_context);
243 fprintf (stderr, "Unable to create server\n");
248 server_thread = g_thread_create (run_server_thread, test_server,
251 soup_server_run_async (test_server);
257 run_server_thread (gpointer user_data)
259 SoupServer *server = user_data;
261 soup_server_run (server);
266 idle_quit_server (gpointer server)
268 soup_server_quit (server);
273 test_server_shutdown (void)
276 soup_add_idle (soup_server_get_async_context (test_server),
277 idle_quit_server, test_server);
278 g_thread_join (server_thread);
280 soup_server_quit (test_server);
281 g_object_unref (test_server);