aboutsummaryrefslogtreecommitdiff
path: root/models/openid-php-openid-782224d/Tests/Auth/Yadis/Discover_Yadis.php
blob: 0ca3b2eeaa6fd6791d15ca1ae31fd0d789249fc0 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php

require_once "Tests/Auth/Yadis/DiscoverData.php";
require_once "Auth/Yadis/Yadis.php";
require_once "Auth/Yadis/HTTPFetcher.php";

global $__status_header_re;
$__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 Auth_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 Auth_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 BlankContentTypeFetcher {
    function get($url, $headers=null)
    {
        return new Auth_Yadis_HTTPResponse(
            $url, 200, array("Content-Type" => ""), '');
    }
}

class NoContentTypeFetcher {
    function get($url, $headers=null)
    {
        return new Auth_Yadis_HTTPResponse($url, 200, array(), '');
    }
}

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 Auth_Yadis_HTTPResponse($uri, 200, $headers, '');
        } else {
            return new Auth_Yadis_HTTPResponse($uri, 404);
        }
    }
}

class TestSecondGet extends PHPUnit_Framework_TestCase {
    function test_404()
    {
        $uri = "http://something.unittest/";
        $response = null;
        $fetcher = new MockFetcher();
        $this->assertTrue(
               Auth_Yadis_Yadis::discover($uri, $response, $fetcher) === null);
    }
}

class _TestCase extends PHPUnit_Framework_TestCase {
    var $base_url = 'http://invalid.unittest/';

    function _TestCase($input_name, $id_name, $result_name, $success)
    {
      parent::__construct();
        $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);
    }

    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) {
            $result = Auth_Yadis_Yadis::discover($this->input_url,
                                                     $this->fetcher);
            $this->assertTrue($result->isFailure());
        } else {
            $result = Auth_Yadis_Yadis::discover($this->input_url,
                                                     $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->normalized_uri, $this->expected->uri);
            $this->assertEquals($this->expected->uri, $result->normalized_uri, $msg);

            $msg = 'Content mismatch: actual = %s, expected = %s';
            $msg = sprintf($msg, $result->response_text, $this->expected->body);
            $this->assertEquals($this->expected->body, $result->response_text, $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_Auth_Yadis_Discover_Yadis extends PHPUnit_Framework_TestSuite {

    function getName()
    {
        return "Tests_Auth_Yadis_Discover_Yadis";
    }

    function Tests_Auth_Yadis_Discover_Yadis()
    {
        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));
        }
    }
}

class Tests_Auth_Yadis_Discover_Yadis_ContentTypes extends PHPUnit_Framework_TestCase {
    function test_is_xrds_yadis_location()
    {
        $result = new Auth_Yadis_DiscoveryResult('http://request.uri/');
        $result->normalized_uri = "http://normalized/";
        $result->xrds_uri = "http://normalized/xrds";

        $this->assertTrue($result->isXRDS());
    }

    function test_is_xrds_content_type()
    {
        $result = new Auth_Yadis_DiscoveryResult('http://request.uri/');
        $result->normalized_uri = $result->xrds_uri = "http://normalized/";
        $result->content_type = Auth_Yadis_CONTENT_TYPE;

        $this->assertTrue($result->isXRDS());
      }

    function test_is_xrds_neither()
    {
        $result = new Auth_Yadis_DiscoveryResult('http://request.uri/');
        $result->normalized_uri = $result->xrds_uri = "http://normalized/";
        $result->content_type = "another/content-type";

        $this->assertTrue(!$result->isXRDS());
      }

    function test_no_content_type()
    {
        $fetcher = new NoContentTypeFetcher();
        $result = Auth_Yadis_Yadis::discover("http://bogus", $fetcher);
        $this->assertEquals(null, $result->content_type);
    }

    function test_blank_content_type()
    {
        $fetcher = new BlankContentTypeFetcher();
        $result = Auth_Yadis_Yadis::discover("http://bogus", $fetcher);
        $this->assertEquals("", $result->content_type);
    }
}

global $Tests_Auth_Yadis_Discover_Yadis_other;
$Tests_Auth_Yadis_Discover_Yadis_other = array(
      new Tests_Auth_Yadis_Discover_Yadis_ContentTypes()
      );