Release 2.1.0, merged in 1313 to HEAD.
[htmlpurifier.git] / tests / HTMLPurifier / URITest.php
blob9da37a7a1deadd57a17698e420d0e8d47c8015d7
1 <?php
3 require_once 'HTMLPurifier/URI.php';
4 require_once 'HTMLPurifier/URIParser.php';
6 class HTMLPurifier_URITest extends HTMLPurifier_URIHarness
9 function createURI($uri) {
10 $parser = new HTMLPurifier_URIParser();
11 return $parser->parse($uri);
14 function test_construct() {
15 $uri1 = new HTMLPurifier_URI('HTTP', 'bob', 'example.com', '23', '/foo', 'bar=2', 'slash');
16 $uri2 = new HTMLPurifier_URI('http', 'bob', 'example.com', 23, '/foo', 'bar=2', 'slash');
17 $this->assertIdentical($uri1, $uri2);
20 var $oldRegistry;
22 function &setUpSchemeRegistryMock() {
23 $this->oldRegistry = HTMLPurifier_URISchemeRegistry::instance();
24 generate_mock_once('HTMLPurifier_URIScheme');
25 generate_mock_once('HTMLPurifier_URISchemeRegistry');
26 $registry =& HTMLPurifier_URISchemeRegistry::instance(
27 new HTMLPurifier_URISchemeRegistryMock()
29 return $registry;
32 function &setUpSchemeMock($name) {
33 $registry =& $this->setUpSchemeRegistryMock();
34 $scheme_mock = new HTMLPurifier_URISchemeMock();
35 $registry->setReturnValue('getScheme', $scheme_mock, array($name, '*', '*'));
36 return $scheme_mock;
39 function setUpNoValidSchemes() {
40 $registry =& $this->setUpSchemeRegistryMock();
41 $registry->setReturnValue('getScheme', false, array('*', '*', '*'));
44 function tearDownSchemeRegistryMock() {
45 HTMLPurifier_URISchemeRegistry::instance($this->oldRegistry);
48 function test_getSchemeObj() {
49 $scheme_mock =& $this->setUpSchemeMock('http');
51 $uri = $this->createURI('http:');
52 $scheme_obj = $uri->getSchemeObj($this->config, $this->context);
53 $this->assertIdentical($scheme_obj, $scheme_mock);
55 $this->tearDownSchemeRegistryMock();
58 function test_getSchemeObj_invalidScheme() {
59 $this->setUpNoValidSchemes();
61 $uri = $this->createURI('http:');
62 $result = $uri->getSchemeObj($this->config, $this->context);
63 $this->assertIdentical($result, false);
65 $this->tearDownSchemeRegistryMock();
68 function test_getSchemaObj_defaultScheme() {
69 $scheme = 'foobar';
71 $scheme_mock =& $this->setUpSchemeMock($scheme);
72 $this->config->set('URI', 'DefaultScheme', $scheme);
74 $uri = $this->createURI('hmm');
75 $scheme_obj = $uri->getSchemeObj($this->config, $this->context);
76 $this->assertIdentical($scheme_obj, $scheme_mock);
78 $this->tearDownSchemeRegistryMock();
81 function test_getSchemaObj_invalidDefaultScheme() {
82 $this->setUpNoValidSchemes();
83 $this->config->set('URI', 'DefaultScheme', 'foobar');
85 $uri = $this->createURI('hmm');
87 $this->expectError('Default scheme object "foobar" was not readable');
88 $result = $uri->getSchemeObj($this->config, $this->context);
89 $this->assertIdentical($result, false);
91 $this->tearDownSchemeRegistryMock();
94 function assertToString($expect_uri, $scheme, $userinfo, $host, $port, $path, $query, $fragment) {
95 $uri = new HTMLPurifier_URI($scheme, $userinfo, $host, $port, $path, $query, $fragment);
96 $string = $uri->toString();
97 $this->assertIdentical($string, $expect_uri);
100 function test_toString_full() {
101 $this->assertToString(
102 'http://bob@example.com:300/foo?bar=baz#fragment',
103 'http', 'bob', 'example.com', 300, '/foo', 'bar=baz', 'fragment'
107 function test_toString_scheme() {
108 $this->assertToString(
109 'http:',
110 'http', null, null, null, '', null, null
114 function test_toString_authority() {
115 $this->assertToString(
116 '//bob@example.com:8080',
117 null, 'bob', 'example.com', 8080, '', null, null
121 function test_toString_path() {
122 $this->assertToString(
123 '/path/to',
124 null, null, null, null, '/path/to', null, null
128 function test_toString_query() {
129 $this->assertToString(
130 '?q=string',
131 null, null, null, null, '', 'q=string', null
135 function test_toString_fragment() {
136 $this->assertToString(
137 '#fragment',
138 null, null, null, null, '', null, 'fragment'
142 function assertValidation($uri, $expect_uri = true) {
143 if ($expect_uri === true) $expect_uri = $uri;
144 $uri = $this->createURI($uri);
145 $result = $uri->validate($this->config, $this->context);
146 if ($expect_uri === false) {
147 $this->assertFalse($result);
148 } else {
149 $this->assertTrue($result);
150 $this->assertIdentical($uri->toString(), $expect_uri);
154 function test_validate_overlongPort() {
155 $this->assertValidation('http://example.com:65536', 'http://example.com');
158 function test_validate_zeroPort() {
159 $this->assertValidation('http://example.com:00', 'http://example.com');
162 function test_validate_invalidHostThatLooksLikeIPv6() {
163 $this->assertValidation('http://[2001:0db8:85z3:08d3:1319:8a2e:0370:7334]', 'http:');