You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

  1. <?php
  2. namespace p3k;
  3. class HTTPTest extends HTTP {
  4. private $_testDataPath;
  5. public function __construct($testDataPath) {
  6. $this->_testDataPath = $testDataPath;
  7. }
  8. public function get($url) {
  9. return $this->_read_file($url);
  10. }
  11. public function post($url, $body, $headers=array()) {
  12. return $this->_read_file($url);
  13. }
  14. public function head($url) {
  15. $response = $this->_read_file($url);
  16. return array(
  17. 'code' => $response['code'],
  18. 'headers' => $response['headers'],
  19. 'error' => '',
  20. 'error_description' => ''
  21. );
  22. }
  23. private function _read_file($url) {
  24. $filename = $this->_testDataPath.preg_replace('/https?:\/\//', '', $url);
  25. if(!file_exists($filename)) {
  26. $filename = $this->_testDataPath.'404.response.txt';
  27. }
  28. $response = file_get_contents($filename);
  29. $split = explode("\r\n\r\n", $response);
  30. if(count($split) != 2) {
  31. throw new \Exception("Invalid file contents in test data, check that newlines are CRLF: $url");
  32. }
  33. list($headers, $body) = $split;
  34. if(preg_match('/HTTP\/1\.1 (\d+)/', $headers, $match)) {
  35. $code = $match[1];
  36. }
  37. $headers = preg_replace('/HTTP\/1\.1 \d+ .+/', '', $headers);
  38. return array(
  39. 'code' => $code,
  40. 'headers' => self::parse_headers($headers),
  41. 'body' => $body,
  42. 'error' => '',
  43. 'error_description' => ''
  44. );
  45. }
  46. }