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.

68 lines
1.7 KiB

  1. <?php
  2. namespace p3k;
  3. class HTTPTest extends HTTPCurl {
  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. 'url' => $response['url']
  22. );
  23. }
  24. private function _read_file($url) {
  25. $filename = $this->_testDataPath.preg_replace('/https?:\/\//', '', $url);
  26. if(!file_exists($filename)) {
  27. $filename = $this->_testDataPath.'404.response.txt';
  28. }
  29. $response = file_get_contents($filename);
  30. $split = explode("\r\n\r\n", $response);
  31. if(count($split) < 2) {
  32. throw new \Exception("Invalid file contents in test data, check that newlines are CRLF: $url");
  33. }
  34. $headers = array_shift($split);
  35. $body = implode("\r\n", $split);
  36. if(preg_match('/HTTP\/1\.1 (\d+)/', $headers, $match)) {
  37. $code = $match[1];
  38. }
  39. $headers = preg_replace('/HTTP\/1\.1 \d+ .+/', '', $headers);
  40. $parsedHeaders = self::parse_headers($headers);
  41. if(array_key_exists('Location', $parsedHeaders)) {
  42. $effectiveUrl = \mf2\resolveUrl($url, $parsedHeaders['Location']);
  43. } else {
  44. $effectiveUrl = $url;
  45. }
  46. return array(
  47. 'code' => $code,
  48. 'headers' => $parsedHeaders,
  49. 'body' => $body,
  50. 'error' => '',
  51. 'error_description' => '',
  52. 'url' => $effectiveUrl
  53. );
  54. }
  55. }