9 #include "libsoup/soup.h"
10 #include "apache-wrapper.h"
15 const char *explanation;
17 const guint final_status;
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 },
27 int ntests = sizeof (tests) / sizeof (tests[0]);
29 #define HTTP_SERVER "http://127.0.0.1:47524"
30 #define HTTPS_SERVER "https://127.0.0.1:47525"
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"
42 static const char *proxy_names[] = {
44 "authenticated proxy",
45 "unauthenticatable-to proxy"
49 authenticate (SoupSession *session, SoupMessage *msg,
50 const char *auth_type, const char *auth_realm,
51 char **username, char **password, gpointer data)
53 *username = g_strdup ("user1");
54 *password = g_strdup ("realm1");
58 test_url (const char *url, int proxy, guint expected, gboolean sync)
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;
68 /* We create a new session for each request to ensure that
69 * connections/auth aren't cached between tests.
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,
75 soup_uri_free (proxy_uri);
76 g_signal_connect (session, "authenticate",
77 G_CALLBACK (authenticate), NULL);
79 msg = soup_message_new (SOUP_METHOD_GET, url);
81 fprintf (stderr, "proxy-test: Could not parse URI\n");
85 soup_session_send_message (session, msg);
87 printf (" %d %s\n", msg->status_code, msg->reason_phrase);
88 if (msg->status_code != expected) {
89 printf (" EXPECTED %d!\n", expected);
94 soup_session_abort (session);
95 g_object_unref (session);
99 run_test (int i, gboolean sync)
101 char *http_url, *https_url;
103 printf ("Test %d: %s (%s)\n", i + 1, tests[i].explanation,
104 sync ? "sync" : "async");
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);
110 http_url = g_strconcat (HTTP_SERVER, tests[i].url, NULL);
111 https_url = g_strconcat (HTTPS_SERVER, tests[i].url, NULL);
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);
127 main (int argc, char **argv)
132 g_thread_init (NULL);
134 if (!apache_init ()) {
135 fprintf (stderr, "Could not start apache\n");
139 for (i = 0; i < ntests; i++) {
146 printf ("proxy-test: %d errors\n", errors);