summaryrefslogtreecommitdiff
path: root/tests/resource-test.c
blob: 6fcb899e44d8e765e7855fc1546546f3637ea02f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2012 Igalia S.L.
 */

#include "test-utils.h"

SoupBuffer *index_buffer;

typedef struct {
	GString *body;
	char buffer[1024];
	GMainLoop *loop;
} AsyncRequestData;

static void
stream_closed (GObject *source, GAsyncResult *result, gpointer user_data)
{
	GInputStream *in = G_INPUT_STREAM (source);
	AsyncRequestData *data = user_data;
	GError *error = NULL;

	g_input_stream_close_finish (in, result, &error);
	g_assert_no_error (error);
	g_main_loop_quit (data->loop);
	g_object_unref (in);
}

static void
test_read_ready (GObject *source, GAsyncResult *result, gpointer user_data)
{
	GInputStream *in = G_INPUT_STREAM (source);
	AsyncRequestData *data = user_data;
	gssize nread;
	GError *error = NULL;

	nread = g_input_stream_read_finish (in, result, &error);
	if (nread == -1) {
		g_assert_no_error (error);
		g_clear_error (&error);
		g_input_stream_close (in, NULL, NULL);
		g_object_unref (in);
		return;
	} else if (nread == 0) {
		g_input_stream_close_async (in, G_PRIORITY_DEFAULT, NULL,
					    stream_closed, data);
		return;
	}

	g_string_append_len (data->body, data->buffer, nread);
	g_input_stream_read_async (in, data->buffer, sizeof (data->buffer),
				   G_PRIORITY_DEFAULT, NULL,
				   test_read_ready, data);
}

static void
async_request_sent (GObject *source, GAsyncResult *result, gpointer user_data)
{
	GInputStream *in;
	AsyncRequestData *data = user_data;
	GError *error = NULL;

	in = soup_request_send_finish (SOUP_REQUEST (source), result, &error);
	if (!in) {
		g_assert_no_error (error);
		g_clear_error (&error);
		return;
	}

	g_input_stream_read_async (in, data->buffer, sizeof (data->buffer),
				   G_PRIORITY_DEFAULT, NULL,
				   test_read_ready, data);
}

static void
do_async_request (SoupRequest *request)
{
	AsyncRequestData data;

	data.body = g_string_new (NULL);
	soup_request_send_async (request, NULL, async_request_sent, &data);

	data.loop = g_main_loop_new (soup_session_get_async_context (soup_request_get_session (request)), TRUE);
	g_main_loop_run (data.loop);
	g_main_loop_unref (data.loop);

	soup_assert_cmpmem (data.body->str, data.body->len,
			    index_buffer->data, index_buffer->length);
	g_string_free (data.body, TRUE);
}

static void
do_sync_request (SoupRequest *request)
{
	GInputStream *in;
	GString *body;
	char buffer[1024];
	gssize nread;
	GError *error = NULL;

	in = soup_request_send (request, NULL, &error);
	if (!in) {
		g_assert_no_error (error);
		g_clear_error (&error);
		return;
	}

	body = g_string_new (NULL);
	do {
		nread = g_input_stream_read (in, buffer, sizeof (buffer),
					     NULL, &error);
		if (nread == -1) {
			g_assert_no_error (error);
			g_clear_error (&error);
			break;
		}
		g_string_append_len (body, buffer, nread);
	} while (nread > 0);

	g_input_stream_close (in, NULL, &error);
	g_assert_no_error (error);
	g_clear_error (&error);
	g_object_unref (in);

	soup_assert_cmpmem (body->str, body->len, index_buffer->data, index_buffer->length);
	g_string_free (body, TRUE);
}

static void
do_request (const char *uri_string, gconstpointer type)
{
	SoupSession *session;
	SoupRequest *request;
	GError *error = NULL;

	session = soup_test_session_new (GPOINTER_TO_SIZE (type),
					 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
					 NULL);

	request = soup_session_request (session, uri_string, &error);
	g_assert_no_error (error);

	if (SOUP_IS_SESSION_ASYNC (session))
		do_async_request (request);
	else
		do_sync_request (request);

	g_object_unref (request);
	soup_test_session_abort_unref (session);
}

static void
do_request_file_test (gconstpointer type)
{
	GFile *index;
	char *uri_string;

	index = g_file_new_for_path (g_test_get_filename (G_TEST_DIST, "index.txt", NULL));
	uri_string = g_file_get_uri (index);
	g_object_unref (index);

	do_request (uri_string, type);
	g_free (uri_string);
}

static void
do_request_data_test (gconstpointer type)
{
	gchar *base64;
	char *uri_string;

	base64 = g_base64_encode ((const guchar *)index_buffer->data, index_buffer->length);
	uri_string = g_strdup_printf ("data:text/plain;charset=utf8;base64,%s", base64);
	g_free (base64);

	do_request (uri_string, type);
	g_free (uri_string);
}

static void
do_request_gresource_test (gconstpointer type)
{
	do_request ("resource:///org/gnome/libsoup/tests/index.txt", type);
}

int
main (int argc, char **argv)
{
	int ret;

	test_init (argc, argv, NULL);

	index_buffer = soup_test_get_index ();
	soup_test_register_resources ();

	g_test_add_data_func ("/resource/sync/file",
			      GSIZE_TO_POINTER (SOUP_TYPE_SESSION_SYNC),
			      do_request_file_test);
	g_test_add_data_func ("/resource/sync/data",
			      GSIZE_TO_POINTER (SOUP_TYPE_SESSION_SYNC),
			      do_request_data_test);
	g_test_add_data_func ("/resource/sync/gresource",
			      GSIZE_TO_POINTER (SOUP_TYPE_SESSION_SYNC),
			      do_request_gresource_test);

	g_test_add_data_func ("/resource/async/file",
			      GSIZE_TO_POINTER (SOUP_TYPE_SESSION_ASYNC),
			      do_request_file_test);
	g_test_add_data_func ("/resource/async/data",
			      GSIZE_TO_POINTER (SOUP_TYPE_SESSION_ASYNC),
			      do_request_data_test);
	g_test_add_data_func ("/resource/async/gresource",
			      GSIZE_TO_POINTER (SOUP_TYPE_SESSION_ASYNC),
			      do_request_gresource_test);

	ret = g_test_run ();

	test_cleanup ();
	return ret;
}