summaryrefslogtreecommitdiff
path: root/tests/uri-parsing-test.c
blob: 48e3964e2a1662647fae7cf47d076c56e1d419c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

#include "test-utils.h"
#include "soup-uri-utils-private.h"

static struct {
	const char *one, *two;
        gboolean equal;
        GUriFlags flags_one, flags_two;
} eq_tests[] = {
        // NOTE: GUri doesn't remove dot segments from absolute URIs
	// { "example://a/b/c/%7Bfoo%7D", "eXAMPLE://a/./b/../b/%63/%7Bfoo%7D", "628728" },
	{ "http://example.com", "http://example.com/", TRUE,
          SOUP_HTTP_URI_FLAGS, SOUP_HTTP_URI_FLAGS },
	/* From RFC 2616 */
	{ "http://abc.com:80/~smith/home.html", "http://abc.com:80/~smith/home.html", TRUE,
          SOUP_HTTP_URI_FLAGS, SOUP_HTTP_URI_FLAGS },
	{ "http://abc.com:80/~smith/home.html", "http://ABC.com/%7Esmith/home.html", TRUE,
          SOUP_HTTP_URI_FLAGS, SOUP_HTTP_URI_FLAGS },
	{ "http://abc.com:80/~smith/home.html", "http://ABC.com:/%7esmith/home.html", TRUE,
          SOUP_HTTP_URI_FLAGS, SOUP_HTTP_URI_FLAGS },
        /* Test flags affecting comparisons */
        { "http://example.com/%2F", "http://example.com/%2F", FALSE,
          G_URI_FLAGS_ENCODED_PATH, G_URI_FLAGS_NONE },
        { "http://example.com/%2F", "http://example.com/%2F", TRUE,
          G_URI_FLAGS_PARSE_RELAXED, G_URI_FLAGS_NONE },
        { "http://example.com/%2F", "http://example.com/%2F", TRUE,
          G_URI_FLAGS_PARSE_RELAXED, G_URI_FLAGS_HAS_PASSWORD },
};
static int num_eq_tests = G_N_ELEMENTS(eq_tests);

static void
do_equality_tests (void)
{
	GUri *uri1, *uri2;
	int i;

	for (i = 0; i < num_eq_tests; i++) {
		uri1 = g_uri_parse (eq_tests[i].one, eq_tests[i].flags_one, NULL);
		uri2 = g_uri_parse (eq_tests[i].two, eq_tests[i].flags_two, NULL);

		debug_printf (1, "<%s> %c= <%s>\n", eq_tests[i].one, eq_tests[i].equal ? '=' : '!', eq_tests[i].two);
                g_assert_cmpint (soup_uri_equal (uri1, uri2), ==, eq_tests[i].equal);

		g_uri_unref (uri1);
		g_uri_unref (uri2);
	}
}

static void
do_copy_tests (void)
{
        GUri *uri;
        GUri *copy;
        char *str;

        uri = g_uri_parse ("http://127.0.0.1:1234/foo#bar", SOUP_HTTP_URI_FLAGS, NULL);

        /* Exact copy */
        copy = soup_uri_copy (uri, SOUP_URI_NONE);
        str = g_uri_to_string (copy);
        g_assert_cmpstr (str, ==, "http://127.0.0.1:1234/foo#bar");
        g_free (str);
        g_uri_unref (copy);

        /* Update the path */
        copy = soup_uri_copy (uri, SOUP_URI_PATH, "/baz", SOUP_URI_NONE);
        str = g_uri_to_string (copy);
        g_assert_cmpstr (str, ==, "http://127.0.0.1:1234/baz#bar");
        g_free (str);
        g_uri_unref (copy);

        /* Add credentials */
        copy = soup_uri_copy (uri, SOUP_URI_USER, "user", SOUP_URI_PASSWORD, "password", SOUP_URI_NONE);
        str = g_uri_to_string (copy);
        g_assert_cmpstr (str, ==, "http://user:password@127.0.0.1:1234/foo#bar");
        g_free (str);
        g_uri_unref (copy);

        /* Remove the fragment and add a query */
        copy = soup_uri_copy (uri, SOUP_URI_FRAGMENT, NULL, SOUP_URI_QUERY, "baz=1", SOUP_URI_NONE);
        str = g_uri_to_string (copy);
        g_assert_cmpstr (str, ==, "http://127.0.0.1:1234/foo?baz=1");
        g_free (str);
        g_uri_unref (copy);

        /* Update host and port */
        copy = soup_uri_copy (uri, SOUP_URI_HOST, "localhost", SOUP_URI_PORT, -1, SOUP_URI_NONE);
        str = g_uri_to_string (copy);
        g_assert_cmpstr (str, ==, "http://localhost/foo#bar");
        g_free (str);
        g_uri_unref (copy);

        /* Update everything */
        copy = soup_uri_copy (uri,
                              SOUP_URI_SCHEME, "https",
                              SOUP_URI_USER, "user",
                              SOUP_URI_PASSWORD, "password",
                              SOUP_URI_HOST, "localhost",
                              SOUP_URI_PORT, 4321,
                              SOUP_URI_PATH, "/baz",
                              SOUP_URI_FRAGMENT, "foo",
                              SOUP_URI_NONE);
        str = g_uri_to_string (copy);
        g_assert_cmpstr (str, ==, "https://user:password@localhost:4321/baz#foo");
        g_free (str);
        g_uri_unref (copy);

        /* Convert to file */
        copy = soup_uri_copy (uri, SOUP_URI_SCHEME, "file", SOUP_URI_HOST, "", SOUP_URI_PORT, -1, SOUP_URI_FRAGMENT, NULL, SOUP_URI_NONE);
        str = g_uri_to_string (copy);
        g_assert_cmpstr (str, ==, "file:///foo");
        g_free (str);
        g_uri_unref (copy);

        g_uri_unref (uri);
}

#define CONTENT_TYPE_DEFAULT "text/plain;charset=US-ASCII"

static struct {
	const char *input;
        const char *output;
        const char *content_type;
} data_uri_tests[] = {
        { "invalid:", NULL, NULL },
        { "data:", "", CONTENT_TYPE_DEFAULT },
        { "data:hello", "hello", CONTENT_TYPE_DEFAULT },
        { "data:text/plain,hello", "hello", "text/plain" },
        { "data:text/plain;charset=UTF-8,hello", "hello", "text/plain;charset=UTF-8" },
        { "data:text/plain;base64,aGVsbG8=", "hello", "text/plain" },
        { "data:text/plain;base64,invalid=", "", "text/plain" },
        { "data:,", "", CONTENT_TYPE_DEFAULT },
};

static void
do_data_uri_tests (void)
{
	int i;

	for (i = 0; i < G_N_ELEMENTS (data_uri_tests); i++) {
                char *content_type = NULL;
                GBytes *output = soup_uri_decode_data_uri (data_uri_tests[i].input, &content_type);

                if (data_uri_tests[i].output == NULL) {
                        g_assert_null (output);
                        g_assert_null (content_type);
                        continue;
                }

                g_assert_nonnull (output);
                g_assert_cmpstr (content_type, ==, data_uri_tests[i].content_type);

		g_free (content_type);
		g_bytes_unref (output);
	}
}

static struct {
        const char *input;
        const char *output;
} path_and_query_tests[] = {
        { "https://simple/one?two", "/one?two" },
        { "https://double_path//one?two", "//one?two" },
        { "https://empty", "/" },
};

static void
do_path_and_query_tests (void)
{
        for (int i = 0; i < G_N_ELEMENTS (path_and_query_tests); i++) {
                GUri *uri = g_uri_parse (path_and_query_tests[i].input, SOUP_HTTP_URI_FLAGS, NULL);
                g_assert_nonnull (uri);

                char *path_and_query = soup_uri_get_path_and_query (uri);
                g_assert_cmpstr (path_and_query, ==, path_and_query_tests[i].output);

                g_free (path_and_query);
                g_uri_unref (uri);
        }
}

int
main (int argc, char **argv)
{
	int ret;

	test_init (argc, argv, NULL);

	g_test_add_func ("/uri/equality", do_equality_tests);
	g_test_add_func ("/uri/copy", do_copy_tests);
        g_test_add_func ("/data", do_data_uri_tests);
        g_test_add_func ("/path_and_query", do_path_and_query_tests);

	ret = g_test_run ();

	test_cleanup ();
	return ret;
}