Skip to content

Commit

Permalink
handle http errors with consistent interfax exception creation
Browse files Browse the repository at this point in the history
  • Loading branch information
splatEric committed Mar 13, 2018
1 parent d1b3a62 commit 278a478
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/Interfax/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ protected function getBaseRequestParams()
'auth' => [$this->username, $this->password],
'headers' => [
'User-Agent' => $this->getUserAgent()
]
],
'http_errors' => false
];
}

Expand Down
36 changes: 24 additions & 12 deletions tests/Interfax/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,36 +242,48 @@ public function test_prevents_trailing_slash()
}

/**
* Accept any 2xx status codes, throw an exception otherwise
* Accept any 2xx status codes
*/
public function test_response_status_parsing()
public function test_success_response_status_parsing()
{
for ($i = 0; $i < 10; $i++) {
$container = [];
$client = $this->getClientWithResponses([
new Response(rand(200, 299), [], 'foo')
], $container);

$response = $client->get('test/uri',['query' => ['foo' => true, 'bar' => false]]);
$response = $client->get('test/uri', ['query' => ['foo' => true, 'bar' => false]]);

$this->assertEquals('foo', $response);
}

}

public function errorCodeProvider()
{
$data = [];
for ($i = 0; $i < 10; $i++) {
$status_code = rand(100, 550);
if ($status_code >= 200 && $status_code <=299) {
$status_code += 100;
}
$data[] = [$status_code];
}
return $data;
}

/**
* @dataProvider errorCodeProvider
*/
public function test_error_response_status_parsing($status_code)
{
$container = [];
$client = $this->getClientWithResponses([
new Response($status_code, [], 'foo')
], $container);

$container = [];
$client = $this->getClientWithResponses([
new Response($status_code, [], 'foo')
], $container);

$this->setExpectedException('Interfax\Exception\RequestException', 'Unsuccessful request: foo');
$this->setExpectedException('Interfax\Exception\RequestException', 'Unsuccessful request: foo');

$response = $client->get('test/uri',['query' => ['foo' => true, 'bar' => false]]);
}
$response = $client->get('test/uri', ['query' => ['foo' => true, 'bar' => false]]);
}

}

0 comments on commit 278a478

Please sign in to comment.