2009-06-10 Gustavo Noronha Silva <gns@gnome.org>
authorkov@webkit.org <kov@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 11 Jun 2009 02:48:29 +0000 (02:48 +0000)
committerkov@webkit.org <kov@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 11 Jun 2009 02:48:29 +0000 (02:48 +0000)
        Reviewed by Xan Lopez.

        Add unit tests for our WebKitNetworkRequest object.

        * GNUmakefile.am:

WebKit/gtk

2009-06-10  Gustavo Noronha Silva  <gns@gnome.org>

        Reviewed by Xan Lopez.

        Make WebKitNetworkRequest a proper GObject, making URI and
        SoupMessage properties. Also adding unit tests for creation and
        destruction.

        * tests/testnetworkrequest.c: Added.
        (test_network_request_create_destroy):
        (test_network_request_properties):
        (main):
        * webkit/webkitnetworkrequest.cpp:
        (webkit_network_request_get_property):
        (webkit_network_request_set_property):
        (webkit_network_request_class_init):
        (webkit_network_request_init):
        (webkit_network_request_constructor):
        (webkit_network_request_new_with_core_request):
        (webkit_network_request_new):
        (webkit_network_request_set_uri):
        (webkit_network_request_get_uri):
        * webkit/webkitnetworkrequest.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@44598 268f45cc-cd09-0410-ab3c-d52691b4dbfc

ChangeLog
GNUmakefile.am
WebKit/gtk/ChangeLog
WebKit/gtk/tests/testnetworkrequest.c [new file with mode: 0644]
WebKit/gtk/webkit/webkitnetworkrequest.cpp
WebKit/gtk/webkit/webkitnetworkrequest.h
WebKit/gtk/webkit/webkitwebframe.cpp

index b114a9c..addc182 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2009-06-10  Gustavo Noronha Silva  <gns@gnome.org>
+
+        Reviewed by Xan Lopez.
+
+        Add unit tests for our WebKitNetworkRequest object.
+
+        * GNUmakefile.am:
+
 2009-06-10  Xan Lopez  <xlopez@igalia.com>
 
         Reviewed by Gustavo Noronha.
index 722e467..e527ca0 100644 (file)
@@ -526,6 +526,7 @@ webkit_tests_ldflags = \
        -no-fast-install
 
 TEST_PROGS += Programs/unittests/testhttpbackend \
+       Programs/unittests/testnetworkrequest \
        Programs/unittests/testwebframe \
        Programs/unittests/testwebbackforwardlist \
        Programs/unittests/testwebhistoryitem \
@@ -538,6 +539,11 @@ Programs_unittests_testhttpbackend_CFLAGS = $(webkit_tests_cflags)
 Programs_unittests_testhttpbackend_LDADD = $(webkit_tests_ldadd)
 Programs_unittests_testhttpbackend_LDFLAGS = $(webkit_tests_ldflags)
 
+Programs_unittests_testnetworkrequest_SOURCES = WebKit/gtk/tests/testnetworkrequest.c
+Programs_unittests_testnetworkrequest_CFLAGS = $(webkit_tests_cflags)
+Programs_unittests_testnetworkrequest_LDADD = $(webkit_tests_ldadd)
+Programs_unittests_testnetworkrequest_LDFLAGS = $(webkit_tests_ldflags)
+
 Programs_unittests_testwebframe_SOURCES = WebKit/gtk/tests/testwebframe.c
 Programs_unittests_testwebframe_CFLAGS = $(webkit_tests_cflags)
 Programs_unittests_testwebframe_LDADD = $(webkit_tests_ldadd)
index 0c53559..b99652e 100644 (file)
@@ -1,3 +1,27 @@
+2009-06-10  Gustavo Noronha Silva  <gns@gnome.org>
+
+        Reviewed by Xan Lopez.
+
+        Make WebKitNetworkRequest a proper GObject, making URI and
+        SoupMessage properties. Also adding unit tests for creation and
+        destruction.
+
+        * tests/testnetworkrequest.c: Added.
+        (test_network_request_create_destroy):
+        (test_network_request_properties):
+        (main):
+        * webkit/webkitnetworkrequest.cpp:
+        (webkit_network_request_get_property):
+        (webkit_network_request_set_property):
+        (webkit_network_request_class_init):
+        (webkit_network_request_init):
+        (webkit_network_request_constructor):
+        (webkit_network_request_new_with_core_request):
+        (webkit_network_request_new):
+        (webkit_network_request_set_uri):
+        (webkit_network_request_get_uri):
+        * webkit/webkitnetworkrequest.h:
+
 2009-06-10  Gustavo Noronha Silva  <gustavo.noronha@collabora.co.uk>
 
         Reviewed by Xan Lopez.
diff --git a/WebKit/gtk/tests/testnetworkrequest.c b/WebKit/gtk/tests/testnetworkrequest.c
new file mode 100644 (file)
index 0000000..a4f2885
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2009 Gustavo Noronha Silva
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include <errno.h>
+#include <unistd.h>
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <gtk/gtk.h>
+#include <stdlib.h>
+#include <webkit/webkit.h>
+
+#if GLIB_CHECK_VERSION(2, 16, 0) && GTK_CHECK_VERSION(2, 14, 0)
+
+static void test_network_request_create_destroy()
+{
+    WebKitNetworkRequest* request;
+    SoupMessage* message;
+
+    /* Test creation with URI */
+    request = WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "uri", "http://debian.org/", NULL));
+    g_assert(WEBKIT_IS_NETWORK_REQUEST(request));
+    message = webkit_network_request_get_message(request);
+    g_assert_cmpint(message, ==, NULL);
+    g_object_unref(request);
+
+    /* Test creation with SoupMessage */
+    message = soup_message_new("GET", "http://debian.org/");
+    request = WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "message", message, NULL));
+    g_assert(WEBKIT_IS_NETWORK_REQUEST(request));
+    g_assert_cmpint(G_OBJECT(message)->ref_count, ==, 2);
+    g_object_unref(request);
+    g_assert_cmpint(G_OBJECT(message)->ref_count, ==, 1);
+    g_object_unref(message);
+
+    /* Test creation with both SoupMessage and URI */
+    message = soup_message_new("GET", "http://debian.org/");
+    request = WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "message", message, "uri", "http://gnome.org/", NULL));
+    g_assert(WEBKIT_IS_NETWORK_REQUEST(request));
+    g_assert_cmpint(G_OBJECT(message)->ref_count, ==, 2);
+    g_assert_cmpstr(webkit_network_request_get_uri(request), ==, "http://gnome.org/");
+    g_object_unref(request);
+    g_assert_cmpint(G_OBJECT(message)->ref_count, ==, 1);
+    g_object_unref(message);
+}
+
+static void test_network_request_properties()
+{
+    WebKitNetworkRequest* request;
+    SoupMessage* message;
+    gchar* soupURI;
+
+    /* Test URI is set correctly when creating with URI */
+    request = webkit_network_request_new("http://debian.org/");
+    g_assert(WEBKIT_IS_NETWORK_REQUEST(request));
+    g_assert_cmpstr(webkit_network_request_get_uri(request), ==, "http://debian.org/");
+    g_object_unref(request);
+
+    /* Test URI is set correctly when creating with Message */
+    message = soup_message_new("GET", "http://debian.org/");
+    request = WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "message", message, NULL));
+    g_assert(WEBKIT_IS_NETWORK_REQUEST(request));
+    g_object_unref(message);
+
+    message = webkit_network_request_get_message(request);
+    soupURI = soup_uri_to_string(soup_message_get_uri(message), FALSE);
+    g_assert_cmpstr(soupURI, ==, "http://debian.org/");
+    g_free(soupURI);
+
+    g_assert_cmpstr(webkit_network_request_get_uri(request), ==, "http://debian.org/");
+    g_object_unref(request);
+}
+
+int main(int argc, char** argv)
+{
+    g_thread_init(NULL);
+    gtk_test_init(&argc, &argv, NULL);
+
+    g_test_bug_base("https://bugs.webkit.org/");
+    g_test_add_func("/webkit/networkrequest/createdestroy", test_network_request_create_destroy);
+    g_test_add_func("/webkit/networkrequest/properties", test_network_request_properties);
+    return g_test_run ();
+}
+
+#else
+int main(int argc, char** argv)
+{
+    g_critical("You will need at least glib-2.16.0 and gtk-2.14.0 to run the unit tests. Doing nothing now.");
+    return 0;
+}
+
+#endif
index 2f80dfb..c1b76f5 100644 (file)
@@ -60,6 +60,13 @@ struct _WebKitNetworkRequestPrivate {
 
 #define WEBKIT_NETWORK_REQUEST_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_NETWORK_REQUEST, WebKitNetworkRequestPrivate))
 
+enum {
+    PROP_0,
+
+    PROP_URI,
+    PROP_MESSAGE,
+};
+
 static void webkit_network_request_finalize(GObject* object)
 {
     WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
@@ -75,9 +82,74 @@ static void webkit_network_request_finalize(GObject* object)
     G_OBJECT_CLASS(webkit_network_request_parent_class)->finalize(object);
 }
 
+static void webkit_network_request_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* pspec)
+{
+    WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
+
+    switch(propertyID) {
+    case PROP_URI:
+        g_value_set_string(value, webkit_network_request_get_uri(request));
+        break;
+    case PROP_MESSAGE:
+        g_value_set_object(value, webkit_network_request_get_message(request));
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
+    }
+}
+
+static void webkit_network_request_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* pspec)
+{
+    WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
+    WebKitNetworkRequestPrivate* priv = request->priv;
+
+    switch(propertyID) {
+    case PROP_URI:
+        webkit_network_request_set_uri(request, g_value_get_string(value));
+        break;
+    case PROP_MESSAGE:
+        priv->message = SOUP_MESSAGE(g_value_dup_object(value));
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
+    }
+}
+
 static void webkit_network_request_class_init(WebKitNetworkRequestClass* requestClass)
 {
-    G_OBJECT_CLASS(requestClass)->finalize = webkit_network_request_finalize;
+    GObjectClass* objectClass = G_OBJECT_CLASS(requestClass);
+
+    objectClass->finalize = webkit_network_request_finalize;
+    objectClass->get_property = webkit_network_request_get_property;
+    objectClass->set_property = webkit_network_request_set_property;
+
+    /**
+     * WebKitNetworkRequest:uri:
+     *
+     * The URI to which the request will be made.
+     *
+     * Since: 1.1.9
+     */
+    g_object_class_install_property(objectClass, PROP_URI,
+                                    g_param_spec_string("uri",
+                                                        _("URI"),
+                                                        _("The URI to which the request will be made."),
+                                                        NULL,
+                                                        (GParamFlags)(WEBKIT_PARAM_READWRITE)));
+
+    /**
+     * WebKitNetworkRequest:message:
+     *
+     * The #SoupMessage that backs the request.
+     *
+     * Since: 1.1.9
+     */
+    g_object_class_install_property(objectClass, PROP_MESSAGE,
+                                    g_param_spec_object("message",
+                                                        _("Message"),
+                                                        _("The SoupMessage that backs the request."),
+                                                        SOUP_TYPE_MESSAGE,
+                                                        (GParamFlags)(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
 
     g_type_class_add_private(requestClass, sizeof(WebKitNetworkRequestPrivate));
 }
@@ -91,15 +163,11 @@ static void webkit_network_request_init(WebKitNetworkRequest* request)
 // for internal use only
 WebKitNetworkRequest* webkit_network_request_new_with_core_request(const WebCore::ResourceRequest& resourceRequest)
 {
-    WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, NULL));
-    WebKitNetworkRequestPrivate* priv = request->priv;
-
     GOwnPtr<SoupMessage> soupMessage(resourceRequest.toSoupMessage());
     if (soupMessage)
-        priv->message = SOUP_MESSAGE(g_object_ref(soupMessage.get()));
-    priv->uri = g_strdup(resourceRequest.url().string().utf8().data());
+        return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "message", soupMessage.get(), NULL));
 
-    return request;
+    return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "uri", resourceRequest.url().string().utf8().data(), NULL));
 }
 
 /**
@@ -115,16 +183,7 @@ WebKitNetworkRequest* webkit_network_request_new(const gchar* uri)
 {
     g_return_val_if_fail(uri, NULL);
 
-    WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, NULL));
-    WebKitNetworkRequestPrivate* priv = request->priv;
-
-    priv->message = soup_message_new("GET", uri);
-    if (!priv->message)
-        return NULL;
-
-    priv->uri = g_strdup(uri);
-
-    return request;
+    return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "uri", uri, NULL));
 }
 
 /**
@@ -132,7 +191,10 @@ WebKitNetworkRequest* webkit_network_request_new(const gchar* uri)
  * @request: a #WebKitNetworkRequest
  * @uri: an URI
  *
- * Sets the URI held and used by the given request.
+ * Sets the URI held and used by the given request. When the request
+ * has an associated #SoupMessage, its URI will also be set by this
+ * call.
+ *
  */
 void webkit_network_request_set_uri(WebKitNetworkRequest* request, const gchar* uri)
 {
@@ -141,8 +203,21 @@ void webkit_network_request_set_uri(WebKitNetworkRequest* request, const gchar*
 
     WebKitNetworkRequestPrivate* priv = request->priv;
 
-    g_free(priv->uri);
+    if (priv->uri)
+        g_free(priv->uri);
     priv->uri = g_strdup(uri);
+
+    if (!priv->message)
+        return;
+
+    SoupURI* soupURI = soup_uri_new(uri);
+    if (!soupURI) {
+        g_warning("Invalid URI: %s", uri);
+        return;
+    }
+
+    soup_message_set_uri(priv->message, soupURI);
+    soup_uri_free(soupURI);
 }
 
 /**
@@ -158,18 +233,27 @@ G_CONST_RETURN gchar* webkit_network_request_get_uri(WebKitNetworkRequest* reque
     g_return_val_if_fail(WEBKIT_IS_NETWORK_REQUEST(request), NULL);
 
     WebKitNetworkRequestPrivate* priv = request->priv;
+
+    if (priv->uri)
+        return priv->uri;
+
+    SoupURI* soupURI = soup_message_get_uri(priv->message);
+    priv->uri = soup_uri_to_string(soupURI, FALSE);
     return priv->uri;
 }
 
-// NOT YET API; For internal use only, for now
-// webkit_network_request_get_soup_message:
-// @request: a #WebKitNetworkRequest
-//
-// Obtains the #SoupMessage held and used by the given request. Notice
-// that modification of the SoupMessage of a request by signal
-// handlers is only supported where explicitly documented.
-//
-// Returns: the #SoupMessage
+/**
+ * webkit_network_request_get_soup_message:
+ * @request: a #WebKitNetworkRequest
+ *
+ * Obtains the #SoupMessage held and used by the given request. Notice
+ * that modification of the SoupMessage of a request by signal
+ * handlers is only supported (as in, will only affect what is
+ * actually sent to the server) where explicitly documented.
+ *
+ * Returns: the #SoupMessage
+ * Since: 1.1.9
+ */
 SoupMessage* webkit_network_request_get_message(WebKitNetworkRequest* request)
 {
     g_return_val_if_fail(WEBKIT_IS_NETWORK_REQUEST(request), NULL);
index 01ab8bb..78e04a1 100644 (file)
@@ -21,6 +21,7 @@
 #define WEBKIT_NETWORK_REQUEST_H
 
 #include <glib-object.h>
+#include <libsoup/soup.h>
 
 #include <webkit/webkitdefines.h>
 
@@ -65,6 +66,9 @@ webkit_network_request_set_uri  (WebKitNetworkRequest *request,
 WEBKIT_API G_CONST_RETURN gchar *
 webkit_network_request_get_uri  (WebKitNetworkRequest *request);
 
+WEBKIT_API SoupMessage *
+webkit_network_request_get_message(WebKitNetworkRequest* request);
+
 G_END_DECLS
 
 #endif
index b1eac26..60fb1bb 100644 (file)
@@ -510,7 +510,14 @@ void webkit_web_frame_load_request(WebKitWebFrame* frame, WebKitNetworkRequest*
     if (!coreFrame)
         return;
 
-    coreFrame->loader()->load(ResourceRequest(webkit_network_request_get_message(request)), false);
+    SoupMessage* soupMessage = webkit_network_request_get_message(request);
+    if (soupMessage) {
+        coreFrame->loader()->load(ResourceRequest(soupMessage), false);
+        return;
+    }
+
+    KURL url = KURL(KURL(), String::fromUTF8(webkit_network_request_get_uri(request)));
+    coreFrame->loader()->load(ResourceRequest(url), false);
 }
 
 /**