[librest] tests: Add test case for custom serialization



commit 5eea445286314730a86c01dd31700512f56275d5
Author: Michal Mhr <michal mhr gmail com>
Date:   Wed Apr 13 16:49:56 2011 +0100

    tests: Add test case for custom serialization

 tests/Makefile.am        |    3 +-
 tests/custom-serialize.c |  164 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 166 insertions(+), 1 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0172e62..5d77f9c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,4 +1,4 @@
-TESTS = proxy proxy-continuous threaded oauth oauth-async oauth2 flickr lastfm xml
+TESTS = proxy proxy-continuous threaded oauth oauth-async oauth2 flickr lastfm xml custom-serialize
 # TODO: fix this test case
 XFAIL_TESTS = xml
 
@@ -17,3 +17,4 @@ oauth2_SOURCES = oauth2.c
 flickr_SOURCES = flickr.c
 lastfm_SOURCES = lastfm.c
 xml_SOURCES = xml.c
+custom_serialize_SOURCES = custom-serialize.c
diff --git a/tests/custom-serialize.c b/tests/custom-serialize.c
new file mode 100644
index 0000000..f435070
--- /dev/null
+++ b/tests/custom-serialize.c
@@ -0,0 +1,164 @@
+/*
+ * librest - RESTful web services access
+ * Copyright (c) 2009 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 <libsoup/soup.h>
+#include <rest/rest-proxy.h>
+
+static volatile int errors = 0;
+static const gboolean verbose = FALSE;
+
+#define REST_TYPE_CUSTOM_PROXY_CALL custom_proxy_call_get_type()
+
+#define REST_CUSTOM_PROXY_CALL(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), REST_TYPE_CUSTOM_PROXY_CALL, CustomProxyCall))
+
+#define REST_CUSTOM_PROXY_CALL_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST ((klass), REST_TYPE_CUSTOM_PROXY_CALL, CustomProxyCallClass))
+
+#define REST_IS_CUSTOM_PROXY_CALL(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), REST_TYPE_CUSTOM_PROXY_CALL))
+
+#define REST_IS_CUSTOM_PROXY_CALL_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass), REST_TYPE_CUSTOM_PROXY_CALL))
+
+#define REST_CUSTOM_PROXY_CALL_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), REST_TYPE_CUSTOM_PROXY_CALL, CustomProxyCallClass))
+
+typedef struct {
+  RestProxyCall parent;
+} CustomProxyCall;
+
+typedef struct {
+  RestProxyCallClass parent_class;
+} CustomProxyCallClass;
+
+GType custom_proxy_call_get_type (void);
+
+G_DEFINE_TYPE (CustomProxyCall, custom_proxy_call, REST_TYPE_PROXY_CALL)
+
+static gboolean
+custom_proxy_call_serialize (RestProxyCall *self,
+                             gchar **content_type,
+                             gchar **content, gsize *content_len,
+                             GError **error)
+{
+  g_return_val_if_fail (REST_IS_CUSTOM_PROXY_CALL (self), FALSE);
+
+  *content_type = g_strdup ("application/json");
+  *content = g_strdup ("{}");
+  *content_len = strlen (*content);
+
+  return TRUE;
+}
+
+static void
+custom_proxy_call_class_init (CustomProxyCallClass *klass)
+{
+  RestProxyCallClass *parent_klass = (RestProxyCallClass*) klass;
+
+  parent_klass->serialize_params = custom_proxy_call_serialize;
+}
+
+static void
+custom_proxy_call_init (CustomProxyCall *self)
+{
+}
+
+static void
+server_callback (SoupServer *server, SoupMessage *msg,
+                 const char *path, GHashTable *query,
+                 SoupClientContext *client, gpointer user_data)
+{
+  if (g_str_equal (path, "/ping")) {
+    const char *content_type = NULL;
+    SoupMessageHeaders *headers = msg->request_headers;
+    SoupMessageBody *body = msg->request_body;
+    content_type = soup_message_headers_get_content_type (headers, NULL);
+    g_assert_cmpstr (content_type, ==, "application/json");
+
+    g_assert_cmpstr (body->data, ==, "{}");
+
+    soup_message_set_status (msg, SOUP_STATUS_OK);
+  } else {
+    soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
+  }
+}
+
+static gpointer
+func (gpointer data)
+{
+  return NULL;
+}
+
+int
+main (int argc, char **argv)
+{
+  SoupSession *session;
+  SoupServer *server;
+  RestProxy *proxy;
+  RestProxyCall *call;
+  char *url;
+  int i;
+  GError *error;
+
+  g_thread_init (NULL);
+  g_type_init ();
+
+  session = soup_session_sync_new ();
+
+  server = soup_server_new (NULL);
+  soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
+  g_thread_create ((GThreadFunc)soup_server_run, server, FALSE, NULL);
+
+  url = g_strdup_printf ("http://127.0.0.1:%d/";, soup_server_get_port (server));
+
+  proxy = rest_proxy_new (url, FALSE);
+  call = g_object_new (REST_TYPE_CUSTOM_PROXY_CALL, "proxy", proxy, NULL);
+
+  rest_proxy_call_set_function (call, "ping");
+
+  if (!rest_proxy_call_sync (call, &error)) {
+    g_printerr ("Call failed: %s\n", error->message);
+    g_error_free (error);
+    g_atomic_int_add (&errors, 1);
+    goto done;
+  }
+
+  if (rest_proxy_call_get_status_code (call) != SOUP_STATUS_OK) {
+    g_printerr ("Wrong response code, got %d\n", rest_proxy_call_get_status_code (call));
+    g_atomic_int_add (&errors, 1);
+    goto done;
+  }
+
+  done:
+  
+  g_object_unref (call);
+  g_object_unref (proxy);
+  soup_server_quit (server);
+  g_free (url);
+
+  return errors != 0;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]