public function makeRequest($method, $url, $data = array()) { global $map; # append params to url (for fixtures) $uri = str_replace($this->app->baseUri, '', $url); if ($method == 'GET' && !empty($data)) { $uri = $uri . '?' . http_build_query($data); } # extract response map parameters # $status = $map[$method][$uri][0]; $resource = $map[$method][$uri][1]; # record the request $this->addRequest($method, $uri, $data); # load response from fixture and return data $mock_data = MockData::load($resource); if (!$mock_data) { $msg = 'Unable to connect to the AWeber API. Please ensure that CURL is enabled and your '; $msg .= 'firewall allows outbound SSL requests from your web server.'; $error = array('message' => $msg, 'type' => 'APIUnreachableError', 'documentation_url' => 'https://labs.aweber.com/docs/troubleshooting'); throw new AWeberAPIException($error, $url); } $headers = array(); $headers['Status-Code'] = $status; if ($status == 201) { $headers['Location'] = $resource; } $mock_data->headers = $headers; if ($headers['Status-Code'] >= 400) { $data = json_decode($mock_data->body, true); throw new AWeberAPIException($data['error'], $url); } return $mock_data; }
/** * When authorization fails, an exception is raised */ public function test_should_raise_exception_if_auth_fails() { MockData::$oauth = false; $this->aweber->setAdapter($this->adapter); try { $account = $this->aweber->getAccount($this->user['token'], $this->user['secret']); $this->assertTrue(false, 'This should not run due to an exception'); } catch (Exception $e) { } MockData::$oauth = true; }
private function make_request() { $url = 'http://acme.org/new-authz'; $response = MockData::get_head_response(); \WP_Mock::setUp(); // Mock the remote request \WP_Mock::wpFunction('wp_remote_request', array('args' => array($url, array('body' => '')), 'times' => 1, 'return' => $response)); $nonce = new \LEWP\Request\Nonce($url); $nonce->send(); \WP_Mock::tearDown(); return $nonce; }
private function get_directory_object() { $url = 'http://acme.org/directory'; $args = array('body' => ''); $response = MockData::get_directory_response(); \WP_Mock::setUp(); // Mock the remote request \WP_Mock::wpFunction('wp_remote_request', array('args' => array($url, $args), 'times' => 1, 'return' => $response)); $directory = new \LEWP\Request\Directory($url); $directory->send(); \WP_Mock::tearDown(); return $directory; }
public function request($method, $uri, $data = array(), $options = array()) { if ($method == 'GET' && !empty($data)) { $uri = $uri . '?' . http_build_query($data); } $this->addRequest($method, $uri, $data); if (!empty($options['return']) && $options['return'] == 'status') { return $this->requests[$method][$uri]; } $data = MockData::load($this->requests[$method][$uri]); $this->parseAsError($data); return $data; }
public function test_send_generates_response_and_sets_properties() { $url = 'http://acme.org/directory'; $args = array('body' => ''); $response = MockData::get_directory_response(); \WP_Mock::setUp(); // Mock the remote request \WP_Mock::wpFunction('wp_remote_request', array('args' => array($url, $args), 'times' => 1, 'return' => $response)); $directory = new \LEWP\Request\Directory($url); $this->assertEquals($response, $directory->send()); $this->assertEquals($response, $directory->get_response()); $this->assertEquals($response['body'], $directory->get_body()); $this->assertEquals($response['headers']['replay-nonce'], $directory->get_response_nonce()); \WP_Mock::tearDown(); }
public function test_successful_purge_request_for_all_items() { $expected_result = MockData::purge_all_response_200(); $request_url = PURGELY_API_ENDPOINT . '/service/' . PURGELY_FASTLY_SERVICE_ID . '/purge_all'; // Mock the remote request \WP_Mock::wpFunction('wp_remote_request', array('args' => array($request_url, array('method' => 'POST', 'headers' => array('Fastly-Key' => PURGELY_FASTLY_KEY, 'Fastly-Soft-Purge' => 1))), 'times' => 1, 'return' => $expected_result)); \WP_Mock::wpFunction('trailingslashit', array('args' => array(PURGELY_API_ENDPOINT), 'times' => 1, 'return' => PURGELY_API_ENDPOINT . '/')); $purge = new Purgely_Purge(); $actual_result = $purge->purge('all', '', array('allow-all' => true)); $this->assertEquals($expected_result, $actual_result); $this->assertEquals($expected_result, $purge->get_response()); // The request is successful so ensure is_wp_error returns false \WP_Mock::wpFunction('is_wp_error', array('args' => array($expected_result), 'times' => 1, 'return' => false)); \WP_Mock::wpFunction('wp_remote_retrieve_response_code', array('args' => array($expected_result), 'times' => 1, 'return' => '200')); $this->assertEquals('success', $purge->get_result()); }
/** * Run before each test. Sets up mock adapter, which uses fixture * data for requests, and creates a new collection. */ public function setUp() { $this->adapter = new MockOAuthAdapter(); $this->collection = new AWeberCollection(MockData::load('lists'), '/accounts/1/lists', $this->adapter); }
public function test_purge_related_result_when_one_request_produces_a_wp_error() { $url = 'http://example.com/2015/09/my-url'; $object = $this->setup_standard_collection($url); // We are going to issue a number of remote requests, which need some mocking \WP_Mock::wpFunction('wp_remote_request', array('args' => array(\WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('array')), 'times' => 14, 'return' => MockData::purge_url_response_200())); \WP_Mock::wpFunction('wp_remote_request', array('args' => array(\WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('array')), 'times' => 1, 'return' => MockData::purge_url_response_405())); \WP_Mock::wpFunction('is_wp_error', array('args' => array('*'), 'times' => 14, 'return' => false)); \WP_Mock::wpFunction('is_wp_error', array('args' => array('*'), 'times' => 1, 'return' => true)); \WP_Mock::wpFunction('wp_remote_retrieve_response_code', array('args' => array('*'), 'times' => 14, 'return' => '200')); $object->purge_related(); $result = $object->get_result(); $this->assertEquals('failure', $result); }