summaryrefslogtreecommitdiff
path: root/tests/proxy-continuous.c
blob: 2141dab71d743c2bcc210a1255278247a5d3240e (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * librest - RESTful web services access
 * Copyright (c) 2010 Intel Corporation.
 *
 * Authors: Rob Bradford <rob@linux.intel.com>
 *          Ross Burton <ross@linux.intel.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <config.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <libsoup/soup.h>
#include <rest/rest-proxy.h>

static int errors = 0;
static GMainLoop *loop = NULL;

#define NUM_CHUNKS 20
static int server_count = 0;
static gint client_count = 0;
static SoupServer *server;

static gboolean
send_chunks (gpointer user_data)
{
  SoupMessage *msg = SOUP_MESSAGE (user_data);
  char *s;
  SoupBuffer *buf;

  s = g_strdup_printf ("%d %d %d %d\n",
                       server_count, server_count,
                       server_count, server_count);
  buf = soup_buffer_new (SOUP_MEMORY_TAKE, s, strlen (s));

  soup_message_body_append_buffer (msg->response_body, buf);
  soup_server_unpause_message (server, msg);

  if (++server_count == NUM_CHUNKS)
  {
    soup_message_body_complete (msg->response_body);
    return FALSE;
  } else {
    return TRUE;
  }
}

static void
server_callback (SoupServer *server, SoupMessage *msg,
                 const char *path, GHashTable *query,
                 SoupClientContext *client, gpointer user_data)
{
  if (g_str_equal (path, "/stream")) {
    soup_message_set_status (msg, SOUP_STATUS_OK);
    soup_message_headers_set_encoding (msg->response_headers,
                                       SOUP_ENCODING_CHUNKED);
    soup_server_pause_message (server, msg);

    server_count = 1;
    g_idle_add (send_chunks, msg);
  }
}

static void
_call_continuous_cb (RestProxyCall *call,
                     const gchar   *buf,
                     gsize          len,
                     const GError  *error,
                     GObject       *weak_object,
                     gpointer       userdata)
{
  gint a = 0, b = 0, c = 0, d = 0;

  if (error)
  {
    g_printerr ("Error: %s\n", error->message);
    errors++;
    goto out;
  }

  if (!REST_IS_PROXY (weak_object))
  {
    g_printerr ("weak object not as expected\n");
    errors++;
    goto out;
  }

  if (buf == NULL && len == 0)
  {
    if (client_count != NUM_CHUNKS)
    {
      g_printerr ("stream ended prematurely\n");
      errors++;
    }
    goto out;
  }

  if (sscanf (buf, "%d %d %d %d\n", &a, &b, &c, &d) != 4)
  {
    g_printerr ("stream data not formatted as expected\n");
    errors++;
    goto out;
  }

  if (a != client_count ||
      b != client_count ||
      c != client_count ||
      d != client_count)
  {
    g_printerr ("stream data not as expected (got %d %d %d %d, expected %d)\n",
                a, b, c, d, client_count);
    errors++;
    goto out;
  }

  client_count++;
  return;
out:
  g_main_loop_quit (loop);
  return;
}

static void
stream_test (RestProxy *proxy)
{
  RestProxyCall *call;
  GError *error;

  client_count = 1;

  call = rest_proxy_new_call (proxy);
  rest_proxy_call_set_function (call, "stream");

  if (!rest_proxy_call_continuous (call,
                                   _call_continuous_cb,
                                   (GObject *)proxy,
                                   NULL,
                                   &error))
  {
    g_printerr ("Making stream failed: %s", error->message);
    g_error_free (error);
    errors++;
    return;
  }

  g_object_unref (call);
}

int
main (int argc, char **argv)
{
  SoupSession *session;
  char *url;
  RestProxy *proxy;

  g_type_init ();
  loop = g_main_loop_new (NULL, FALSE);

  session = soup_session_async_new ();

  server = soup_server_new (NULL);
  soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
  soup_server_run_async (server);

  url = g_strdup_printf ("http://127.0.0.1:%d/", soup_server_get_port (server));
  proxy = rest_proxy_new (url, FALSE);
  g_free (url);

  stream_test (proxy);
  g_main_loop_run (loop);

  return errors != 0;
}