UrlValidatorTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Constraints;
  11. use Symfony\Bridge\PhpUnit\DnsMock;
  12. use Symfony\Component\Validator\Constraints\Url;
  13. use Symfony\Component\Validator\Constraints\UrlValidator;
  14. use Symfony\Component\Validator\Validation;
  15. /**
  16. * @group dns-sensitive
  17. */
  18. class UrlValidatorTest extends AbstractConstraintValidatorTest
  19. {
  20. protected function getApiVersion()
  21. {
  22. return Validation::API_VERSION_2_5;
  23. }
  24. protected function createValidator()
  25. {
  26. return new UrlValidator();
  27. }
  28. public function testNullIsValid()
  29. {
  30. $this->validator->validate(null, new Url());
  31. $this->assertNoViolation();
  32. }
  33. public function testEmptyStringIsValid()
  34. {
  35. $this->validator->validate('', new Url());
  36. $this->assertNoViolation();
  37. }
  38. public function testEmptyStringFromObjectIsValid()
  39. {
  40. $this->validator->validate(new EmailProvider(), new Url());
  41. $this->assertNoViolation();
  42. }
  43. /**
  44. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  45. */
  46. public function testExpectsStringCompatibleType()
  47. {
  48. $this->validator->validate(new \stdClass(), new Url());
  49. }
  50. /**
  51. * @dataProvider getValidUrls
  52. */
  53. public function testValidUrls($url)
  54. {
  55. $this->validator->validate($url, new Url());
  56. $this->assertNoViolation();
  57. }
  58. public function getValidUrls()
  59. {
  60. return array(
  61. array('http://a.pl'),
  62. array('http://www.google.com'),
  63. array('http://www.google.com.'),
  64. array('http://www.google.museum'),
  65. array('https://google.com/'),
  66. array('https://google.com:80/'),
  67. array('http://www.example.coop/'),
  68. array('http://www.test-example.com/'),
  69. array('http://www.symfony.com/'),
  70. array('http://symfony.fake/blog/'),
  71. array('http://symfony.com/?'),
  72. array('http://symfony.com/search?type=&q=url+validator'),
  73. array('http://symfony.com/#'),
  74. array('http://symfony.com/#?'),
  75. array('http://www.symfony.com/doc/current/book/validation.html#supported-constraints'),
  76. array('http://very.long.domain.name.com/'),
  77. array('http://localhost/'),
  78. array('http://myhost123/'),
  79. array('http://127.0.0.1/'),
  80. array('http://127.0.0.1:80/'),
  81. array('http://[::1]/'),
  82. array('http://[::1]:80/'),
  83. array('http://[1:2:3::4:5:6:7]/'),
  84. array('http://sãopaulo.com/'),
  85. array('http://xn--sopaulo-xwa.com/'),
  86. array('http://sãopaulo.com.br/'),
  87. array('http://xn--sopaulo-xwa.com.br/'),
  88. array('http://пример.испытание/'),
  89. array('http://xn--e1afmkfd.xn--80akhbyknj4f/'),
  90. array('http://مثال.إختبار/'),
  91. array('http://xn--mgbh0fb.xn--kgbechtv/'),
  92. array('http://例子.测试/'),
  93. array('http://xn--fsqu00a.xn--0zwm56d/'),
  94. array('http://例子.測試/'),
  95. array('http://xn--fsqu00a.xn--g6w251d/'),
  96. array('http://例え.テスト/'),
  97. array('http://xn--r8jz45g.xn--zckzah/'),
  98. array('http://مثال.آزمایشی/'),
  99. array('http://xn--mgbh0fb.xn--hgbk6aj7f53bba/'),
  100. array('http://실례.테스트/'),
  101. array('http://xn--9n2bp8q.xn--9t4b11yi5a/'),
  102. array('http://العربية.idn.icann.org/'),
  103. array('http://xn--ogb.idn.icann.org/'),
  104. array('http://xn--e1afmkfd.xn--80akhbyknj4f.xn--e1afmkfd/'),
  105. array('http://xn--espaa-rta.xn--ca-ol-fsay5a/'),
  106. array('http://xn--d1abbgf6aiiy.xn--p1ai/'),
  107. array('http://☎.com/'),
  108. array('http://username:password@symfony.com'),
  109. array('http://user.name:password@symfony.com'),
  110. array('http://username:pass.word@symfony.com'),
  111. array('http://user.name:pass.word@symfony.com'),
  112. array('http://user-name@symfony.com'),
  113. array('http://symfony.com?'),
  114. array('http://symfony.com?query=1'),
  115. array('http://symfony.com/?query=1'),
  116. array('http://symfony.com#'),
  117. array('http://symfony.com#fragment'),
  118. array('http://symfony.com/#fragment'),
  119. array('http://symfony.com/#one_more%20test'),
  120. );
  121. }
  122. /**
  123. * @dataProvider getInvalidUrls
  124. */
  125. public function testInvalidUrls($url)
  126. {
  127. $constraint = new Url(array(
  128. 'message' => 'myMessage',
  129. ));
  130. $this->validator->validate($url, $constraint);
  131. $this->buildViolation('myMessage')
  132. ->setParameter('{{ value }}', '"'.$url.'"')
  133. ->setCode(Url::INVALID_URL_ERROR)
  134. ->assertRaised();
  135. }
  136. public function getInvalidUrls()
  137. {
  138. return array(
  139. array('google.com'),
  140. array('://google.com'),
  141. array('http ://google.com'),
  142. array('http:/google.com'),
  143. array('http://goog_le.com'),
  144. array('http://google.com::aa'),
  145. array('http://google.com:aa'),
  146. array('ftp://google.fr'),
  147. array('faked://google.fr'),
  148. array('http://127.0.0.1:aa/'),
  149. array('ftp://[::1]/'),
  150. array('http://[::1'),
  151. array('http://hello.☎/'),
  152. array('http://:password@symfony.com'),
  153. array('http://:password@@symfony.com'),
  154. array('http://username:passwordsymfony.com'),
  155. array('http://usern@me:password@symfony.com'),
  156. array('http://example.com/exploit.html?<script>alert(1);</script>'),
  157. array('http://example.com/exploit.html?hel lo'),
  158. array('http://example.com/exploit.html?not_a%hex'),
  159. array('http://'),
  160. );
  161. }
  162. /**
  163. * @dataProvider getValidCustomUrls
  164. */
  165. public function testCustomProtocolIsValid($url)
  166. {
  167. $constraint = new Url(array(
  168. 'protocols' => array('ftp', 'file', 'git'),
  169. ));
  170. $this->validator->validate($url, $constraint);
  171. $this->assertNoViolation();
  172. }
  173. public function getValidCustomUrls()
  174. {
  175. return array(
  176. array('ftp://google.com'),
  177. array('file://127.0.0.1'),
  178. array('git://[::1]/'),
  179. );
  180. }
  181. /**
  182. * @dataProvider getCheckDns
  183. * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
  184. */
  185. public function testCheckDns($violation)
  186. {
  187. DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? '' : 'A'))));
  188. $constraint = new Url(array(
  189. 'checkDNS' => true,
  190. 'dnsMessage' => 'myMessage',
  191. ));
  192. $this->validator->validate('http://example.com', $constraint);
  193. if (!$violation) {
  194. $this->assertNoViolation();
  195. } else {
  196. $this->buildViolation('myMessage')
  197. ->setParameter('{{ value }}', '"example.com"')
  198. ->setCode(Url::INVALID_URL_ERROR)
  199. ->assertRaised();
  200. }
  201. }
  202. public function getCheckDns()
  203. {
  204. return array(array(true), array(false));
  205. }
  206. }
  207. class EmailProvider
  208. {
  209. public function __toString()
  210. {
  211. return '';
  212. }
  213. }