1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
<?php
require_once "PHPUnit.php";
require_once "Tests/Services/Yadis/DiscoverData.php";
require_once "Services/Yadis/Yadis.php";
require_once "Services/Yadis/HTTPFetcher.php";
$__status_header_re = '/Status: (\d+) .*?$/m';
function mkResponse($data)
{
global $__status_header_re;
$matches = array();
$status_mo = preg_match($__status_header_re, $data, $matches);
list($headers_str, $body) = explode("\n\n", $data, 2);
$headers = array();
foreach (explode("\n", $headers_str) as $line) {
list($k, $v) = explode(":", $line, 2);
$k = strtolower(trim($k));
$v = trim($v);
$headers[$k] = $v;
}
$status = intval($matches[1]);
$r = new Services_Yadis_HTTPResponse(null, $status, $headers, $body);
return $r;
}
class TestFetcher {
function TestFetcher($base_url)
{
$this->base_url = $base_url;
}
function get($url, $headers = null)
{
$current_url = $url;
while (true) {
$parsed = parse_url($current_url);
$path = substr($parsed['path'], 1);
$data = generateSample($path, $this->base_url);
if ($data === null) {
return new Services_Yadis_HTTPResponse($current_url,
404,
array(),
'');
}
$response = mkResponse($data);
if (in_array($response->status, array(301, 302, 303, 307))) {
$current_url = $response->headers['location'];
} else {
$response->final_url = $current_url;
return $response;
}
}
}
}
class MockFetcher {
function MockFetcher() {
$this->count = 0;
}
function get($uri, $headers = null, $body = null)
{
$this->count++;
if ($this->count == 1) {
$headers = array(strtolower('X-XRDS-Location') . ': http://unittest/404');
return new Services_Yadis_HTTPResponse($uri, 200, $headers, '');
} else {
return new Services_Yadis_HTTPResponse($uri, 404);
}
}
}
class TestSecondGet extends PHPUnit_TestCase {
function test_404()
{
$uri = "http://something.unittest/";
$response = null;
$fetcher = new MockFetcher();
$this->assertTrue(
Services_Yadis_Yadis::discover($uri, $response, $fetcher) === null);
}
}
class _TestCase extends PHPUnit_TestCase {
var $base_url = 'http://invalid.unittest/';
function _TestCase($input_name, $id_name, $result_name, $success)
{
$this->input_name = $input_name;
$this->id_name = $id_name;
$this->result_name = $result_name;
$this->success = $success;
$this->fetcher = new TestFetcher($this->base_url);
parent::PHPUnit_TestCase();
}
function setUp()
{
list($this->input_url, $this->expected) = generateResult($this->base_url,
$this->input_name,
$this->id_name,
$this->result_name,
$this->success);
}
function runTest()
{
if ($this->expected === null) {
$response = array();
$this->assertTrue(
Services_Yadis_Yadis::discover($this->input_url, $response,
$this->fetcher) === null);
} else {
$response = array();
$result = Services_Yadis_Yadis::discover($this->input_url,
$response,
$this->fetcher);
if ($result === null) {
$this->fail("Discovery result was null");
return;
}
$this->assertEquals($this->input_url, $result->request_uri);
$msg = 'Identity URL mismatch: actual = %s, expected = %s';
$msg = sprintf($msg, $result->uri, $this->expected->uri);
$this->assertEquals($this->expected->uri, $result->uri, $msg);
$msg = 'Content mismatch: actual = %s, expected = %s';
$msg = sprintf($msg, $result->body, $this->expected->body);
$this->assertEquals($this->expected->body, $result->body, $msg);
$this->assertEquals($this->expected->xrds_uri, $result->xrds_uri);
$this->assertEquals($this->expected->content_type, $result->content_type);
}
}
function getName()
{
if ($this->input_url) {
return $this->input_url;
} else {
return $this->input_name;
}
}
}
class Tests_Services_Yadis_Discover extends PHPUnit_TestSuite {
function getName()
{
return "Tests_Services_Yadis_Discover";
}
function Tests_Services_Yadis_Discover()
{
global $testlist;
foreach ($testlist as $test) {
list($success, $input_name, $id_name, $result_name) = $test;
$this->addTest(new _TestCase($input_name, $id_name, $result_name, $success));
}
}
}
?>
|