MockSocket.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\mocks\net\http;
  9. class MockSocket extends \lithium\net\Socket {
  10. public $data = null;
  11. public $configs = array();
  12. public function __construct(array $config = array()) {
  13. parent::__construct((array) $config);
  14. }
  15. public function open(array $options = array()) {
  16. parent::open($options);
  17. return true;
  18. }
  19. public function close() {
  20. return true;
  21. }
  22. public function eof() {
  23. return true;
  24. }
  25. public function read() {
  26. if ($this->data->path === '/http_auth/') {
  27. if (is_array($this->data->auth)) {
  28. $request = $this->data->to('array');
  29. $data = $this->data->auth;
  30. $data['nc'] = '00000001';
  31. $data['cnonce'] = md5(time());
  32. $username = $this->data->username;
  33. $password = $this->data->password;
  34. $part1 = md5("{$username}:{$data['realm']}:{$password}");
  35. $part2 = "{$data['nonce']}:{$data['nc']}:{$data['cnonce']}:{$data['qop']}";
  36. $part3 = md5($this->data->method . ':' . $this->data->path);
  37. $hash = md5("{$part1}:{$part2}:{$part3}");
  38. preg_match('/response="(.*?)"/', $this->data->headers('Authorization'), $matches);
  39. list($match, $response) = $matches;
  40. if ($hash === $response) {
  41. return 'success';
  42. }
  43. }
  44. $header = 'Digest realm="app",qop="auth",nonce="4bca0fbca7bd0",';
  45. $header .= 'opaque="d3fb67a7aa4d887ec4bf83040a820a46";';
  46. $this->data->headers('WWW-Authenticate', $header);
  47. $status = "GET HTTP/1.1 401 Authorization Required";
  48. $response = array($status, join("\r\n", $this->data->headers()), "", "not authorized");
  49. return join("\r\n", $response);
  50. }
  51. return (string) $this->data;
  52. }
  53. public function write($data) {
  54. if (!is_object($data)) {
  55. $data = $this->_instance($this->_classes['request'], (array) $data + $this->_config);
  56. }
  57. $this->data = $data;
  58. return true;
  59. }
  60. public function timeout($time) {
  61. return true;
  62. }
  63. public function encoding($charset) {
  64. return true;
  65. }
  66. public function config() {
  67. return $this->_config;
  68. }
  69. }
  70. ?>