/** * Called before each test exits, and before tearDown() */ public function assertPostConditions() { if (!$this->getExpectedException()) { $this->assertEquals(200, $this->response->getCode()); $this->assertNotEmpty($this->response->getTime()); $this->assertEquals("HTTP/1.1", $this->response->getVersion()); $this->assertContains($this->url, $this->response->getInformation()["url"]); $headers = $this->response->getHeaders(); $this->assertEquals("unit-testing", $headers["X-Testing-Pong"]); } }
/** * 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::getCode */ public function testGetCode() { $this->assertEquals($this->values["code"], $this->response->getCode()); }