summaryrefslogtreecommitdiffstats
blob: 09912ed3af25958a204c2d273407164ac65799bf (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
/*
 * Copyright (c) 2012 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

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

#include <Swiften/Base/URL.h>
#include <boost/lexical_cast.hpp>

using namespace Swift;

class URLTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(URLTest);
		CPPUNIT_TEST(testFromString);
		CPPUNIT_TEST(testFromString_WithoutPath);
		CPPUNIT_TEST(testFromString_WithRootPath);
		CPPUNIT_TEST(testFromString_WithPort);
		CPPUNIT_TEST(testFromString_WithPortWithoutPath);
		CPPUNIT_TEST(testFromString_WithUserInfo);
		CPPUNIT_TEST(testFromString_NonASCIIHost);
		CPPUNIT_TEST(testFromString_NonASCIIPath);
		CPPUNIT_TEST(testToString);
		CPPUNIT_TEST(testToString_WithPort);
		CPPUNIT_TEST_SUITE_END();

	public:
		void testFromString() {
			URL url = URL::fromString("http://foo.bar/baz/bam");
			
			CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
			CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
			CPPUNIT_ASSERT(!url.getPort());
			CPPUNIT_ASSERT_EQUAL(std::string("baz/bam"), url.getPath());
		}

		void testFromString_WithoutPath() {
			URL url = URL::fromString("http://foo.bar");
			
			CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
			CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
			CPPUNIT_ASSERT(!url.getPort());
			CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath());
		}

		void testFromString_WithRootPath() {
			URL url = URL::fromString("http://foo.bar/");
			
			CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
			CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
			CPPUNIT_ASSERT(!url.getPort());
			CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath());
		}

		void testFromString_WithPort() {
			URL url = URL::fromString("http://foo.bar:1234/baz/bam");
			
			CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
			CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
			CPPUNIT_ASSERT_EQUAL(1234, *url.getPort());
			CPPUNIT_ASSERT_EQUAL(std::string("baz/bam"), url.getPath());
		}

		void testFromString_WithPortWithoutPath() {
			URL url = URL::fromString("http://foo.bar:1234");
			
			CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
			CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
			CPPUNIT_ASSERT_EQUAL(1234, *url.getPort());
			CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath());
		}

		void testFromString_WithUserInfo() {
			URL url = URL::fromString("http://user:pass@foo.bar/baz/bam");
			
			CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
			CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
			CPPUNIT_ASSERT_EQUAL(std::string("baz/bam"), url.getPath());
		}

		void testFromString_NonASCIIHost() {
			URL url = URL::fromString("http://www.tron%C3%A7on.be/baz/bam");

			CPPUNIT_ASSERT_EQUAL(std::string("www.tron\xc3\xa7on.be"), url.getHost());
		}

		void testFromString_NonASCIIPath() {
			URL url = URL::fromString("http://foo.bar/baz/tron%C3%A7on/bam");
			
			CPPUNIT_ASSERT_EQUAL(std::string("baz/tron\xc3\xa7on/bam"), url.getPath());
		}

		void testToString() {
			CPPUNIT_ASSERT_EQUAL(std::string("http://foo.bar/baz/bam"), URL("http", "foo.bar", "/baz/bam").toString());
		}

		void testToString_WithPort() {
			CPPUNIT_ASSERT_EQUAL(std::string("http://foo.bar:1234/baz/bam"), URL("http", "foo.bar", 1234, "/baz/bam").toString());
		}
};

CPPUNIT_TEST_SUITE_REGISTRATION(URLTest);