1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Novell, Inc.
6 #include "test-utils.h"
8 #define SHORT_BODY "This is a test.\r\n"
9 #define LONG_BODY (SHORT_BODY SHORT_BODY)
11 #define MAX_POST_LENGTH (sizeof (SHORT_BODY))
13 static SoupURI *base_uri;
14 static GSList *events;
17 event (SoupMessage *msg, const char *side, const char *message)
19 char *data = g_strdup_printf ("%s-%s", side, message);
20 gboolean record_status =
21 (!strcmp (data, "server-wrote_headers") ||
22 !strcmp (data, "server-wrote_informational"));
24 debug_printf (2, " %s", data);
26 debug_printf (2, " (%s)", msg->reason_phrase);
27 debug_printf (2, "\n");
29 events = g_slist_append (events, data);
31 events = g_slist_append (events, GUINT_TO_POINTER (msg->status_code));
34 #define EVENT_HANDLER(name) \
36 name (SoupMessage *msg, gpointer side) \
38 event (msg, side, #name); \
41 EVENT_HANDLER (got_informational)
42 EVENT_HANDLER (got_headers)
43 EVENT_HANDLER (got_body)
44 EVENT_HANDLER (wrote_informational)
45 EVENT_HANDLER (wrote_headers)
46 EVENT_HANDLER (wrote_body)
47 EVENT_HANDLER (finished)
50 do_message (const char *path, gboolean long_body,
51 gboolean expect_continue, gboolean auth,
59 const char *expected_event;
61 int expected_status, actual_status;
63 uri = soup_uri_copy (base_uri);
65 soup_uri_set_user (uri, "user");
66 soup_uri_set_password (uri, "pass");
68 soup_uri_set_path (uri, path);
69 msg = soup_message_new_from_uri ("POST", uri);
72 body = long_body ? LONG_BODY : SHORT_BODY;
73 soup_message_set_request (msg, "text/plain", SOUP_MEMORY_STATIC,
75 soup_message_headers_append (msg->request_headers, "Connection", "close");
76 if (expect_continue) {
77 soup_message_headers_set_expectations (msg->request_headers,
78 SOUP_EXPECTATION_CONTINUE);
81 g_signal_connect (msg, "got_informational",
82 G_CALLBACK (got_informational), "client");
83 g_signal_connect (msg, "got_headers",
84 G_CALLBACK (got_headers), "client");
85 g_signal_connect (msg, "got_body",
86 G_CALLBACK (got_body), "client");
87 g_signal_connect (msg, "wrote_informational",
88 G_CALLBACK (wrote_informational), "client");
89 g_signal_connect (msg, "wrote_headers",
90 G_CALLBACK (wrote_headers), "client");
91 g_signal_connect (msg, "wrote_body",
92 G_CALLBACK (wrote_body), "client");
93 g_signal_connect (msg, "finished",
94 G_CALLBACK (finished), "client");
97 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
98 soup_session_send_message (session, msg);
99 soup_test_session_abort_unref (session);
102 while ((expected_event = va_arg (ap, const char *))) {
105 soup_test_assert (events != NULL,
106 "Expected '%s', got end of list",
110 actual_event = events->data;
111 g_assert_cmpstr (expected_event, ==, actual_event);
112 events = g_slist_delete_link (events, events);
115 if (!strcmp (expected_event, "server-wrote_headers") ||
116 !strcmp (expected_event, "server-wrote_informational"))
117 expected_status = va_arg (ap, int);
119 expected_status = -1;
120 if (!strcmp (actual_event, "server-wrote_headers") ||
121 !strcmp (actual_event, "server-wrote_informational")) {
122 actual_status = GPOINTER_TO_INT (events->data);
123 events = g_slist_delete_link (events, events);
125 expected_status = -1;
127 if (expected_status != -1 && actual_status != -1 &&
128 expected_status != actual_status) {
129 soup_test_assert (expected_status == actual_status,
130 "Expected status '%s', got '%s'",
131 soup_status_get_phrase (expected_status),
132 soup_status_get_phrase (actual_status));
135 g_free (actual_event);
139 actual_event = events->data;
140 soup_test_assert (events == NULL,
141 "Expected to be done, got '%s'", actual_event);
142 events = g_slist_delete_link (events, events);
144 if (!strcmp (actual_event, "server-wrote_headers") ||
145 !strcmp (actual_event, "server-wrote_informational"))
146 events = g_slist_delete_link (events, events);
148 g_object_unref (msg);
152 do_test_unauth_short_noexpect_nopass (void)
154 do_message ("/unauth", FALSE, FALSE, FALSE,
155 "client-wrote_headers",
157 "server-got_headers",
159 "server-wrote_headers", SOUP_STATUS_CREATED,
162 "client-got_headers",
169 do_test_unauth_long_noexpect_nopass (void)
171 do_message ("/unauth", TRUE, FALSE, FALSE,
172 "client-wrote_headers",
174 "server-got_headers",
176 "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
179 "client-got_headers",
186 do_test_unauth_short_expect_nopass (void)
188 do_message ("/unauth", FALSE, TRUE, FALSE,
189 "client-wrote_headers",
190 "server-got_headers",
191 "server-wrote_informational", SOUP_STATUS_CONTINUE,
192 "client-got_informational",
195 "server-wrote_headers", SOUP_STATUS_CREATED,
198 "client-got_headers",
205 do_test_unauth_long_expect_nopass (void)
207 do_message ("/unauth", TRUE, TRUE, FALSE,
208 "client-wrote_headers",
209 "server-got_headers",
210 "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
213 "client-got_headers",
220 do_test_auth_short_noexpect_nopass (void)
222 do_message ("/auth", FALSE, FALSE, FALSE,
223 "client-wrote_headers",
225 "server-got_headers",
227 "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
230 "client-got_headers",
237 do_test_auth_long_noexpect_nopass (void)
239 do_message ("/auth", TRUE, FALSE, FALSE,
240 "client-wrote_headers",
242 "server-got_headers",
244 "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
247 "client-got_headers",
254 do_test_auth_short_expect_nopass (void)
256 do_message ("/auth", FALSE, TRUE, FALSE,
257 "client-wrote_headers",
258 "server-got_headers",
259 "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
262 "client-got_headers",
269 do_test_auth_long_expect_nopass (void)
271 do_message ("/auth", TRUE, TRUE, FALSE,
272 "client-wrote_headers",
273 "server-got_headers",
274 "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
277 "client-got_headers",
284 do_test_auth_short_noexpect_pass (void)
286 do_message ("/auth", FALSE, FALSE, TRUE,
287 "client-wrote_headers",
289 "server-got_headers",
291 "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
294 "client-got_headers",
296 "client-wrote_headers",
298 "server-got_headers",
300 "server-wrote_headers", SOUP_STATUS_CREATED,
303 "client-got_headers",
310 do_test_auth_long_noexpect_pass (void)
312 do_message ("/auth", TRUE, FALSE, TRUE,
313 "client-wrote_headers",
315 "server-got_headers",
317 "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
320 "client-got_headers",
322 "client-wrote_headers",
324 "server-got_headers",
326 "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
329 "client-got_headers",
336 do_test_auth_short_expect_pass (void)
338 do_message ("/auth", FALSE, TRUE, TRUE,
339 "client-wrote_headers",
340 "server-got_headers",
341 "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
344 "client-got_headers",
346 "client-wrote_headers",
347 "server-got_headers",
348 "server-wrote_informational", SOUP_STATUS_CONTINUE,
349 "client-got_informational",
352 "server-wrote_headers", SOUP_STATUS_CREATED,
355 "client-got_headers",
362 do_test_auth_long_expect_pass (void)
364 do_message ("/auth", TRUE, TRUE, TRUE,
365 "client-wrote_headers",
366 "server-got_headers",
367 "server-wrote_headers", SOUP_STATUS_UNAUTHORIZED,
370 "client-got_headers",
372 "client-wrote_headers",
373 "server-got_headers",
374 "server-wrote_headers", SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,
377 "client-got_headers",
387 server_got_headers (SoupMessage *msg, gpointer server)
390 if (msg->status_code != SOUP_STATUS_CONTINUE &&
391 msg->status_code != 0)
394 if (soup_message_headers_get_expectations (msg->request_headers) &
395 SOUP_EXPECTATION_CONTINUE) {
398 length = soup_message_headers_get_one (msg->request_headers,
400 if (length && atoi (length) > MAX_POST_LENGTH) {
401 soup_message_set_status (msg, SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE);
402 soup_message_headers_append (msg->response_headers, "Connection", "close");
408 request_started (SoupServer *server, SoupMessage *msg,
409 SoupClientContext *client, gpointer user_data)
411 g_signal_connect (msg, "got_headers",
412 G_CALLBACK (server_got_headers), server);
414 g_signal_connect (msg, "got_informational",
415 G_CALLBACK (got_informational), "server");
416 g_signal_connect (msg, "got_headers",
417 G_CALLBACK (got_headers), "server");
418 g_signal_connect (msg, "got_body",
419 G_CALLBACK (got_body), "server");
420 g_signal_connect (msg, "wrote_informational",
421 G_CALLBACK (wrote_informational), "server");
422 g_signal_connect (msg, "wrote_headers",
423 G_CALLBACK (wrote_headers), "server");
424 g_signal_connect (msg, "wrote_body",
425 G_CALLBACK (wrote_body), "server");
426 g_signal_connect (msg, "finished",
427 G_CALLBACK (finished), "server");
431 auth_callback (SoupAuthDomain *auth_domain, SoupMessage *msg,
432 const char *username, const char *password, gpointer user_data)
434 return !strcmp (username, "user") && !strcmp (password, "pass");
438 server_callback (SoupServer *server, SoupMessage *msg,
439 const char *path, GHashTable *query,
440 SoupClientContext *context, gpointer data)
442 if (msg->method != SOUP_METHOD_POST) {
443 soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
444 soup_message_headers_append (msg->response_headers, "Connection", "close");
445 } else if (msg->request_body->length > MAX_POST_LENGTH) {
446 soup_message_set_status (msg, SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE);
447 soup_message_headers_append (msg->response_headers, "Connection", "close");
449 soup_message_set_status (msg, SOUP_STATUS_CREATED);
456 SoupAuthDomain *auth_domain;
458 server = soup_test_server_new (SOUP_TEST_SERVER_DEFAULT);
460 g_signal_connect (server, "request-started",
461 G_CALLBACK (request_started), NULL);
463 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
465 auth_domain = soup_auth_domain_basic_new (
466 SOUP_AUTH_DOMAIN_REALM, "continue-test",
467 SOUP_AUTH_DOMAIN_ADD_PATH, "/auth",
468 SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK, auth_callback,
470 soup_server_add_auth_domain (server, auth_domain);
471 g_object_unref (auth_domain);
479 main (int argc, char **argv)
484 test_init (argc, argv, NULL);
486 server = setup_server ();
487 base_uri = soup_test_server_get_uri (server, "http", NULL);
489 g_test_add_func ("/continue/unauth_short_noexpect_nopass", do_test_unauth_short_noexpect_nopass);
490 g_test_add_func ("/continue/unauth_long_noexpect_nopass", do_test_unauth_long_noexpect_nopass);
491 g_test_add_func ("/continue/unauth_short_expect_nopass", do_test_unauth_short_expect_nopass);
492 g_test_add_func ("/continue/unauth_long_expect_nopass", do_test_unauth_long_expect_nopass);
493 g_test_add_func ("/continue/auth_short_noexpect_nopass", do_test_auth_short_noexpect_nopass);
494 g_test_add_func ("/continue/auth_long_noexpect_nopass", do_test_auth_long_noexpect_nopass);
495 g_test_add_func ("/continue/auth_short_expect_nopass", do_test_auth_short_expect_nopass);
496 g_test_add_func ("/continue/auth_long_expect_nopass", do_test_auth_long_expect_nopass);
497 g_test_add_func ("/continue/auth_short_noexpect_pass", do_test_auth_short_noexpect_pass);
498 g_test_add_func ("/continue/auth_long_noexpect_pass", do_test_auth_long_noexpect_pass);
499 g_test_add_func ("/continue/auth_short_expect_pass", do_test_auth_short_expect_pass);
500 g_test_add_func ("/continue/auth_long_expect_pass", do_test_auth_long_expect_pass);
504 soup_test_server_quit_unref (server);
505 soup_uri_free (base_uri);