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.

43 lines
1.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. namespace p3k\HTTP\Tests;
  3. use p3k\HTTP\Pinnable;
  4. use p3k\HTTP\Transport;
  5. // Answers from a table of canned responses and records every request it
  6. // was asked to make, and what it was pinned to at the time.
  7. class RecordingTransport implements Transport, Pinnable {
  8. public $requests = [];
  9. public $max_redirects = null;
  10. private $pinned = null;
  11. private $responses;
  12. /** @param array $responses url => [code, headers string, body] */
  13. public function __construct(array $responses) {
  14. $this->responses = $responses;
  15. }
  16. public function pin_addresses($resolve) { $this->pinned = $resolve; }
  17. public function set_timeout($timeout) {}
  18. public function set_max_redirects($max) { $this->max_redirects = $max; }
  19. public function get($url, $headers=[]) { return $this->answer('GET', $url, false, $headers); }
  20. public function post($url, $body, $headers=[]) { return $this->answer('POST', $url, $body, $headers); }
  21. public function put($url, $body, $headers=[]) { return $this->answer('PUT', $url, $body, $headers); }
  22. public function head($url, $headers=[]) { return $this->answer('HEAD', $url, false, $headers); }
  23. private function answer($method, $url, $body, $headers) {
  24. $this->requests[] = ['method' => $method, 'url' => $url, 'body' => $body, 'headers' => $headers, 'pinned' => $this->pinned];
  25. list($code, $header, $responseBody) = $this->responses[$url] ?? [404, '', 'not found'];
  26. return [
  27. 'code' => $code,
  28. 'header' => "HTTP/1.1 $code X\r\n" . $header,
  29. 'body' => $responseBody,
  30. 'error' => '',
  31. 'error_description' => '',
  32. 'url' => $url,
  33. 'debug' => '',
  34. ];
  35. }
  36. }