xref: /dokuwiki/_test/tests/inc/httpclient_mock.php (revision 6aad717e42040730c251721a8371ce83b34be862)
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;
1095e6ded1SAndreas Gohr
1195e6ded1SAndreas Gohr    /**
1295e6ded1SAndreas Gohr     * Sets shorter timeout
1395e6ded1SAndreas Gohr     */
14*6aad717eSAndreas Gohr    public function __construct() {
1595e6ded1SAndreas Gohr        parent::__construct();
1695e6ded1SAndreas Gohr        $this->timeout = 8; // slightly faster timeouts
1795e6ded1SAndreas Gohr    }
1895e6ded1SAndreas Gohr
1995e6ded1SAndreas Gohr    /**
2095e6ded1SAndreas Gohr     * Returns true if the connection timed out
2195e6ded1SAndreas Gohr     *
2295e6ded1SAndreas Gohr     * @return bool
2395e6ded1SAndreas Gohr     */
24*6aad717eSAndreas Gohr    public function noconnection() {
2595e6ded1SAndreas Gohr        return ($this->tries === 0);
2695e6ded1SAndreas Gohr    }
2795e6ded1SAndreas Gohr
2895e6ded1SAndreas Gohr    /**
2995e6ded1SAndreas Gohr     * Retries sending the request multiple times
3095e6ded1SAndreas Gohr     *
3195e6ded1SAndreas Gohr     * @param string $url
3295e6ded1SAndreas Gohr     * @param string $data
3395e6ded1SAndreas Gohr     * @param string $method
3495e6ded1SAndreas Gohr     * @return bool
3595e6ded1SAndreas Gohr     */
36*6aad717eSAndreas Gohr    public function sendRequest($url, $data = '', $method = 'GET') {
3795e6ded1SAndreas Gohr        $this->tries = 2; // configures the number of retries
3895e6ded1SAndreas Gohr        $return      = false;
3995e6ded1SAndreas Gohr        while($this->tries) {
4095e6ded1SAndreas Gohr            $return = parent::sendRequest($url, $data, $method);
4195e6ded1SAndreas Gohr            if($this->status != -100) break;
4295e6ded1SAndreas Gohr            $this->tries--;
4395e6ded1SAndreas Gohr        }
4495e6ded1SAndreas Gohr        return $return;
4595e6ded1SAndreas Gohr    }
46*6aad717eSAndreas Gohr
47*6aad717eSAndreas Gohr    /**
48*6aad717eSAndreas Gohr     * Return detailed error data
49*6aad717eSAndreas Gohr     *
50*6aad717eSAndreas Gohr     * @param string $info optional additional info
51*6aad717eSAndreas Gohr     * @return string
52*6aad717eSAndreas Gohr     */
53*6aad717eSAndreas Gohr    public function errorInfo($info = '') {
54*6aad717eSAndreas Gohr        return json_encode(
55*6aad717eSAndreas Gohr            array(
56*6aad717eSAndreas Gohr                'Error' => $this->error,
57*6aad717eSAndreas Gohr                'Status' => $this->status,
58*6aad717eSAndreas Gohr                'Body' => $this->resp_body,
59*6aad717eSAndreas Gohr                'Info' => $info
60*6aad717eSAndreas Gohr            ), JSON_PRETTY_PRINT
61*6aad717eSAndreas Gohr        );
62*6aad717eSAndreas Gohr    }
6395e6ded1SAndreas Gohr}
64