1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2010 Igalia S.L.
7 #include <libsoup/soup.h>
9 #include "test-utils.h"
12 SoupURI *first_party_uri, *third_party_uri;
13 const char *first_party = "http://127.0.0.1/";
14 const char *third_party = "http://localhost/";
17 server_callback (SoupServer *server, SoupMessage *msg,
18 const char *path, GHashTable *query,
19 SoupClientContext *context, gpointer data)
21 if (g_str_equal(path, "/index.html"))
22 soup_message_headers_replace (msg->response_headers,
25 else if (g_str_equal (path, "/foo.jpg"))
26 soup_message_headers_replace (msg->response_headers,
30 g_return_if_reached ();
32 soup_message_set_status (msg, SOUP_STATUS_OK);
36 SoupCookieJarAcceptPolicy policy;
40 static const CookiesForPolicy validResults[] = {
41 { SOUP_COOKIE_JAR_ACCEPT_ALWAYS, 2 },
42 { SOUP_COOKIE_JAR_ACCEPT_NEVER, 0 },
43 { SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY, 1 }
47 do_cookies_accept_policy_test (void)
56 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
57 soup_session_add_feature_by_type (session, SOUP_TYPE_COOKIE_JAR);
58 jar = SOUP_COOKIE_JAR (soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR));
60 for (i = 0; i < G_N_ELEMENTS (validResults); i++) {
61 soup_cookie_jar_set_accept_policy (jar, validResults[i].policy);
63 uri = soup_uri_new_with_base (first_party_uri, "/index.html");
64 msg = soup_message_new_from_uri ("GET", uri);
65 soup_message_set_first_party (msg, first_party_uri);
66 soup_session_send_message (session, msg);
70 /* We can't use to servers due to limitations in
71 * test_server, so let's swap first and third party here
72 * to simulate a cookie coming from a third party.
74 uri = soup_uri_new_with_base (first_party_uri, "/foo.jpg");
75 msg = soup_message_new_from_uri ("GET", uri);
76 soup_message_set_first_party (msg, third_party_uri);
77 soup_session_send_message (session, msg);
81 l = soup_cookie_jar_all_cookies (jar);
82 if (g_slist_length (l) < validResults[i].n_cookies) {
83 debug_printf (1, " accepted less cookies than it should have\n");
85 } else if (g_slist_length (l) > validResults[i].n_cookies) {
86 debug_printf (1, " accepted more cookies than it should have\n");
90 for (p = l; p; p = p->next) {
91 soup_cookie_jar_delete_cookie (jar, p->data);
92 soup_cookie_free (p->data);
98 soup_test_session_abort_unref (session);
102 main (int argc, char **argv)
104 test_init (argc, argv, NULL);
106 server = soup_test_server_new (TRUE);
107 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
108 first_party_uri = soup_uri_new (first_party);
109 third_party_uri = soup_uri_new (third_party);
110 soup_uri_set_port (first_party_uri, soup_server_get_port (server));
111 soup_uri_set_port (third_party_uri, soup_server_get_port (server));
113 do_cookies_accept_policy_test ();
115 soup_uri_free (first_party_uri);
116 soup_uri_free (third_party_uri);
117 soup_test_server_quit_unref (server);