diff options
Diffstat (limited to 'tests/requester-test.c')
-rw-r--r-- | tests/requester-test.c | 152 |
1 files changed, 102 insertions, 50 deletions
diff --git a/tests/requester-test.c b/tests/requester-test.c index 39b30bd5..6d6d5728 100644 --- a/tests/requester-test.c +++ b/tests/requester-test.c @@ -17,6 +17,12 @@ 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" +typedef enum { + NO_CANCEL, + SYNC_CANCEL, + PAUSE_AND_CANCEL_ON_IDLE +} CancelPolicy; + static gboolean slow_finish_message (gpointer msg) { @@ -205,6 +211,32 @@ cancel_message (SoupMessage *msg, gpointer session) soup_session_cancel_message (session, msg, SOUP_STATUS_FORBIDDEN); } +typedef struct { + SoupMessage *msg; + SoupSession *session; +} CancelData; + +static gboolean +cancel_message_idle (CancelData *data) +{ + cancel_message (data->msg, data->session); + return FALSE; +} + +static void +pause_and_cancel_message (SoupMessage *msg, gpointer session) +{ + CancelData *data = g_new (CancelData, 1); + GSource *source = g_idle_source_new (); + + soup_session_pause_message (session, msg); + data->msg = msg; + data->session = session; + g_source_set_callback (source, (GSourceFunc)cancel_message_idle, data, g_free); + g_source_attach (source, soup_session_get_async_context (session)); + g_source_unref (source); +} + static void request_started (SoupSession *session, SoupMessage *msg, SoupSocket *socket, gpointer user_data) @@ -219,7 +251,7 @@ static void do_async_test (SoupSession *session, SoupURI *uri, GAsyncReadyCallback callback, guint expected_status, SoupBuffer *expected_response, - gboolean persistent, gboolean cancel) + gboolean persistent, CancelPolicy cancel_policy) { SoupRequester *requester; SoupRequest *request; @@ -234,16 +266,24 @@ do_async_test (SoupSession *session, SoupURI *uri, requester = NULL; data.body = g_string_new (NULL); - data.cancel = cancel; + data.cancel = cancel_policy != NO_CANCEL; if (requester) request = soup_requester_request_uri (requester, uri, NULL); else request = soup_session_request_uri (session, uri, NULL); msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (request)); - if (cancel) { + switch (cancel_policy) { + case SYNC_CANCEL: g_signal_connect (msg, "got-headers", G_CALLBACK (cancel_message), session); + break; + case PAUSE_AND_CANCEL_ON_IDLE: + g_signal_connect (msg, "got-headers", + G_CALLBACK (pause_and_cancel_message), session); + break; + case NO_CANCEL: + break; } started_id = g_signal_connect (session, "request-started", @@ -279,7 +319,7 @@ do_async_test (SoupSession *session, SoupURI *uri, } static void -do_test_for_thread_and_context (SoupSession *session, const char *base_uri) +do_test_for_thread_and_context (SoupSession *session, SoupURI *base_uri) { SoupRequester *requester; SoupURI *uri; @@ -292,48 +332,50 @@ do_test_for_thread_and_context (SoupSession *session, const char *base_uri) soup_session_add_feature_by_type (session, SOUP_TYPE_CONTENT_SNIFFER); debug_printf (1, " basic test\n"); - uri = soup_uri_new (base_uri); - do_async_test (session, uri, test_sent, + do_async_test (session, base_uri, test_sent, SOUP_STATUS_OK, response, - TRUE, FALSE); - soup_uri_free (uri); + TRUE, NO_CANCEL); debug_printf (1, " chunked test\n"); - uri = soup_uri_new (base_uri); - soup_uri_set_path (uri, "/chunked"); + uri = soup_uri_new_with_base (base_uri, "/chunked"); do_async_test (session, uri, test_sent, SOUP_STATUS_OK, response, - TRUE, FALSE); + TRUE, NO_CANCEL); soup_uri_free (uri); debug_printf (1, " auth test\n"); - uri = soup_uri_new (base_uri); - soup_uri_set_path (uri, "/auth"); + uri = soup_uri_new_with_base (base_uri, "/auth"); do_async_test (session, uri, auth_test_sent, SOUP_STATUS_UNAUTHORIZED, auth_response, - TRUE, FALSE); + TRUE, NO_CANCEL); soup_uri_free (uri); debug_printf (1, " non-persistent test\n"); - uri = soup_uri_new (base_uri); - soup_uri_set_path (uri, "/non-persistent"); + uri = soup_uri_new_with_base (base_uri, "/non-persistent"); do_async_test (session, uri, test_sent, SOUP_STATUS_OK, response, - FALSE, FALSE); + FALSE, NO_CANCEL); soup_uri_free (uri); debug_printf (1, " cancellation test\n"); - uri = soup_uri_new (base_uri); - soup_uri_set_path (uri, "/"); + uri = soup_uri_new_with_base (base_uri, "/"); do_async_test (session, uri, test_sent, SOUP_STATUS_FORBIDDEN, NULL, - FALSE, TRUE); + FALSE, SYNC_CANCEL); + soup_uri_free (uri); + + debug_printf (1, " cancellation after paused test\n"); + uri = soup_uri_new_with_base (base_uri, "/"); + do_async_test (session, uri, test_sent, + SOUP_STATUS_FORBIDDEN, NULL, + FALSE, PAUSE_AND_CANCEL_ON_IDLE); soup_uri_free (uri); } static void -do_simple_plain_test (gconstpointer uri) +do_simple_plain_test (gconstpointer data) { + SoupURI *uri = (SoupURI *)data; SoupSession *session; g_test_bug ("653707"); @@ -344,8 +386,9 @@ do_simple_plain_test (gconstpointer uri) } static void -do_simple_async_test (gconstpointer uri) +do_simple_async_test (gconstpointer data) { + SoupURI *uri = (SoupURI *)data; SoupSession *session; g_test_bug ("653707"); @@ -358,7 +401,7 @@ do_simple_async_test (gconstpointer uri) } static void -do_test_with_context_and_type (const char *uri, gboolean plain_session) +do_test_with_context_and_type (SoupURI *uri, gboolean plain_session) { GMainContext *async_context; SoupSession *session; @@ -381,14 +424,18 @@ do_test_with_context_and_type (const char *uri, gboolean plain_session) } static void -do_async_test_with_context (gconstpointer uri) +do_async_test_with_context (gconstpointer data) { + SoupURI *uri = (SoupURI *)data; + do_test_with_context_and_type (uri, FALSE); } static void -do_plain_test_with_context (gconstpointer uri) +do_plain_test_with_context (gconstpointer data) { + SoupURI *uri = (SoupURI *)data; + do_test_with_context_and_type (uri, TRUE); } @@ -407,8 +454,9 @@ plain_test_thread (gpointer uri) } static void -do_async_test_in_thread (gconstpointer uri) +do_async_test_in_thread (gconstpointer data) { + SoupURI *uri = (SoupURI *)data; GThread *thread; thread = g_thread_new ("do_async_test_in_thread", @@ -418,8 +466,9 @@ do_async_test_in_thread (gconstpointer uri) } static void -do_plain_test_in_thread (gconstpointer uri) +do_plain_test_in_thread (gconstpointer data) { + SoupURI *uri = (SoupURI *)data; GThread *thread; thread = g_thread_new ("do_plain_test_in_thread", @@ -431,7 +480,7 @@ do_plain_test_in_thread (gconstpointer uri) static void do_sync_request (SoupSession *session, SoupRequest *request, guint expected_status, SoupBuffer *expected_response, - gboolean persistent, gboolean cancel) + gboolean persistent, CancelPolicy cancel_policy) { GInputStream *in; SoupMessage *msg; @@ -443,7 +492,7 @@ do_sync_request (SoupSession *session, SoupRequest *request, SoupSocket *socket = NULL; msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (request)); - if (cancel) { + if (cancel_policy == SYNC_CANCEL) { g_signal_connect (msg, "got-headers", G_CALLBACK (cancel_message), session); } @@ -454,7 +503,7 @@ do_sync_request (SoupSession *session, SoupRequest *request, in = soup_request_send (request, NULL, &error); g_signal_handler_disconnect (session, started_id); - if (cancel) { + if (cancel_policy == SYNC_CANCEL) { g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED); g_clear_error (&error); g_object_unref (msg); @@ -504,7 +553,7 @@ do_sync_request (SoupSession *session, SoupRequest *request, } static void -do_sync_tests_for_session (SoupSession *session, const char *uri_string) +do_sync_tests_for_session (SoupSession *session, SoupURI *base_uri) { SoupRequester *requester; SoupRequest *request; @@ -512,7 +561,7 @@ do_sync_tests_for_session (SoupSession *session, const char *uri_string) requester = SOUP_REQUESTER (soup_session_get_feature (session, SOUP_TYPE_REQUESTER)); - uri = soup_uri_new (uri_string); + uri = soup_uri_copy (base_uri); debug_printf (1, " basic test\n"); if (requester) @@ -521,7 +570,7 @@ do_sync_tests_for_session (SoupSession *session, const char *uri_string) request = soup_session_request_uri (session, uri, NULL); do_sync_request (session, request, SOUP_STATUS_OK, response, - TRUE, FALSE); + TRUE, NO_CANCEL); g_object_unref (request); debug_printf (1, " chunked test\n"); @@ -532,7 +581,7 @@ do_sync_tests_for_session (SoupSession *session, const char *uri_string) request = soup_session_request_uri (session, uri, NULL); do_sync_request (session, request, SOUP_STATUS_OK, response, - TRUE, FALSE); + TRUE, NO_CANCEL); g_object_unref (request); debug_printf (1, " auth test\n"); @@ -543,7 +592,7 @@ do_sync_tests_for_session (SoupSession *session, const char *uri_string) request = soup_session_request_uri (session, uri, NULL); do_sync_request (session, request, SOUP_STATUS_UNAUTHORIZED, auth_response, - TRUE, FALSE); + TRUE, NO_CANCEL); g_object_unref (request); debug_printf (1, " non-persistent test\n"); @@ -554,7 +603,7 @@ do_sync_tests_for_session (SoupSession *session, const char *uri_string) request = soup_session_request_uri (session, uri, NULL); do_sync_request (session, request, SOUP_STATUS_OK, response, - FALSE, FALSE); + FALSE, NO_CANCEL); g_object_unref (request); debug_printf (1, " cancel test\n"); @@ -565,15 +614,16 @@ do_sync_tests_for_session (SoupSession *session, const char *uri_string) request = soup_session_request_uri (session, uri, NULL); do_sync_request (session, request, SOUP_STATUS_FORBIDDEN, NULL, - TRUE, TRUE); + TRUE, SYNC_CANCEL); g_object_unref (request); soup_uri_free (uri); } static void -do_plain_sync_test (gconstpointer uri) +do_plain_sync_test (gconstpointer data) { + SoupURI *uri = (SoupURI *)data; SoupSession *session; session = soup_test_session_new (SOUP_TYPE_SESSION, NULL); @@ -582,8 +632,9 @@ do_plain_sync_test (gconstpointer uri) } static void -do_sync_sync_test (gconstpointer uri) +do_sync_sync_test (gconstpointer data) { + SoupURI *uri = (SoupURI *)data; SoupSession *session; SoupRequester *requester; @@ -743,6 +794,7 @@ do_close_test_for_session (SoupSession *session, if (error) g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED); g_clear_error (&error); + g_object_unref (cancellable); g_assert_true (finished); @@ -751,16 +803,16 @@ do_close_test_for_session (SoupSession *session, } static void -do_async_close_test (gconstpointer uri) +do_async_close_test (gconstpointer data) { + SoupURI *uri = (SoupURI *)data; SoupSession *session; SoupURI *slow_uri; g_test_bug ("695652"); g_test_bug ("711260"); - slow_uri = soup_uri_new (uri); - soup_uri_set_path (slow_uri, "/slow"); + slow_uri = soup_uri_new_with_base ((SoupURI *)uri, "/slow"); session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, @@ -772,18 +824,17 @@ do_async_close_test (gconstpointer uri) } static void -do_sync_close_test (gconstpointer uri) +do_sync_close_test (gconstpointer data) { + SoupURI *uri = (SoupURI *)data; SoupSession *session; SoupURI *slow_uri; g_test_bug ("695652"); g_test_bug ("711260"); - slow_uri = soup_uri_new (uri); - soup_uri_set_path (slow_uri, "/slow"); + slow_uri = soup_uri_new_with_base ((SoupURI *)uri, "/slow"); - debug_printf (1, " SoupSessionSync\n"); session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, NULL); @@ -796,7 +847,7 @@ do_sync_close_test (gconstpointer uri) int main (int argc, char **argv) { - char *uri; + SoupURI *uri; int ret; test_init (argc, argv, NULL); @@ -806,10 +857,11 @@ main (int argc, char **argv) AUTH_HTML_BODY, strlen (AUTH_HTML_BODY)); - server = soup_test_server_new (TRUE); + server = soup_test_server_new (SOUP_TEST_SERVER_IN_THREAD); soup_server_add_handler (server, NULL, server_callback, NULL, NULL); - uri = g_strdup_printf ("http://127.0.0.1:%u/foo", soup_server_get_port (server)); + uri = soup_test_server_get_uri (server, "http", NULL); + soup_uri_set_path (uri, "/foo"); 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); @@ -826,7 +878,7 @@ main (int argc, char **argv) ret = g_test_run (); - g_free (uri); + soup_uri_free (uri); soup_buffer_free (auth_response); soup_test_server_quit_unref (server); |