xref: /dokuwiki/_test/tests/inc/httpclient_mock.php (revision cd7164f4e81070b25b2459b81bbaebdc6ac1dc22)
195e6ded1SAndreas Gohr<?php
295e6ded1SAndreas Gohr/**
395e6ded1SAndreas Gohr * Class HTTPMockClient
495e6ded1SAndreas Gohr *
595e6ded1SAndreas Gohr * Does not really mock the client, it still does real connections but will retry failed connections
695e6ded1SAndreas Gohr * to work around shaky connectivity.
795e6ded1SAndreas Gohr */
895e6ded1SAndreas Gohrclass HTTPMockClient extends HTTPClient {
995e6ded1SAndreas Gohr    protected $tries;
10*cd7164f4SAndreas Gohr    protected $lasturl;
1195e6ded1SAndreas Gohr
1295e6ded1SAndreas Gohr    /**
1395e6ded1SAndreas Gohr     * Sets shorter timeout
1495e6ded1SAndreas Gohr     */
156aad717eSAndreas Gohr    public function __construct() {
1695e6ded1SAndreas Gohr        parent::__construct();
1795e6ded1SAndreas Gohr        $this->timeout = 8; // slightly faster timeouts
1895e6ded1SAndreas Gohr    }
1995e6ded1SAndreas Gohr
2095e6ded1SAndreas Gohr    /**
2195e6ded1SAndreas Gohr     * Returns true if the connection timed out
2295e6ded1SAndreas Gohr     *
2395e6ded1SAndreas Gohr     * @return bool
2495e6ded1SAndreas Gohr     */
256aad717eSAndreas Gohr    public function noconnection() {
2695e6ded1SAndreas Gohr        return ($this->tries === 0);
2795e6ded1SAndreas Gohr    }
2895e6ded1SAndreas Gohr
2995e6ded1SAndreas Gohr    /**
3095e6ded1SAndreas Gohr     * Retries sending the request multiple times
3195e6ded1SAndreas Gohr     *
3295e6ded1SAndreas Gohr     * @param string $url
3395e6ded1SAndreas Gohr     * @param string $data
3495e6ded1SAndreas Gohr     * @param string $method
3595e6ded1SAndreas Gohr     * @return bool
3695e6ded1SAndreas Gohr     */
376aad717eSAndreas Gohr    public function sendRequest($url, $data = '', $method = 'GET') {
38*cd7164f4SAndreas Gohr        $this->lasturl = $url;
3995e6ded1SAndreas Gohr        $this->tries = 2; // configures the number of retries
4095e6ded1SAndreas Gohr        $return      = false;
4195e6ded1SAndreas Gohr        while($this->tries) {
4295e6ded1SAndreas Gohr            $return = parent::sendRequest($url, $data, $method);
43*cd7164f4SAndreas Gohr            if($this->status != -100 && $this->status != 408) break;
44*cd7164f4SAndreas Gohr            usleep((3 - $this->tries) * 250000);
4595e6ded1SAndreas Gohr            $this->tries--;
4695e6ded1SAndreas Gohr        }
4795e6ded1SAndreas Gohr        return $return;
4895e6ded1SAndreas Gohr    }
496aad717eSAndreas Gohr
506aad717eSAndreas Gohr    /**
516aad717eSAndreas Gohr     * Return detailed error data
526aad717eSAndreas Gohr     *
536aad717eSAndreas Gohr     * @param string $info optional additional info
546aad717eSAndreas Gohr     * @return string
556aad717eSAndreas Gohr     */
566aad717eSAndreas Gohr    public function errorInfo($info = '') {
576aad717eSAndreas Gohr        return json_encode(
586aad717eSAndreas Gohr            array(
59*cd7164f4SAndreas Gohr                'URL' => $this->lasturl,
606aad717eSAndreas Gohr                'Error' => $this->error,
616aad717eSAndreas Gohr                'Status' => $this->status,
626aad717eSAndreas Gohr                'Body' => $this->resp_body,
636aad717eSAndreas Gohr                'Info' => $info
646aad717eSAndreas Gohr            ), JSON_PRETTY_PRINT
656aad717eSAndreas Gohr        );
666aad717eSAndreas Gohr    }
6795e6ded1SAndreas Gohr}
68