xref: /dokuwiki/_test/tests/inc/httpclient_mock.php (revision 95e6ded1b26c14b1ef55699b171ce422827364b1)
1*95e6ded1SAndreas Gohr<?php
2*95e6ded1SAndreas Gohr/**
3*95e6ded1SAndreas Gohr * Class HTTPMockClient
4*95e6ded1SAndreas Gohr *
5*95e6ded1SAndreas Gohr * Does not really mock the client, it still does real connections but will retry failed connections
6*95e6ded1SAndreas Gohr * to work around shaky connectivity.
7*95e6ded1SAndreas Gohr */
8*95e6ded1SAndreas Gohrclass HTTPMockClient extends HTTPClient {
9*95e6ded1SAndreas Gohr    protected $tries;
10*95e6ded1SAndreas Gohr
11*95e6ded1SAndreas Gohr    /**
12*95e6ded1SAndreas Gohr     * Sets shorter timeout
13*95e6ded1SAndreas Gohr     */
14*95e6ded1SAndreas Gohr    function __construct() {
15*95e6ded1SAndreas Gohr        parent::__construct();
16*95e6ded1SAndreas Gohr        $this->timeout = 8; // slightly faster timeouts
17*95e6ded1SAndreas Gohr    }
18*95e6ded1SAndreas Gohr
19*95e6ded1SAndreas Gohr    /**
20*95e6ded1SAndreas Gohr     * Returns true if the connection timed out
21*95e6ded1SAndreas Gohr     *
22*95e6ded1SAndreas Gohr     * @return bool
23*95e6ded1SAndreas Gohr     */
24*95e6ded1SAndreas Gohr    function noconnection() {
25*95e6ded1SAndreas Gohr        return ($this->tries === 0);
26*95e6ded1SAndreas Gohr    }
27*95e6ded1SAndreas Gohr
28*95e6ded1SAndreas Gohr    /**
29*95e6ded1SAndreas Gohr     * Retries sending the request multiple times
30*95e6ded1SAndreas Gohr     *
31*95e6ded1SAndreas Gohr     * @param string $url
32*95e6ded1SAndreas Gohr     * @param string $data
33*95e6ded1SAndreas Gohr     * @param string $method
34*95e6ded1SAndreas Gohr     * @return bool
35*95e6ded1SAndreas Gohr     */
36*95e6ded1SAndreas Gohr    function sendRequest($url, $data = '', $method = 'GET') {
37*95e6ded1SAndreas Gohr        $this->tries = 2; // configures the number of retries
38*95e6ded1SAndreas Gohr        $return      = false;
39*95e6ded1SAndreas Gohr        while($this->tries) {
40*95e6ded1SAndreas Gohr            $return = parent::sendRequest($url, $data, $method);
41*95e6ded1SAndreas Gohr            if($this->status != -100) break;
42*95e6ded1SAndreas Gohr            $this->tries--;
43*95e6ded1SAndreas Gohr        }
44*95e6ded1SAndreas Gohr        return $return;
45*95e6ded1SAndreas Gohr    }
46*95e6ded1SAndreas Gohr}