1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008 Red Hat, Inc.
13 #include <libsoup/soup.h>
15 #include "test-utils.h"
27 TestRequest requests[3];
30 /* A redirecty response to a GET or HEAD should cause a redirect */
32 { { { "GET", "/301", 301 },
35 { { { "GET", "/302", 302 },
38 { { { "GET", "/303", 303 },
41 { { { "GET", "/307", 307 },
44 { { { "HEAD", "/301", 301 },
47 { { { "HEAD", "/302", 302 },
50 /* 303 is a nonsensical response to HEAD, but some sites do
53 { { { "HEAD", "/303", 303 },
56 { { { "HEAD", "/307", 307 },
60 /* A non-redirecty response to a GET or HEAD should not */
62 { { { "GET", "/300", 300 },
64 { { { "GET", "/304", 304 },
66 { { { "GET", "/305", 305 },
68 { { { "GET", "/306", 306 },
70 { { { "GET", "/308", 308 },
72 { { { "HEAD", "/300", 300 },
74 { { { "HEAD", "/304", 304 },
76 { { { "HEAD", "/305", 305 },
78 { { { "HEAD", "/306", 306 },
80 { { { "HEAD", "/308", 308 },
83 /* Test double-redirect */
85 { { { "GET", "/301/302", 301 },
86 { "GET", "/302", 302 },
87 { "GET", "/", 200 } }, 200 },
88 { { { "HEAD", "/301/302", 301 },
89 { "HEAD", "/302", 302 },
90 { "HEAD", "/", 200 } }, 200 },
92 /* POST should only automatically redirect on 301, 302 and 303 */
94 { { { "POST", "/301", 301 },
97 { { { "POST", "/302", 302 },
100 { { { "POST", "/303", 303 },
103 { { { "POST", "/307", 307 },
106 /* Test behavior with recoverably-bad Location header */
107 { { { "GET", "/bad", 302 },
108 { "GET", "/bad%20with%20spaces", 200 },
111 /* Test behavior with irrecoverably-bad Location header */
112 { { { "GET", "/bad-no-host", 302 },
113 { NULL } }, SOUP_STATUS_MALFORMED },
115 /* Test infinite redirection */
116 { { { "GET", "/bad-recursive", 302, TRUE },
117 { NULL } }, SOUP_STATUS_TOO_MANY_REDIRECTS },
119 /* Test redirection to a different server */
120 { { { "GET", "/server2", 302 },
121 { "GET", "/on-server2", 200 },
124 static const int n_tests = G_N_ELEMENTS (tests);
127 got_headers (SoupMessage *msg, gpointer user_data)
129 TestRequest **req = user_data;
130 const char *location;
132 debug_printf (2, " -> %d %s\n", msg->status_code,
134 location = soup_message_headers_get_one (msg->response_headers,
137 debug_printf (2, " Location: %s\n", location);
142 if (msg->status_code != (*req)->status_code) {
143 debug_printf (1, " - Expected %d !\n",
144 (*req)->status_code);
150 restarted (SoupMessage *msg, gpointer user_data)
152 TestRequest **req = user_data;
153 SoupURI *uri = soup_message_get_uri (msg);
155 debug_printf (2, " %s %s\n", msg->method, uri->path);
157 if ((*req)->method && !(*req)->repeat)
160 if (!(*req)->method) {
161 debug_printf (1, " - Expected to be done!\n");
166 if (strcmp (msg->method, (*req)->method) != 0) {
167 debug_printf (1, " - Expected %s !\n", (*req)->method);
170 if (strcmp (uri->path, (*req)->path) != 0) {
171 debug_printf (1, " - Expected %s !\n", (*req)->path);
177 do_test (SoupSession *session, SoupURI *base_uri, int n)
183 debug_printf (1, "%2d. %s %s\n", n + 1,
184 tests[n].requests[0].method,
185 tests[n].requests[0].path);
187 uri = soup_uri_new_with_base (base_uri, tests[n].requests[0].path);
188 msg = soup_message_new_from_uri (tests[n].requests[0].method, uri);
191 if (msg->method == SOUP_METHOD_POST) {
192 soup_message_set_request (msg, "text/plain",
195 strlen ("post body"));
198 req = &tests[n].requests[0];
199 g_signal_connect (msg, "got_headers",
200 G_CALLBACK (got_headers), &req);
201 g_signal_connect (msg, "restarted",
202 G_CALLBACK (restarted), &req);
204 soup_session_send_message (session, msg);
206 if (msg->status_code != tests[n].final_status) {
207 debug_printf (1, " - Expected final status of %d, got %d !\n",
208 tests[n].final_status, msg->status_code);
212 g_object_unref (msg);
213 debug_printf (2, "\n");
217 do_redirect_tests (SoupURI *base_uri)
219 SoupSession *session;
222 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
223 debug_printf (1, "Async session\n");
224 for (n = 0; n < n_tests; n++)
225 do_test (session, base_uri, n);
226 soup_test_session_abort_unref (session);
228 session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, NULL);
229 debug_printf (1, "Sync session\n");
230 for (n = 0; n < n_tests; n++)
231 do_test (session, base_uri, n);
232 soup_test_session_abort_unref (session);
236 server_callback (SoupServer *server, SoupMessage *msg,
237 const char *path, GHashTable *query,
238 SoupClientContext *context, gpointer data)
243 if (g_str_has_prefix (path, "/bad")) {
244 if (!strcmp (path, "/bad")) {
245 soup_message_set_status (msg, SOUP_STATUS_FOUND);
246 soup_message_headers_replace (msg->response_headers,
249 } else if (!strcmp (path, "/bad-recursive")) {
250 soup_message_set_status (msg, SOUP_STATUS_FOUND);
251 soup_message_headers_replace (msg->response_headers,
254 } else if (!strcmp (path, "/bad-no-host")) {
255 soup_message_set_status (msg, SOUP_STATUS_FOUND);
256 soup_message_headers_replace (msg->response_headers,
259 } else if (!strcmp (path, "/bad with spaces"))
260 soup_message_set_status (msg, SOUP_STATUS_OK);
262 soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
264 } else if (!strcmp (path, "/server2")) {
265 soup_message_set_status (msg, SOUP_STATUS_FOUND);
266 soup_message_headers_replace (msg->response_headers,
270 } else if (!strcmp (path, "/")) {
271 if (msg->method != SOUP_METHOD_GET &&
272 msg->method != SOUP_METHOD_HEAD) {
273 soup_message_set_status (msg, SOUP_STATUS_METHOD_NOT_ALLOWED);
277 /* Make sure that redirecting a POST clears the body */
278 if (msg->request_body->length) {
279 soup_message_set_status (msg, SOUP_STATUS_BAD_REQUEST);
283 /* Make sure that a HTTP/1.0 redirect doesn't cause an
284 * HTTP/1.0 re-request. (#521848)
286 if (soup_message_get_http_version (msg) == SOUP_HTTP_1_0) {
287 soup_message_set_status (msg, SOUP_STATUS_BAD_REQUEST);
291 soup_message_set_status (msg, SOUP_STATUS_OK);
293 /* FIXME: this is wrong, though it doesn't matter for
294 * the purposes of this test, and to do the right
295 * thing currently we'd have to set Content-Length by
298 if (msg->method != SOUP_METHOD_HEAD) {
299 soup_message_set_response (msg, "text/plain",
306 status_code = strtoul (path + 1, &remainder, 10);
307 if (!SOUP_STATUS_IS_REDIRECTION (status_code) ||
308 (*remainder && *remainder != '/')) {
309 soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
313 /* See above comment re bug 521848. */
314 soup_message_set_http_version (msg, SOUP_HTTP_1_0);
316 soup_message_set_status (msg, status_code);
318 soup_message_headers_replace (msg->response_headers,
319 "Location", remainder);
321 soup_message_headers_replace (msg->response_headers,
327 server2_callback (SoupServer *server, SoupMessage *msg,
328 const char *path, GHashTable *query,
329 SoupClientContext *context, gpointer data)
331 soup_message_set_status (msg, SOUP_STATUS_OK);
334 static gboolean run_tests = TRUE;
336 static GOptionEntry no_test_entry[] = {
337 { "no-tests", 'n', G_OPTION_FLAG_REVERSE,
338 G_OPTION_ARG_NONE, &run_tests,
339 "Don't run tests, just run the test server", NULL },
344 main (int argc, char **argv)
347 SoupServer *server, *server2;
351 test_init (argc, argv, no_test_entry);
353 server = soup_test_server_new (TRUE);
354 soup_server_add_handler (server, NULL,
355 server_callback, NULL, NULL);
356 port = soup_server_get_port (server);
358 server2 = soup_test_server_new (TRUE);
359 soup_server_add_handler (server2, NULL,
360 server2_callback, NULL, NULL);
361 server2_uri = g_strdup_printf ("http://127.0.0.1:%d/on-server2",
362 soup_server_get_port (server2));
364 loop = g_main_loop_new (NULL, TRUE);
367 base_uri = soup_uri_new ("http://127.0.0.1");
368 soup_uri_set_port (base_uri, port);
369 do_redirect_tests (base_uri);
370 soup_uri_free (base_uri);
372 printf ("Listening on port %d\n", port);
373 g_main_loop_run (loop);
376 g_main_loop_unref (loop);
377 g_free (server2_uri);
378 soup_test_server_quit_unref (server);
379 soup_test_server_quit_unref (server2);