1 /*
2  * Copyright (C) 2010 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
27  */
28 
29 #include "cxxtools/net/uri.h"
30 #include "cxxtools/unit/testsuite.h"
31 #include "cxxtools/unit/registertest.h"
32 
33 class UriTest : public cxxtools::unit::TestSuite
34 {
35     public:
UriTest()36         UriTest()
37         : cxxtools::unit::TestSuite("uri")
38         {
39             registerMethod("testUri_UPHP", *this, &UriTest::testUri_UPHP);
40             registerMethod("testUri_UHP", *this, &UriTest::testUri_UHP);
41             registerMethod("testUri_UPH", *this, &UriTest::testUri_UPH);
42             registerMethod("testUri_HP", *this, &UriTest::testUri_HP);
43             registerMethod("testUri_H", *this, &UriTest::testUri_H);
44             registerMethod("testUri_UPH6P", *this, &UriTest::testUri_UPH6P);
45             registerMethod("testUri_UH6P", *this, &UriTest::testUri_UH6P);
46             registerMethod("testUri_UPH6", *this, &UriTest::testUri_UPH6);
47             registerMethod("testUri_H6P", *this, &UriTest::testUri_H6P);
48             registerMethod("testUri_H6", *this, &UriTest::testUri_H6);
49             registerMethod("testQuery", *this, &UriTest::testQuery);
50             registerMethod("testFragment", *this, &UriTest::testFragment);
51             registerMethod("testQueryFragment", *this, &UriTest::testQueryFragment);
52             registerMethod("testHttpPort", *this, &UriTest::testHttpPort);
53             registerMethod("testHttpsPort", *this, &UriTest::testHttpsPort);
54             registerMethod("testFtpPort", *this, &UriTest::testFtpPort);
55             registerMethod("testUriStr", *this, &UriTest::testUriStr);
56         }
57 
testUri_UPHP()58         void testUri_UPHP()
59         {
60             cxxtools::net::Uri uri("http://user:password@host:56/blah.html");
61 
62             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.protocol(), "http");
63             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.user(), "user");
64             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.password(), "password");
65             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.host(), "host");
66             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 56);
67             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.path(), "/blah.html");
68         }
69 
testUri_UHP()70         void testUri_UHP()
71         {
72             cxxtools::net::Uri uri("http://user@host:56/blah.html");
73 
74             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.protocol(), "http");
75             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.user(), "user");
76             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.password(), "");
77             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.host(), "host");
78             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 56);
79             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.path(), "/blah.html");
80         }
81 
testUri_UPH()82         void testUri_UPH()
83         {
84             cxxtools::net::Uri uri("http://user:password@host/blah.html");
85 
86             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.protocol(), "http");
87             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.user(), "user");
88             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.password(), "password");
89             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.host(), "host");
90             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 80);
91             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.path(), "/blah.html");
92         }
93 
testUri_HP()94         void testUri_HP()
95         {
96             cxxtools::net::Uri uri("http://host:56/blah.html");
97 
98             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.protocol(), "http");
99             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.user(), "");
100             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.password(), "");
101             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.host(), "host");
102             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 56);
103             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.path(), "/blah.html");
104         }
105 
testUri_H()106         void testUri_H()
107         {
108             cxxtools::net::Uri uri("http://host/blah.html");
109 
110             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.protocol(), "http");
111             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.user(), "");
112             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.password(), "");
113             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.host(), "host");
114             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 80);
115             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.path(), "/blah.html");
116         }
117 
testUri_UPH6P()118         void testUri_UPH6P()
119         {
120             cxxtools::net::Uri uri("http://user:password@[::1]:56/blah.html");
121 
122             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.protocol(), "http");
123             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.user(), "user");
124             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.password(), "password");
125             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.host(), "::1");
126             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 56);
127             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.path(), "/blah.html");
128         }
129 
testUri_UH6P()130         void testUri_UH6P()
131         {
132             cxxtools::net::Uri uri("http://user@[::1]:56/blah.html");
133 
134             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.protocol(), "http");
135             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.user(), "user");
136             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.password(), "");
137             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.host(), "::1");
138             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 56);
139             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.path(), "/blah.html");
140         }
141 
testUri_UPH6()142         void testUri_UPH6()
143         {
144             cxxtools::net::Uri uri("http://user:password@[::1]/blah.html");
145 
146             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.protocol(), "http");
147             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.user(), "user");
148             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.password(), "password");
149             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.host(), "::1");
150             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 80);
151             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.path(), "/blah.html");
152         }
153 
testUri_H6P()154         void testUri_H6P()
155         {
156             cxxtools::net::Uri uri("http://[::1]:56/blah.html");
157 
158             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.protocol(), "http");
159             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.user(), "");
160             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.password(), "");
161             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.host(), "::1");
162             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 56);
163             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.path(), "/blah.html");
164         }
165 
testUri_H6()166         void testUri_H6()
167         {
168             cxxtools::net::Uri uri("http://[::1]/blah.html");
169 
170             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.protocol(), "http");
171             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.user(), "");
172             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.password(), "");
173             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.host(), "::1");
174             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 80);
175             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.path(), "/blah.html");
176         }
177 
testQuery()178         void testQuery()
179         {
180             cxxtools::net::Uri uri("http://host/?abc=1");
181             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.query(), "abc=1");
182         }
183 
testFragment()184         void testFragment()
185         {
186             cxxtools::net::Uri uri("http://host/#foo");
187             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.fragment(), "foo");
188         }
189 
testQueryFragment()190         void testQueryFragment()
191         {
192             cxxtools::net::Uri uri("http://host/?abc=1#foo");
193             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.query(), "abc=1");
194             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.fragment(), "foo");
195         }
196 
testHttpPort()197         void testHttpPort()
198         {
199             cxxtools::net::Uri uri("http://host/");
200             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 80);
201         }
202 
testHttpsPort()203         void testHttpsPort()
204         {
205             cxxtools::net::Uri uri("https://host/");
206             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 443);
207         }
208 
testFtpPort()209         void testFtpPort()
210         {
211             cxxtools::net::Uri uri("ftp://host/");
212             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.port(), 21);
213         }
214 
testUriStr()215         void testUriStr()
216         {
217             cxxtools::net::Uri uri("http://user:password@host:80/blah.html");
218             CXXTOOLS_UNIT_ASSERT_EQUALS(uri.str(), "http://user:password@host/blah.html");
219         }
220 
221 };
222 
223 cxxtools::unit::RegisterTest<UriTest> register_UriTest;
224