Unit testing libsoup-based code



I am writing unit tests for a library which itself makes use of
libsoup. One of my tests involves coaxing libsoup to execute my
"wrote_chunk" callback. I would prefer to do this without involving
too much of my other code, and I would also like to avoid actually
creating a socket connection.

I presently set things up and then generate artificial "wrote_chunk"
messages. At the end I check to ensure that the generated SoupMessage
matches what I expect. A test for MYFUNC() (a GET path handler) looks
something like this:

        server = soup_server_new(NULL, NULL)
        message = soup_message_new(SOUP_METHOD_GET, "http://test";)
        
        /*
         * Manually call test subject MYFUNC which libsoup would 
         * otherwise call upon receiving a GET request.
         */
        MYFUNC(..., server, message, path, ...)

        g_signal_emit_by_name(message, "wrote_headers", NULL)

        for (int i = 0; i < expected_size / CHUNK_SIZE + 1; i++) {
                g_signal_emit_by_name(message, "wrote_chunk", NULL)
        }

        g_signal_emit_by_name(message, "finished", NULL)

        g_object_get(message, "response-body", &body, NULL)
        soup_message_body_set_accumulate (message->response_body, TRUE)
        buffer = soup_message_body_flatten(body)
        soup_buffer_get_data(buffer, &contents, &size)

        assert(size == expected_size)
        assert(0 == memcmp(contents, expected_contents, size)

The problem with this approach is that my wrote_chunk callback repeatedly
calls soup_server_unpause_message(), and this results in a number of
error messages since the message's priv->io_data is NULL during the
course of the test.

I could precede the call to soup_server_unpause_message() in the callback
with a check using soup_message_io_in_progress(). However, libsoup does
not export this function (its declaration is not in the libsoup header
files).

Does anyone have any suggestions about how to better handle this?

-- 
Mike

:wq


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