|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WpOrg\Requests\Tests\Fixtures; |
| 4 | + |
| 5 | +use WpOrg\Requests\Transport; |
| 6 | + |
| 7 | +final class TransportRedirectMock implements Transport { |
| 8 | + public $code = 302; |
| 9 | + public $chunked = false; |
| 10 | + public $body = ''; |
| 11 | + public $raw_headers = ''; |
| 12 | + |
| 13 | + private $redirected = []; |
| 14 | + |
| 15 | + public $redirected_transport = NULL; |
| 16 | + |
| 17 | + private static $messages = [ |
| 18 | + 100 => '100 Continue', |
| 19 | + 101 => '101 Switching Protocols', |
| 20 | + 200 => '200 OK', |
| 21 | + 201 => '201 Created', |
| 22 | + 202 => '202 Accepted', |
| 23 | + 203 => '203 Non-Authoritative Information', |
| 24 | + 204 => '204 No Content', |
| 25 | + 205 => '205 Reset Content', |
| 26 | + 206 => '206 Partial Content', |
| 27 | + 300 => '300 Multiple Choices', |
| 28 | + 301 => '301 Moved Permanently', |
| 29 | + 302 => '302 Found', |
| 30 | + 303 => '303 See Other', |
| 31 | + 304 => '304 Not Modified', |
| 32 | + 305 => '305 Use Proxy', |
| 33 | + 306 => '306 (Unused)', |
| 34 | + 307 => '307 Temporary Redirect', |
| 35 | + 400 => '400 Bad Request', |
| 36 | + 401 => '401 Unauthorized', |
| 37 | + 402 => '402 Payment Required', |
| 38 | + 403 => '403 Forbidden', |
| 39 | + 404 => '404 Not Found', |
| 40 | + 405 => '405 Method Not Allowed', |
| 41 | + 406 => '406 Not Acceptable', |
| 42 | + 407 => '407 Proxy Authentication Required', |
| 43 | + 408 => '408 Request Timeout', |
| 44 | + 409 => '409 Conflict', |
| 45 | + 410 => '410 Gone', |
| 46 | + 411 => '411 Length Required', |
| 47 | + 412 => '412 Precondition Failed', |
| 48 | + 413 => '413 Request Entity Too Large', |
| 49 | + 414 => '414 Request-URI Too Long', |
| 50 | + 415 => '415 Unsupported Media Type', |
| 51 | + 416 => '416 Requested Range Not Satisfiable', |
| 52 | + 417 => '417 Expectation Failed', |
| 53 | + 418 => '418 I\'m a teapot', |
| 54 | + 428 => '428 Precondition Required', |
| 55 | + 429 => '429 Too Many Requests', |
| 56 | + 431 => '431 Request Header Fields Too Large', |
| 57 | + 500 => '500 Internal Server Error', |
| 58 | + 501 => '501 Not Implemented', |
| 59 | + 502 => '502 Bad Gateway', |
| 60 | + 503 => '503 Service Unavailable', |
| 61 | + 504 => '504 Gateway Timeout', |
| 62 | + 505 => '505 HTTP Version Not Supported', |
| 63 | + 511 => '511 Network Authentication Required', |
| 64 | + ]; |
| 65 | + |
| 66 | + public function request($url, $headers = [], $data = [], $options = []) { |
| 67 | + if (array_key_exists($url, $this->redirected)) |
| 68 | + { |
| 69 | + return $this->redirected_transport->request($url, $headers, $data, $options); |
| 70 | + } |
| 71 | + |
| 72 | + $redirect_url = "https://example.com/redirected?url=" . urlencode($url); |
| 73 | + |
| 74 | + $status = isset(self::$messages[$this->code]) ? self::$messages[$this->code] : $this->code . ' unknown'; |
| 75 | + $response = "HTTP/1.0 $status\r\n"; |
| 76 | + $response .= "Content-Type: text/plain\r\n"; |
| 77 | + if ($this->chunked) { |
| 78 | + $response .= "Transfer-Encoding: chunked\r\n"; |
| 79 | + } |
| 80 | + $response .= "Location: $redirect_url\r\n"; |
| 81 | + $response .= $this->raw_headers; |
| 82 | + $response .= "Connection: close\r\n\r\n"; |
| 83 | + $response .= $this->body; |
| 84 | + |
| 85 | + $this->redirected[$url] = TRUE; |
| 86 | + $this->redirected[$redirect_url] = TRUE; |
| 87 | + |
| 88 | + return $response; |
| 89 | + } |
| 90 | + |
| 91 | + public function request_multiple($requests, $options) { |
| 92 | + $responses = []; |
| 93 | + foreach ($requests as $id => $request) { |
| 94 | + $handler = new self(); |
| 95 | + $handler->code = $request['options']['mock.code']; |
| 96 | + $handler->chunked = $request['options']['mock.chunked']; |
| 97 | + $handler->body = $request['options']['mock.body']; |
| 98 | + $handler->raw_headers = $request['options']['mock.raw_headers']; |
| 99 | + $responses[$id] = $handler->request($request['url'], $request['headers'], $request['data'], $request['options']); |
| 100 | + |
| 101 | + if (!empty($options['mock.parse'])) { |
| 102 | + $request['options']['hooks']->dispatch('transport.internal.parse_response', [&$responses[$id], $request]); |
| 103 | + $request['options']['hooks']->dispatch('multiple.request.complete', [&$responses[$id], $id]); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return $responses; |
| 108 | + } |
| 109 | + |
| 110 | + public static function test($capabilities = []) { |
| 111 | + return true; |
| 112 | + } |
| 113 | +} |
0 commit comments