xref: /dokuwiki/_test/tests/inc/httpclient_mock.php (revision d39e9bea24a7ba90507d3480f95066fdbe3699a8)
1<?php
2/**
3 * Class HTTPMockClient
4 *
5 * Does not really mock the client, it still does real connections but will retry failed connections
6 * to work around shaky connectivity.
7 */
8class HTTPMockClient extends HTTPClient {
9    protected $tries;
10
11    /**
12     * Sets shorter timeout
13     */
14    public function __construct() {
15        parent::__construct();
16        $this->timeout = 8; // slightly faster timeouts
17    }
18
19    /**
20     * Returns true if the connection timed out
21     *
22     * @return bool
23     */
24    public function noconnection() {
25        return ($this->tries === 0);
26    }
27
28    /**
29     * Retries sending the request multiple times
30     *
31     * @param string $url
32     * @param string $data
33     * @param string $method
34     * @return bool
35     */
36    public function sendRequest($url, $data = '', $method = 'GET') {
37        $this->tries = 2; // configures the number of retries
38        $return      = false;
39        while($this->tries) {
40            $return = parent::sendRequest($url, $data, $method);
41            if($this->status != -100) break;
42            $this->tries--;
43        }
44        return $return;
45    }
46
47    /**
48     * Return detailed error data
49     *
50     * @param string $info optional additional info
51     * @return string
52     */
53    public function errorInfo($info = '') {
54        return json_encode(
55            array(
56                'Error' => $this->error,
57                'Status' => $this->status,
58                'Body' => $this->resp_body,
59                'Info' => $info
60            ), JSON_PRETTY_PRINT
61        );
62    }
63}
64