1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2010 Igalia S.L.
6 #include "test-utils.h"
9 SoupURI *first_party_uri, *third_party_uri;
10 const char *first_party = "http://127.0.0.1/";
11 const char *third_party = "http://localhost/";
14 server_callback (SoupServer *server, SoupMessage *msg,
15 const char *path, GHashTable *query,
16 SoupClientContext *context, gpointer data)
18 if (g_str_equal (path, "/index.html")) {
19 soup_message_headers_replace (msg->response_headers,
22 } else if (g_str_equal (path, "/foo.jpg")) {
23 soup_message_headers_replace (msg->response_headers,
26 } else if (soup_message_headers_get_one (msg->request_headers,
28 soup_message_headers_replace (msg->response_headers,
30 soup_message_headers_get_one (msg->request_headers,
34 soup_message_set_status (msg, SOUP_STATUS_OK);
38 SoupCookieJarAcceptPolicy policy;
42 static const CookiesForPolicy validResults[] = {
43 { SOUP_COOKIE_JAR_ACCEPT_ALWAYS, 2 },
44 { SOUP_COOKIE_JAR_ACCEPT_NEVER, 0 },
45 { SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY, 1 }
49 do_cookies_accept_policy_test (void)
58 debug_printf (1, "SoupCookieJarAcceptPolicy test\n");
60 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
61 soup_session_add_feature_by_type (session, SOUP_TYPE_COOKIE_JAR);
62 jar = SOUP_COOKIE_JAR (soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR));
64 for (i = 0; i < G_N_ELEMENTS (validResults); i++) {
65 soup_cookie_jar_set_accept_policy (jar, validResults[i].policy);
67 uri = soup_uri_new_with_base (first_party_uri, "/index.html");
68 msg = soup_message_new_from_uri ("GET", uri);
69 soup_message_set_first_party (msg, first_party_uri);
70 soup_session_send_message (session, msg);
74 /* We can't use two servers due to limitations in
75 * test_server, so let's swap first and third party here
76 * to simulate a cookie coming from a third party.
78 uri = soup_uri_new_with_base (first_party_uri, "/foo.jpg");
79 msg = soup_message_new_from_uri ("GET", uri);
80 soup_message_set_first_party (msg, third_party_uri);
81 soup_session_send_message (session, msg);
85 l = soup_cookie_jar_all_cookies (jar);
86 if (g_slist_length (l) < validResults[i].n_cookies) {
87 debug_printf (1, " accepted less cookies than it should have\n");
89 } else if (g_slist_length (l) > validResults[i].n_cookies) {
90 debug_printf (1, " accepted more cookies than it should have\n");
94 for (p = l; p; p = p->next) {
95 soup_cookie_jar_delete_cookie (jar, p->data);
96 soup_cookie_free (p->data);
102 soup_test_session_abort_unref (session);
105 /* FIXME: moar tests! */
107 do_cookies_parsing_test (void)
109 SoupSession *session;
112 GSList *cookies, *iter;
114 gboolean got1, got2, got3;
116 debug_printf (1, "\nSoupCookie parsing test\n");
118 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
119 soup_session_add_feature_by_type (session, SOUP_TYPE_COOKIE_JAR);
120 jar = SOUP_COOKIE_JAR (soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR));
122 /* "httponly" is case-insensitive, and its value (if any) is ignored */
123 msg = soup_message_new_from_uri ("GET", first_party_uri);
124 soup_message_headers_append (msg->request_headers, "Echo-Set-Cookie",
125 "one=1; httponly; max-age=100");
126 soup_session_send_message (session, msg);
127 g_object_unref (msg);
129 msg = soup_message_new_from_uri ("GET", first_party_uri);
130 soup_message_headers_append (msg->request_headers, "Echo-Set-Cookie",
131 "two=2; HttpOnly; max-age=100");
132 soup_session_send_message (session, msg);
133 g_object_unref (msg);
135 msg = soup_message_new_from_uri ("GET", first_party_uri);
136 soup_message_headers_append (msg->request_headers, "Echo-Set-Cookie",
137 "three=3; httpONLY=Wednesday; max-age=100");
138 soup_session_send_message (session, msg);
139 g_object_unref (msg);
141 cookies = soup_cookie_jar_get_cookie_list (jar, first_party_uri, TRUE);
142 got1 = got2 = got3 = FALSE;
144 for (iter = cookies; iter; iter = iter->next) {
147 if (!strcmp (soup_cookie_get_name (cookie), "one")) {
149 if (!soup_cookie_get_http_only (cookie)) {
150 debug_printf (1, " cookie 1 is not HttpOnly!\n");
153 if (!soup_cookie_get_expires (cookie)) {
154 debug_printf (1, " cookie 1 did not fully parse!\n");
157 } else if (!strcmp (soup_cookie_get_name (cookie), "two")) {
159 if (!soup_cookie_get_http_only (cookie)) {
160 debug_printf (1, " cookie 2 is not HttpOnly!\n");
163 if (!soup_cookie_get_expires (cookie)) {
164 debug_printf (1, " cookie 3 did not fully parse!\n");
167 } else if (!strcmp (soup_cookie_get_name (cookie), "three")) {
169 if (!soup_cookie_get_http_only (cookie)) {
170 debug_printf (1, " cookie 3 is not HttpOnly!\n");
173 if (!soup_cookie_get_expires (cookie)) {
174 debug_printf (1, " cookie 3 did not fully parse!\n");
178 debug_printf (1, " got unexpected cookie '%s'\n",
179 soup_cookie_get_name (cookie));
183 soup_cookie_free (cookie);
185 g_slist_free (cookies);
188 debug_printf (1, " didn't get cookie 1\n");
192 debug_printf (1, " didn't get cookie 2\n");
196 debug_printf (1, " didn't get cookie 3\n");
200 soup_test_session_abort_unref (session);
204 main (int argc, char **argv)
206 test_init (argc, argv, NULL);
208 server = soup_test_server_new (TRUE);
209 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
210 first_party_uri = soup_uri_new (first_party);
211 third_party_uri = soup_uri_new (third_party);
212 soup_uri_set_port (first_party_uri, soup_server_get_port (server));
213 soup_uri_set_port (third_party_uri, soup_server_get_port (server));
215 do_cookies_accept_policy_test ();
216 do_cookies_parsing_test ();
218 soup_uri_free (first_party_uri);
219 soup_uri_free (third_party_uri);
220 soup_test_server_quit_unref (server);