[libsoup/installed-tests: 2/3] tests: Add resources/* to soup-test.gresource, add utility functions



commit 1ef5769bf20fb3def1410928729e37c617d8d43e
Author: Dan Winship <danw gnome org>
Date:   Wed Dec 11 18:15:13 2013 +0100

    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.

 tests/Makefile.am              |   19 ++-------
 tests/chunk-io-test.c          |   48 +++++++++++-------------
 tests/coding-test.c            |   31 +++++++--------
 tests/forms-test.c             |    6 +-
 tests/range-test.c             |   20 +---------
 tests/requester-test.c         |   27 ++-----------
 tests/resource-test.c          |   36 +-----------------
 tests/sniffing-test.c          |   80 ++++++++++++++--------------------------
 tests/soup-tests.gresource.xml |   13 ++++++-
 tests/streaming-test.c         |   38 +++++--------------
 tests/test-utils.c             |   67 +++++++++++++++++++++++++++++++++
 tests/test-utils.h             |    6 +++
 12 files changed, 174 insertions(+), 217 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 76a87fa..f7293b1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -77,21 +77,10 @@ httpd.conf: $(httpd_conf_in)
 BUILT_SOURCES = httpd.conf
 endif
 
-soup-tests.gresource: soup-tests.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(srcdir) 
--generate-dependencies $(srcdir)/soup-tests.gresource.xml)
-       $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir)  $<
-
-RESOURCES =                    \
-       resources/atom.xml      \
-       resources/home.gif      \
-       resources/html_binary.html \
-       resources/mbox          \
-       resources/mbox.gz       \
-       resources/mbox.raw      \
-       resources/mbox.zlib     \
-       resources/ps_binary.ps  \
-       resources/rss20.xml     \
-       resources/test.html     \
-       resources/text_binary.txt
+RESOURCES = $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(srcdir) --generate-dependencies 
$(srcdir)/soup-tests.gresource.xml)
+
+soup-tests.gresource: soup-tests.gresource.xml $(RESOURCES)
+       $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir) $<
 
 EXTRA_DIST =                    \
        htdigest                 \
diff --git a/tests/chunk-io-test.c b/tests/chunk-io-test.c
index 4003e34..29062a6 100644
--- a/tests/chunk-io-test.c
+++ b/tests/chunk-io-test.c
@@ -351,8 +351,8 @@ do_io_tests (void)
        GInputStream *imem, *islow, *in;
        GOutputStream *omem, *oslow, *out;
        GMemoryOutputStream *mem;
-       char *raw_contents, *buf;
-       gsize raw_length;
+       SoupBuffer *raw_contents;
+       char *buf;
        GString *chunkified;
        GError *error = NULL;
        gssize nread, nwrote, total;
@@ -360,13 +360,8 @@ do_io_tests (void)
 
        debug_printf (1, "\nI/O tests\n");
 
-       if (!g_file_get_contents (SRCDIR "/index.txt", &raw_contents, &raw_length, &error)) {
-               g_printerr ("Could not read index.txt: %s\n",
-                           error->message);
-               exit (1);
-       }
-
-       chunkified = chunkify (raw_contents, raw_length);
+       raw_contents = soup_test_get_index ();
+       chunkified = chunkify (raw_contents->data, raw_contents->length);
 
        debug_printf (1, "  sync read\n");
 
@@ -383,10 +378,11 @@ do_io_tests (void)
        g_object_unref (imem);
        g_object_unref (islow);
 
-       buf = g_malloc (raw_length);
+       buf = g_malloc (raw_contents->length);
        total = 0;
        while (TRUE) {
-               nread = g_input_stream_read (in, buf + total, raw_length - total,
+               nread = g_input_stream_read (in, buf + total,
+                                            raw_contents->length - total,
                                             NULL, &error);
                g_assert_no_error (error);
                g_clear_error (&error);
@@ -401,7 +397,7 @@ do_io_tests (void)
        g_clear_error (&error);
        g_object_unref (in);
 
-       soup_assert_cmpmem (buf, total, raw_contents, raw_length);
+       soup_assert_cmpmem (buf, total, raw_contents->data, raw_contents->length);
        g_free (buf);
 
        debug_printf (1, "  async read\n");
@@ -419,11 +415,12 @@ do_io_tests (void)
        g_object_unref (imem);
        g_object_unref (islow);
 
-       buf = g_malloc (raw_length);
+       buf = g_malloc (raw_contents->length);
        total = 0;
        while (TRUE) {
                nread = g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (in),
-                                                                 buf + total, raw_length - total,
+                                                                 buf + total,
+                                                                 raw_contents->length - total,
                                                                  NULL, &error);
                if (nread == -1 && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
                        GSource *source;
@@ -452,7 +449,7 @@ do_io_tests (void)
        g_clear_error (&error);
        g_object_unref (in);
 
-       soup_assert_cmpmem (buf, total, raw_contents, raw_length);
+       soup_assert_cmpmem (buf, total, raw_contents->data, raw_contents->length);
        g_free (buf);
 
        debug_printf (1, "  sync write\n");
@@ -472,12 +469,12 @@ do_io_tests (void)
        g_object_unref (oslow);
 
        total = chunk_length = chunk_total = 0;
-       while (total < raw_length) {
+       while (total < raw_contents->length) {
                if (chunk_total == chunk_length) {
-                       chunk_length = MIN (CHUNK_SIZE, raw_length - total);
+                       chunk_length = MIN (CHUNK_SIZE, raw_contents->length - total);
                        chunk_total = 0;
                }
-               nwrote = g_output_stream_write (out, raw_contents + total,
+               nwrote = g_output_stream_write (out, raw_contents->data + total,
                                                chunk_length - chunk_total, NULL, &error);
                g_assert_no_error (error);
                g_clear_error (&error);
@@ -517,13 +514,13 @@ do_io_tests (void)
        g_object_unref (oslow);
 
        total = chunk_length = chunk_total = 0;
-       while (total < raw_length) {
+       while (total < raw_contents->length) {
                if (chunk_total == chunk_length) {
-                       chunk_length = MIN (CHUNK_SIZE, raw_length - total);
+                       chunk_length = MIN (CHUNK_SIZE, raw_contents->length - total);
                        chunk_total = 0;
                }
                nwrote = g_pollable_output_stream_write_nonblocking (G_POLLABLE_OUTPUT_STREAM (out),
-                                                                    raw_contents + total,
+                                                                    raw_contents->data + total,
                                                                     chunk_length - chunk_total,
                                                                     NULL, &error);
                if (nwrote == -1 && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
@@ -578,16 +575,16 @@ do_io_tests (void)
        g_object_unref (oslow);
 
        total = 0;
-       while (total < raw_length) {
-               nwrote = g_output_stream_write (out, raw_contents + total,
-                                               raw_length - total, NULL, NULL);
+       while (total < raw_contents->length) {
+               nwrote = g_output_stream_write (out, raw_contents->data + total,
+                                               raw_contents->length - total, NULL, NULL);
                if (nwrote == -1)
                        break;
                else
                        total += nwrote;
        }
 
-       g_assert_cmpint (total, !=, raw_length);
+       g_assert_cmpint (total, !=, raw_contents->length);
 
        g_output_stream_close (out, NULL, NULL);
        g_object_unref (out);
@@ -595,7 +592,6 @@ do_io_tests (void)
        g_free (buf);
 
        g_string_free (chunkified, TRUE);
-       g_free (raw_contents);
 }
 
 int
diff --git a/tests/coding-test.c b/tests/coding-test.c
index 8bc3e5f..d922a19 100644
--- a/tests/coding-test.c
+++ b/tests/coding-test.c
@@ -16,8 +16,7 @@ server_callback (SoupServer *server, SoupMessage *msg,
 {
        const char *accept_encoding, *options;
        GSList *codings;
-       char *file = NULL, *contents;
-       gsize length;
+       SoupBuffer *response = NULL;
 
        options = soup_message_headers_get_one (msg->request_headers,
                                                "X-Test-Options");
@@ -53,32 +52,32 @@ server_callback (SoupServer *server, SoupMessage *msg,
                        }
                }
                if (extension && encoding) {
-                       file = g_strdup_printf (SRCDIR "/resources%s.%s", path, extension);
-                       if (g_file_test (file, G_FILE_TEST_EXISTS)) {
+                       char *resource;
+
+                       resource = g_strdup_printf ("%s.%s", path, extension);
+                       response = soup_test_load_resource (resource, NULL);
+
+                       if (response) {
                                soup_message_headers_append (msg->response_headers,
                                                             "Content-Encoding",
                                                             encoding);
-                       } else {
-                               g_free (file);
-                               file = NULL;
                        }
+                       g_free (resource);
                }
        }
 
        soup_header_free_list (codings);
 
-       if (!file)
-               file = g_strdup_printf (SRCDIR "/resources%s", path);
-       if (!g_file_get_contents (file, &contents, &length, NULL)) {
+       if (!response)
+               response = soup_test_load_resource (path, NULL);
+       if (!response) {
                /* If path.gz exists but can't be read, we'll send back
                 * the error with "Content-Encoding: gzip" but there's
                 * no body, so, eh.
                 */
-               g_free (file);
                soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
                return;
        }
-       g_free (file);
 
        if (soup_header_contains (options, "force-encode")) {
                const gchar *encoding = "gzip";
@@ -106,11 +105,9 @@ server_callback (SoupServer *server, SoupMessage *msg,
        soup_message_set_status (msg, SOUP_STATUS_OK);
        soup_message_headers_set_encoding (msg->response_headers, SOUP_ENCODING_CHUNKED);
 
-       if (!soup_header_contains (options, "empty")) {
-               soup_message_body_append (msg->response_body,
-                                         SOUP_MEMORY_TAKE, contents, length);
-       } else
-               g_free (contents);
+       if (!soup_header_contains (options, "empty"))
+               soup_message_body_append_buffer (msg->response_body, response);
+       soup_buffer_free (response);
 
        if (soup_header_contains (options, "trailing-junk")) {
                soup_message_body_append (msg->response_body, SOUP_MEMORY_COPY,
diff --git a/tests/forms-test.c b/tests/forms-test.c
index 8747e73..8fd0de4 100644
--- a/tests/forms-test.c
+++ b/tests/forms-test.c
@@ -131,9 +131,9 @@ do_md5_test_curl (const char *uri, const char *file, const char *md5)
        g_free (file_arg);
 }
 
-#define MD5_TEST_FILE SRCDIR "/resources/home.gif"
-#define MD5_TEST_FILE_BASENAME "home.gif"
-#define MD5_TEST_FILE_MIME_TYPE "image/gif"
+#define MD5_TEST_FILE SRCDIR "/index.txt"
+#define MD5_TEST_FILE_BASENAME "index.txt"
+#define MD5_TEST_FILE_MIME_TYPE "text/plain"
 
 static void
 do_md5_test_libsoup (const char *uri, const char *contents,
diff --git a/tests/range-test.c b/tests/range-test.c
index 89ae59c..4d008d4 100644
--- a/tests/range-test.c
+++ b/tests/range-test.c
@@ -9,23 +9,6 @@ int total_length;
 char *test_response;
 
 static void
-get_full_response (void)
-{
-       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);
-       }
-
-       full_response = soup_buffer_new (SOUP_MEMORY_TAKE, contents, length);
-       debug_printf (1, "Total response length is %d\n\n", (int)length);
-}
-
-static void
 check_part (SoupMessageHeaders *headers, const char *body, gsize body_len,
            gboolean check_start_end, int expected_start, int expected_end)
 {
@@ -401,7 +384,7 @@ main (int argc, char **argv)
        test_init (argc, argv, NULL);
        apache_init ();
 
-       get_full_response ();
+       full_response = soup_test_get_index ();
        test_response = g_malloc0 (full_response->length);
 
        g_test_add_func ("/ranges/apache", do_apache_range_test);
@@ -409,7 +392,6 @@ main (int argc, char **argv)
 
        ret = g_test_run ();
 
-       soup_buffer_free (full_response);
        g_free (test_response);
 
        test_cleanup ();
diff --git a/tests/requester-test.c b/tests/requester-test.c
index 61a69f2..52c3eac 100644
--- a/tests/requester-test.c
+++ b/tests/requester-test.c
@@ -17,26 +17,6 @@ SoupBuffer *response, *auth_response;
 #define REDIRECT_HTML_BODY "<html><body>Try again</body></html>\r\n"
 #define AUTH_HTML_BODY "<html><body>Unauthorized</body></html>\r\n"
 
-static void
-get_index (void)
-{
-       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);
-       }
-
-       response = soup_buffer_new (SOUP_MEMORY_TAKE, contents, length);
-
-       auth_response = soup_buffer_new (SOUP_MEMORY_STATIC,
-                                        AUTH_HTML_BODY,
-                                        strlen (AUTH_HTML_BODY));
-}
-
 static gboolean
 slow_finish_message (gpointer msg)
 {
@@ -808,7 +788,11 @@ main (int argc, char **argv)
        int ret;
 
        test_init (argc, argv, NULL);
-       get_index ();
+
+       response = soup_test_get_index ();
+       auth_response = soup_buffer_new (SOUP_MEMORY_STATIC,
+                                        AUTH_HTML_BODY,
+                                        strlen (AUTH_HTML_BODY));
 
        server = soup_test_server_new (TRUE);
        soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
@@ -831,7 +815,6 @@ main (int argc, char **argv)
        ret = g_test_run ();
 
        g_free (uri);
-       soup_buffer_free (response);
        soup_buffer_free (auth_response);
        soup_test_server_quit_unref (server);
 
diff --git a/tests/resource-test.c b/tests/resource-test.c
index e41c603..50d63ab 100644
--- a/tests/resource-test.c
+++ b/tests/resource-test.c
@@ -7,38 +7,6 @@
 
 SoupBuffer *index_buffer;
 
-static void
-get_index (void)
-{
-       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);
-}
-
-static void
-register_gresource (void)
-{
-       GResource *resource;
-       GError *error = NULL;
-
-       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);
-}
-
 typedef struct {
        GString *body;
        char buffer[1024];
@@ -222,8 +190,8 @@ main (int argc, char **argv)
 
        test_init (argc, argv, NULL);
 
-       get_index ();
-       register_gresource ();
+       index_buffer = soup_test_get_index ();
+       soup_test_register_resources ();
 
        g_test_add_data_func ("/resource/sync/file",
                              GSIZE_TO_POINTER (SOUP_TYPE_SESSION_SYNC),
diff --git a/tests/sniffing-test.c b/tests/sniffing-test.c
index b5088d4..532e6ed 100644
--- a/tests/sniffing-test.c
+++ b/tests/sniffing-test.c
@@ -16,8 +16,8 @@ server_callback (SoupServer *server, SoupMessage *msg,
 {
        GError *error = NULL;
        char *query_key;
-       char *contents;
-       gsize length = 0, offset;
+       SoupBuffer *response = NULL;
+       gsize offset;
        gboolean empty_response = FALSE;
 
        if (msg->method != SOUP_METHOD_GET) {
@@ -40,13 +40,8 @@ server_callback (SoupServer *server, SoupMessage *msg,
        }
 
        if (!strcmp (path, "/mbox")) {
-               if (empty_response) {
-                       contents = g_strdup ("");
-                       length = 0;
-               } else {
-                       g_file_get_contents (SRCDIR "/resources/mbox",
-                                            &contents, &length,
-                                            &error);
+               if (!empty_response) {
+                       response = soup_test_load_resource ("mbox", &error);
                        g_assert_no_error (error);
                }
 
@@ -56,15 +51,10 @@ server_callback (SoupServer *server, SoupMessage *msg,
 
        if (g_str_has_prefix (path, "/text_or_binary/")) {
                char *base_name = g_path_get_basename (path);
-               char *file_name = g_strdup_printf (SRCDIR "/resources/%s", base_name);
 
-               g_file_get_contents (file_name,
-                                    &contents, &length,
-                                    &error);
+               response = soup_test_load_resource (base_name, &error);
                g_assert_no_error (error);
-
                g_free (base_name);
-               g_free (file_name);
 
                soup_message_headers_append (msg->response_headers,
                                             "Content-Type", "text/plain");
@@ -72,15 +62,10 @@ server_callback (SoupServer *server, SoupMessage *msg,
 
        if (g_str_has_prefix (path, "/unknown/")) {
                char *base_name = g_path_get_basename (path);
-               char *file_name = g_strdup_printf (SRCDIR "/resources/%s", base_name);
 
-               g_file_get_contents (file_name,
-                                    &contents, &length,
-                                    &error);
+               response = soup_test_load_resource (base_name, &error);
                g_assert_no_error (error);
-
                g_free (base_name);
-               g_free (file_name);
 
                soup_message_headers_append (msg->response_headers,
                                             "Content-Type", "UNKNOWN/unknown");
@@ -91,15 +76,10 @@ server_callback (SoupServer *server, SoupMessage *msg,
                char *ptr;
 
                char *base_name = g_path_get_basename (path);
-               char *file_name = g_strdup_printf (SRCDIR "/resources/%s", base_name);
 
-               g_file_get_contents (file_name,
-                                    &contents, &length,
-                                    &error);
+               response = soup_test_load_resource (base_name, &error);
                g_assert_no_error (error);
-
                g_free (base_name);
-               g_free (file_name);
 
                /* Hack to allow passing type in the URI */
                ptr = g_strrstr (components[2], "_");
@@ -112,15 +92,10 @@ server_callback (SoupServer *server, SoupMessage *msg,
 
        if (g_str_has_prefix (path, "/multiple_headers/")) {
                char *base_name = g_path_get_basename (path);
-               char *file_name = g_strdup_printf (SRCDIR "/resources/%s", base_name);
 
-               g_file_get_contents (file_name,
-                                    &contents, &length,
-                                    &error);
+               response = soup_test_load_resource (base_name, &error);
                g_assert_no_error (error);
-
                g_free (base_name);
-               g_free (file_name);
 
                soup_message_headers_append (msg->response_headers,
                                             "Content-Type", "text/xml");
@@ -128,15 +103,18 @@ server_callback (SoupServer *server, SoupMessage *msg,
                                             "Content-Type", "text/plain");
        }
 
-       for (offset = 0; offset < length; offset += 500) {
-               soup_message_body_append (msg->response_body,
-                                         SOUP_MEMORY_COPY,
-                                         contents + offset,
-                                         MIN(500, length - offset));
+       if (response) {
+               for (offset = 0; offset < response->length; offset += 500) {
+                       soup_message_body_append (msg->response_body,
+                                                 SOUP_MEMORY_COPY,
+                                                 response->data + offset,
+                                                 MIN (500, response->length - offset));
+               }
+
+               soup_buffer_free (response);
        }
-       soup_message_body_complete (msg->response_body);
 
-       g_free (contents);
+       soup_message_body_complete (msg->response_body);
 }
 
 static gboolean
@@ -212,8 +190,7 @@ do_signals_test (gboolean should_content_sniff,
 {
        SoupURI *uri = soup_uri_new_with_base (base_uri, "/mbox");
        SoupMessage *msg = soup_message_new_from_uri ("GET", uri);
-       char *contents;
-       gsize length;
+       SoupBuffer *expected;
        GError *error = NULL;
        SoupBuffer *body = NULL;
 
@@ -256,13 +233,10 @@ do_signals_test (gboolean should_content_sniff,
                                  "content-sniffed got emitted without a sniffer");
        }
 
-       if (empty_response) {
-               contents = g_strdup ("");
-               length = 0;
-       } else {
-               g_file_get_contents (SRCDIR "/resources/mbox",
-                                    &contents, &length,
-                                    &error);
+       if (empty_response)
+               expected = soup_buffer_new (SOUP_MEMORY_STATIC, "", 0);
+       else {
+               expected = soup_test_load_resource ("mbox", &error);
                g_assert_no_error (error);
        }
 
@@ -271,10 +245,12 @@ do_signals_test (gboolean should_content_sniff,
        else if (msg->response_body)
                body = soup_message_body_flatten (msg->response_body);
 
-       if (body)
-               soup_assert_cmpmem (body->data, body->length, contents, length);
+       if (body) {
+               soup_assert_cmpmem (body->data, body->length,
+                                   expected->data, expected->length);
+       }
 
-       g_free (contents);
+       soup_buffer_free (expected);
        if (body)
                soup_buffer_free (body);
        if (chunk_data) {
diff --git a/tests/soup-tests.gresource.xml b/tests/soup-tests.gresource.xml
index 969a6bf..9b580a3 100644
--- a/tests/soup-tests.gresource.xml
+++ b/tests/soup-tests.gresource.xml
@@ -2,5 +2,16 @@
 <gresources>
   <gresource prefix="/org/gnome/libsoup/tests">
     <file>index.txt</file>
+    <file>resources/atom.xml</file>
+    <file>resources/home.gif</file>
+    <file>resources/html_binary.html</file>
+    <file>resources/mbox</file>
+    <file>resources/mbox.gz</file>
+    <file>resources/mbox.raw</file>
+    <file>resources/mbox.zlib</file>
+    <file>resources/ps_binary.ps</file>
+    <file>resources/rss20.xml</file>
+    <file>resources/test.html</file>
+    <file>resources/text_binary.txt</file>
   </gresource>
-</gresources>
\ No newline at end of file
+</gresources>
diff --git a/tests/streaming-test.c b/tests/streaming-test.c
index 9790766..f15e189 100644
--- a/tests/streaming-test.c
+++ b/tests/streaming-test.c
@@ -7,28 +7,8 @@
 
 #define RESPONSE_CHUNK_SIZE 1024
 
-char *full_response, *full_response_md5;
-gsize full_response_length;
-
-static void
-get_full_response (void)
-{
-       GError *error = NULL;
-
-       if (!g_file_get_contents (SRCDIR "/index.txt",
-                                 &full_response,
-                                 &full_response_length,
-                                 &error)) {
-               g_printerr ("Could not read index file %s: %s\n",
-                           SRCDIR "/index.txt", error->message);
-               g_error_free (error);
-               exit (1);
-       }
-
-       full_response_md5 = g_compute_checksum_for_data (G_CHECKSUM_MD5,
-                                                        (guchar *)full_response,
-                                                        full_response_length);
-}
+SoupBuffer *full_response;
+char *full_response_md5;
 
 static void
 write_next_chunk (SoupMessage *msg, gpointer user_data)
@@ -36,12 +16,12 @@ write_next_chunk (SoupMessage *msg, gpointer user_data)
        gsize *offset = user_data;
        gsize chunk_length;
 
-       chunk_length = MIN (RESPONSE_CHUNK_SIZE, full_response_length - *offset);
+       chunk_length = MIN (RESPONSE_CHUNK_SIZE, full_response->length - *offset);
        if (chunk_length > 0) {
                debug_printf (2, "  writing chunk\n");
                soup_message_body_append (msg->response_body,
                                          SOUP_MEMORY_STATIC,
-                                         full_response + *offset,
+                                         full_response->data + *offset,
                                          chunk_length);
                *offset += chunk_length;
        } else {
@@ -74,7 +54,7 @@ server_callback (SoupServer *server, SoupMessage *msg,
                soup_message_headers_set_encoding (msg->response_headers,
                                                   SOUP_ENCODING_CONTENT_LENGTH);
                soup_message_headers_set_content_length (msg->response_headers,
-                                                        full_response_length);
+                                                        full_response->length);
        } else if (!strcmp (path, "/eof")) {
                soup_message_headers_set_encoding (msg->response_headers,
                                                   SOUP_ENCODING_EOF);
@@ -107,7 +87,7 @@ do_request (SoupSession *session, SoupURI *base_uri, char *path)
        soup_session_send_message (session, msg);
 
        soup_test_assert_message_status (msg, SOUP_STATUS_OK);
-       g_assert_cmpint (msg->response_body->length, ==, full_response_length);
+       g_assert_cmpint (msg->response_body->length, ==, full_response->length);
 
        md5 = g_compute_checksum_for_data (G_CHECKSUM_MD5,
                                           (guchar *)msg->response_body->data,
@@ -162,7 +142,10 @@ main (int argc, char **argv)
 
        test_init (argc, argv, NULL);
 
-       get_full_response ();
+       full_response = soup_test_get_index ();
+       full_response_md5 = g_compute_checksum_for_data (G_CHECKSUM_MD5,
+                                                        (guchar *)full_response->data,
+                                                        full_response->length);
 
        server = soup_test_server_new (FALSE);
        soup_server_add_handler (server, NULL,
@@ -183,7 +166,6 @@ main (int argc, char **argv)
        soup_uri_free (base_uri);
        g_main_loop_unref (loop);
 
-       g_free (full_response);
        g_free (full_response_md5);
        soup_test_server_quit_unref (server);
        test_cleanup ();
diff --git a/tests/test-utils.c b/tests/test-utils.c
index d951f3c..cf7d5ea 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;
@@ -109,6 +110,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 ());
 
@@ -513,6 +516,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, ...)
diff --git a/tests/test-utils.h b/tests/test-utils.h
index 9774151..315d111 100644
--- a/tests/test-utils.h
+++ b/tests/test-utils.h
@@ -54,6 +54,12 @@ gboolean      soup_test_request_close_stream (SoupRequest   *req,
                                              GCancellable  *cancellable,
                                              GError       **error);
 
+void        soup_test_register_resources (void);
+SoupBuffer *soup_test_load_resource      (const char  *name,
+                                         GError     **error);
+
+SoupBuffer *soup_test_get_index          (void);
+
 #ifdef G_HAVE_ISO_VARARGS
 #define soup_test_assert(expr, ...)                            \
 G_STMT_START {                                                         \


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