soup-multipart-input-stream: belatedly add .h file to soup.h
[platform/upstream/libsoup.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 "test-utils.h"
7
8 SoupServer *server;
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/";
12
13 static void
14 server_callback (SoupServer *server, SoupMessage *msg,
15                  const char *path, GHashTable *query,
16                  SoupClientContext *context, gpointer data)
17 {
18         if (g_str_equal (path, "/index.html")) {
19                 soup_message_headers_replace (msg->response_headers,
20                                               "Set-Cookie",
21                                               "foo=bar");
22         } else if (g_str_equal (path, "/foo.jpg")) {
23                 soup_message_headers_replace (msg->response_headers,
24                                               "Set-Cookie",
25                                               "baz=qux");
26         } else if (soup_message_headers_get_one (msg->request_headers,
27                                                  "Echo-Set-Cookie")) {
28                 soup_message_headers_replace (msg->response_headers,
29                                               "Set-Cookie",
30                                               soup_message_headers_get_one (msg->request_headers,
31                                                                             "Echo-Set-Cookie"));
32         }
33
34         soup_message_set_status (msg, SOUP_STATUS_OK);
35 }
36
37 typedef struct {
38         SoupCookieJarAcceptPolicy policy;
39         int n_cookies;
40 } CookiesForPolicy;
41
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 }
46 };
47
48 static void
49 do_cookies_accept_policy_test (void)
50 {
51         SoupSession *session;
52         SoupMessage *msg;
53         SoupURI *uri;
54         SoupCookieJar *jar;
55         GSList *l, *p;
56         int i;
57
58         debug_printf (1, "SoupCookieJarAcceptPolicy test\n");
59
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));
63
64         for (i = 0; i < G_N_ELEMENTS (validResults); i++) {
65                 soup_cookie_jar_set_accept_policy (jar, validResults[i].policy);
66
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);
71                 soup_uri_free (uri);
72                 g_object_unref (msg);
73
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.
77                  */
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);
82                 soup_uri_free (uri);
83                 g_object_unref (msg);
84
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");
88                         errors++;
89                 } else if (g_slist_length (l) > validResults[i].n_cookies) {
90                         debug_printf (1, " accepted more cookies than it should have\n");
91                         errors++;
92                 }
93
94                 for (p = l; p; p = p->next) {
95                         soup_cookie_jar_delete_cookie (jar, p->data);
96                         soup_cookie_free (p->data);
97                 }
98
99                 g_slist_free (l);
100         }
101
102         soup_test_session_abort_unref (session);
103 }
104
105 /* FIXME: moar tests! */
106 static void
107 do_cookies_parsing_test (void)
108 {
109         SoupSession *session;
110         SoupMessage *msg;
111         SoupCookieJar *jar;
112         GSList *cookies, *iter;
113         SoupCookie *cookie;
114         gboolean got1, got2, got3;
115
116         debug_printf (1, "\nSoupCookie parsing test\n");
117
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));
121
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);
128
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);
134
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);
140
141         cookies = soup_cookie_jar_get_cookie_list (jar, first_party_uri, TRUE);
142         got1 = got2 = got3 = FALSE;
143
144         for (iter = cookies; iter; iter = iter->next) {
145                 cookie = iter->data;
146
147                 if (!strcmp (soup_cookie_get_name (cookie), "one")) {
148                         got1 = TRUE;
149                         if (!soup_cookie_get_http_only (cookie)) {
150                                 debug_printf (1, "  cookie 1 is not HttpOnly!\n");
151                                 errors++;
152                         }
153                         if (!soup_cookie_get_expires (cookie)) {
154                                 debug_printf (1, "  cookie 1 did not fully parse!\n");
155                                 errors++;
156                         }
157                 } else if (!strcmp (soup_cookie_get_name (cookie), "two")) {
158                         got2 = TRUE;
159                         if (!soup_cookie_get_http_only (cookie)) {
160                                 debug_printf (1, "  cookie 2 is not HttpOnly!\n");
161                                 errors++;
162                         }
163                         if (!soup_cookie_get_expires (cookie)) {
164                                 debug_printf (1, "  cookie 3 did not fully parse!\n");
165                                 errors++;
166                         }
167                 } else if (!strcmp (soup_cookie_get_name (cookie), "three")) {
168                         got3 = TRUE;
169                         if (!soup_cookie_get_http_only (cookie)) {
170                                 debug_printf (1, "  cookie 3 is not HttpOnly!\n");
171                                 errors++;
172                         }
173                         if (!soup_cookie_get_expires (cookie)) {
174                                 debug_printf (1, "  cookie 3 did not fully parse!\n");
175                                 errors++;
176                         }
177                 } else {
178                         debug_printf (1, "  got unexpected cookie '%s'\n",
179                                       soup_cookie_get_name (cookie));
180                         errors++;
181                 }
182
183                 soup_cookie_free (cookie);
184         }
185         g_slist_free (cookies);
186
187         if (!got1) {
188                 debug_printf (1, "  didn't get cookie 1\n");
189                 errors++;
190         }
191         if (!got2) {
192                 debug_printf (1, "  didn't get cookie 2\n");
193                 errors++;
194         }
195         if (!got3) {
196                 debug_printf (1, "  didn't get cookie 3\n");
197                 errors++;
198         }
199
200         soup_test_session_abort_unref (session);
201 }       
202
203 int
204 main (int argc, char **argv)
205 {
206         test_init (argc, argv, NULL);
207
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));
214
215         do_cookies_accept_policy_test ();
216         do_cookies_parsing_test ();
217
218         soup_uri_free (first_party_uri);
219         soup_uri_free (third_party_uri);
220         soup_test_server_quit_unref (server);
221
222         test_cleanup ();
223
224         return errors != 0;
225 }