summaryrefslogtreecommitdiff
path: root/tests/requester-test.c
blob: 39b30bd59a657e6b3c3864d06f4c9e4c2b9d91b0 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2011 Red Hat, Inc.
 */

/* Kill SoupRequester-related deprecation warnings */
#define SOUP_VERSION_MIN_REQUIRED SOUP_VERSION_2_40

#include "test-utils.h"

SoupServer *server;
GMainLoop *loop;
char buf[1024];

SoupBuffer *response, *auth_response;

#define REDIRECT_HTML_BODY "<html><body>Try again</body></html>\r\n"
#define AUTH_HTML_BODY "<html><body>Unauthorized</body></html>\r\n"

static gboolean
slow_finish_message (gpointer msg)
{
	SoupServer *server = g_object_get_data (G_OBJECT (msg), "server");

	soup_server_unpause_message (server, msg);
	return FALSE;
}

static void
slow_pause_message (SoupMessage *msg, gpointer server)
{
	soup_server_pause_message (server, msg);
	soup_add_timeout (soup_server_get_async_context (server),
			  1000, slow_finish_message, msg);
}

static void
server_callback (SoupServer *server, SoupMessage *msg,
		 const char *path, GHashTable *query,
		 SoupClientContext *context, gpointer data)
{
	gboolean chunked = FALSE;
	int i;

	if (strcmp (path, "/auth") == 0) {
		soup_message_set_status (msg, SOUP_STATUS_UNAUTHORIZED);
		soup_message_set_response (msg, "text/html",
					   SOUP_MEMORY_STATIC,
					   AUTH_HTML_BODY,
					   strlen (AUTH_HTML_BODY));
		soup_message_headers_append (msg->response_headers,
					     "WWW-Authenticate",
					     "Basic: realm=\"requester-test\"");
		return;
	} else if (strcmp (path, "/foo") == 0) {
		soup_message_set_redirect (msg, SOUP_STATUS_FOUND, "/");
		/* Make the response HTML so if we sniff that instead of the
		 * real body, we'll notice.
		 */
		soup_message_set_response (msg, "text/html",
					   SOUP_MEMORY_STATIC,
					   REDIRECT_HTML_BODY,
					   strlen (REDIRECT_HTML_BODY));
		return;
	} else if (strcmp (path, "/chunked") == 0) {
		chunked = TRUE;
	} else if (strcmp (path, "/non-persistent") == 0) {
		soup_message_headers_append (msg->response_headers,
					     "Connection", "close");
	} else if (!strcmp (path, "/slow")) {
		g_object_set_data (G_OBJECT (msg), "server", server);
		g_signal_connect (msg, "wrote-headers",
				  G_CALLBACK (slow_pause_message), server);
	}

	soup_message_set_status (msg, SOUP_STATUS_OK);

	if (chunked) {
		soup_message_headers_set_encoding (msg->response_headers,
						   SOUP_ENCODING_CHUNKED);

		for (i = 0; i < response->length; i += 8192) {
			SoupBuffer *tmp;

			tmp = soup_buffer_new_subbuffer (response, i,
							 MIN (8192, response->length - i));
			soup_message_body_append_buffer (msg->response_body, tmp);
			soup_buffer_free (tmp);
		}
		soup_message_body_complete (msg->response_body);
	} else
		soup_message_body_append_buffer (msg->response_body, response);
}

typedef struct {
	GString *body;
	gboolean cancel;
} RequestData;

static void
stream_closed (GObject *source, GAsyncResult *res, gpointer user_data)
{
	GInputStream *stream = G_INPUT_STREAM (source);
	GError *error = NULL;

	g_input_stream_close_finish (stream, res, &error);
	g_assert_no_error (error);
	g_main_loop_quit (loop);
	g_object_unref (stream);
}

static void
test_read_ready (GObject *source, GAsyncResult *res, gpointer user_data)
{
	GInputStream *stream = G_INPUT_STREAM (source);
	RequestData *data = user_data;
	GString *body = data->body;
	GError *error = NULL;
	gsize nread;

	nread = g_input_stream_read_finish (stream, res, &error);
	if (nread == -1) {
		g_assert_no_error (error);
		g_error_free (error);
		g_input_stream_close (stream, NULL, NULL);
		g_object_unref (stream);
		g_main_loop_quit (loop);
		return;
	} else if (nread == 0) {
		g_input_stream_close_async (stream,
					    G_PRIORITY_DEFAULT, NULL,
					    stream_closed, NULL);
		return;
	}

	g_string_append_len (body, buf, nread);
	g_input_stream_read_async (stream, buf, sizeof (buf),
				   G_PRIORITY_DEFAULT, NULL,
				   test_read_ready, data);
}

static void
auth_test_sent (GObject *source, GAsyncResult *res, gpointer user_data)
{
	RequestData *data = user_data;
	GInputStream *stream;
	GError *error = NULL;
	SoupMessage *msg;
	const char *content_type;

	stream = soup_request_send_finish (SOUP_REQUEST (source), res, &error);
	if (!stream) {
		g_assert_no_error (error);
		g_clear_error (&error);
		g_main_loop_quit (loop);
		return;
	}

	msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (source));
	soup_test_assert_message_status (msg, SOUP_STATUS_UNAUTHORIZED);
	g_object_unref (msg);

	content_type = soup_request_get_content_type (SOUP_REQUEST (source));
	g_assert_cmpstr (content_type, ==, "text/html");

	g_input_stream_read_async (stream, buf, sizeof (buf),
				   G_PRIORITY_DEFAULT, NULL,
				   test_read_ready, data);
}

static void
test_sent (GObject *source, GAsyncResult *res, gpointer user_data)
{
	RequestData *data = user_data;
	GInputStream *stream;
	GError *error = NULL;
	const char *content_type;

	stream = soup_request_send_finish (SOUP_REQUEST (source), res, &error);
	if (data->cancel) {
		g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
		g_clear_error (&error);
		g_main_loop_quit (loop);
		return;
	} else {
		g_assert_no_error (error);
		if (!stream) {
			g_main_loop_quit (loop);
			g_clear_error (&error);
			return;
		}
	}

	content_type = soup_request_get_content_type (SOUP_REQUEST (source));
	g_assert_cmpstr (content_type, ==, "text/plain");

	g_input_stream_read_async (stream, buf, sizeof (buf),
				   G_PRIORITY_DEFAULT, NULL,
				   test_read_ready, data);
}

static void
cancel_message (SoupMessage *msg, gpointer session)
{
	soup_session_cancel_message (session, msg, SOUP_STATUS_FORBIDDEN);
}

static void
request_started (SoupSession *session, SoupMessage *msg,
		 SoupSocket *socket, gpointer user_data)
{
	SoupSocket **save_socket = user_data;

	g_clear_object (save_socket);
	*save_socket = g_object_ref (socket);
}

static void
do_async_test (SoupSession *session, SoupURI *uri,
	       GAsyncReadyCallback callback, guint expected_status,
	       SoupBuffer *expected_response,
	       gboolean persistent, gboolean cancel)
{
	SoupRequester *requester;
	SoupRequest *request;
	guint started_id;
	SoupSocket *socket = NULL;
	SoupMessage *msg;
	RequestData data;

	if (SOUP_IS_SESSION_ASYNC (session))
		requester = SOUP_REQUESTER (soup_session_get_feature (session, SOUP_TYPE_REQUESTER));
	else
		requester = NULL;

	data.body = g_string_new (NULL);
	data.cancel = cancel;
	if (requester)
		request = soup_requester_request_uri (requester, uri, NULL);
	else
		request = soup_session_request_uri (session, uri, NULL);
	msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));

	if (cancel) {
		g_signal_connect (msg, "got-headers",
				  G_CALLBACK (cancel_message), session);
	}

	started_id = g_signal_connect (session, "request-started",
				       G_CALLBACK (request_started),
				       &socket);

	soup_request_send_async (request, NULL, callback, &data);
	g_object_unref (request);

	loop = g_main_loop_new (soup_session_get_async_context (session), TRUE);
	g_main_loop_run (loop);
	g_main_loop_unref (loop);

	g_signal_handler_disconnect (session, started_id);

	soup_test_assert_message_status (msg, expected_status);
	g_object_unref (msg);

	if (expected_response) {
		soup_assert_cmpmem (data.body->str, data.body->len,
				    expected_response->data, expected_response->length);
	} else
		g_assert_cmpint (data.body->len, ==, 0);

	if (persistent)
		g_assert_true (soup_socket_is_connected (socket));
	else
		g_assert_false (soup_socket_is_connected (socket));

	g_object_unref (socket);

	g_string_free (data.body, TRUE);
}

static void
do_test_for_thread_and_context (SoupSession *session, const char *base_uri)
{
	SoupRequester *requester;
	SoupURI *uri;

	if (SOUP_IS_SESSION_ASYNC (session)) {
		requester = soup_requester_new ();
		soup_session_add_feature (session, SOUP_SESSION_FEATURE (requester));
		g_object_unref (requester);
	}
	soup_session_add_feature_by_type (session, SOUP_TYPE_CONTENT_SNIFFER);

	debug_printf (1, "    basic test\n");
	uri = soup_uri_new (base_uri);
	do_async_test (session, uri, test_sent,
		       SOUP_STATUS_OK, response,
		       TRUE, FALSE);
	soup_uri_free (uri);

	debug_printf (1, "    chunked test\n");
	uri = soup_uri_new (base_uri);
	soup_uri_set_path (uri, "/chunked");
	do_async_test (session, uri, test_sent,
		       SOUP_STATUS_OK, response,
		       TRUE, FALSE);
	soup_uri_free (uri);

	debug_printf (1, "    auth test\n");
	uri = soup_uri_new (base_uri);
	soup_uri_set_path (uri, "/auth");
	do_async_test (session, uri, auth_test_sent,
		       SOUP_STATUS_UNAUTHORIZED, auth_response,
		       TRUE, FALSE);
	soup_uri_free (uri);

	debug_printf (1, "    non-persistent test\n");
	uri = soup_uri_new (base_uri);
	soup_uri_set_path (uri, "/non-persistent");
	do_async_test (session, uri, test_sent,
		       SOUP_STATUS_OK, response,
		       FALSE, FALSE);
	soup_uri_free (uri);

	debug_printf (1, "    cancellation test\n");
	uri = soup_uri_new (base_uri);
	soup_uri_set_path (uri, "/");
	do_async_test (session, uri, test_sent,
		       SOUP_STATUS_FORBIDDEN, NULL,
		       FALSE, TRUE);
	soup_uri_free (uri);
}

static void
do_simple_plain_test (gconstpointer uri)
{
	SoupSession *session;

	g_test_bug ("653707");

	session = soup_test_session_new (SOUP_TYPE_SESSION, NULL);
	do_test_for_thread_and_context (session, uri);
	soup_test_session_abort_unref (session);
}

static void
do_simple_async_test (gconstpointer uri)
{
	SoupSession *session;

	g_test_bug ("653707");

	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
					 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
					 NULL);
	do_test_for_thread_and_context (session, uri);
	soup_test_session_abort_unref (session);
}

static void
do_test_with_context_and_type (const char *uri, gboolean plain_session)
{
	GMainContext *async_context;
	SoupSession *session;

	g_test_bug ("653707");

	async_context = g_main_context_new ();
	g_main_context_push_thread_default (async_context);

	session = soup_test_session_new (plain_session ? SOUP_TYPE_SESSION : SOUP_TYPE_SESSION_ASYNC,
					 SOUP_SESSION_ASYNC_CONTEXT, async_context,
					 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
					 NULL);

	do_test_for_thread_and_context (session, uri);
	soup_test_session_abort_unref (session);

	g_main_context_pop_thread_default (async_context);
	g_main_context_unref (async_context);
}

static void
do_async_test_with_context (gconstpointer uri)
{
	do_test_with_context_and_type (uri, FALSE);
}

static void
do_plain_test_with_context (gconstpointer uri)
{
	do_test_with_context_and_type (uri, TRUE);
}

static gpointer
async_test_thread (gpointer uri)
{
	do_test_with_context_and_type (uri, TRUE);
	return NULL;
}

static gpointer
plain_test_thread (gpointer uri)
{
	do_test_with_context_and_type (uri, FALSE);
	return NULL;
}

static void
do_async_test_in_thread (gconstpointer uri)
{
	GThread *thread;

	thread = g_thread_new ("do_async_test_in_thread",
			       async_test_thread,
			       (gpointer)uri);
	g_thread_join (thread);
}

static void
do_plain_test_in_thread (gconstpointer uri)
{
	GThread *thread;

	thread = g_thread_new ("do_plain_test_in_thread",
			       plain_test_thread,
			       (gpointer)uri);
	g_thread_join (thread);
}

static void
do_sync_request (SoupSession *session, SoupRequest *request,
		 guint expected_status, SoupBuffer *expected_response,
		 gboolean persistent, gboolean cancel)
{
	GInputStream *in;
	SoupMessage *msg;
	GError *error = NULL;
	GString *body;
	char buf[1024];
	gssize nread;
	guint started_id;
	SoupSocket *socket = NULL;

	msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));
	if (cancel) {
		g_signal_connect (msg, "got-headers",
				  G_CALLBACK (cancel_message), session);
	}

	started_id = g_signal_connect (session, "request-started",
				       G_CALLBACK (request_started),
				       &socket);

	in = soup_request_send (request, NULL, &error);
	g_signal_handler_disconnect (session, started_id);
	if (cancel) {
		g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
		g_clear_error (&error);
		g_object_unref (msg);
		g_object_unref (socket);
		return;
	} else if (!in) {
		g_assert_no_error (error);
		g_clear_error (&error);
		g_object_unref (msg);
		g_object_unref (socket);
		return;
	}

	soup_test_assert_message_status (msg, expected_status);
	g_object_unref (msg);

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

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

	if (expected_response) {
		soup_assert_cmpmem (body->str, body->len,
				    expected_response->data, expected_response->length);
	} else
		g_assert_cmpint (body->len, ==, 0);

	if (persistent)
		g_assert_true (soup_socket_is_connected (socket));
	else
		g_assert_false (soup_socket_is_connected (socket));
	g_object_unref (socket);

	g_string_free (body, TRUE);
}

static void
do_sync_tests_for_session (SoupSession *session, const char *uri_string)
{
	SoupRequester *requester;
	SoupRequest *request;
	SoupURI *uri;

	requester = SOUP_REQUESTER (soup_session_get_feature (session, SOUP_TYPE_REQUESTER));

	uri = soup_uri_new (uri_string);

	debug_printf (1, "    basic test\n");
	if (requester)
		request = soup_requester_request_uri (requester, uri, NULL);
	else
		request = soup_session_request_uri (session, uri, NULL);
	do_sync_request (session, request,
			 SOUP_STATUS_OK, response,
			 TRUE, FALSE);
	g_object_unref (request);

	debug_printf (1, "    chunked test\n");
	soup_uri_set_path (uri, "/chunked");
	if (requester)
		request = soup_requester_request_uri (requester, uri, NULL);
	else
		request = soup_session_request_uri (session, uri, NULL);
	do_sync_request (session, request,
			 SOUP_STATUS_OK, response,
			 TRUE, FALSE);
	g_object_unref (request);

	debug_printf (1, "    auth test\n");
	soup_uri_set_path (uri, "/auth");
	if (requester)
		request = soup_requester_request_uri (requester, uri, NULL);
	else
		request = soup_session_request_uri (session, uri, NULL);
	do_sync_request (session, request,
			 SOUP_STATUS_UNAUTHORIZED, auth_response,
			 TRUE, FALSE);
	g_object_unref (request);

	debug_printf (1, "    non-persistent test\n");
	soup_uri_set_path (uri, "/non-persistent");
	if (requester)
		request = soup_requester_request_uri (requester, uri, NULL);
	else
		request = soup_session_request_uri (session, uri, NULL);
	do_sync_request (session, request,
			 SOUP_STATUS_OK, response,
			 FALSE, FALSE);
	g_object_unref (request);

	debug_printf (1, "    cancel test\n");
	soup_uri_set_path (uri, "/");
	if (requester)
		request = soup_requester_request_uri (requester, uri, NULL);
	else
		request = soup_session_request_uri (session, uri, NULL);
	do_sync_request (session, request,
			 SOUP_STATUS_FORBIDDEN, NULL,
			 TRUE, TRUE);
	g_object_unref (request);

	soup_uri_free (uri);
}

static void
do_plain_sync_test (gconstpointer uri)
{
	SoupSession *session;

	session = soup_test_session_new (SOUP_TYPE_SESSION, NULL);
	do_sync_tests_for_session (session, uri);
	soup_test_session_abort_unref (session);
}

static void
do_sync_sync_test (gconstpointer uri)
{
	SoupSession *session;
	SoupRequester *requester;

	session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, NULL);
	requester = soup_requester_new ();
	soup_session_add_feature (session, SOUP_SESSION_FEATURE (requester));
	g_object_unref (requester);
	do_sync_tests_for_session (session, uri);
	soup_test_session_abort_unref (session);
}

static void
do_null_char_request (SoupSession *session, const char *encoded_data,
		      const char *expected_data, int expected_len)
{
	GError *error = NULL;
	GInputStream *stream;
	SoupRequest *request;
	SoupURI *uri;
	char *uri_string, buf[256];
	gsize nread;

	uri_string = g_strdup_printf ("data:text/html,%s", encoded_data);
	uri = soup_uri_new (uri_string);
	g_free (uri_string);

	request = soup_session_request_uri (session, uri, NULL);
	stream = soup_test_request_send (request, NULL, 0, &error);
	g_assert_no_error (error);
	if (error) {
		g_error_free (error);
		g_object_unref (request);
		soup_uri_free (uri);
		return;
	}

	g_input_stream_read_all (stream, buf, sizeof (buf), &nread, NULL, &error);
	g_assert_no_error (error);
	g_clear_error (&error);

	soup_test_request_close_stream (request, stream, NULL, &error);
	g_assert_no_error (error);
	g_clear_error (&error);

	soup_assert_cmpmem (buf, nread, expected_data, expected_len);

	g_object_unref (stream);
	g_object_unref (request);
	soup_uri_free (uri);
}

static void
do_null_char_test_for_session (SoupSession *session)
{
	static struct {
		const char *encoded_data;
		const char *expected_data;
		int expected_len;
	} test_cases[] = {
		{ "%3Cscript%3Ea%3D'%00'%3C%2Fscript%3E", "<script>a='\0'</script>", 22 },
		{ "%00%3Cscript%3Ea%3D42%3C%2Fscript%3E", "\0<script>a=42</script>", 22 },
		{ "%3Cscript%3E%00%3Cbr%2F%3E%3C%2Fscript%3E%00", "<script>\0<br/></script>\0", 24 },
	};
	static int num_test_cases = G_N_ELEMENTS(test_cases);
	int i;

	for (i = 0; i < num_test_cases; i++) {
		do_null_char_request (session, test_cases[i].encoded_data,
				      test_cases[i].expected_data, test_cases[i].expected_len);
	}
}

static void
do_plain_null_char_test (void)
{
	SoupSession *session;

	session = soup_test_session_new (SOUP_TYPE_SESSION, NULL);
	do_null_char_test_for_session (session);
	soup_test_session_abort_unref (session);
}

static void
do_async_null_char_test (void)
{
	SoupSession *session;

	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
					 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
					 NULL);
	do_null_char_test_for_session (session);
	soup_test_session_abort_unref (session);
}

static void
close_test_msg_finished (SoupMessage *msg,
			 gpointer     user_data)
{
	gboolean *finished = user_data;

	*finished = TRUE;
}

static void
do_close_test_for_session (SoupSession *session,
			   SoupURI     *uri)
{
	GError *error = NULL;
	GInputStream *stream;
	SoupRequest *request;
	guint64 start, end;
	GCancellable *cancellable;
	SoupMessage *msg;
	gboolean finished = FALSE;

	debug_printf (1, "    normal close\n");

	request = soup_session_request_uri (session, uri, NULL);
	stream = soup_test_request_send (request, NULL, 0, &error);
	g_assert_no_error (error);
	if (error) {
		g_error_free (error);
		g_object_unref (request);
		return;
	}

	start = g_get_monotonic_time ();
	soup_test_request_close_stream (request, stream, NULL, &error);
	g_assert_no_error (error);
	g_clear_error (&error);
	end = g_get_monotonic_time ();

	g_assert_cmpint (end - start, <=, 500000);

	g_object_unref (stream);
	g_object_unref (request);


	debug_printf (1, "    error close\n");

	request = soup_session_request_uri (session, uri, NULL);
	msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));
	g_signal_connect (msg, "finished", G_CALLBACK (close_test_msg_finished), &finished);
	g_object_unref (msg);

	stream = soup_test_request_send (request, NULL, 0, &error);
	g_assert_no_error (error);
	if (error) {
		g_error_free (error);
		g_object_unref (request);
		return;
	}

	cancellable = g_cancellable_new ();
	g_cancellable_cancel (cancellable);
	soup_test_request_close_stream (request, stream, cancellable, &error);
	if (error)
		g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
	g_clear_error (&error);

	g_assert_true (finished);

	g_object_unref (stream);
	g_object_unref (request);
}

static void
do_async_close_test (gconstpointer uri)
{
	SoupSession *session;
	SoupURI *slow_uri;

	g_test_bug ("695652");
	g_test_bug ("711260");

	slow_uri = soup_uri_new (uri);
	soup_uri_set_path (slow_uri, "/slow");

	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
					 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
					 NULL);
	do_close_test_for_session (session, slow_uri);
	soup_test_session_abort_unref (session);

	soup_uri_free (slow_uri);
}

static void
do_sync_close_test (gconstpointer uri)
{
	SoupSession *session;
	SoupURI *slow_uri;

	g_test_bug ("695652");
	g_test_bug ("711260");

	slow_uri = soup_uri_new (uri);
	soup_uri_set_path (slow_uri, "/slow");

	debug_printf (1, "  SoupSessionSync\n");
	session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC,
					 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
					 NULL);
	do_close_test_for_session (session, slow_uri);
	soup_test_session_abort_unref (session);

	soup_uri_free (slow_uri);
}

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

	test_init (argc, argv, NULL);

	response = soup_test_get_index ();
	auth_response = soup_buffer_new (SOUP_MEMORY_STATIC,
					 AUTH_HTML_BODY,
					 strlen (AUTH_HTML_BODY));

	server = soup_test_server_new (TRUE);
	soup_server_add_handler (server, NULL, server_callback, NULL, NULL);

	uri = g_strdup_printf ("http://127.0.0.1:%u/foo", soup_server_get_port (server));

	g_test_add_data_func ("/requester/simple/SoupSession", uri, do_simple_plain_test);
	g_test_add_data_func ("/requester/simple/SoupSessionAsync", uri, do_simple_async_test);
	g_test_add_data_func ("/requester/threaded/SoupSession", uri, do_plain_test_in_thread);
	g_test_add_data_func ("/requester/threaded/SoupSessionAsync", uri, do_async_test_in_thread);
	g_test_add_data_func ("/requester/context/SoupSession", uri, do_plain_test_with_context);
	g_test_add_data_func ("/requester/context/SoupSessionAsync", uri, do_async_test_with_context);
	g_test_add_data_func ("/requester/sync/SoupSession", uri, do_plain_sync_test);
	g_test_add_data_func ("/requester/sync/SoupSessionSync", uri, do_sync_sync_test);
	g_test_add_func ("/requester/null-char/SoupSession", do_plain_null_char_test);
	g_test_add_func ("/requester/null-char/SoupSessionAsync", do_async_null_char_test);
	g_test_add_data_func ("/requester/close/SoupSessionAsync", uri, do_async_close_test);
	g_test_add_data_func ("/requester/close/SoupSessionSync", uri, do_sync_close_test);

	ret = g_test_run ();

	g_free (uri);
	soup_buffer_free (auth_response);
	soup_test_server_quit_unref (server);

	test_cleanup ();
	return ret;
}