Пример #1
0
gint
main (gint argc,
      gchar **argv)
{
	CamelURL *base_url, *url;
	gchar *url_string;
	gint i;
	GError *error = NULL;

	camel_test_init (argc, argv);

	camel_test_start ("URL parsing");

	camel_test_push ("base URL parsing");
	base_url = camel_url_new (base, &error);
	if (!base_url) {
		camel_test_fail (
			"Could not parse %s: %s\n",
			base, error->message);
	}
	camel_test_pull ();

	camel_test_push ("base URL unparsing");
	url_string = camel_url_to_string (base_url, 0);
	if (strcmp (url_string, base) != 0) {
		camel_test_fail ("URL <%s> unparses to <%s>\n",
				 base, url_string);
	}
	camel_test_pull ();
	g_free (url_string);

	for (i = 0; i < G_N_ELEMENTS (tests); i++) {
		camel_test_push ("<%s> + <%s> = <%s>?", base, tests[i].url_string, tests[i].result);
		url = camel_url_new_with_base (base_url, tests[i].url_string);
		if (!url) {
			camel_test_fail ("could not parse");
			camel_test_pull ();
			continue;
		}

		url_string = camel_url_to_string (url, 0);
		if (strcmp (url_string, tests[i].result) != 0)
			camel_test_fail ("got <%s>!", url_string);
		g_free (url_string);
		camel_test_pull ();
	}

	camel_test_end ();

	return 0;
}
Пример #2
0
/**
 * camel_url_new:
 * @url_string: a URL string
 * @error: return location for a #GError, or %NULL
 *
 * Parses an absolute URL.
 *
 * Returns: a #CamelURL if it can be parsed, or %NULL otherwise
 **/
CamelURL *
camel_url_new (const gchar *url_string,
               GError **error)
{
	CamelURL *url;

	if (!url_string || !*url_string)
		return NULL;

	url = camel_url_new_with_base (NULL, url_string);

	if (!url->protocol) {
		camel_url_free (url);
		g_set_error (
			error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
			_("Could not parse URL '%s'"), url_string);
		return NULL;
	}
	return url;
}