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