xref: /dokuwiki/_test/tests/inc/httpclient_mock.php (revision 5a8d6e48ea57255f66cd0126355f1b73f1000de9)
195e6ded1SAndreas Gohr<?php
2198564abSMichael Große
3*5a8d6e48SMichael Großeuse dokuwiki\HTTP\HTTPClient;
4198564abSMichael Große
595e6ded1SAndreas Gohr/**
695e6ded1SAndreas Gohr * Class HTTPMockClient
795e6ded1SAndreas Gohr *
895e6ded1SAndreas Gohr * Does not really mock the client, it still does real connections but will retry failed connections
995e6ded1SAndreas Gohr * to work around shaky connectivity.
1095e6ded1SAndreas Gohr */
1195e6ded1SAndreas Gohrclass HTTPMockClient extends HTTPClient {
1295e6ded1SAndreas Gohr    protected $tries;
13cd7164f4SAndreas Gohr    protected $lasturl;
1495e6ded1SAndreas Gohr
1595e6ded1SAndreas Gohr    /**
1695e6ded1SAndreas Gohr     * Sets shorter timeout
1795e6ded1SAndreas Gohr     */
186aad717eSAndreas Gohr    public function __construct() {
1995e6ded1SAndreas Gohr        parent::__construct();
2095e6ded1SAndreas Gohr        $this->timeout = 8; // slightly faster timeouts
2195e6ded1SAndreas Gohr    }
2295e6ded1SAndreas Gohr
2395e6ded1SAndreas Gohr    /**
2495e6ded1SAndreas Gohr     * Returns true if the connection timed out
2595e6ded1SAndreas Gohr     *
2695e6ded1SAndreas Gohr     * @return bool
2795e6ded1SAndreas Gohr     */
286aad717eSAndreas Gohr    public function noconnection() {
2995e6ded1SAndreas Gohr        return ($this->tries === 0);
3095e6ded1SAndreas Gohr    }
3195e6ded1SAndreas Gohr
3295e6ded1SAndreas Gohr    /**
3395e6ded1SAndreas Gohr     * Retries sending the request multiple times
3495e6ded1SAndreas Gohr     *
3595e6ded1SAndreas Gohr     * @param string $url
3695e6ded1SAndreas Gohr     * @param string $data
3795e6ded1SAndreas Gohr     * @param string $method
3895e6ded1SAndreas Gohr     * @return bool
3995e6ded1SAndreas Gohr     */
406aad717eSAndreas Gohr    public function sendRequest($url, $data = '', $method = 'GET') {
41cd7164f4SAndreas Gohr        $this->lasturl = $url;
4295e6ded1SAndreas Gohr        $this->tries = 2; // configures the number of retries
4395e6ded1SAndreas Gohr        $return      = false;
4495e6ded1SAndreas Gohr        while($this->tries) {
4595e6ded1SAndreas Gohr            $return = parent::sendRequest($url, $data, $method);
46cd7164f4SAndreas Gohr            if($this->status != -100 && $this->status != 408) break;
47cd7164f4SAndreas Gohr            usleep((3 - $this->tries) * 250000);
4895e6ded1SAndreas Gohr            $this->tries--;
4995e6ded1SAndreas Gohr        }
5095e6ded1SAndreas Gohr        return $return;
5195e6ded1SAndreas Gohr    }
526aad717eSAndreas Gohr
536aad717eSAndreas Gohr    /**
546aad717eSAndreas Gohr     * Return detailed error data
556aad717eSAndreas Gohr     *
566aad717eSAndreas Gohr     * @param string $info optional additional info
576aad717eSAndreas Gohr     * @return string
586aad717eSAndreas Gohr     */
596aad717eSAndreas Gohr    public function errorInfo($info = '') {
606aad717eSAndreas Gohr        return json_encode(
616aad717eSAndreas Gohr            array(
62cd7164f4SAndreas Gohr                'URL' => $this->lasturl,
636aad717eSAndreas Gohr                'Error' => $this->error,
646aad717eSAndreas Gohr                'Status' => $this->status,
656aad717eSAndreas Gohr                'Body' => $this->resp_body,
666aad717eSAndreas Gohr                'Info' => $info
676aad717eSAndreas Gohr            ), JSON_PRETTY_PRINT
686aad717eSAndreas Gohr        );
696aad717eSAndreas Gohr    }
7095e6ded1SAndreas Gohr}
71