Git init
[profile/ivi/libsoup2.4.git] / tests / cookies-test.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2010 Igalia S.L.
4  */
5
6 #include <glib.h>
7 #include <libsoup/soup.h>
8
9 #include "test-utils.h"
10
11 SoupServer *server;
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/";
15
16 static void
17 server_callback (SoupServer *server, SoupMessage *msg,
18                  const char *path, GHashTable *query,
19                  SoupClientContext *context, gpointer data)
20 {
21     if (g_str_equal(path, "/index.html"))
22         soup_message_headers_replace (msg->response_headers,
23                                       "Set-Cookie",
24                                       "foo=bar");
25     else if (g_str_equal (path, "/foo.jpg"))
26         soup_message_headers_replace (msg->response_headers,
27                                       "Set-Cookie",
28                                       "baz=qux");
29     else
30         g_return_if_reached ();
31
32     soup_message_set_status (msg, SOUP_STATUS_OK);
33 }
34
35 typedef struct {
36     SoupCookieJarAcceptPolicy policy;
37     int n_cookies;
38 } CookiesForPolicy;
39
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 }
44 };
45
46 static void
47 do_cookies_accept_policy_test (void)
48 {
49         SoupSession *session;
50         SoupMessage *msg;
51         SoupURI *uri;
52         SoupCookieJar *jar;
53         GSList *l, *p;
54         int i;
55
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));
59
60         for (i = 0; i < G_N_ELEMENTS (validResults); i++) {
61             soup_cookie_jar_set_accept_policy (jar, validResults[i].policy);
62
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);
67             soup_uri_free (uri);
68             g_object_unref (msg);
69
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.
73              */
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);
78             soup_uri_free (uri);
79             g_object_unref (msg);
80
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");
84                     errors++;
85             } else if (g_slist_length (l) > validResults[i].n_cookies) {
86                     debug_printf (1, " accepted more cookies than it should have\n");
87                     errors++;
88             }
89
90             for (p = l; p; p = p->next) {
91                 soup_cookie_jar_delete_cookie (jar, p->data);
92                 soup_cookie_free (p->data);
93             }
94
95             g_slist_free (l);
96         }
97
98         soup_test_session_abort_unref (session);
99 }
100
101 int
102 main (int argc, char **argv)
103 {
104         test_init (argc, argv, NULL);
105
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));
112
113         do_cookies_accept_policy_test ();
114
115         soup_uri_free (first_party_uri);
116         soup_uri_free (third_party_uri);
117         soup_test_server_quit_unref (server);
118
119         test_cleanup ();
120
121         return errors != 0;
122 }