summaryrefslogtreecommitdiff
path: root/tests/unix-socket-test.c
blob: 3979a6be2f438e19bca75097d4f8ddfb950fb39e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */

#include "test-utils.h"

#include <gio/gunixsocketaddress.h>

static SoupServer *server;

static void
server_callback (SoupServer        *server,
                 SoupServerMessage *msg,
                 const char        *path,
                 GHashTable        *query,
                 gpointer           data)
{
        const char *method;

        method = soup_server_message_get_method (msg);
        if (method != SOUP_METHOD_GET) {
                soup_server_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED, NULL);
                return;
        }

        soup_server_message_set_status (msg, SOUP_STATUS_OK, NULL);
        soup_server_message_set_response (msg, "application/json",
                                          SOUP_MEMORY_STATIC, "{\"count\":42}", 12);
}

static void
do_load_uri_test (void)
{
        SoupSession *session;
        GSocketAddress *address;
        SoupMessage *msg;
        GBytes *body;
        const char *content_type;
        char *json;
        GSocketAddress *remote_address;
        GError *error = NULL;

        address = g_unix_socket_address_new (soup_test_server_get_unix_path (server));
        session = soup_test_session_new ("remote-connectable", address, NULL);
        g_object_unref (address);

        msg = soup_message_new (SOUP_METHOD_GET, "http://locahost/foo");
        body = soup_session_send_and_read (session, msg, NULL, &error);
        g_assert_no_error (error);
        g_assert_nonnull (body);

        remote_address = soup_message_get_remote_address (msg);
        g_assert_nonnull (remote_address);
        g_assert_true (G_IS_UNIX_SOCKET_ADDRESS (remote_address));
        g_assert_cmpstr (g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (remote_address)), ==, soup_test_server_get_unix_path (server));

        content_type = soup_message_headers_get_one (soup_message_get_response_headers (msg), "Content-Type");
        g_assert_cmpstr (content_type, ==, "application/json");
        g_object_unref (msg);

        json = g_strndup (g_bytes_get_data (body, NULL), g_bytes_get_size (body));
        g_assert_cmpstr (json, ==, "{\"count\":42}");
        g_free (json);
        g_bytes_unref (body);

        soup_test_session_abort_unref (session);
}

int
main (int argc,
      char *argv[])
{
        int ret;

        test_init (argc, argv, NULL);

        server = soup_test_server_new (SOUP_TEST_SERVER_IN_THREAD | SOUP_TEST_SERVER_UNIX_SOCKET);
        soup_server_add_handler (server, NULL,
                                 server_callback, NULL, NULL);

        g_test_add_func ("/unix-socket/load-uri", do_load_uri_test);

        ret = g_test_run ();

        soup_test_server_quit_unref (server);

        test_cleanup ();
        return ret;
}