summaryrefslogtreecommitdiff
path: root/tests/cache-test.c
blob: ceba2dce551b7e7d0e8945fcd69377c8e7a05911 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2009 Igalia S.L.
 */
#include <stdlib.h>

#include <libsoup/soup.h>

#include "test-utils.h"

SoupURI *base_uri;
SoupSession *session;
gboolean server_returned_data = FALSE;
GMainLoop *loop;
char *reply_headers = NULL;
SoupCache *cache;

static void
server_callback (SoupServer *server,
		 SoupMessage *msg,
		 const char *path,
		 GHashTable *query,
		 SoupClientContext *context,
		 gpointer data)
{
	GError *error = NULL;
	char *contents = NULL;
	gsize length = 0;

	if (g_str_has_prefix (path, "/plain/")) {
		char *base_name = g_path_get_basename (path);
		char *file_name = g_strdup_printf (SRCDIR "/resources/%s", base_name);

		g_file_get_contents (file_name,
				     &contents, &length,
				     &error);

		g_free (base_name);
		g_free (file_name);

		if (error) {
			g_error ("%s", error->message);
			g_error_free (error);
			exit (1);
		}
	}

	if (reply_headers) {
		GSList *l;
		GSList *list = soup_header_parse_list (reply_headers);
		for (l = list; l; l = l->next) {
			char **result;

			result = g_strsplit (l->data, ":", -1);
			soup_message_headers_append (msg->response_headers,
						     result[0], result[1]);
			g_free (l->data);
			g_strfreev (result);
		}
		g_slist_free (list);
	}

	if (contents && length)
		soup_message_body_append (msg->response_body,
					  SOUP_MEMORY_COPY,
					  contents,
					  length);

	g_free (contents);
	server_returned_data = TRUE;
	soup_message_set_status (msg, SOUP_STATUS_OK);
	soup_message_body_complete (msg->response_body);
}

static void
finished (SoupSession *session, SoupMessage *msg, gpointer data)
{
	soup_cache_flush (cache, session);

	if (g_main_loop_is_running (loop))
		g_main_loop_quit (loop);
}

static void
test_caching (const char *path,
	      const char *request_headers)
{
	SoupURI *uri = soup_uri_new_with_base (base_uri, path);
	SoupMessage *msg = soup_message_new_from_uri ("GET", uri);
	loop = g_main_loop_new (NULL, TRUE);

	/* Reset state variable */
	server_returned_data = FALSE;

	g_object_ref (msg);
	soup_session_queue_message (session, msg, finished, loop);

	g_main_loop_run (loop);
	soup_uri_free (uri);
	g_object_unref (msg);
	g_main_loop_unref (loop);
	loop = NULL;
}

int
main (int argc, char **argv)
{
	SoupServer *server;

	test_init (argc, argv, NULL);

	server = soup_test_server_new (TRUE);
	soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
	base_uri = soup_uri_new ("http://127.0.0.1/");
	soup_uri_set_port (base_uri, soup_server_get_port (server));

	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
        cache = soup_cache_new ("/tmp/");
        soup_session_add_feature (session, SOUP_SESSION_FEATURE (cache));

        /* 1. Test that an image with max-age=0 is not cached */

	/* Get it once so that it hits the cache */
	reply_headers = "Cache-Control: max-age=0";
	test_caching ("/plain/home.gif", NULL);
	g_assert (server_returned_data == TRUE);

	/* Get it again, we should hit the server because max-age was 0*/
	test_caching ("/plain/home.gif", NULL);
	g_assert (server_returned_data == TRUE);

	soup_cache_clear (cache);

        /* 2. Test that an image with max-age=100 is properly cached */

	/* Get it once so that it hits the cache */
	reply_headers = "Cache-Control: max-age=100";
	test_caching ("/plain/home.gif", NULL);
	g_assert (server_returned_data == TRUE);

	/* Get it again, this time we should not hit the server because max-age was 100 */
	test_caching ("/plain/home.gif", NULL);
	g_assert (server_returned_data == FALSE);

	soup_cache_clear (cache);

	/* 3. Test that Cache-Control: no-cache works */

	reply_headers = "Cache-Control: no-cache; max-age=100";
	test_caching ("/plain/home.gif", NULL);
	g_assert (server_returned_data == TRUE);

	test_caching ("/plain/home.gif", NULL);
	g_assert (server_returned_data == TRUE);

	soup_cache_clear (cache);

	/* */

	test_cleanup ();
	return errors != 0;
}