1 /* 2 * Copyright 2016, Andrew Lindesay, apl@lindesay.co.nz. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "NetworkUrlTest.h" 8 9 #include <Url.h> 10 11 #include <cppunit/TestCaller.h> 12 #include <cppunit/TestSuite.h> 13 14 15 NetworkUrlTest::NetworkUrlTest() 16 { 17 } 18 19 20 NetworkUrlTest::~NetworkUrlTest() 21 { 22 } 23 24 25 void 26 NetworkUrlTest::setUp() 27 { 28 } 29 30 31 void 32 NetworkUrlTest::tearDown() 33 { 34 } 35 36 37 // General Tests --------------------------------------------------------------- 38 39 /* 40 This is the "happy days" tests that checks that a URL featuring all of the 41 parsed elements successfully processes and the elements are present. 42 */ 43 44 void NetworkUrlTest::TestValidFullUrl() 45 { 46 BUrl url("http://ewe:pea@www.something.co.nz:8888/some/path?key1=value1#fragment"); 47 CPPUNIT_ASSERT(url.IsValid()); 48 CPPUNIT_ASSERT(url.Protocol() == "http"); 49 CPPUNIT_ASSERT(url.HasProtocol()); 50 CPPUNIT_ASSERT(url.UserName() == "ewe"); 51 CPPUNIT_ASSERT(url.HasUserName()); 52 CPPUNIT_ASSERT(url.Password() == "pea"); 53 CPPUNIT_ASSERT(url.HasPassword()); 54 CPPUNIT_ASSERT(url.Host() == "www.something.co.nz"); 55 CPPUNIT_ASSERT(url.HasHost()); 56 CPPUNIT_ASSERT(url.Port() == 8888); 57 CPPUNIT_ASSERT(url.HasPort()); 58 CPPUNIT_ASSERT(url.Path() == "/some/path"); 59 CPPUNIT_ASSERT(url.HasPath()); 60 CPPUNIT_ASSERT(url.Request() == "key1=value1"); 61 CPPUNIT_ASSERT(url.HasRequest()); 62 CPPUNIT_ASSERT(url.Fragment() == "fragment"); 63 CPPUNIT_ASSERT(url.HasFragment()); 64 } 65 66 67 void NetworkUrlTest::TestFileUrl() 68 { 69 BUrl url("file:///northisland/wellington/brooklyn/windturbine"); 70 CPPUNIT_ASSERT(url.IsValid()); 71 CPPUNIT_ASSERT(url.Protocol() == "file"); 72 CPPUNIT_ASSERT(url.HasProtocol()); 73 CPPUNIT_ASSERT(!url.HasUserName()); 74 CPPUNIT_ASSERT(!url.HasPassword()); 75 CPPUNIT_ASSERT(url.Host() == ""); 76 CPPUNIT_ASSERT(url.HasHost()); 77 CPPUNIT_ASSERT(!url.HasPort()); 78 CPPUNIT_ASSERT(url.Path() == "/northisland/wellington/brooklyn/windturbine"); 79 CPPUNIT_ASSERT(url.HasPath()); 80 CPPUNIT_ASSERT(!url.HasRequest()); 81 CPPUNIT_ASSERT(!url.HasFragment()); 82 } 83 84 85 // Authority Tests (UserName, Password, Host, Port) ---------------------------- 86 87 88 void NetworkUrlTest::TestWithUserNameAndPasswordNoHostAndPort() 89 { 90 BUrl url("wierd://tea:tree@/x"); 91 CPPUNIT_ASSERT(url.IsValid()); 92 CPPUNIT_ASSERT(url.Protocol() == "wierd"); 93 CPPUNIT_ASSERT(url.HasProtocol()); 94 CPPUNIT_ASSERT(url.UserName() == "tea"); 95 CPPUNIT_ASSERT(url.HasUserName()); 96 CPPUNIT_ASSERT(url.Password() == "tree"); 97 CPPUNIT_ASSERT(url.HasPassword()); 98 CPPUNIT_ASSERT(url.Host() == ""); 99 CPPUNIT_ASSERT(!url.HasHost()); 100 CPPUNIT_ASSERT(!url.HasPort()); 101 CPPUNIT_ASSERT(url.Path() == "/x"); 102 CPPUNIT_ASSERT(url.HasPath()); 103 CPPUNIT_ASSERT(!url.HasRequest()); 104 CPPUNIT_ASSERT(!url.HasFragment()); 105 } 106 107 108 void NetworkUrlTest::TestHostAndPortWithNoUserNameAndPassword() 109 { 110 BUrl url("https://www.something.co.nz:443/z"); 111 CPPUNIT_ASSERT(url.IsValid()); 112 CPPUNIT_ASSERT(url.Protocol() == "https"); 113 CPPUNIT_ASSERT(url.HasProtocol()); 114 CPPUNIT_ASSERT(!url.HasUserName()); 115 CPPUNIT_ASSERT(!url.HasPassword()); 116 CPPUNIT_ASSERT(url.Port() == 443); 117 CPPUNIT_ASSERT(url.HasPort()); 118 CPPUNIT_ASSERT(url.Host() == "www.something.co.nz"); 119 CPPUNIT_ASSERT(url.HasHost()); 120 CPPUNIT_ASSERT(url.Path() == "/z"); 121 CPPUNIT_ASSERT(url.HasPath()); 122 CPPUNIT_ASSERT(!url.HasRequest()); 123 CPPUNIT_ASSERT(!url.HasFragment()); 124 } 125 126 127 void NetworkUrlTest::TestHostWithNoPortOrUserNameAndPassword() 128 { 129 BUrl url("https://www.something.co.nz/z"); 130 CPPUNIT_ASSERT(url.IsValid()); 131 CPPUNIT_ASSERT(url.Protocol() == "https"); 132 CPPUNIT_ASSERT(url.HasProtocol()); 133 CPPUNIT_ASSERT(!url.HasUserName()); 134 CPPUNIT_ASSERT(!url.HasPassword()); 135 CPPUNIT_ASSERT(url.Host() == "www.something.co.nz"); 136 CPPUNIT_ASSERT(url.HasHost()); 137 CPPUNIT_ASSERT(!url.HasPort()); 138 CPPUNIT_ASSERT(url.Path() == "/z"); 139 CPPUNIT_ASSERT(url.HasPath()); 140 CPPUNIT_ASSERT(!url.HasRequest()); 141 CPPUNIT_ASSERT(!url.HasFragment()); 142 } 143 144 145 void NetworkUrlTest::TestHostWithNoPortNoPath() 146 { 147 BUrl url("https://www.something.co.nz"); 148 CPPUNIT_ASSERT(url.IsValid()); 149 CPPUNIT_ASSERT(url.Protocol() == "https"); 150 CPPUNIT_ASSERT(url.HasProtocol()); 151 CPPUNIT_ASSERT(!url.HasUserName()); 152 CPPUNIT_ASSERT(!url.HasPassword()); 153 CPPUNIT_ASSERT(url.Host() == "www.something.co.nz"); 154 CPPUNIT_ASSERT(url.HasHost()); 155 CPPUNIT_ASSERT(!url.HasPort()); 156 CPPUNIT_ASSERT(!url.HasPath()); 157 CPPUNIT_ASSERT(!url.HasRequest()); 158 CPPUNIT_ASSERT(!url.HasFragment()); 159 } 160 161 162 void NetworkUrlTest::TestHostWithPortNoPath() 163 { 164 BUrl url("https://www.something.co.nz:1234"); 165 CPPUNIT_ASSERT(url.IsValid()); 166 CPPUNIT_ASSERT(url.Protocol() == "https"); 167 CPPUNIT_ASSERT(url.HasProtocol()); 168 CPPUNIT_ASSERT(!url.HasUserName()); 169 CPPUNIT_ASSERT(!url.HasPassword()); 170 CPPUNIT_ASSERT(url.Host() == "www.something.co.nz"); 171 CPPUNIT_ASSERT(url.HasHost()); 172 CPPUNIT_ASSERT(url.Port() == 1234); 173 CPPUNIT_ASSERT(url.HasPort()); 174 CPPUNIT_ASSERT(!url.HasPath()); 175 CPPUNIT_ASSERT(!url.HasRequest()); 176 CPPUNIT_ASSERT(!url.HasFragment()); 177 } 178 179 180 void NetworkUrlTest::TestHostWithEmptyPort() 181 { 182 BUrl url("https://www.something.co.nz:"); 183 CPPUNIT_ASSERT(url.IsValid()); 184 CPPUNIT_ASSERT(url.Protocol() == "https"); 185 CPPUNIT_ASSERT(url.HasProtocol()); 186 CPPUNIT_ASSERT(!url.HasUserName()); 187 CPPUNIT_ASSERT(!url.HasPassword()); 188 CPPUNIT_ASSERT(url.Host() == "www.something.co.nz"); 189 CPPUNIT_ASSERT(url.HasHost()); 190 CPPUNIT_ASSERT(!url.HasPort()); 191 CPPUNIT_ASSERT(!url.HasPath()); 192 CPPUNIT_ASSERT(!url.HasRequest()); 193 CPPUNIT_ASSERT(!url.HasFragment()); 194 } 195 196 197 // Invalid Forms --------------------------------------------------------------- 198 199 200 void NetworkUrlTest::TestWhitespaceBefore() 201 { 202 BUrl url(" https://www.something.co.nz/z"); 203 CPPUNIT_ASSERT(!url.IsValid()); 204 } 205 206 207 void NetworkUrlTest::TestWhitespaceAfter() 208 { 209 BUrl url("https://www.something.co.nz/z\t\t "); 210 CPPUNIT_ASSERT(!url.IsValid()); 211 } 212 213 214 void NetworkUrlTest::TestWhitespaceMiddle() 215 { 216 BUrl url("https://www. something.co.nz/z"); 217 CPPUNIT_ASSERT(!url.IsValid()); 218 } 219 220 221 void NetworkUrlTest::TestHttpNoHost() 222 { 223 BUrl url("https:///z"); 224 CPPUNIT_ASSERT(!url.IsValid()); 225 } 226 227 228 // Control --------------------------------------------------------------------- 229 230 231 /*static*/ void 232 NetworkUrlTest::AddTests(BTestSuite& parent) 233 { 234 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NetworkUrlTest"); 235 236 suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>( 237 "NetworkUrlTest::TestHostAndPortWithNoUserNameAndPassword", 238 &NetworkUrlTest::TestHostAndPortWithNoUserNameAndPassword)); 239 suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>( 240 "NetworkUrlTest::TestWithUserNameAndPasswordNoHostAndPort", 241 &NetworkUrlTest::TestWithUserNameAndPasswordNoHostAndPort)); 242 suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>( 243 "NetworkUrlTest::TestHostWithNoPortOrUserNameAndPassword", 244 &NetworkUrlTest::TestHostWithNoPortOrUserNameAndPassword)); 245 suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>( 246 "NetworkUrlTest::TestHostWithNoPortNoPath", 247 &NetworkUrlTest::TestHostWithNoPortNoPath)); 248 suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>( 249 "NetworkUrlTest::TestHostWithPortNoPath", 250 &NetworkUrlTest::TestHostWithPortNoPath)); 251 suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>( 252 "NetworkUrlTest::TestHostWithEmptyPort", 253 &NetworkUrlTest::TestHostWithEmptyPort)); 254 255 suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>( 256 "NetworkUrlTest::TestWhitespaceBefore", 257 &NetworkUrlTest::TestWhitespaceBefore)); 258 suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>( 259 "NetworkUrlTest::TestWhitespaceAfter", 260 &NetworkUrlTest::TestWhitespaceAfter)); 261 suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>( 262 "NetworkUrlTest::TestWhitespaceMiddle", 263 &NetworkUrlTest::TestWhitespaceMiddle)); 264 265 suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>( 266 "NetworkUrlTest::TestFileUrl", 267 &NetworkUrlTest::TestFileUrl)); 268 suite.addTest(new CppUnit::TestCaller<NetworkUrlTest>( 269 "NetworkUrlTest::TestValidFullUrl", &NetworkUrlTest::TestValidFullUrl)); 270 271 parent.addTest("NetworkUrlTest", &suite); 272 } 273