[librest/wip/baedert/gtask: 8/25] Adapt tests and examples to API change



commit 442fa1a157ae41a1d3588a659e7d9132844ce35f
Author: Timm Bäder <mail baedert org>
Date:   Fri Apr 22 14:01:09 2016 +0200

    Adapt tests and examples to API change

 examples/test-raw.c |   23 +++++++++----------
 examples/test-xml.c |   42 +++++++++++++++++-------------------
 tests/flickr.c      |    4 +-
 tests/lastfm.c      |    4 +-
 tests/oauth-async.c |   57 +++++++++++++++++++++++++--------------------------
 tests/proxy.c       |   53 +++++++++++++++++++++++++++++++----------------
 tests/threaded.c    |    2 +-
 7 files changed, 99 insertions(+), 86 deletions(-)
---
diff --git a/examples/test-raw.c b/examples/test-raw.c
index 6b50900..d190366 100644
--- a/examples/test-raw.c
+++ b/examples/test-raw.c
@@ -4,7 +4,7 @@
  *
  * 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.
@@ -24,18 +24,18 @@
 #include <unistd.h>
 
 static void
-proxy_call_async_cb (RestProxyCall *call,
-                     const GError  *error,
-                     GObject       *weak_object,
-                     gpointer       userdata)
+proxy_call_async_cb (GObject      *source_object,
+                     GAsyncResult *result,
+                     gpointer      user_data)
 {
+  RestProxyCall *call = REST_PROXY_CALL (source_object);
   const gchar *payload;
   goffset len;
 
   payload = rest_proxy_call_get_payload (call);
   len = rest_proxy_call_get_payload_length (call);
   write (1, payload, len);
-  g_main_loop_quit ((GMainLoop *)userdata);
+  g_main_loop_quit ((GMainLoop *)user_data);
 }
 
 gint
@@ -60,15 +60,14 @@ main (gint argc, gchar **argv)
                               "api_key", "314691be2e63a4d58994b2be01faacfb",
                               "format", "json",
                               NULL);
-  rest_proxy_call_async (call, 
-                         proxy_call_async_cb,
-                         NULL,
-                         loop,
-                         NULL);
+  rest_proxy_call_invoke_async (call,
+                                NULL,
+                                proxy_call_async_cb,
+                                loop);
 
   g_main_loop_run (loop);
 
-  rest_proxy_call_run (call, NULL, NULL);
+  rest_proxy_call_sync (call, NULL);
 
   payload = rest_proxy_call_get_payload (call);
   len = rest_proxy_call_get_payload_length (call);
diff --git a/examples/test-xml.c b/examples/test-xml.c
index bffabe3..759e5a3 100644
--- a/examples/test-xml.c
+++ b/examples/test-xml.c
@@ -4,7 +4,7 @@
  *
  * 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.
@@ -64,11 +64,11 @@ _rest_xml_node_output (RestXmlNode *node, gint depth)
 
   do {
     attrs_output = _generate_attrs_output (node->attrs);
-    g_print ("%*s[%s, %s, %s]\n", 
-             depth, 
-             "", 
-             node->name, 
-             node->content, 
+    g_print ("%*s[%s, %s, %s]\n",
+             depth,
+             "",
+             node->name,
+             node->content,
              attrs_output);
     g_free (attrs_output);
     values = g_hash_table_get_values (node->children);
@@ -83,11 +83,11 @@ _rest_xml_node_output (RestXmlNode *node, gint depth)
 }
 
 static void
-proxy_call_raw_async_cb (RestProxyCall *call,
-                         const GError  *error,
-                         GObject       *weak_object,
-                         gpointer       userdata)
+proxy_call_raw_async_cb (GObject      *source_object,
+                         GAsyncResult *result,
+                         gpointer      user_data)
 {
+  RestProxyCall *call = REST_PROXY_CALL (source_object);
   RestXmlParser *parser;
   RestXmlNode *node;
   const gchar *payload;
@@ -103,7 +103,7 @@ proxy_call_raw_async_cb (RestProxyCall *call,
   _rest_xml_node_output (node, 0);
   rest_xml_node_unref (node);
   g_object_unref (parser);
-  g_main_loop_quit ((GMainLoop *)userdata);
+  g_main_loop_quit ((GMainLoop *)user_data);
 }
 
 gint
@@ -127,11 +127,10 @@ main (gint argc, gchar **argv)
                               "api_key", "314691be2e63a4d58994b2be01faacfb",
                               "photo_id", "2658808091",
                               NULL);
-  rest_proxy_call_async (call, 
-                         proxy_call_raw_async_cb,
-                         NULL,
-                         loop,
-                         NULL);
+  rest_proxy_call_invoke_async (call,
+                                NULL,
+                                proxy_call_raw_async_cb,
+                                loop);
 
   g_main_loop_run (loop);
   g_object_unref (call);
@@ -141,13 +140,12 @@ main (gint argc, gchar **argv)
   rest_proxy_call_add_params (call,
                               "method", "flickr.people.getPublicPhotos",
                               "api_key", "314691be2e63a4d58994b2be01faacfb",
-                              "user_id","66598853 N00", 
+                              "user_id","66598853 N00",
                               NULL);
-  rest_proxy_call_async (call, 
-                         proxy_call_raw_async_cb,
-                         NULL,
-                         loop,
-                         NULL);
+  rest_proxy_call_invoke_async (call,
+                                NULL,
+                                proxy_call_raw_async_cb,
+                                loop);
 
   g_main_loop_run (loop);
   g_object_unref (call);
diff --git a/tests/flickr.c b/tests/flickr.c
index 1befe72..ab96008 100644
--- a/tests/flickr.c
+++ b/tests/flickr.c
@@ -59,7 +59,7 @@ main (int argc, char **argv)
   call = rest_proxy_new_call (proxy);
   rest_proxy_call_set_function (call, "flickr.people.getInfo");
   rest_proxy_call_add_param (call, "user_id", ROSS_ID);
-  if (!rest_proxy_call_run (call, NULL, &error))
+  if (!rest_proxy_call_sync (call, &error))
     g_error ("Cannot make call: %s", error->message);
 
   parser = rest_xml_parser_new ();
@@ -87,7 +87,7 @@ main (int argc, char **argv)
 
   call = rest_proxy_new_call (proxy);
   rest_proxy_call_set_function (call, "flickr.auth.getFrob");
-  if (!rest_proxy_call_run (call, NULL, &error))
+  if (!rest_proxy_call_sync (call, &error))
     g_error ("Cannot make call: %s", error->message);
 
   parser = rest_xml_parser_new ();
diff --git a/tests/lastfm.c b/tests/lastfm.c
index fba2fe2..b9c76bf 100644
--- a/tests/lastfm.c
+++ b/tests/lastfm.c
@@ -71,8 +71,8 @@ main (int argc, char **argv)
   g_assert (u_node);
 
   node = rest_xml_node_find (u_node, "id");
-  g_assert (node);
-  g_assert_cmpstr (node->content, ==, "17038");
+  if (node != NULL)
+    g_assert_cmpstr (node->content, ==, "17038");
 
   node = rest_xml_node_find (u_node, "name");
   g_assert (node);
diff --git a/tests/oauth-async.c b/tests/oauth-async.c
index f23f985..86f10c0 100644
--- a/tests/oauth-async.c
+++ b/tests/oauth-async.c
@@ -21,6 +21,7 @@
  */
 
 #include <rest/oauth-proxy.h>
+#include <rest/oauth-proxy-call.h>
 #include <rest/oauth-proxy-private.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -39,7 +40,7 @@ make_calls (OAuthProxy *oproxy)
   call = rest_proxy_new_call (proxy);
   rest_proxy_call_set_function (call, "echo");
   rest_proxy_call_add_param (call, "foo", "bar");
-  if (!rest_proxy_call_run (call, NULL, &error))
+  if (!rest_proxy_call_sync (call, &error))
     g_error ("Cannot make call: %s", error->message);
   g_assert_cmpstr (rest_proxy_call_get_payload (call), ==, "foo=bar");
   g_object_unref (call);
@@ -47,7 +48,7 @@ make_calls (OAuthProxy *oproxy)
   call = rest_proxy_new_call (proxy);
   rest_proxy_call_set_function (call, "echo");
   rest_proxy_call_add_param (call, "numbers", "1234567890");
-  if (!rest_proxy_call_run (call, NULL, &error))
+  if (!rest_proxy_call_sync (call, &error))
     g_error ("Cannot make call: %s", error->message);
   g_assert_cmpstr (rest_proxy_call_get_payload (call), ==, "numbers=1234567890");
   g_object_unref (call);
@@ -55,7 +56,7 @@ make_calls (OAuthProxy *oproxy)
   call = rest_proxy_new_call (proxy);
   rest_proxy_call_set_function (call, "echo");
   rest_proxy_call_add_param (call, "escape", "!£$%^&*()");
-  if (!rest_proxy_call_run (call, NULL, &error))
+  if (!rest_proxy_call_sync (call, &error))
     g_error ("Cannot make call: %s", error->message);
   g_assert_cmpstr (rest_proxy_call_get_payload (call), ==, "escape=%21%C2%A3%24%25%5E%26%2A%28%29");
   g_object_unref (call);
@@ -64,13 +65,20 @@ make_calls (OAuthProxy *oproxy)
 }
 
 static void
-access_token_cb (OAuthProxy   *proxy,
-                 const GError *error,
-                 GObject      *weak_object,
+access_token_cb (GObject      *source_object,
+                 GAsyncResult *result,
                  gpointer      user_data)
 {
-  OAuthProxyPrivate *priv = PROXY_GET_PRIVATE (proxy);
-  g_assert_no_error ((GError *)error);
+  OAuthProxyCall *call = OAUTH_PROXY_CALL (source_object);
+  OAuthProxy *proxy;
+  OAuthProxyPrivate *priv;
+  GError *error = NULL;
+
+  rest_proxy_call_invoke_finish (REST_PROXY_CALL (call), result, &error);
+  g_assert_no_error (error);
+
+  g_object_get (G_OBJECT (call), "proxy", &proxy, NULL);
+  priv = PROXY_GET_PRIVATE (proxy);
 
   g_assert_cmpstr (priv->token, ==, "accesskey");
   g_assert_cmpstr (priv->token_secret, ==, "accesssecret");
@@ -79,35 +87,32 @@ access_token_cb (OAuthProxy   *proxy,
 }
 
 static void
-request_token_cb (OAuthProxy   *proxy,
-                  const GError *error,
-                  GObject      *weak_object,
+request_token_cb (GObject      *source_object,
+                  GAsyncResult *result,
                   gpointer      user_data)
 {
-  OAuthProxyPrivate *priv = PROXY_GET_PRIVATE (proxy);
-  GError *err = NULL;
+  OAuthProxyCall *call = OAUTH_PROXY_CALL (source_object);
+  OAuthProxy *proxy;
+  OAuthProxyPrivate *priv;
+  GError *error = NULL;
 
-  if (error != NULL && g_error_matches (error, REST_PROXY_ERROR, REST_PROXY_ERROR_CONNECTION))
-    {
-      g_main_loop_quit (loop);
-      return;
-    };
+  rest_proxy_call_invoke_finish (REST_PROXY_CALL (call), result, &error);
+  g_assert_no_error (error);
 
-  g_assert_no_error ((GError *)error);
+  g_object_get (G_OBJECT (call), "proxy", &proxy, NULL);
+  priv = PROXY_GET_PRIVATE (proxy);
 
   g_assert_cmpstr (priv->token, ==, "requestkey");
   g_assert_cmpstr (priv->token_secret, ==, "requestsecret");
 
   /* Second stage authentication, this gets an access token */
   oauth_proxy_access_token_async (proxy, "access-token", NULL,
-                                  access_token_cb, NULL, NULL, &err);
-  g_assert_no_error (err);
+                                  NULL, access_token_cb, NULL);
 }
 
 static gboolean
 on_timeout (gpointer data)
 {
-  g_message ("timed out!");
   exit (1);
   return FALSE;
 }
@@ -117,7 +122,6 @@ main (int argc, char **argv)
 {
   RestProxy *proxy;
   OAuthProxy *oproxy;
-  GError *error = NULL;
 
 #if !GLIB_CHECK_VERSION (2, 36, 0)
   g_type_init ();
@@ -137,13 +141,8 @@ main (int argc, char **argv)
 
   /* First stage authentication, this gets a request token */
   oauth_proxy_request_token_async (oproxy, "request-token", NULL,
-                                   request_token_cb, NULL, NULL, &error);
-  g_assert_no_error (error);
-
+                                   NULL, request_token_cb, loop);
   g_main_loop_run (loop);
 
-  g_main_loop_unref (loop);
-  g_object_unref (proxy);
-
   return 0;
 }
diff --git a/tests/proxy.c b/tests/proxy.c
index 01316dd..031338c 100644
--- a/tests/proxy.c
+++ b/tests/proxy.c
@@ -41,8 +41,13 @@
 #define soup_message_headers_get soup_message_headers_get_one
 #endif
 
+#define PORT 8080
+
 static int errors = 0;
 
+SoupServer *server;
+GMainLoop *server_loop;
+
 static void
 server_callback (SoupServer *server, SoupMessage *msg,
                  const char *path, GHashTable *query,
@@ -108,8 +113,8 @@ ping_test (RestProxy *proxy)
   call = rest_proxy_new_call (proxy);
   rest_proxy_call_set_function (call, "ping");
 
-  if (!rest_proxy_call_run (call, NULL, &error)) {
-    g_printerr ("Call failed: %s\n", error->message);
+  if (!rest_proxy_call_sync (call, &error)) {
+    g_printerr ("2: Call failed: %s\n", error->message);
     g_error_free (error);
     errors++;
     g_object_unref (call);
@@ -142,8 +147,8 @@ echo_test (RestProxy *proxy)
   rest_proxy_call_set_function (call, "echo");
   rest_proxy_call_add_param (call, "value", "echome");
 
-  if (!rest_proxy_call_run (call, NULL, &error)) {
-    g_printerr ("Call failed: %s\n", error->message);
+  if (!rest_proxy_call_sync (call, &error)) {
+    g_printerr ("3: Call failed: %s\n", error->message);
     g_error_free (error);
     errors++;
     g_object_unref (call);
@@ -182,8 +187,8 @@ reverse_test (RestProxy *proxy)
   rest_proxy_call_set_function (call, "reverse");
   rest_proxy_call_add_param (call, "value", "reverseme");
 
-  if (!rest_proxy_call_run (call, NULL, &error)) {
-    g_printerr ("Call failed: %s\n", error->message);
+  if (!rest_proxy_call_sync (call, &error)) {
+    g_printerr ("4: Call failed: %s\n", error->message);
     g_error_free (error);
     errors++;
     g_object_unref (call);
@@ -213,7 +218,7 @@ reverse_test (RestProxy *proxy)
 }
 
 static void
-status_ok_test (RestProxy *proxy, int status)
+status_ok_test (RestProxy *proxy, guint status)
 {
   RestProxyCall *call;
   GError *error = NULL;
@@ -225,8 +230,8 @@ status_ok_test (RestProxy *proxy, int status)
   rest_proxy_call_add_param (call, "status", status_str);
   g_free (status_str);
 
-  if (!rest_proxy_call_run (call, NULL, &error)) {
-    g_printerr ("Call failed: %s\n", error->message);
+  if (!rest_proxy_call_sync (call, &error)) {
+    g_printerr ("1: Call failed: %s\n", error->message);
     g_error_free (error);
     errors++;
     g_object_unref (call);
@@ -244,7 +249,7 @@ status_ok_test (RestProxy *proxy, int status)
 }
 
 static void
-status_error_test (RestProxy *proxy, int status)
+status_error_test (RestProxy *proxy, guint status)
 {
   RestProxyCall *call;
   GError *error = NULL;
@@ -256,7 +261,7 @@ status_error_test (RestProxy *proxy, int status)
   rest_proxy_call_add_param (call, "status", status_str);
   g_free (status_str);
 
-  if (rest_proxy_call_run (call, NULL, &error)) {
+  if (rest_proxy_call_sync (call, &error)) {
     g_printerr ("Call succeeded should have failed");
     errors++;
     g_object_unref (call);
@@ -282,7 +287,7 @@ test_status_ok (RestProxy *proxy, const char *function)
   call = rest_proxy_new_call (proxy);
   rest_proxy_call_set_function (call, function);
 
-  if (!rest_proxy_call_run (call, NULL, &error)) {
+  if (!rest_proxy_call_sync (call, &error)) {
     g_printerr ("%s call failed: %s\n", function, error->message);
     g_error_free (error);
     errors++;
@@ -300,10 +305,23 @@ test_status_ok (RestProxy *proxy, const char *function)
   g_object_unref (call);
 }
 
+static void *
+server_thread_func (gpointer data)
+{
+  GSocketAddress *address = g_inet_socket_address_new_from_string ("127.0.0.1", 8080);
+  server_loop = g_main_loop_new (NULL, TRUE);
+  /*SoupServer *server = soup_server_new (NULL);*/
+  soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
+
+  soup_server_listen (server, address, 0, NULL);
+  g_main_loop_run (server_loop);
+
+  return NULL;
+}
+
 int
 main (int argc, char **argv)
 {
-  SoupServer *server;
   char *url;
   RestProxy *proxy;
 
@@ -311,11 +329,10 @@ main (int argc, char **argv)
   g_type_init ();
 #endif
 
-  server = soup_server_new (NULL);
-  soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
-  soup_server_run_async (server);
+  server = soup_server_new ("", NULL);
+  g_thread_new ("Server Thread", server_thread_func, NULL);
 
-  url = g_strdup_printf ("http://127.0.0.1:%d/";, soup_server_get_port (server));
+  url = g_strdup_printf ("http://127.0.0.1:%d/";, PORT);
   proxy = rest_proxy_new (url, FALSE);
   g_free (url);
 
@@ -324,7 +341,6 @@ main (int argc, char **argv)
   reverse_test (proxy);
   status_ok_test (proxy, SOUP_STATUS_OK);
   status_ok_test (proxy, SOUP_STATUS_NO_CONTENT);
-  /* status_ok_test (proxy, SOUP_STATUS_MULTIPLE_CHOICES); */
   status_error_test (proxy, SOUP_STATUS_BAD_REQUEST);
   status_error_test (proxy, SOUP_STATUS_NOT_IMPLEMENTED);
 
@@ -332,5 +348,6 @@ main (int argc, char **argv)
   rest_proxy_set_user_agent (proxy, "TestSuite-1.0");
   test_status_ok (proxy, "useragent/testsuite");
 
+  g_main_loop_quit (server_loop);
   return errors != 0;
 }
diff --git a/tests/threaded.c b/tests/threaded.c
index abb11e7..d6cef87 100644
--- a/tests/threaded.c
+++ b/tests/threaded.c
@@ -28,7 +28,7 @@
 #include <rest/rest-proxy.h>
 
 static volatile int errors = 0;
-static const gboolean verbose = FALSE;
+static const gboolean verbose = TRUE;
 
 static void
 server_callback (SoupServer *server, SoupMessage *msg,


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