[libsoup] SoupURI: fix fallback handling of soup_uri_set_path(uri, NULL)



commit 5dc4d7af3cb8c2d3ba805ee7b87022faf19a794f
Author: Dan Winship <danw gnome org>
Date:   Sat Feb 25 08:54:58 2012 -0500

    SoupURI: fix fallback handling of soup_uri_set_path(uri, NULL)
    
    In the old code, if you set a URI's path to NULL and then did
    soup_uri_to_string(uri, FALSE), you'd get back a path of "/". Fix the
    new code to behave the same way (and test it).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=670431

 libsoup/soup-uri.c  |    6 +++++
 tests/uri-parsing.c |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 57 insertions(+), 1 deletions(-)
---
diff --git a/libsoup/soup-uri.c b/libsoup/soup-uri.c
index bda843d..24cd6b0 100644
--- a/libsoup/soup-uri.c
+++ b/libsoup/soup-uri.c
@@ -480,10 +480,16 @@ soup_uri_to_string (SoupURI *uri, gboolean just_path_and_query)
 			g_string_append_printf (str, ":%u", uri->port);
 		if (!uri->path && (uri->query || uri->fragment))
 			g_string_append_c (str, '/');
+		else if ((!uri->path || !*uri->path) &&
+			 (uri->scheme == SOUP_URI_SCHEME_HTTP ||
+			  uri->scheme == SOUP_URI_SCHEME_HTTPS))
+			g_string_append_c (str, '/');
 	}
 
 	if (uri->path && *uri->path)
 		g_string_append (str, uri->path);
+	else if (just_path_and_query)
+		g_string_append_c (str, '/');
 
 	if (uri->query) {
 		g_string_append_c (str, '?');
diff --git a/tests/uri-parsing.c b/tests/uri-parsing.c
index 5869ec6..fb04144 100644
--- a/tests/uri-parsing.c
+++ b/tests/uri-parsing.c
@@ -408,6 +408,19 @@ do_soup_uri_null_tests (void)
 		errors++;
 	}
 
+	expect_warning = TRUE;
+	uri_string = soup_uri_to_string (uri, FALSE);
+	if (expect_warning) {
+		debug_printf (1, "  ERROR: soup_uri_to_string didn't fail on scheme-only URI?\n");
+		errors++;
+		expect_warning = FALSE;
+	} else if (strcmp (uri_string, "http:") != 0) {
+		debug_printf (1, "  ERROR: soup_uri_to_string returned '%s' instead of 'http:'\n",
+			      uri_string);
+		errors++;
+	}
+	g_free (uri_string);
+
 	soup_uri_set_host (uri, "localhost");
 	if (SOUP_URI_IS_VALID (uri)) {
 		debug_printf (1, "  ERROR: setting scheme+host on NULL URI makes it valid?\n");
@@ -419,6 +432,19 @@ do_soup_uri_null_tests (void)
 	}
 
 	expect_warning = TRUE;
+	uri_string = soup_uri_to_string (uri, FALSE);
+	if (expect_warning) {
+		debug_printf (1, "  ERROR: soup_uri_to_string didn't fail on scheme+host URI?\n");
+		errors++;
+		expect_warning = FALSE;
+	} else if (strcmp (uri_string, "http://localhost/";) != 0) {
+		debug_printf (1, "  ERROR: soup_uri_to_string with NULL path returned '%s' instead of 'http://localhost/'\n",
+			      uri_string);
+		errors++;
+	}
+	g_free (uri_string);
+
+	expect_warning = TRUE;
 	uri2 = soup_uri_new_with_base (uri, "/path");
 	if (expect_warning) {
 		debug_printf (1, "  ERROR: soup_uri_new_with_base didn't warn on NULL+scheme URI?\n");
@@ -427,8 +453,21 @@ do_soup_uri_null_tests (void)
 	} else if (!uri2) {
 		debug_printf (1, "  ERROR: soup_uri_new_with_base didn't fix path on NULL+scheme URI\n");
 		errors++;
-	} else
+	}
+
+	if (uri2) {
+		uri_string = soup_uri_to_string (uri2, FALSE);
+		if (!uri_string) {
+			debug_printf (1, "  ERROR: soup_uri_to_string failed on uri2?\n");
+			errors++;
+		} else if (strcmp (uri_string, "http://localhost/path";) != 0) {
+			debug_printf (1, "  ERROR: soup_uri_to_string returned '%s' instead of 'http://localhost/path'\n",
+				      uri_string);
+			errors++;
+		}
+		g_free (uri_string);
 		soup_uri_free (uri2);
+	}
 
 	expect_warning = TRUE;
 	soup_uri_set_path (uri, NULL);
@@ -443,6 +482,17 @@ do_soup_uri_null_tests (void)
 		soup_uri_set_path (uri, "");
 	}
 
+	uri_string = soup_uri_to_string (uri, FALSE);
+	if (!uri_string) {
+		debug_printf (1, "  ERROR: soup_uri_to_string failed on complete URI?\n");
+		errors++;
+	} else if (strcmp (uri_string, "http://localhost/";) != 0) {
+		debug_printf (1, "  ERROR: soup_uri_to_string with empty path returned '%s' instead of 'http://localhost/'\n",
+			      uri_string);
+		errors++;
+	}
+	g_free (uri_string);
+
 	if (!SOUP_URI_IS_VALID (uri)) {
 		debug_printf (1, "  ERROR: setting scheme+path on NULL URI doesn't make it valid?\n");
 		errors++;



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