forked from apereo/phpCAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceWebTest.php
More file actions
executable file
·433 lines (396 loc) · 15.7 KB
/
ServiceWebTest.php
File metadata and controls
executable file
·433 lines (396 loc) · 15.7 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once dirname(__FILE__).'/../harness/DummyRequest.php';
require_once dirname(__FILE__).'/../harness/BasicResponse.php';
/**
* Test class for verifying the operation of service tickets.
*
*
* Generated by PHPUnit on 2010-09-07 at 13:33:53.
*/
class ServiceWebTest extends PHPUnit_Framework_TestCase
{
/**
* @var CAS_Client
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
// phpCAS::setDebug(dirname(__FILE__).'/../test.log');
// error_reporting(E_ALL);
$_SERVER['SERVER_NAME'] = 'www.clientapp.com';
$_SERVER['SERVER_PORT'] = '80';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['SERVER_ADMIN'] = 'root@localhost';
$_SERVER['REQUEST_URI'] = '/';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['PHP_SELF'] = '/index.php';
$_SESSION = array();
$this->object = new CAS_Client(
CAS_VERSION_2_0, // Server Version
true, // Proxy
'cas.example.edu', // Server Hostname
443, // Server port
'/cas/', // Server URI
false // Start Session
);
$this->object->setRequestImplementation('CAS_TestHarness_DummyRequest');
$this->object->setCasServerCACert('/path/to/ca_cert.crt');
// Bypass PGT storage since CAS_Client->callback() will exit. Just build up the session manually
// so that we are in a state from which we can attempt to fetch proxy tickets and make proxied requests.
$_SESSION['phpCAS']['user'] = 'jdoe';
$_SESSION['phpCAS']['pgt'] = 'PGT-clientapp-abc123';
$_SESSION['phpCAS']['proxies'] = array();
$_SESSION['phpCAS']['service_cookies'] = array();
$_SESSION['phpCAS']['attributes'] = array();
// Force Authentication to initialize the client.
$this->object->forceAuthentication();
/*********************************************************
* Enumerate our responses
*********************************************************/
/*********************************************************
* 1. Valid Proxy ticket and service
*********************************************************/
// Proxy ticket Response
$response = new CAS_TestHarness_BasicResponse('https', 'cas.example.edu', '/cas/proxy');
$response->matchQueryParameters(array(
'targetService' => 'http://www.service.com/my_webservice',
'pgt' => 'PGT-clientapp-abc123',
));
$response->ensureIsGet();
$response->setResponseHeaders(array(
'HTTP/1.1 200 OK',
'Date: Wed, 29 Sep 2010 19:20:57 GMT',
'Server: Apache-Coyote/1.1',
'Pragma: no-cache',
'Expires: Thu, 01 Jan 1970 00:00:00 GMT',
'Cache-Control: no-cache, no-store',
'Content-Type: text/html;charset=UTF-8',
'Content-Language: en-US',
'Via: 1.1 cas.example.edu',
'Connection: close',
'Transfer-Encoding: chunked',
));
$response->setResponseBody(
"<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
<cas:proxySuccess>
<cas:proxyTicket>PT-asdfas-dfasgww2323radf3</cas:proxyTicket>
</cas:proxySuccess>
</cas:serviceResponse>
");
$response->ensureCaCertPathEquals('/path/to/ca_cert.crt');
CAS_TestHarness_DummyRequest::addResponse($response);
// Valid Service Response
$response = new CAS_TestHarness_BasicResponse('http', 'www.service.com', '/my_webservice');
$response->matchQueryParameters(array(
'ticket' => 'PT-asdfas-dfasgww2323radf3',
));
$response->ensureIsGet();
$response->setResponseHeaders(array(
'HTTP/1.1 200 OK',
'Date: Wed, 29 Sep 2010 19:20:57 GMT',
'Server: Apache-Coyote/1.1',
'Pragma: no-cache',
'Expires: Thu, 01 Jan 1970 00:00:00 GMT',
'Cache-Control: no-cache, no-store',
'Content-Type: text/plain;charset=UTF-8',
'Content-Language: en-US',
'Via: 1.1 cas.example.edu',
'Connection: close',
'Transfer-Encoding: chunked',
));
$response->setResponseBody("Hello from the service.");
CAS_TestHarness_DummyRequest::addResponse($response);
/*********************************************************
* 2. Proxy Ticket Error
*********************************************************/
// Error Proxy ticket Response
$response = new CAS_TestHarness_BasicResponse('https', 'cas.example.edu', '/cas/proxy');
$response->matchQueryParameters(array(
'targetService' => 'http://www.service.com/my_other_webservice',
'pgt' => 'PGT-clientapp-abc123',
));
$response->ensureIsGet();
$response->setResponseHeaders(array(
'HTTP/1.1 200 OK',
'Date: Wed, 29 Sep 2010 19:20:57 GMT',
'Server: Apache-Coyote/1.1',
'Pragma: no-cache',
'Expires: Thu, 01 Jan 1970 00:00:00 GMT',
'Cache-Control: no-cache, no-store',
'Content-Type: text/html;charset=UTF-8',
'Content-Language: en-US',
'Via: 1.1 cas.example.edu',
'Connection: close',
'Transfer-Encoding: chunked',
));
$response->setResponseBody(
"<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
<cas:proxyFailure code='INTERNAL_ERROR'>
an internal error occurred during ticket validation
</cas:proxyFailure>
</cas:serviceResponse>
");
$response->ensureCaCertPathEquals('/path/to/ca_cert.crt');
CAS_TestHarness_DummyRequest::addResponse($response);
/*********************************************************
* 3. Server that doesn't respond/exist (sending failure)
*********************************************************/
// Proxy ticket Response
$response = new CAS_TestHarness_BasicResponse('https', 'cas.example.edu', '/cas/proxy');
$response->matchQueryParameters(array(
'targetService' => 'ssh://me.example.net',
'pgt' => 'PGT-clientapp-abc123',
));
$response->ensureIsGet();
$response->setResponseHeaders(array(
'HTTP/1.1 200 OK',
'Date: Wed, 29 Sep 2010 19:20:57 GMT',
'Server: Apache-Coyote/1.1',
'Pragma: no-cache',
'Expires: Thu, 01 Jan 1970 00:00:00 GMT',
'Cache-Control: no-cache, no-store',
'Content-Type: text/html;charset=UTF-8',
'Content-Language: en-US',
'Via: 1.1 cas.example.edu',
'Connection: close',
'Transfer-Encoding: chunked',
));
$response->setResponseBody(
"<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
<cas:proxySuccess>
<cas:proxyTicket>PT-ssh-1234abce</cas:proxyTicket>
</cas:proxySuccess>
</cas:serviceResponse>
");
$response->ensureCaCertPathEquals('/path/to/ca_cert.crt');
CAS_TestHarness_DummyRequest::addResponse($response);
/*********************************************************
* 4. Service With Error status.
*********************************************************/
// Proxy ticket Response
$response = new CAS_TestHarness_BasicResponse('https', 'cas.example.edu', '/cas/proxy');
$response->matchQueryParameters(array(
'targetService' => 'http://www.service.com/my_webservice_that_has_problems',
'pgt' => 'PGT-clientapp-abc123',
));
$response->ensureIsGet();
$response->setResponseHeaders(array(
'HTTP/1.1 200 OK',
'Date: Wed, 29 Sep 2010 19:20:57 GMT',
'Server: Apache-Coyote/1.1',
'Pragma: no-cache',
'Expires: Thu, 01 Jan 1970 00:00:00 GMT',
'Cache-Control: no-cache, no-store',
'Content-Type: text/html;charset=UTF-8',
'Content-Language: en-US',
'Via: 1.1 cas.example.edu',
'Connection: close',
'Transfer-Encoding: chunked',
));
$response->setResponseBody(
"<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
<cas:proxySuccess>
<cas:proxyTicket>PT-12345-abscasdfasdf</cas:proxyTicket>
</cas:proxySuccess>
</cas:serviceResponse>
");
$response->ensureCaCertPathEquals('/path/to/ca_cert.crt');
CAS_TestHarness_DummyRequest::addResponse($response);
// Service Error Response
$response = new CAS_TestHarness_BasicResponse('http', 'www.service.com', '/my_webservice_that_has_problems');
$response->matchQueryParameters(array(
'ticket' => 'PT-12345-abscasdfasdf',
));
$response->ensureIsGet();
$response->setResponseHeaders(array(
'HTTP/1.1 500 INTERNAL SERVER ERROR',
'Date: Wed, 29 Sep 2010 19:20:57 GMT',
'Server: Apache-Coyote/1.1',
'Pragma: no-cache',
'Expires: Thu, 01 Jan 1970 00:00:00 GMT',
'Cache-Control: no-cache, no-store',
'Content-Type: text/plain;charset=UTF-8',
'Content-Language: en-US',
'Via: 1.1 cas.example.edu',
'Connection: close',
'Transfer-Encoding: chunked',
));
$response->setResponseBody("Problems have Occurred.");
CAS_TestHarness_DummyRequest::addResponse($response);
/*********************************************************
* 5. Valid Proxy ticket and POST service
*********************************************************/
// Proxy ticket Response
$response = new CAS_TestHarness_BasicResponse('https', 'cas.example.edu', '/cas/proxy');
$response->matchQueryParameters(array(
'targetService' => 'http://www.service.com/post_webservice',
'pgt' => 'PGT-clientapp-abc123',
));
$response->ensureIsGet();
$response->setResponseHeaders(array(
'HTTP/1.1 200 OK',
'Date: Wed, 29 Sep 2010 19:20:57 GMT',
'Server: Apache-Coyote/1.1',
'Pragma: no-cache',
'Expires: Thu, 01 Jan 1970 00:00:00 GMT',
'Cache-Control: no-cache, no-store',
'Content-Type: text/html;charset=UTF-8',
'Content-Language: en-US',
'Via: 1.1 cas.example.edu',
'Connection: close',
'Transfer-Encoding: chunked',
));
$response->setResponseBody(
"<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
<cas:proxySuccess>
<cas:proxyTicket>PT-posting-dfasgww2323radf3</cas:proxyTicket>
</cas:proxySuccess>
</cas:serviceResponse>
");
$response->ensureCaCertPathEquals('/path/to/ca_cert.crt');
CAS_TestHarness_DummyRequest::addResponse($response);
// Valid Service Response
$response = new CAS_TestHarness_BasicResponse('http', 'www.service.com', '/post_webservice');
$response->matchQueryParameters(array(
'ticket' => 'PT-posting-dfasgww2323radf3',
));
$response->ensureIsPost();
$response->ensurePostBodyEquals('<request><method>doSomething</method><param type="string">with this</param></request>');
$response->ensureHasHeader('Content-Length: '.strlen('<request><method>doSomething</method><param type="string">with this</param></request>'));
$response->ensureHasHeader('Content-Type: text/xml');
$response->setResponseHeaders(array(
'HTTP/1.1 200 OK',
'Date: Wed, 29 Sep 2010 19:20:57 GMT',
'Server: Apache-Coyote/1.1',
'Pragma: no-cache',
'Expires: Thu, 01 Jan 1970 00:00:00 GMT',
'Cache-Control: no-cache, no-store',
'Content-Type: text/xml;charset=UTF-8',
'Content-Language: en-US',
'Via: 1.1 cas.example.edu',
'Connection: close',
'Transfer-Encoding: chunked',
));
$response->setResponseBody("<result><string>Yay, it worked.</string></result>");
CAS_TestHarness_DummyRequest::addResponse($response);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
CAS_TestHarness_DummyRequest::clearResponses();
}
/**
* Test that we can at least retrieve a proxy-ticket for the service.
*/
public function test_retrievePT() {
$pt = $this->object->retrievePT('http://www.service.com/my_webservice', $err_code, $err_msg);
$this->assertEquals('PT-asdfas-dfasgww2323radf3', $pt);
}
/**
* Test that we can at least retrieve a proxy-ticket for the service.
*/
public function test_serviceWeb() {
$result = $this->object->serviceWeb('http://www.service.com/my_webservice', $err_code, $output);
$this->assertTrue($result, $output);
$this->assertEquals(PHPCAS_SERVICE_OK, $err_code);
$this->assertEquals("Hello from the service.", $output);
}
/**
* Verify that proxy-ticket Exceptions are caught and converted to error codes in serviceWeb().
*/
public function test_serviceWeb_pt_error() {
$result = $this->object->serviceWeb('http://www.service.com/my_other_webservice', $err_code, $output);
$this->assertFalse($result, "serviceWeb() should have returned false on a PT error.");
$this->assertEquals(PHPCAS_SERVICE_PT_FAILURE, $err_code);
$this->assertStringStartsWith("PT retrieving failed", $output);
}
/**
* Verify that proxied-service Exceptions are caught and converted to error codes in serviceWeb().
*/
public function test_serviceWeb_service_error() {
$result = $this->object->serviceWeb('ssh://me.example.net', $err_code, $output);
$this->assertFalse($result, "serviceWeb() should have returned false on a service error.");
$this->assertEquals(PHPCAS_SERVICE_NOT_AVAILABLE, $err_code);
$this->assertStringStartsWith("The service", $output);
}
/**
* Direct usage of the Proxied GET service.
*/
public function test_http_get() {
$service = $this->object->getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_GET);
$service->setUrl('http://www.service.com/my_webservice');
$service->send();
$this->assertEquals(200, $service->getResponseStatusCode());
$this->assertEquals("Hello from the service.", $service->getResponseBody());
}
/**
* Verify that a CAS_ProxyTicketException is thrown if we try to access a service
* that results in a proxy-ticket failure.
*
* @expectedException CAS_ProxyTicketException
*/
public function test_pt_exception() {
$service = $this->object->getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_GET);
$service->setUrl('http://www.service.com/my_other_webservice');
$this->assertFalse($service->send(), 'Sending should have failed');
}
/**
* Verify that sending fails if we try to access a service
* that has a valid proxy ticket, but where the service has a sending error.
*
* @expectedException CAS_ProxiedService_Exception
*/
public function test_http_get_service_failure() {
$service = $this->object->getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_GET);
$service->setUrl('ssh://me.example.net');
$service->send();
}
/**
* Verify that sending fails if we try to access a service
* that has a valid proxy ticket, but where the service sends an HTTP error status.
*/
public function test_http_get_service_500_error() {
$service = $this->object->getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_GET);
$service->setUrl('http://www.service.com/my_webservice_that_has_problems');
$service->send();
$this->assertEquals(500, $service->getResponseStatusCode());
$this->assertEquals("Problems have Occurred.", $service->getResponseBody());
}
/**
* Direct usage of the Proxied POST service.
*/
public function test_http_post() {
$service = $this->object->getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_POST);
$service->setUrl('http://www.service.com/post_webservice');
$service->setBody('<request><method>doSomething</method><param type="string">with this</param></request>');
$service->setContentType('text/xml');
$service->send();
$this->assertEquals(200, $service->getResponseStatusCode());
$this->assertEquals("<result><string>Yay, it worked.</string></result>", $service->getResponseBody());
}
}
?>