10 #include "libsoup/soup.h"
11 #include "test-utils.h"
14 const char *explanation;
16 const guint final_status;
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 },
26 static int ntests = sizeof (tests) / sizeof (tests[0]);
28 #define HTTP_SERVER "http://127.0.0.1:47524"
29 #define HTTPS_SERVER "https://127.0.0.1:47525"
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"
41 static const char *proxy_names[] = {
43 "authenticated proxy",
44 "unauthenticatable-to proxy"
48 authenticate (SoupSession *session, SoupMessage *msg,
49 SoupAuth *auth, gboolean retrying, gpointer data)
52 soup_auth_authenticate (auth, "user1", "realm1");
56 test_url (const char *url, int proxy, guint expected, gboolean sync)
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;
66 /* We create a new session for each request to ensure that
67 * connections/auth aren't cached between tests.
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,
73 soup_uri_free (proxy_uri);
74 g_signal_connect (session, "authenticate",
75 G_CALLBACK (authenticate), NULL);
77 msg = soup_message_new (SOUP_METHOD_GET, url);
79 fprintf (stderr, "proxy-test: Could not parse URI\n");
83 soup_session_send_message (session, msg);
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);
92 soup_session_abort (session);
93 g_object_unref (session);
97 run_test (int i, gboolean sync)
99 char *http_url, *https_url;
101 debug_printf (1, "Test %d: %s (%s)\n", i + 1, tests[i].explanation,
102 sync ? "sync" : "async");
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);
108 http_url = g_strconcat (HTTP_SERVER, tests[i].url, NULL);
109 https_url = g_strconcat (HTTPS_SERVER, tests[i].url, NULL);
111 test_url (http_url, SIMPLE_PROXY, tests[i].final_status, sync);
113 test_url (https_url, SIMPLE_PROXY, tests[i].final_status, sync);
115 test_url (http_url, AUTH_PROXY, tests[i].final_status, sync);
117 test_url (https_url, AUTH_PROXY, tests[i].final_status, sync);
119 test_url (http_url, UNAUTH_PROXY, tests[i].final_status, sync);
121 test_url (https_url, UNAUTH_PROXY, tests[i].final_status, sync);
127 debug_printf (1, "\n");
131 main (int argc, char **argv)
135 test_init (argc, argv, NULL);
138 for (i = 0; i < ntests; i++) {