1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2011 Red Hat, Inc.
10 #define LIBSOUP_USE_UNSTABLE_REQUEST_API
11 #include <libsoup/soup.h>
12 #include <libsoup/soup-requester.h>
13 #include <libsoup/soup-request-http.h>
15 #include "test-utils.h"
30 if (!g_file_get_contents (SRCDIR "/index.txt", &contents, &length, &error)) {
31 fprintf (stderr, "Could not read index.txt: %s\n",
36 response = soup_buffer_new (SOUP_MEMORY_TAKE, contents, length);
39 #define REDIRECT_HTML_BODY "<html><body>Try again</body></html>\r\n"
42 server_callback (SoupServer *server, SoupMessage *msg,
43 const char *path, GHashTable *query,
44 SoupClientContext *context, gpointer data)
46 if (strcmp (path, "/") != 0) {
47 soup_message_set_redirect (msg, SOUP_STATUS_FOUND, "/");
48 /* Make the response HTML so if we sniff that instead of the
49 * real body, we'll notice.
51 soup_message_set_response (msg, "text/html",
54 strlen (REDIRECT_HTML_BODY));
58 soup_message_set_status (msg, SOUP_STATUS_OK);
59 soup_message_body_append_buffer (msg->response_body, response);
63 test_read_ready (GObject *source, GAsyncResult *res, gpointer user_data)
65 GInputStream *stream = G_INPUT_STREAM (source);
66 GString *body = user_data;
70 nread = g_input_stream_read_finish (stream, res, &error);
72 debug_printf (1, " read_async failed: %s", error->message);
74 g_object_unref (stream);
75 g_main_loop_quit (loop);
77 } else if (nread == 0) {
78 g_object_unref (stream);
79 g_main_loop_quit (loop);
83 g_string_append_len (body, buf, nread);
84 g_input_stream_read_async (stream, buf, sizeof (buf),
85 G_PRIORITY_DEFAULT, NULL,
86 test_read_ready, body);
90 test_sent (GObject *source, GAsyncResult *res, gpointer user_data)
92 GString *body = user_data;
96 const char *content_type;
98 stream = soup_request_send_finish (SOUP_REQUEST (source), res, &error);
100 debug_printf (1, " send_async failed: %s\n", error->message);
102 g_main_loop_quit (loop);
106 msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (source));
107 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
108 debug_printf (1, " GET failed: %d %s\n", msg->status_code,
111 g_main_loop_quit (loop);
114 g_object_unref (msg);
116 content_type = soup_request_get_content_type (SOUP_REQUEST (source));
117 if (g_strcmp0 (content_type, "text/plain") != 0) {
118 debug_printf (1, " failed to sniff Content-Type: got %s\n",
119 content_type ? content_type : "(NULL)");
123 g_input_stream_read_async (stream, buf, sizeof (buf),
124 G_PRIORITY_DEFAULT, NULL,
125 test_read_ready, body);
129 do_test_for_thread_and_context (SoupSession *session, const char *uri)
131 SoupRequester *requester;
132 SoupRequest *request;
135 requester = soup_requester_new ();
136 soup_session_add_feature (session, SOUP_SESSION_FEATURE (requester));
137 g_object_unref (requester);
138 soup_session_add_feature_by_type (session, SOUP_TYPE_CONTENT_SNIFFER);
140 body = g_string_new (NULL);
142 request = soup_requester_request (requester, uri, NULL);
143 soup_request_send_async (request, NULL, test_sent, body);
144 g_object_unref (request);
146 loop = g_main_loop_new (soup_session_get_async_context (session), TRUE);
147 g_main_loop_run (loop);
148 g_main_loop_unref (loop);
150 if (body->len != response->length) {
151 debug_printf (1, " body length mismatch: expected %d, got %d\n",
152 (int)response->length, (int)body->len);
154 } else if (memcmp (body->str, response->data, response->length) != 0) {
155 debug_printf (1, " body data mismatch\n");
159 g_string_free (body, TRUE);
163 do_simple_test (const char *uri)
165 SoupSession *session;
167 debug_printf (1, "Simple streaming test\n");
169 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
170 do_test_for_thread_and_context (session, uri);
171 soup_test_session_abort_unref (session);
175 do_test_with_context (const char *uri)
177 GMainContext *async_context;
178 SoupSession *session;
180 async_context = g_main_context_new ();
181 g_main_context_push_thread_default (async_context);
183 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
184 SOUP_SESSION_ASYNC_CONTEXT, async_context,
187 do_test_for_thread_and_context (session, uri);
188 soup_test_session_abort_unref (session);
190 g_main_context_pop_thread_default (async_context);
191 g_main_context_unref (async_context);
196 do_context_test (const char *uri)
198 debug_printf (1, "Streaming with a non-default-context\n");
199 do_test_with_context (uri);
203 do_thread_test (const char *uri)
207 debug_printf (1, "Streaming in another thread\n");
209 thread = g_thread_new ("do_test_with_context",
210 (GThreadFunc)do_test_with_context,
212 g_thread_join (thread);
216 main (int argc, char **argv)
220 test_init (argc, argv, NULL);
223 server = soup_test_server_new (TRUE);
224 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
225 uri = g_strdup_printf ("http://127.0.0.1:%u/foo", soup_server_get_port (server));
227 do_simple_test (uri);
228 do_thread_test (uri);
229 do_context_test (uri);
232 soup_buffer_free (response);
233 soup_test_server_quit_unref (server);