update the handling of CONNECT: it has no response body by default, but
[platform/upstream/libsoup.git] / tests / proxy-test.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "libsoup/soup.h"
10 #include "apache-wrapper.h"
11
12 int errors = 0;
13
14 typedef struct {
15         const char *explanation;
16         const char *url;
17         const guint final_status;
18 } SoupProxyTest;
19
20 SoupProxyTest tests[] = {
21         { "GET -> 200", "", SOUP_STATUS_OK },
22         { "GET -> 404", "/not-found", SOUP_STATUS_NOT_FOUND },
23         { "GET -> 401 -> 200", "/Basic/realm1/", SOUP_STATUS_OK },
24         { "GET -> 401 -> 401", "/Basic/realm2/", SOUP_STATUS_UNAUTHORIZED },
25         { "GET -> 403", "http://no-proxy.example.com/", SOUP_STATUS_FORBIDDEN },
26 };
27 int ntests = sizeof (tests) / sizeof (tests[0]);
28
29 #define HTTP_SERVER    "http://127.0.0.1:47524"
30 #define HTTPS_SERVER   "https://127.0.0.1:47525"
31
32 enum {
33         SIMPLE_PROXY,
34         AUTH_PROXY,
35         UNAUTH_PROXY
36 };
37 static const char *proxies[] = {
38         "http://127.0.0.1:47526",
39         "http://127.0.0.1:47527",
40         "http://127.0.0.1:47528"
41 };
42 static const char *proxy_names[] = {
43         "simple proxy",
44         "authenticated proxy",
45         "unauthenticatable-to proxy"
46 };
47
48 static void
49 authenticate (SoupSession *session, SoupMessage *msg,
50               const char *auth_type, const char *auth_realm,
51               char **username, char **password, gpointer data)
52 {
53         *username = g_strdup ("user1");
54         *password = g_strdup ("realm1");
55 }
56
57 static void
58 test_url (const char *url, int proxy, guint expected, gboolean sync)
59 {
60         SoupSession *session;
61         SoupUri *proxy_uri;
62         SoupMessage *msg;
63
64         printf ("  GET %s via %s\n", url, proxy_names[proxy]);
65         if (proxy == UNAUTH_PROXY && expected != SOUP_STATUS_FORBIDDEN)
66                 expected = SOUP_STATUS_PROXY_UNAUTHORIZED;
67
68         /* We create a new session for each request to ensure that
69          * connections/auth aren't cached between tests.
70          */
71         proxy_uri = soup_uri_new (proxies[proxy]);
72         session = g_object_new (sync ? SOUP_TYPE_SESSION_SYNC : SOUP_TYPE_SESSION_ASYNC,
73                                 SOUP_SESSION_PROXY_URI, proxy_uri,
74                                 NULL);
75         soup_uri_free (proxy_uri);
76         g_signal_connect (session, "authenticate",
77                           G_CALLBACK (authenticate), NULL);
78
79         msg = soup_message_new (SOUP_METHOD_GET, url);
80         if (!msg) {
81                 fprintf (stderr, "proxy-test: Could not parse URI\n");
82                 exit (1);
83         }
84
85         soup_session_send_message (session, msg);
86
87         printf ("  %d %s\n", msg->status_code, msg->reason_phrase);
88         if (msg->status_code != expected) {
89                 printf ("  EXPECTED %d!\n", expected);
90                 errors++;
91         }
92
93         g_object_unref (msg);
94         soup_session_abort (session);
95         g_object_unref (session);
96 }
97
98 static void
99 run_test (int i, gboolean sync)
100 {
101         char *http_url, *https_url;
102
103         printf ("Test %d: %s (%s)\n", i + 1, tests[i].explanation,
104                 sync ? "sync" : "async");
105
106         if (!strncmp (tests[i].url, "http", 4)) {
107                 http_url = g_strdup (tests[i].url);
108                 https_url = g_strdup_printf ("https%s", tests[i].url + 4);
109         } else {
110                 http_url = g_strconcat (HTTP_SERVER, tests[i].url, NULL);
111                 https_url = g_strconcat (HTTPS_SERVER, tests[i].url, NULL);
112         }
113         test_url (http_url, SIMPLE_PROXY, tests[i].final_status, sync);
114         test_url (https_url, SIMPLE_PROXY, tests[i].final_status, sync);
115         test_url (http_url, AUTH_PROXY, tests[i].final_status, sync);
116         test_url (https_url, AUTH_PROXY, tests[i].final_status, sync);
117         test_url (http_url, UNAUTH_PROXY, tests[i].final_status, sync);
118         test_url (https_url, UNAUTH_PROXY, tests[i].final_status, sync);
119
120         g_free (http_url);
121         g_free (https_url);
122
123         printf ("\n");
124 }
125
126 int
127 main (int argc, char **argv)
128 {
129         int i;
130
131         g_type_init ();
132         g_thread_init (NULL);
133
134         if (!apache_init ()) {
135                 fprintf (stderr, "Could not start apache\n");
136                 return 1;
137         }
138
139         for (i = 0; i < ntests; i++) {
140                 run_test (i, FALSE);
141                 run_test (i, TRUE);
142         }
143
144         apache_cleanup ();
145
146         printf ("proxy-test: %d errors\n", errors);
147         return errors;
148 }