aboutsummaryrefslogtreecommitdiffstats
path: root/test/UriUtilTest.hxx
blob: 07f52a475f4e36527082a323855d4d87f6840072 (plain) (blame)
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
/*
 * Unit tests for src/util/
 */

#include "check.h"
#include "util/UriUtil.hxx"

#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>

#include <string.h>

class UriUtilTest : public CppUnit::TestFixture {
	CPPUNIT_TEST_SUITE(UriUtilTest);
	CPPUNIT_TEST(TestSuffix);
	CPPUNIT_TEST(TestRemoveAuth);
	CPPUNIT_TEST_SUITE_END();

public:
	void TestSuffix() {
		CPPUNIT_ASSERT_EQUAL((const char *)nullptr,
				     uri_get_suffix("/foo/bar"));
		CPPUNIT_ASSERT_EQUAL((const char *)nullptr,
				     uri_get_suffix("/foo.jpg/bar"));
		CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo/bar.jpg"),
					       "jpg"));
		CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo.png/bar.jpg"),
					       "jpg"));
		CPPUNIT_ASSERT_EQUAL((const char *)nullptr,
				     uri_get_suffix(".jpg"));
		CPPUNIT_ASSERT_EQUAL((const char *)nullptr,
				     uri_get_suffix("/foo/.jpg"));

		/* the first overload does not eliminate the query
		   string */
		CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo/bar.jpg?query_string"),
					       "jpg?query_string"));

		/* ... but the second one does */
		UriSuffixBuffer buffer;
		CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo/bar.jpg?query_string",
							      buffer),
					       "jpg"));

		/* repeat some of the above tests with the second overload */
		CPPUNIT_ASSERT_EQUAL((const char *)nullptr,
				     uri_get_suffix("/foo/bar", buffer));
		CPPUNIT_ASSERT_EQUAL((const char *)nullptr,
				     uri_get_suffix("/foo.jpg/bar", buffer));
		CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo/bar.jpg", buffer),
					       "jpg"));
	}

	void TestRemoveAuth() {
		CPPUNIT_ASSERT_EQUAL(std::string(),
				     uri_remove_auth("http://www.example.com/"));
		CPPUNIT_ASSERT_EQUAL(std::string("http://www.example.com/"),
				     uri_remove_auth("http://foo:bar@www.example.com/"));
		CPPUNIT_ASSERT_EQUAL(std::string("http://www.example.com/"),
				     uri_remove_auth("http://foo@www.example.com/"));
		CPPUNIT_ASSERT_EQUAL(std::string(),
				     uri_remove_auth("http://www.example.com/f:oo@bar"));
		CPPUNIT_ASSERT_EQUAL(std::string("ftp://ftp.example.com/"),
				     uri_remove_auth("ftp://foo:bar@ftp.example.com/"));
	}
};