summaryrefslogtreecommitdiff
path: root/tests/test-utils.c
diff options
context:
space:
mode:
authorDan Winship <danw@gnome.org>2013-12-11 18:15:13 +0100
committerDan Winship <danw@gnome.org>2014-02-08 13:20:21 +0100
commit094eee8d1e0ac31e83f45e026ef26688e1468884 (patch)
treef5aa4dc398c5117ca5e3392794811fd39c048f22 /tests/test-utils.c
parent64e667bda5009c8f1acd03659c457e26b16457a6 (diff)
downloadlibsoup-094eee8d1e0ac31e83f45e026ef26688e1468884.tar.gz
tests: Add resources/* to soup-test.gresource, add utility functions
Add the files in resources/ to soup-test.gresource, and add soup_test_load_resource() to get the contents of one of them, and then use this in places that were previously reading them by hand. Except forms-test, which needs to pass a filename to curl, so have it use index.txt instead of one of the resource files. Now none of the tests access the resources/ directory at runtime. Also add soup_test_get_index() to read index.txt rather than reimplementing it in multiple test programs.
Diffstat (limited to 'tests/test-utils.c')
-rw-r--r--tests/test-utils.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/test-utils.c b/tests/test-utils.c
index c28994a1..3d2546f5 100644
--- a/tests/test-utils.c
+++ b/tests/test-utils.c
@@ -12,6 +12,7 @@ static gboolean apache_running;
#endif
static SoupLogger *logger;
+static SoupBuffer *index_buffer;
int debug_level;
gboolean expect_warning, tls_available;
@@ -110,6 +111,8 @@ test_cleanup (void)
if (logger)
g_object_unref (logger);
+ if (index_buffer)
+ soup_buffer_free (index_buffer);
g_main_context_unref (g_main_context_default ());
@@ -514,6 +517,70 @@ soup_test_request_close_stream (SoupRequest *req,
return ok;
}
+void
+soup_test_register_resources (void)
+{
+ static gboolean registered = FALSE;
+ GResource *resource;
+ GError *error = NULL;
+
+ if (registered)
+ return;
+
+ resource = g_resource_load ("soup-tests.gresource", &error);
+ if (!resource) {
+ g_printerr ("Could not load resource soup-tests.gresource: %s\n",
+ error->message);
+ exit (1);
+ }
+ g_resources_register (resource);
+ g_resource_unref (resource);
+
+ registered = TRUE;
+}
+
+SoupBuffer *
+soup_test_load_resource (const char *name,
+ GError **error)
+{
+ GBytes *bytes;
+ char *path;
+
+ soup_test_register_resources ();
+
+ path = g_build_path ("/", "/org/gnome/libsoup/tests/resources", name, NULL);
+ bytes = g_resources_lookup_data (path, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
+ g_free (path);
+
+ if (!bytes)
+ return NULL;
+
+ return soup_buffer_new_with_owner (g_bytes_get_data (bytes, NULL),
+ g_bytes_get_size (bytes),
+ bytes,
+ (GDestroyNotify) g_bytes_unref);
+}
+
+SoupBuffer *
+soup_test_get_index (void)
+{
+ if (!index_buffer) {
+ char *contents;
+ gsize length;
+ GError *error = NULL;
+
+ if (!g_file_get_contents (SRCDIR "/index.txt", &contents, &length, &error)) {
+ g_printerr ("Could not read index.txt: %s\n",
+ error->message);
+ exit (1);
+ }
+
+ index_buffer = soup_buffer_new (SOUP_MEMORY_TAKE, contents, length);
+ }
+
+ return index_buffer;
+}
+
#ifndef G_HAVE_ISO_VARARGS
void
soup_test_assert (gboolean expr, const char *fmt, ...)