xref: /haiku/src/tests/kits/net/libnetapi/NetworkUrlTest.cpp (revision 8f30879b98789e56d7aec4eadb15898112150ccf)
1fa2dd9c4SAndrew Lindesay /*
2*8f30879bSAndrew Lindesay  * Copyright 2016-2018, Andrew Lindesay, apl@lindesay.co.nz.
3fa2dd9c4SAndrew Lindesay  * Distributed under the terms of the MIT License.
4fa2dd9c4SAndrew Lindesay  */
5fa2dd9c4SAndrew Lindesay 
6fa2dd9c4SAndrew Lindesay 
7fa2dd9c4SAndrew Lindesay #include "NetworkUrlTest.h"
8fa2dd9c4SAndrew Lindesay 
9fa2dd9c4SAndrew Lindesay #include <Url.h>
10fa2dd9c4SAndrew Lindesay 
11fa2dd9c4SAndrew Lindesay #include <cppunit/TestCaller.h>
12fa2dd9c4SAndrew Lindesay #include <cppunit/TestSuite.h>
13fa2dd9c4SAndrew Lindesay 
14fa2dd9c4SAndrew Lindesay 
NetworkUrlTest()15fa2dd9c4SAndrew Lindesay NetworkUrlTest::NetworkUrlTest()
16fa2dd9c4SAndrew Lindesay {
17fa2dd9c4SAndrew Lindesay }
18fa2dd9c4SAndrew Lindesay 
19fa2dd9c4SAndrew Lindesay 
~NetworkUrlTest()20fa2dd9c4SAndrew Lindesay NetworkUrlTest::~NetworkUrlTest()
21fa2dd9c4SAndrew Lindesay {
22fa2dd9c4SAndrew Lindesay }
23fa2dd9c4SAndrew Lindesay 
24fa2dd9c4SAndrew Lindesay 
25fa2dd9c4SAndrew Lindesay void
setUp()26fa2dd9c4SAndrew Lindesay NetworkUrlTest::setUp()
27fa2dd9c4SAndrew Lindesay {
28fa2dd9c4SAndrew Lindesay }
29fa2dd9c4SAndrew Lindesay 
30fa2dd9c4SAndrew Lindesay 
31fa2dd9c4SAndrew Lindesay void
tearDown()32fa2dd9c4SAndrew Lindesay NetworkUrlTest::tearDown()
33fa2dd9c4SAndrew Lindesay {
34fa2dd9c4SAndrew Lindesay }
35fa2dd9c4SAndrew Lindesay 
36fa2dd9c4SAndrew Lindesay 
37*8f30879bSAndrew Lindesay // #pragma mark - General Tests
38fa2dd9c4SAndrew Lindesay 
39fa2dd9c4SAndrew Lindesay /*
40fa2dd9c4SAndrew Lindesay This is the "happy days" tests that checks that a URL featuring all of the
41fa2dd9c4SAndrew Lindesay parsed elements successfully processes and the elements are present.
42fa2dd9c4SAndrew Lindesay */
43fa2dd9c4SAndrew Lindesay 
TestValidFullUrl()44fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestValidFullUrl()
45fa2dd9c4SAndrew Lindesay {
46fa2dd9c4SAndrew Lindesay 	BUrl url("http://ewe:pea@www.something.co.nz:8888/some/path?key1=value1#fragment");
47fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
48fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "http");
49fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
50fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.UserName() == "ewe");
51fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasUserName());
52fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Password() == "pea");
53fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPassword());
54fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "www.something.co.nz");
55fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
56fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Port() == 8888);
57fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPort());
58fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Path() == "/some/path");
59fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPath());
60fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Request() == "key1=value1");
61fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasRequest());
62fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Fragment() == "fragment");
63fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasFragment());
64fa2dd9c4SAndrew Lindesay }
65fa2dd9c4SAndrew Lindesay 
66fa2dd9c4SAndrew Lindesay 
TestHostWithPathAndFragment()67cf657294SAndrew Lindesay void NetworkUrlTest::TestHostWithPathAndFragment()
68cf657294SAndrew Lindesay {
69cf657294SAndrew Lindesay 	BUrl url("http://1.2.3.4/some/path#frag/ment");
70cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
71cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "http");
72cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
73cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
74cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
75cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "1.2.3.4");
76cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
77cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
78cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Path() == "/some/path");
79cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPath());
80cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
81cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Fragment() == "frag/ment");
82cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasFragment());
83cf657294SAndrew Lindesay }
84cf657294SAndrew Lindesay 
85cf657294SAndrew Lindesay 
TestHostWithFragment()86cf657294SAndrew Lindesay void NetworkUrlTest::TestHostWithFragment()
87cf657294SAndrew Lindesay {
88cf657294SAndrew Lindesay 	BUrl url("http://1.2.3.4#frag/ment");
89cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
90cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "http");
91cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
92cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
93cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
94cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "1.2.3.4");
95cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
96cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
97cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPath()); // see Url.cpp - evidently an empty path is still a path?
98cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
99cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Fragment() == "frag/ment");
100cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasFragment());
101cf657294SAndrew Lindesay }
102cf657294SAndrew Lindesay 
103cf657294SAndrew Lindesay 
TestIpv6HostPortPathAndRequest()104cf657294SAndrew Lindesay void NetworkUrlTest::TestIpv6HostPortPathAndRequest()
105cf657294SAndrew Lindesay {
106*8f30879bSAndrew Lindesay 	BUrl url("http://[123:a3:0:E3::123]:8080/some/path?key1=value1");
107cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
108cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "http");
109cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
110cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
111cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
112*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "[123:a3:0:E3::123]");
113cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
114cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Port() == 8080);
115cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPort());
116cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Path() == "/some/path");
117cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPath());
118cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Request() == "key1=value1");
119cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasRequest());
120cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasFragment());
121cf657294SAndrew Lindesay }
122cf657294SAndrew Lindesay 
123cf657294SAndrew Lindesay 
TestFileUrl()124fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestFileUrl()
125fa2dd9c4SAndrew Lindesay {
126fa2dd9c4SAndrew Lindesay 	BUrl url("file:///northisland/wellington/brooklyn/windturbine");
127fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
128fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "file");
129fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
130fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
131fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
132fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "");
133fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
134fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
135fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Path() == "/northisland/wellington/brooklyn/windturbine");
136fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPath());
137fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
138fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasFragment());
139fa2dd9c4SAndrew Lindesay }
140fa2dd9c4SAndrew Lindesay 
141fa2dd9c4SAndrew Lindesay 
TestDataUrl()142cf657294SAndrew Lindesay void NetworkUrlTest::TestDataUrl()
143cf657294SAndrew Lindesay {
144cf657294SAndrew Lindesay 	BUrl url("data:image/png;base64,iVBORw0KGI12P4//8/w38GIErkJggg==");
145cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
146cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "data");
147cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
148cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
149cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasHost());
150cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPath());
151cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Path() == "image/png;base64,iVBORw0KGI12P4//8/w38GIErkJggg==");
152cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
153cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasFragment());
154cf657294SAndrew Lindesay }
155cf657294SAndrew Lindesay 
156cf657294SAndrew Lindesay 
157*8f30879bSAndrew Lindesay // #pragma mark - Authority Tests (UserName, Password, Host, Port)
158fa2dd9c4SAndrew Lindesay 
159fa2dd9c4SAndrew Lindesay 
TestWithUserNameAndPasswordNoHostAndPort()160fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestWithUserNameAndPasswordNoHostAndPort()
161fa2dd9c4SAndrew Lindesay {
162fa2dd9c4SAndrew Lindesay 	BUrl url("wierd://tea:tree@/x");
163fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
164fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "wierd");
165fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
166fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.UserName() == "tea");
167fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasUserName());
168fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Password() == "tree");
169fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPassword());
170fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "");
171cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost()); // any authority means there "is a host" - see SetAuthority comment.
172fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
173fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Path() == "/x");
174fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPath());
175fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
176fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasFragment());
177fa2dd9c4SAndrew Lindesay }
178fa2dd9c4SAndrew Lindesay 
179fa2dd9c4SAndrew Lindesay 
TestHostAndPortWithNoUserNameAndPassword()180fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestHostAndPortWithNoUserNameAndPassword()
181fa2dd9c4SAndrew Lindesay {
182fa2dd9c4SAndrew Lindesay 	BUrl url("https://www.something.co.nz:443/z");
183fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
184fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "https");
185fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
186fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
187fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
188fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Port() == 443);
189fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPort());
190fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "www.something.co.nz");
191fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
192fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Path() == "/z");
193fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPath());
194fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
195fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasFragment());
196fa2dd9c4SAndrew Lindesay }
197fa2dd9c4SAndrew Lindesay 
198fa2dd9c4SAndrew Lindesay 
TestHostWithNoPortOrUserNameAndPassword()199fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestHostWithNoPortOrUserNameAndPassword()
200fa2dd9c4SAndrew Lindesay {
201fa2dd9c4SAndrew Lindesay 	BUrl url("https://www.something.co.nz/z");
202fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
203fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "https");
204fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
205fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
206fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
207fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "www.something.co.nz");
208fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
209fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
210fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Path() == "/z");
211fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPath());
212fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
213fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasFragment());
214fa2dd9c4SAndrew Lindesay }
215fa2dd9c4SAndrew Lindesay 
216fa2dd9c4SAndrew Lindesay 
TestHostWithNoPortNoPath()217fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestHostWithNoPortNoPath()
218fa2dd9c4SAndrew Lindesay {
219fa2dd9c4SAndrew Lindesay 	BUrl url("https://www.something.co.nz");
220fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
221fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "https");
222fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
223fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
224fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
225fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "www.something.co.nz");
226fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
227fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
228fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPath());
229fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
230fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasFragment());
231fa2dd9c4SAndrew Lindesay }
232fa2dd9c4SAndrew Lindesay 
233fa2dd9c4SAndrew Lindesay 
TestHostWithPortNoPath()234fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestHostWithPortNoPath()
235fa2dd9c4SAndrew Lindesay {
236fa2dd9c4SAndrew Lindesay 	BUrl url("https://www.something.co.nz:1234");
237fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
238fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "https");
239fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
240fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
241fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
242fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "www.something.co.nz");
243fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
244fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Port() == 1234);
245fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPort());
246fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPath());
247fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
248fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasFragment());
249fa2dd9c4SAndrew Lindesay }
250fa2dd9c4SAndrew Lindesay 
251fa2dd9c4SAndrew Lindesay 
TestHostWithEmptyPort()252fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestHostWithEmptyPort()
253fa2dd9c4SAndrew Lindesay {
254fa2dd9c4SAndrew Lindesay 	BUrl url("https://www.something.co.nz:");
255fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
256fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "https");
257fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
258fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
259fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
260fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "www.something.co.nz");
261fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
262fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
263fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPath());
264fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
265fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasFragment());
266fa2dd9c4SAndrew Lindesay }
267fa2dd9c4SAndrew Lindesay 
268fa2dd9c4SAndrew Lindesay 
TestProtocol()269cf657294SAndrew Lindesay void NetworkUrlTest::TestProtocol()
270cf657294SAndrew Lindesay {
271cf657294SAndrew Lindesay 	BUrl url("olala:");
272cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
273cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "olala");
274cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
275cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
276cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
277cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasHost());
278cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
279cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPath());
280cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
281cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasFragment());
282cf657294SAndrew Lindesay }
283cf657294SAndrew Lindesay 
284cf657294SAndrew Lindesay 
TestMailTo()285cf657294SAndrew Lindesay void NetworkUrlTest::TestMailTo()
286cf657294SAndrew Lindesay {
287cf657294SAndrew Lindesay 	BUrl url("mailto:eric@example.com");
288cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
289cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Protocol() == "mailto");
290cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasProtocol());
291cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
292cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
293cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasHost());
294cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
295cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Path() == "eric@example.com");
296cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPath());
297cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasRequest());
298cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasFragment());
299cf657294SAndrew Lindesay }
300cf657294SAndrew Lindesay 
301cf657294SAndrew Lindesay 
302*8f30879bSAndrew Lindesay // #pragma mark - Various Authority Checks
303cf657294SAndrew Lindesay 
304cf657294SAndrew Lindesay 
TestAuthorityNoUserName()305cf657294SAndrew Lindesay void NetworkUrlTest::TestAuthorityNoUserName()
306cf657294SAndrew Lindesay {
307cf657294SAndrew Lindesay 	BUrl url("anything://:pwd@host");
308cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
309cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
310cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasPassword());
311cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Password() == "pwd");
312cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
313cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "host");
314cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
315cf657294SAndrew Lindesay }
316cf657294SAndrew Lindesay 
317cf657294SAndrew Lindesay 
TestAuthorityWithCredentialsSeparatorNoPassword()318cf657294SAndrew Lindesay void NetworkUrlTest::TestAuthorityWithCredentialsSeparatorNoPassword()
319cf657294SAndrew Lindesay {
320cf657294SAndrew Lindesay 	BUrl url("anything://unam:@host");
321cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
322cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasUserName());
323cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.UserName() == "unam");
324cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
325cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
326cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "host");
327cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
328cf657294SAndrew Lindesay }
329cf657294SAndrew Lindesay 
330cf657294SAndrew Lindesay 
TestAuthorityWithoutCredentialsSeparatorNoPassword()331cf657294SAndrew Lindesay void NetworkUrlTest::TestAuthorityWithoutCredentialsSeparatorNoPassword()
332cf657294SAndrew Lindesay {
333cf657294SAndrew Lindesay 	BUrl url("anything://unam@host");
334cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
335cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasUserName());
336cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.UserName() == "unam");
337cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
338cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
339cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "host");
340cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
341cf657294SAndrew Lindesay }
342cf657294SAndrew Lindesay 
343cf657294SAndrew Lindesay 
TestAuthorityBadPort()344cf657294SAndrew Lindesay void NetworkUrlTest::TestAuthorityBadPort()
345cf657294SAndrew Lindesay {
346cf657294SAndrew Lindesay 	BUrl url("anything://host:aaa");
347cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.IsValid());
348cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasUserName());
349cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPassword());
350cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.HasHost());
351cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(url.Host() == "host");
352cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.HasPort());
353cf657294SAndrew Lindesay }
354cf657294SAndrew Lindesay 
355cf657294SAndrew Lindesay 
356*8f30879bSAndrew Lindesay // #pragma mark - Invalid Forms
357fa2dd9c4SAndrew Lindesay 
358fa2dd9c4SAndrew Lindesay 
TestWhitespaceBefore()359fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestWhitespaceBefore()
360fa2dd9c4SAndrew Lindesay {
361fa2dd9c4SAndrew Lindesay 	BUrl url("   https://www.something.co.nz/z");
362fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.IsValid());
363fa2dd9c4SAndrew Lindesay }
364fa2dd9c4SAndrew Lindesay 
365fa2dd9c4SAndrew Lindesay 
TestWhitespaceAfter()366fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestWhitespaceAfter()
367fa2dd9c4SAndrew Lindesay {
368fa2dd9c4SAndrew Lindesay 	BUrl url("https://www.something.co.nz/z\t\t ");
369fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.IsValid());
370fa2dd9c4SAndrew Lindesay }
371fa2dd9c4SAndrew Lindesay 
372fa2dd9c4SAndrew Lindesay 
TestWhitespaceMiddle()373fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestWhitespaceMiddle()
374fa2dd9c4SAndrew Lindesay {
375fa2dd9c4SAndrew Lindesay 	BUrl url("https://www.  something.co.nz/z");
376fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.IsValid());
377fa2dd9c4SAndrew Lindesay }
378fa2dd9c4SAndrew Lindesay 
379fa2dd9c4SAndrew Lindesay 
TestHttpNoHost()380fa2dd9c4SAndrew Lindesay void NetworkUrlTest::TestHttpNoHost()
381fa2dd9c4SAndrew Lindesay {
382fa2dd9c4SAndrew Lindesay 	BUrl url("https:///z");
383fa2dd9c4SAndrew Lindesay 	CPPUNIT_ASSERT(!url.IsValid());
384fa2dd9c4SAndrew Lindesay }
385fa2dd9c4SAndrew Lindesay 
386fa2dd9c4SAndrew Lindesay 
TestEmpty()387cf657294SAndrew Lindesay void NetworkUrlTest::TestEmpty()
388cf657294SAndrew Lindesay {
389cf657294SAndrew Lindesay 	BUrl url("");
390cf657294SAndrew Lindesay 	CPPUNIT_ASSERT(!url.IsValid());
391cf657294SAndrew Lindesay }
392cf657294SAndrew Lindesay 
393cf657294SAndrew Lindesay 
394*8f30879bSAndrew Lindesay // #pragma mark - Host validation
395*8f30879bSAndrew Lindesay 
396*8f30879bSAndrew Lindesay 
TestBadHosts()397*8f30879bSAndrew Lindesay void NetworkUrlTest::TestBadHosts()
398*8f30879bSAndrew Lindesay {
399*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT_MESSAGE("control check",
400*8f30879bSAndrew Lindesay 		BUrl("http://host.example.com").IsValid());
401*8f30879bSAndrew Lindesay 
402*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT_MESSAGE("hyphen in middle",
403*8f30879bSAndrew Lindesay 		(BUrl("http://host.peppermint_tea.com").IsValid()));
404*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT_MESSAGE("dot at end",
405*8f30879bSAndrew Lindesay 		(BUrl("http://host.camomile.co.nz.").IsValid()));
406*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT_MESSAGE("simple host",
407*8f30879bSAndrew Lindesay 		(BUrl("http://tumeric").IsValid()));
408*8f30879bSAndrew Lindesay 
409*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT_MESSAGE("idn domain encoded",
410*8f30879bSAndrew Lindesay 		(BUrl("http://xn--bcher-kva.tld").IsValid()));
411*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT_MESSAGE("idn domain unencoded",
412*8f30879bSAndrew Lindesay 		(BUrl("http://www.b\xc3\xbcch.at").IsValid()));
413*8f30879bSAndrew Lindesay 
414*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT_MESSAGE("dot at start",
415*8f30879bSAndrew Lindesay 		!(BUrl("http://.host.example.com").IsValid()));
416*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT_MESSAGE("double dot in domain",
417*8f30879bSAndrew Lindesay 		!(BUrl("http://host.example..com").IsValid()));
418*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT_MESSAGE("double dot",
419*8f30879bSAndrew Lindesay 		!(BUrl("http://host.example..com").IsValid()));
420*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT_MESSAGE("unexpected characters",
421*8f30879bSAndrew Lindesay 		!(BUrl("http://<unexpected.characters>").IsValid()));
422*8f30879bSAndrew Lindesay 	CPPUNIT_ASSERT_MESSAGE("whitespace",
423*8f30879bSAndrew Lindesay 		!(BUrl("http://host.exa ple.com").IsValid()));
424*8f30879bSAndrew Lindesay }
425*8f30879bSAndrew Lindesay 
426*8f30879bSAndrew Lindesay 
427*8f30879bSAndrew Lindesay // #pragma mark - Control
428fa2dd9c4SAndrew Lindesay 
429fa2dd9c4SAndrew Lindesay 
430fa2dd9c4SAndrew Lindesay /*static*/ void
AddTests(BTestSuite & parent)431fa2dd9c4SAndrew Lindesay NetworkUrlTest::AddTests(BTestSuite& parent)
432fa2dd9c4SAndrew Lindesay {
433fa2dd9c4SAndrew Lindesay 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NetworkUrlTest");
434fa2dd9c4SAndrew Lindesay 
435fa2dd9c4SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
436fa2dd9c4SAndrew Lindesay 		"NetworkUrlTest::TestHostAndPortWithNoUserNameAndPassword",
437fa2dd9c4SAndrew Lindesay 		&NetworkUrlTest::TestHostAndPortWithNoUserNameAndPassword));
438fa2dd9c4SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
439fa2dd9c4SAndrew Lindesay 		"NetworkUrlTest::TestWithUserNameAndPasswordNoHostAndPort",
440fa2dd9c4SAndrew Lindesay 		&NetworkUrlTest::TestWithUserNameAndPasswordNoHostAndPort));
441fa2dd9c4SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
442fa2dd9c4SAndrew Lindesay 		"NetworkUrlTest::TestHostWithNoPortOrUserNameAndPassword",
443fa2dd9c4SAndrew Lindesay 		&NetworkUrlTest::TestHostWithNoPortOrUserNameAndPassword));
444fa2dd9c4SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
445fa2dd9c4SAndrew Lindesay 		"NetworkUrlTest::TestHostWithNoPortNoPath",
446fa2dd9c4SAndrew Lindesay 		&NetworkUrlTest::TestHostWithNoPortNoPath));
447fa2dd9c4SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
448fa2dd9c4SAndrew Lindesay 		"NetworkUrlTest::TestHostWithPortNoPath",
449fa2dd9c4SAndrew Lindesay 		&NetworkUrlTest::TestHostWithPortNoPath));
450fa2dd9c4SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
451fa2dd9c4SAndrew Lindesay 		"NetworkUrlTest::TestHostWithEmptyPort",
452fa2dd9c4SAndrew Lindesay 		&NetworkUrlTest::TestHostWithEmptyPort));
453cf657294SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
454cf657294SAndrew Lindesay 		"NetworkUrlTest::TestHostWithPathAndFragment",
455cf657294SAndrew Lindesay 		&NetworkUrlTest::TestHostWithPathAndFragment));
456cf657294SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
457cf657294SAndrew Lindesay 		"NetworkUrlTest::TestHostWithFragment",
458cf657294SAndrew Lindesay 		&NetworkUrlTest::TestHostWithFragment));
459cf657294SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
460cf657294SAndrew Lindesay 		"NetworkUrlTest::TestIpv6HostPortPathAndRequest",
461cf657294SAndrew Lindesay 		&NetworkUrlTest::TestIpv6HostPortPathAndRequest));
462cf657294SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
463cf657294SAndrew Lindesay 		"NetworkUrlTest::TestProtocol",
464cf657294SAndrew Lindesay 		&NetworkUrlTest::TestProtocol));
465cf657294SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
466cf657294SAndrew Lindesay 		"NetworkUrlTest::TestMailTo",
467cf657294SAndrew Lindesay 		&NetworkUrlTest::TestMailTo));
468cf657294SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
469cf657294SAndrew Lindesay 		"NetworkUrlTest::TestDataUrl",
470cf657294SAndrew Lindesay 		&NetworkUrlTest::TestDataUrl));
471cf657294SAndrew Lindesay 
472cf657294SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
473cf657294SAndrew Lindesay 		"NetworkUrlTest::TestAuthorityNoUserName",
474cf657294SAndrew Lindesay 		&NetworkUrlTest::TestAuthorityNoUserName));
475cf657294SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
476cf657294SAndrew Lindesay         "NetworkUrlTest::TestAuthorityWithCredentialsSeparatorNoPassword",
477cf657294SAndrew Lindesay         &NetworkUrlTest::TestAuthorityWithCredentialsSeparatorNoPassword));
478cf657294SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
479cf657294SAndrew Lindesay         "NetworkUrlTest::TestAuthorityWithoutCredentialsSeparatorNoPassword",
480cf657294SAndrew Lindesay         &NetworkUrlTest::TestAuthorityWithoutCredentialsSeparatorNoPassword));
481cf657294SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
482cf657294SAndrew Lindesay         "NetworkUrlTest::TestAuthorityBadPort",
483cf657294SAndrew Lindesay         &NetworkUrlTest::TestAuthorityBadPort));
484fa2dd9c4SAndrew Lindesay 
485fa2dd9c4SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
486fa2dd9c4SAndrew Lindesay 		"NetworkUrlTest::TestWhitespaceBefore",
487fa2dd9c4SAndrew Lindesay 		&NetworkUrlTest::TestWhitespaceBefore));
488fa2dd9c4SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
489fa2dd9c4SAndrew Lindesay 		"NetworkUrlTest::TestWhitespaceAfter",
490fa2dd9c4SAndrew Lindesay 		&NetworkUrlTest::TestWhitespaceAfter));
491fa2dd9c4SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
492fa2dd9c4SAndrew Lindesay 		"NetworkUrlTest::TestWhitespaceMiddle",
493fa2dd9c4SAndrew Lindesay 		&NetworkUrlTest::TestWhitespaceMiddle));
494cf657294SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
495cf657294SAndrew Lindesay 		"NetworkUrlTest::TestEmpty",
496cf657294SAndrew Lindesay 		&NetworkUrlTest::TestEmpty));
497fa2dd9c4SAndrew Lindesay 
498fa2dd9c4SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
499fa2dd9c4SAndrew Lindesay 		"NetworkUrlTest::TestFileUrl",
500fa2dd9c4SAndrew Lindesay 		&NetworkUrlTest::TestFileUrl));
501fa2dd9c4SAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
502fa2dd9c4SAndrew Lindesay 		"NetworkUrlTest::TestValidFullUrl", &NetworkUrlTest::TestValidFullUrl));
503*8f30879bSAndrew Lindesay 	suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>(
504*8f30879bSAndrew Lindesay 		"NetworkUrlTest::TestBadHosts", &NetworkUrlTest::TestBadHosts));
505fa2dd9c4SAndrew Lindesay 
506fa2dd9c4SAndrew Lindesay 	parent.addTest("NetworkUrlTest", &suite);
507fa2dd9c4SAndrew Lindesay }
508