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);
40 server_callback (SoupServer *server, SoupMessage *msg,
41 const char *path, GHashTable *query,
42 SoupClientContext *context, gpointer data)
44 soup_message_set_status (msg, SOUP_STATUS_OK);
45 soup_message_set_response (msg, "text/plain",
46 SOUP_MEMORY_STATIC, NULL, 0);
47 soup_message_body_append_buffer (msg->response_body, response);
51 test_read_ready (GObject *source, GAsyncResult *res, gpointer user_data)
53 GInputStream *stream = G_INPUT_STREAM (source);
54 GString *body = user_data;
58 nread = g_input_stream_read_finish (stream, res, &error);
60 debug_printf (1, " read_async failed: %s", error->message);
62 g_object_unref (stream);
63 g_main_loop_quit (loop);
65 } else if (nread == 0) {
66 g_object_unref (stream);
67 g_main_loop_quit (loop);
71 g_string_append_len (body, buf, nread);
72 g_input_stream_read_async (stream, buf, sizeof (buf),
73 G_PRIORITY_DEFAULT, NULL,
74 test_read_ready, body);
78 test_sent (GObject *source, GAsyncResult *res, gpointer user_data)
80 GString *body = user_data;
85 stream = soup_request_send_finish (SOUP_REQUEST (source), res, &error);
87 debug_printf (1, " send_async failed: %s", error->message);
89 g_main_loop_quit (loop);
93 msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (source));
94 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
95 debug_printf (1, " GET failed: %d %s", msg->status_code,
98 g_main_loop_quit (loop);
101 g_object_unref (msg);
103 g_input_stream_read_async (stream, buf, sizeof (buf),
104 G_PRIORITY_DEFAULT, NULL,
105 test_read_ready, body);
109 do_test_for_thread_and_context (SoupSession *session, const char *uri)
111 SoupRequester *requester;
112 SoupRequest *request;
115 requester = soup_requester_new ();
116 soup_session_add_feature (session, SOUP_SESSION_FEATURE (requester));
117 g_object_unref (requester);
119 body = g_string_new (NULL);
121 request = soup_requester_request (requester, uri, NULL);
122 soup_request_send_async (request, NULL, test_sent, body);
123 g_object_unref (request);
125 loop = g_main_loop_new (soup_session_get_async_context (session), TRUE);
126 g_main_loop_run (loop);
127 g_main_loop_unref (loop);
129 if (body->len != response->length) {
130 debug_printf (1, " body length mismatch: expected %d, got %d\n",
131 (int)response->length, (int)body->len);
133 } else if (memcmp (body->str, response->data, response->length) != 0) {
134 debug_printf (1, " body data mismatch\n");
138 g_string_free (body, TRUE);
142 do_simple_test (const char *uri)
144 SoupSession *session;
146 debug_printf (1, "Simple streaming test\n");
148 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
149 do_test_for_thread_and_context (session, uri);
150 soup_test_session_abort_unref (session);
154 do_test_with_context (const char *uri)
156 GMainContext *async_context;
157 SoupSession *session;
159 async_context = g_main_context_new ();
160 g_main_context_push_thread_default (async_context);
162 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
163 SOUP_SESSION_ASYNC_CONTEXT, async_context,
166 do_test_for_thread_and_context (session, uri);
167 soup_test_session_abort_unref (session);
169 g_main_context_pop_thread_default (async_context);
170 g_main_context_unref (async_context);
175 do_context_test (const char *uri)
177 debug_printf (1, "Streaming with a non-default-context\n");
178 do_test_with_context (uri);
182 do_thread_test (const char *uri)
186 debug_printf (1, "Streaming in another thread\n");
188 thread = g_thread_create ((GThreadFunc)do_test_with_context,
189 (gpointer)uri, TRUE, NULL);
190 g_thread_join (thread);
194 main (int argc, char **argv)
198 test_init (argc, argv, NULL);
201 server = soup_test_server_new (TRUE);
202 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
203 uri = g_strdup_printf ("http://127.0.0.1:%u/", soup_server_get_port (server));
205 do_simple_test (uri);
206 do_thread_test (uri);
207 do_context_test (uri);
210 soup_buffer_free (response);
211 soup_test_server_quit_unref (server);