/** * Called $this->web->request(), and decode the json response * * @param string $query Query string to append to the test url * @return array * @throws Exception */ protected function request($query = null) { $this->url = self::TEST_URL; if (null !== $query) { $query = ltrim($query, "?"); $this->url .= "?{$query}"; } $this->response = $this->web->request($this->url); $array = json_decode($this->response->getBody(), true); if (!$array) { throw new Exception("Testing server did not return json encoded data."); } return $array; }
/** * Creates an array of error information based on the server response * * The Bitcoin api is a real mess. For some errors the server responds with a json encoded string. * For other errors an html string is returned, but the format of the html varies depending on * the error. Some of the html is well formatted, and can be parsed with SimpleXML, and other html * strings are not well formatted. * * This method attempts to parse the various error responses, and returns an array with the keys: * ``` * "message" - (string) A message describing the error * "code" - (int) An error code * ``` * * Returns `["message" => "", "code" => 0]` when there is no error. * * @param WebResponse $response The server response * @return array */ protected function getResponseError(WebResponse $response) { $error = ["message" => "", "code" => 0]; $response_text = $response->getBody(); $obj = json_decode($response_text, true); if (!$obj) { if (stripos($response_text, "<H1>401 Unauthorized.</H1>") !== false) { $error["message"] = "Unauthorized"; $error["code"] = RPCErrorCodes::WALLET_PASSPHRASE_INCORRECT; } else { if (strpos($response_text, "<?xml") !== false) { $xml = simplexml_load_string($response_text); if ($xml && isset($xml->body->p[0])) { $error["message"] = trim((string) $xml->body->p[0]); $error["message"] = preg_replace("/[\\n\\r]/", "", $error["message"]); $error["message"] = preg_replace("/\\s{2,}/", " ", $error["message"]); $error["code"] = $response->getCode(); } else { $error["message"] = $response_text; $error["code"] = $response->getCode(); } } } } else { if (!empty($obj["error"])) { $error = $obj["error"]; } } return $error; }
/** * @covers Headzoo\Web\Tools\WebResponse::getBody */ public function testGetBody() { $this->assertEquals($this->values["body"], $this->response->getBody()); }