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);
102 g_input_stream_read_async (stream, buf, sizeof (buf),
103 G_PRIORITY_DEFAULT, NULL,
104 test_read_ready, body);
108 do_test_for_thread_and_context (SoupSession *session, const char *uri)
110 SoupRequester *requester;
111 SoupRequest *request;
114 requester = soup_requester_new ();
115 soup_session_add_feature (session, SOUP_SESSION_FEATURE (requester));
116 g_object_unref (requester);
118 body = g_string_new (NULL);
120 request = soup_requester_request (requester, uri, NULL);
121 soup_request_send_async (request, NULL, test_sent, body);
122 g_object_unref (request);
124 loop = g_main_loop_new (soup_session_get_async_context (session), TRUE);
125 g_main_loop_run (loop);
126 g_main_loop_unref (loop);
128 if (body->len != response->length) {
129 debug_printf (1, " body length mismatch: expected %d, got %d\n",
130 (int)response->length, (int)body->len);
132 } else if (memcmp (body->str, response->data, response->length) != 0) {
133 debug_printf (1, " body data mismatch\n");
137 g_string_free (body, TRUE);
141 do_simple_test (const char *uri)
143 SoupSession *session;
145 debug_printf (1, "Simple streaming test\n");
147 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
148 do_test_for_thread_and_context (session, uri);
149 soup_test_session_abort_unref (session);
153 do_test_with_context (const char *uri)
155 GMainContext *async_context;
156 SoupSession *session;
158 async_context = g_main_context_new ();
159 g_main_context_push_thread_default (async_context);
161 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
162 SOUP_SESSION_ASYNC_CONTEXT, async_context,
164 g_main_context_unref (async_context);
166 do_test_for_thread_and_context (session, uri);
167 soup_test_session_abort_unref (session);
173 do_context_test (const char *uri)
175 debug_printf (1, "Streaming with a non-default-context\n");
176 do_test_with_context (uri);
180 do_thread_test (const char *uri)
184 debug_printf (1, "Streaming in another thread\n");
186 thread = g_thread_create ((GThreadFunc)do_test_with_context,
187 (gpointer)uri, TRUE, NULL);
188 g_thread_join (thread);
192 main (int argc, char **argv)
196 test_init (argc, argv, NULL);
199 server = soup_test_server_new (TRUE);
200 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
201 uri = g_strdup_printf ("http://127.0.0.1:%u/", soup_server_get_port (server));
203 do_simple_test (uri);
204 do_thread_test (uri);
205 do_context_test (uri);
208 soup_buffer_free (response);
209 soup_test_server_quit_unref (server);