From 547bf35d435a6c40710bbf68d53b26f483cd9597 Mon Sep 17 00:00:00 2001 From: Diego Escalante Urrelo Date: Tue, 23 Mar 2010 23:48:37 -0500 Subject: [PATCH] tests: add test for ephy-http-server --- tests/Makefile.am | 7 ++ tests/testephyhttpserver.c | 176 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+), 0 deletions(-) create mode 100644 tests/testephyhttpserver.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 5e93db7..eaf9f73 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,5 +1,6 @@ noinst_PROGRAMS = \ testephyembedpersist \ + testephyhttpserver \ testephysearchentry \ testephylocationentry \ testephyzoomcontrol @@ -48,6 +49,12 @@ libephyhttpserver_la_SOURCES = \ testephyembedpersist_SOURCES = \ testephyembedpersist.c +testephyhttpserver_SOURCES = \ + testephyhttpserver.c +testephyhttpserver_LDADD = \ + $(LDADD) \ + libephyhttpserver.la + testephysearchentry_SOURCES = \ testephysearchentry.c diff --git a/tests/testephyhttpserver.c b/tests/testephyhttpserver.c new file mode 100644 index 0000000..e23b0db --- /dev/null +++ b/tests/testephyhttpserver.c @@ -0,0 +1,176 @@ +/* vim: set sw=2 ts=2 sts=2 et: */ +/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * ephy-http-server.c + * This file is part of Epiphany + * + * Copyright © 2010 - Igalia S.L. + * + * Epiphany is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Epiphany 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Epiphany; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#include "config.h" +#include "ephy-http-server.h" +#include "ephy-debug.h" + +typedef struct { + GMainLoop *loop; + SoupSession *session; + EphyHttpServer *server; +} EphyHttpServerFixture; + +static void +test_ephy_http_server_fixture_setup (EphyHttpServerFixture *fixture, + gconstpointer data) +{ + GMainContext *context; + + fixture->loop = g_main_loop_new (NULL, FALSE); + context = g_main_loop_get_context (fixture->loop); + fixture->server = ephy_http_server_new (); + fixture->session = soup_session_async_new (); +} + +static void +test_ephy_http_server_fixture_teardown (EphyHttpServerFixture *fixture, + gconstpointer data) +{ + g_object_unref (fixture->server); + g_object_unref (fixture->session); + g_main_loop_unref (fixture->loop); +} + +static void +test_ephy_http_server_new (EphyHttpServerFixture *fixture, + gconstpointer data) +{ + g_assert (EPHY_IS_HTTP_SERVER (fixture->server)); +} + +static void +test_ephy_http_server_get_uri_for_path (EphyHttpServerFixture *fixture, + gconstpointer data) +{ + SoupURI *uri; + char *uri_string; + + uri_string = ephy_http_server_get_uri_for_path (fixture->server, "/test/"); + uri = soup_uri_new (uri_string); + + /* The default is to be at 127.0.0.1, so... */ + g_assert_cmpstr (uri->host, ==, "127.0.0.1"); + /* Ports should match. */ + g_assert_cmpuint (uri->port, ==, + soup_server_get_port (SOUP_SERVER (fixture->server))); + g_assert_cmpstr (uri->path, ==, "/test/"); + + soup_uri_free (uri); + g_free (uri_string); +} + +static void +test_ephy_http_server_add_response (EphyHttpServerFixture *fixture, + gconstpointer data) +{ + g_assert (ephy_http_server_remove_response (fixture->server, "/") == FALSE); + + ephy_http_server_add_response (fixture->server, + "/", "HELLO!", + SOUP_STATUS_OK); + + g_assert (ephy_http_server_remove_response (fixture->server, "/") == TRUE); + + g_assert (ephy_http_server_remove_response (fixture->server, "/") == FALSE); +} + +static gboolean +kill_loop (EphyHttpServerFixture *fixture) +{ + SoupMessage *msg; + guint status; + char *uri; + + uri = ephy_http_server_get_uri_for_path (fixture->server, "/quit"); + + msg = soup_message_new ("GET", uri); + status = soup_session_send_message (fixture->session, msg); + + g_free (uri); + + if (status == SOUP_STATUS_OK) { + ephy_http_server_remove_response (fixture->server, "/quit"); + return TRUE; + } else if (status == SOUP_STATUS_NOT_FOUND) { + g_main_loop_quit (fixture->loop); + return FALSE; + } else { + g_assert_not_reached (); + } +} + +static void +test_ephy_http_server_run (EphyHttpServerFixture *fixture, + gconstpointer data) +{ + ephy_http_server_add_response (fixture->server, + "/quit", "HELLO!", + SOUP_STATUS_OK); + + ephy_http_server_run (fixture->server); + + g_idle_add ((GSourceFunc) kill_loop, fixture); + g_main_loop_run (fixture->loop); +} + +int +main (int argc, char *argv[]) +{ + int ret; + + g_test_init (&argc, &argv, NULL); + g_type_init (); + g_thread_init (NULL); + + ephy_debug_init (); + + g_test_add ("/tests/ephy-http-server/new", + EphyHttpServerFixture, NULL, + test_ephy_http_server_fixture_setup, + test_ephy_http_server_new, + test_ephy_http_server_fixture_teardown); + + g_test_add ("/tests/ephy-http-server/get_uri_for_path", + EphyHttpServerFixture, NULL, + test_ephy_http_server_fixture_setup, + test_ephy_http_server_get_uri_for_path, + test_ephy_http_server_fixture_teardown); + + g_test_add ("/tests/ephy-http-server/add_response", + EphyHttpServerFixture, NULL, + test_ephy_http_server_fixture_setup, + test_ephy_http_server_add_response, + test_ephy_http_server_fixture_teardown); + + g_test_add ("/tests/ephy-http-server/run", + EphyHttpServerFixture, NULL, + test_ephy_http_server_fixture_setup, + test_ephy_http_server_run, + test_ephy_http_server_fixture_teardown); + + ret = g_test_run (); + + return ret; +} -- 1.7.0.2