11 #include "libsoup/soup.h"
13 #include "test-utils.h"
16 do_message_to_session (SoupSession *session, const char *uri,
17 const char *comment, guint expected_status)
21 debug_printf (1, " %s\n", comment);
22 msg = soup_message_new ("GET", uri);
23 soup_session_send_message (session, msg);
25 if (msg->status_code != expected_status) {
26 debug_printf (1, " FAILED: %d %s (expected %d %s)\n",
27 msg->status_code, msg->reason_phrase,
29 soup_status_get_phrase (expected_status));
33 if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code) &&
34 !soup_message_is_keepalive (msg)) {
35 debug_printf (1, " ERROR: message is not keepalive!");
43 request_started_cb (SoupSession *session, SoupMessage *msg,
44 SoupSocket *socket, gpointer user_data)
46 SoupSocket **ret = user_data;
52 do_tests_for_session (SoupSession *timeout_session,
53 SoupSession *idle_session,
54 SoupSession *plain_session,
55 char *fast_uri, char *slow_uri)
57 SoupSocket *ret, *idle_first, *idle_second;
58 SoupSocket *plain_first, *plain_second;
61 g_signal_connect (idle_session, "request-started",
62 G_CALLBACK (request_started_cb), &ret);
63 do_message_to_session (idle_session, fast_uri, "fast to idle", SOUP_STATUS_OK);
68 g_signal_connect (plain_session, "request-started",
69 G_CALLBACK (request_started_cb), &ret);
70 do_message_to_session (plain_session, fast_uri, "fast to plain", SOUP_STATUS_OK);
74 do_message_to_session (timeout_session, fast_uri, "fast to timeout", SOUP_STATUS_OK);
75 do_message_to_session (timeout_session, slow_uri, "slow to timeout", SOUP_STATUS_IO_ERROR);
78 do_message_to_session (idle_session, fast_uri, "fast to idle", SOUP_STATUS_OK);
80 g_signal_handlers_disconnect_by_func (idle_session,
81 (gpointer)request_started_cb,
84 if (idle_first == idle_second) {
85 debug_printf (1, " ERROR: idle_session did not close first connection\n");
91 do_message_to_session (plain_session, fast_uri, "fast to plain", SOUP_STATUS_OK);
93 g_signal_handlers_disconnect_by_func (plain_session,
94 (gpointer)request_started_cb,
97 if (plain_first != plain_second) {
98 debug_printf (1, " ERROR: plain_session closed connection\n");
105 do_timeout_tests (char *fast_uri, char *slow_uri)
107 SoupSession *timeout_session, *idle_session, *plain_session;
109 debug_printf (1, " async\n");
110 timeout_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
111 SOUP_SESSION_TIMEOUT, 1,
113 idle_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
114 SOUP_SESSION_IDLE_TIMEOUT, 1,
116 /* The "plain" session also has an idle timeout, but it's longer
117 * than the test takes, so for our purposes it should behave like
120 plain_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
121 SOUP_SESSION_IDLE_TIMEOUT, 2,
123 do_tests_for_session (timeout_session, idle_session, plain_session,
125 soup_test_session_abort_unref (timeout_session);
126 soup_test_session_abort_unref (idle_session);
127 soup_test_session_abort_unref (plain_session);
129 debug_printf (1, " sync\n");
130 timeout_session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC,
131 SOUP_SESSION_TIMEOUT, 1,
133 /* SOUP_SESSION_TIMEOUT doesn't work with sync sessions */
134 plain_session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC,
136 do_tests_for_session (timeout_session, NULL, plain_session, fast_uri, slow_uri);
137 soup_test_session_abort_unref (timeout_session);
138 soup_test_session_abort_unref (plain_session);
142 timeout_finish_message (gpointer msg)
144 SoupServer *server = g_object_get_data (G_OBJECT (msg), "server");
146 soup_server_unpause_message (server, msg);
151 server_handler (SoupServer *server,
155 SoupClientContext *client,
158 soup_message_set_status (msg, SOUP_STATUS_OK);
159 soup_message_set_response (msg, "text/plain",
163 if (!strcmp (path, "/slow")) {
164 soup_server_pause_message (server, msg);
165 g_object_set_data (G_OBJECT (msg), "server", server);
166 soup_add_timeout (soup_server_get_async_context (server),
167 1100, timeout_finish_message, msg);
172 main (int argc, char **argv)
175 char *fast_uri, *slow_uri;
177 test_init (argc, argv, NULL);
179 debug_printf (1, "http\n");
180 server = soup_test_server_new (TRUE);
181 soup_server_add_handler (server, NULL, server_handler, NULL, NULL);
182 fast_uri = g_strdup_printf ("http://127.0.0.1:%u/",
183 soup_server_get_port (server));
184 slow_uri = g_strdup_printf ("http://127.0.0.1:%u/slow",
185 soup_server_get_port (server));
186 do_timeout_tests (fast_uri, slow_uri);
189 soup_test_server_quit_unref (server);
192 debug_printf (1, "\nhttps\n");
193 server = soup_test_server_new_ssl (TRUE);
194 soup_server_add_handler (server, NULL, server_handler, NULL, NULL);
195 fast_uri = g_strdup_printf ("https://127.0.0.1:%u/",
196 soup_server_get_port (server));
197 slow_uri = g_strdup_printf ("https://127.0.0.1:%u/slow",
198 soup_server_get_port (server));
199 do_timeout_tests (fast_uri, slow_uri);
202 soup_test_server_quit_unref (server);