diff options
Diffstat (limited to 'tests/requester-test.c')
-rw-r--r-- | tests/requester-test.c | 349 |
1 files changed, 129 insertions, 220 deletions
diff --git a/tests/requester-test.c b/tests/requester-test.c index a202c164..61a69f28 100644 --- a/tests/requester-test.c +++ b/tests/requester-test.c @@ -123,11 +123,8 @@ stream_closed (GObject *source, GAsyncResult *res, gpointer user_data) GInputStream *stream = G_INPUT_STREAM (source); GError *error = NULL; - if (!g_input_stream_close_finish (stream, res, &error)) { - debug_printf (1, " close failed: %s\n", error->message); - g_error_free (error); - errors++; - } + g_input_stream_close_finish (stream, res, &error); + g_assert_no_error (error); g_main_loop_quit (loop); g_object_unref (stream); } @@ -143,9 +140,8 @@ test_read_ready (GObject *source, GAsyncResult *res, gpointer user_data) nread = g_input_stream_read_finish (stream, res, &error); if (nread == -1) { - debug_printf (1, " read_async failed: %s\n", error->message); + g_assert_no_error (error); g_error_free (error); - errors++; g_input_stream_close (stream, NULL, NULL); g_object_unref (stream); g_main_loop_quit (loop); @@ -174,29 +170,18 @@ auth_test_sent (GObject *source, GAsyncResult *res, gpointer user_data) stream = soup_request_send_finish (SOUP_REQUEST (source), res, &error); if (!stream) { - debug_printf (1, " send_async failed: %s\n", error->message); + g_assert_no_error (error); g_clear_error (&error); - errors++; g_main_loop_quit (loop); return; } msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (source)); - if (msg->status_code != SOUP_STATUS_UNAUTHORIZED) { - debug_printf (1, " GET failed: %d %s\n", msg->status_code, - msg->reason_phrase); - errors++; - g_main_loop_quit (loop); - return; - } + soup_test_assert_message_status (msg, SOUP_STATUS_UNAUTHORIZED); g_object_unref (msg); content_type = soup_request_get_content_type (SOUP_REQUEST (source)); - if (g_strcmp0 (content_type, "text/html") != 0) { - debug_printf (1, " failed to sniff Content-Type: got %s\n", - content_type ? content_type : "(NULL)"); - errors++; - } + g_assert_cmpstr (content_type, ==, "text/html"); g_input_stream_read_async (stream, buf, sizeof (buf), G_PRIORITY_DEFAULT, NULL, @@ -213,22 +198,13 @@ test_sent (GObject *source, GAsyncResult *res, gpointer user_data) stream = soup_request_send_finish (SOUP_REQUEST (source), res, &error); if (data->cancel) { - if (stream) { - debug_printf (1, " send_async succeeded??\n"); - errors++; - g_input_stream_close (stream, NULL, NULL); - g_object_unref (stream); - } else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { - debug_printf (1, " send_async failed with wrong error: %s\n", error->message); - errors++; - } + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED); g_clear_error (&error); g_main_loop_quit (loop); return; } else { + g_assert_no_error (error); if (!stream) { - debug_printf (1, " send_async failed: %s\n", error->message); - errors++; g_main_loop_quit (loop); g_clear_error (&error); return; @@ -236,11 +212,7 @@ test_sent (GObject *source, GAsyncResult *res, gpointer user_data) } content_type = soup_request_get_content_type (SOUP_REQUEST (source)); - if (g_strcmp0 (content_type, "text/plain") != 0) { - debug_printf (1, " failed to sniff Content-Type: got %s\n", - content_type ? content_type : "(NULL)"); - errors++; - } + g_assert_cmpstr (content_type, ==, "text/plain"); g_input_stream_read_async (stream, buf, sizeof (buf), G_PRIORITY_DEFAULT, NULL, @@ -307,44 +279,20 @@ do_async_test (SoupSession *session, SoupURI *uri, g_signal_handler_disconnect (session, started_id); - if (msg->status_code != expected_status) { - debug_printf (1, " GET failed: %d %s (expected %d)\n", - msg->status_code, msg->reason_phrase, - expected_status); - g_object_unref (msg); - g_object_unref (socket); - errors++; - return; - } + soup_test_assert_message_status (msg, expected_status); g_object_unref (msg); - if (!expected_response) { - if (data.body->len) { - debug_printf (1, " body length mismatch: expected 0, got %d\n", - (int)data.body->len); - errors++; - } - } else if (data.body->len != expected_response->length) { - debug_printf (1, " body length mismatch: expected %d, got %d\n", - (int)expected_response->length, (int)data.body->len); - errors++; - } else if (memcmp (data.body->str, expected_response->data, - expected_response->length) != 0) { - debug_printf (1, " body data mismatch\n"); - errors++; - } + if (expected_response) { + soup_assert_cmpmem (data.body->str, data.body->len, + expected_response->data, expected_response->length); + } else + g_assert_cmpint (data.body->len, ==, 0); + + if (persistent) + g_assert_true (soup_socket_is_connected (socket)); + else + g_assert_false (soup_socket_is_connected (socket)); - if (persistent) { - if (!soup_socket_is_connected (socket)) { - debug_printf (1, " socket not still connected!\n"); - errors++; - } - } else { - if (soup_socket_is_connected (socket)) { - debug_printf (1, " socket still connected!\n"); - errors++; - } - } g_object_unref (socket); g_string_free (data.body, TRUE); @@ -404,18 +352,20 @@ do_test_for_thread_and_context (SoupSession *session, const char *base_uri) } static void -do_simple_tests (const char *uri) +do_simple_plain_test (gconstpointer uri) { SoupSession *session; - debug_printf (1, "Simple streaming test\n"); - - debug_printf (1, " SoupSession\n"); session = soup_test_session_new (SOUP_TYPE_SESSION, NULL); do_test_for_thread_and_context (session, uri); soup_test_session_abort_unref (session); +} + +static void +do_simple_async_test (gconstpointer uri) +{ + SoupSession *session; - debug_printf (1, " SoupSessionAsync\n"); session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, NULL); @@ -444,45 +394,50 @@ do_test_with_context_and_type (const char *uri, gboolean plain_session) g_main_context_unref (async_context); } -static gpointer -do_test_with_context (gpointer uri) +static void +do_async_test_with_context (gconstpointer uri) { - debug_printf (1, " SoupSessionAsync\n"); do_test_with_context_and_type (uri, FALSE); - return NULL; +} + +static void +do_plain_test_with_context (gconstpointer uri) +{ + do_test_with_context_and_type (uri, TRUE); } static gpointer -do_plain_test_with_context (gpointer uri) +async_test_thread (gpointer uri) { - debug_printf (1, " SoupSession\n"); do_test_with_context_and_type (uri, TRUE); return NULL; } -static void -do_context_tests (const char *uri) +static gpointer +plain_test_thread (gpointer uri) { - debug_printf (1, "\nStreaming with a non-default-context\n"); - - do_plain_test_with_context ((gpointer)uri); - do_test_with_context ((gpointer)uri); + do_test_with_context_and_type (uri, FALSE); + return NULL; } static void -do_thread_tests (const char *uri) +do_async_test_in_thread (gconstpointer uri) { GThread *thread; - debug_printf (1, "\nStreaming in another thread\n"); - - thread = g_thread_new ("do_test_with_context", - do_plain_test_with_context, + thread = g_thread_new ("do_async_test_in_thread", + async_test_thread, (gpointer)uri); g_thread_join (thread); +} - thread = g_thread_new ("do_test_with_context", - do_test_with_context, +static void +do_plain_test_in_thread (gconstpointer uri) +{ + GThread *thread; + + thread = g_thread_new ("do_plain_test_in_thread", + plain_test_thread, (gpointer)uri); g_thread_join (thread); } @@ -514,88 +469,49 @@ do_sync_request (SoupSession *session, SoupRequest *request, in = soup_request_send (request, NULL, &error); g_signal_handler_disconnect (session, started_id); if (cancel) { - if (in) { - debug_printf (1, " send succeeded??\n"); - errors++; - g_input_stream_close (in, NULL, NULL); - g_object_unref (in); - } else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { - debug_printf (1, " send failed with wrong error: %s\n", error->message); - errors++; - } + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED); g_clear_error (&error); g_object_unref (msg); g_object_unref (socket); return; } else if (!in) { - debug_printf (1, " soup_request_send failed: %s\n", - error->message); - g_object_unref (msg); + g_assert_no_error (error); g_clear_error (&error); - g_object_unref (socket); - errors++; - return; - } - - if (msg->status_code != expected_status) { - debug_printf (1, " GET failed: %d %s\n", msg->status_code, - msg->reason_phrase); g_object_unref (msg); - g_object_unref (in); g_object_unref (socket); - errors++; return; } + + soup_test_assert_message_status (msg, expected_status); g_object_unref (msg); body = g_string_new (NULL); do { nread = g_input_stream_read (in, buf, sizeof (buf), NULL, &error); + g_assert_no_error (error); if (nread == -1) { - debug_printf (1, " g_input_stream_read failed: %s\n", - error->message); g_clear_error (&error); - errors++; break; } g_string_append_len (body, buf, nread); } while (nread > 0); - if (!g_input_stream_close (in, NULL, &error)) { - debug_printf (1, " g_input_stream_close failed: %s\n", - error->message); - g_clear_error (&error); - errors++; - } + g_input_stream_close (in, NULL, &error); + g_assert_no_error (error); + g_clear_error (&error); g_object_unref (in); - if (!expected_response) { - if (body->len) { - debug_printf (1, " body length mismatch: expected 0, got %d\n", - (int)body->len); - errors++; - } - } else if (body->len != expected_response->length) { - debug_printf (1, " body length mismatch: expected %d, got %d\n", - (int)expected_response->length, (int)body->len); - errors++; - } else if (memcmp (body->str, expected_response->data, body->len) != 0) { - debug_printf (1, " body data mismatch\n"); - errors++; - } + if (expected_response) { + soup_assert_cmpmem (body->str, body->len, + expected_response->data, expected_response->length); + } else + g_assert_cmpint (body->len, ==, 0); - if (persistent) { - if (!soup_socket_is_connected (socket)) { - debug_printf (1, " socket not still connected!\n"); - errors++; - } - } else { - if (soup_socket_is_connected (socket)) { - debug_printf (1, " socket still connected!\n"); - errors++; - } - } + if (persistent) + g_assert_true (soup_socket_is_connected (socket)); + else + g_assert_false (soup_socket_is_connected (socket)); g_object_unref (socket); g_string_free (body, TRUE); @@ -670,24 +586,26 @@ do_sync_tests_for_session (SoupSession *session, const char *uri_string) } static void -do_sync_tests (const char *uri_string) +do_plain_sync_test (gconstpointer uri) { SoupSession *session; - SoupRequester *requester; - debug_printf (1, "\nSync streaming\n"); - - debug_printf (1, " SoupSession\n"); session = soup_test_session_new (SOUP_TYPE_SESSION, NULL); - do_sync_tests_for_session (session, uri_string); + do_sync_tests_for_session (session, uri); soup_test_session_abort_unref (session); +} + +static void +do_sync_sync_test (gconstpointer uri) +{ + SoupSession *session; + SoupRequester *requester; - debug_printf (1, " SoupSessionSync\n"); session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, NULL); requester = soup_requester_new (); soup_session_add_feature (session, SOUP_SESSION_FEATURE (requester)); g_object_unref (requester); - do_sync_tests_for_session (session, uri_string); + do_sync_tests_for_session (session, uri); soup_test_session_abort_unref (session); } @@ -708,10 +626,8 @@ do_null_char_request (SoupSession *session, const char *encoded_data, request = soup_session_request_uri (session, uri, NULL); stream = soup_test_request_send (request, NULL, 0, &error); - + g_assert_no_error (error); if (error) { - debug_printf (1, " could not send request: %s\n", error->message); - errors++; g_error_free (error); g_object_unref (request); soup_uri_free (uri); @@ -719,27 +635,14 @@ do_null_char_request (SoupSession *session, const char *encoded_data, } g_input_stream_read_all (stream, buf, sizeof (buf), &nread, NULL, &error); - if (error) { - debug_printf (1, " could not read response: %s\n", error->message); - errors++; - g_clear_error (&error); - } + g_assert_no_error (error); + g_clear_error (&error); soup_test_request_close_stream (request, stream, NULL, &error); - if (error) { - debug_printf (1, " could not close stream: %s\n", error->message); - errors++; - g_clear_error (&error); - } + g_assert_no_error (error); + g_clear_error (&error); - if (nread != expected_len) { - debug_printf (1, " response length mismatch: expected %d, got %lu\n", - expected_len, (gulong)nread); - errors++; - } else if (memcmp (buf, expected_data, nread) != 0) { - debug_printf (1, " response data mismatch\n"); - errors++; - } + soup_assert_cmpmem (buf, nread, expected_data, expected_len); g_object_unref (stream); g_object_unref (request); @@ -768,18 +671,20 @@ do_null_char_test_for_session (SoupSession *session) } static void -do_null_char_tests (void) +do_plain_null_char_test (void) { SoupSession *session; - debug_printf (1, "\nStreaming data URLs containing null chars\n"); - - debug_printf (1, " SoupSession\n"); session = soup_test_session_new (SOUP_TYPE_SESSION, NULL); do_null_char_test_for_session (session); soup_test_session_abort_unref (session); +} + +static void +do_async_null_char_test (void) +{ + SoupSession *session; - debug_printf (1, " SoupSessionAsync\n"); session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, NULL); @@ -812,10 +717,8 @@ do_close_test_for_session (SoupSession *session, request = soup_session_request_uri (session, uri, NULL); stream = soup_test_request_send (request, NULL, 0, &error); - + g_assert_no_error (error); if (error) { - debug_printf (1, " could not send request: %s\n", error->message); - errors++; g_error_free (error); g_object_unref (request); return; @@ -823,17 +726,11 @@ do_close_test_for_session (SoupSession *session, start = g_get_monotonic_time (); soup_test_request_close_stream (request, stream, NULL, &error); - if (error) { - debug_printf (1, " could not close stream: %s\n", error->message); - errors++; - g_clear_error (&error); - } + g_assert_no_error (error); + g_clear_error (&error); end = g_get_monotonic_time (); - if (end - start > 500000) { - debug_printf (1, " close() waited for response to complete!\n"); - errors++; - } + g_assert_cmpint (end - start, <=, 500000); g_object_unref (stream); g_object_unref (request); @@ -847,9 +744,8 @@ do_close_test_for_session (SoupSession *session, g_object_unref (msg); stream = soup_test_request_send (request, NULL, 0, &error); + g_assert_no_error (error); if (error) { - debug_printf (1, " could not send request: %s\n", error->message); - errors++; g_error_free (error); g_object_unref (request); return; @@ -858,39 +754,43 @@ do_close_test_for_session (SoupSession *session, cancellable = g_cancellable_new (); g_cancellable_cancel (cancellable); soup_test_request_close_stream (request, stream, cancellable, &error); - if (error && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { - debug_printf (1, " did not get expected error: %s\n", error->message); - errors++; - g_clear_error (&error); - } + if (error) + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED); + g_clear_error (&error); - if (!finished) { - debug_printf (1, " message did not finish!\n"); - errors++; - } + g_assert_true (finished); g_object_unref (stream); g_object_unref (request); } static void -do_close_tests (const char *uri) +do_async_close_test (gconstpointer uri) { SoupSession *session; SoupURI *slow_uri; - debug_printf (1, "\nClosing stream before end should cancel\n"); - slow_uri = soup_uri_new (uri); soup_uri_set_path (slow_uri, "/slow"); - debug_printf (1, " SoupSessionAsync\n"); session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, NULL); do_close_test_for_session (session, slow_uri); soup_test_session_abort_unref (session); + soup_uri_free (slow_uri); +} + +static void +do_sync_close_test (gconstpointer uri) +{ + SoupSession *session; + SoupURI *slow_uri; + + slow_uri = soup_uri_new (uri); + soup_uri_set_path (slow_uri, "/slow"); + debug_printf (1, " SoupSessionSync\n"); session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, @@ -905,6 +805,7 @@ int main (int argc, char **argv) { char *uri; + int ret; test_init (argc, argv, NULL); get_index (); @@ -914,12 +815,20 @@ main (int argc, char **argv) uri = g_strdup_printf ("http://127.0.0.1:%u/foo", soup_server_get_port (server)); - do_simple_tests (uri); - do_thread_tests (uri); - do_context_tests (uri); - do_sync_tests (uri); - do_null_char_tests (); - do_close_tests (uri); + g_test_add_data_func ("/requester/simple/SoupSession", uri, do_simple_plain_test); + g_test_add_data_func ("/requester/simple/SoupSessionAsync", uri, do_simple_async_test); + g_test_add_data_func ("/requester/threaded/SoupSession", uri, do_plain_test_in_thread); + g_test_add_data_func ("/requester/threaded/SoupSessionAsync", uri, do_async_test_in_thread); + g_test_add_data_func ("/requester/context/SoupSession", uri, do_plain_test_with_context); + g_test_add_data_func ("/requester/context/SoupSessionAsync", uri, do_async_test_with_context); + g_test_add_data_func ("/requester/sync/SoupSession", uri, do_plain_sync_test); + g_test_add_data_func ("/requester/sync/SoupSessionSync", uri, do_sync_sync_test); + g_test_add_func ("/requester/null-char/SoupSession", do_plain_null_char_test); + g_test_add_func ("/requester/null-char/SoupSessionAsync", do_async_null_char_test); + g_test_add_data_func ("/requester/close/SoupSessionAsync", uri, do_async_close_test); + g_test_add_data_func ("/requester/close/SoupSessionSync", uri, do_sync_close_test); + + ret = g_test_run (); g_free (uri); soup_buffer_free (response); @@ -927,5 +836,5 @@ main (int argc, char **argv) soup_test_server_quit_unref (server); test_cleanup (); - return errors != 0; + return ret; } |