summaryrefslogtreecommitdiff
path: root/test/TestUriRelative.cxx
blob: 75f52a1a2a4b86dac2f9572c5a0a3a68536c0569 (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
/*
 * Unit tests for src/util/UriRelative.hxx
 */

#include "util/UriRelative.hxx"

#include <gtest/gtest.h>

TEST(UriRelative, IsChild)
{
	static constexpr struct {
		const char *parent;
		const char *child;
		bool is_child;
		bool is_child_or_same;
	} tests[] = {
		{ "/foo", "/foo", false, true },
		{ "/foo", "/foo/bar", true, true },
		{ "/foo/", "/foo/bar", true, true },
		{ "/foo/", "/foo/", false, true },
		{ "/foo/", "/foo", false, false },
		{ "/bar", "/foo", false, false },
		{ "/foo", "/foobar", false, false },
	};

	for (const auto &i : tests) {
		EXPECT_EQ(uri_is_child(i.parent, i.child), i.is_child);
		EXPECT_EQ(uri_is_child_or_same(i.parent, i.child),
			  i.is_child_or_same);
	}
}

TEST(UriRelative, ApplyBase)
{
	static constexpr struct {
		const char *uri;
		const char *base;
		const char *result;
	} tests[] = {
		{ "foo", "bar", "bar/foo" },
		{ "foo", "/bar", "/bar/foo" },
		{ "/foo", "/bar", "/foo" },
		{ "/foo", "bar", "/foo" },
		{ "/foo", "http://localhost/bar", "http://localhost/foo" },
		{ "/foo", "http://localhost/", "http://localhost/foo" },
		{ "/foo", "http://localhost", "http://localhost/foo" },
	};

	for (const auto &i : tests) {
		EXPECT_STREQ(uri_apply_base(i.uri, i.base).c_str(), i.result);
	}
}

TEST(UriRelative, ApplyRelative)
{
	static constexpr struct {
		const char *relative;
		const char *base;
		const char *result;
	} tests[] = {
		{ "", "bar", "bar" },
		{ ".", "bar", "" },
		{ "foo", "bar", "foo" },
		{ "", "/bar", "/bar" },
		{ ".", "/bar", "/" },
		{ "foo", "/bar", "/foo" },
		{ "", "/bar/", "/bar/" },
		{ ".", "/bar/", "/bar/" },
		{ ".", "/bar/foo", "/bar/" },
		{ "/foo", "/bar/", "/foo" },
		{ "foo", "/bar/", "/bar/foo" },
		{ "../foo", "/bar/", "/foo" },
		{ "./foo", "/bar/", "/bar/foo" },
		{ "./../foo", "/bar/", "/foo" },
		{ ".././foo", "/bar/", "/foo" },
		{ "../../foo", "/bar/", "" },
		{ "/foo", "http://localhost/bar/", "http://localhost/foo" },
		{ "/foo", "http://localhost/bar", "http://localhost/foo" },
		{ "/foo", "http://localhost/", "http://localhost/foo" },
		{ "/foo", "http://localhost", "http://localhost/foo" },
		{ "/", "http://localhost", "http://localhost/" },
		{ "/", "http://localhost/bar", "http://localhost/" },
		{ "/", "http://localhost/bar/", "http://localhost/" },
		{ "/", "http://localhost/bar/foo", "http://localhost/" },
		{ "../foo", "http://localhost/bar/", "http://localhost/foo" },
		{ "../foo", "http://localhost/bar", "" },
		{ "../foo", "http://localhost/", "" },
		{ "../foo", "http://localhost", "" },
		{ ".", "http://localhost", "http://localhost/" },
	};

	for (const auto &i : tests) {
		EXPECT_STREQ(uri_apply_relative(i.relative, i.base).c_str(),
			     i.result);
	}
}