Skip to content

Commit

Permalink
Merge c41785e into 693789b
Browse files Browse the repository at this point in the history
  • Loading branch information
maknz committed May 28, 2016
2 parents 693789b + c41785e commit 7d06faa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Client.php
Expand Up @@ -3,6 +3,7 @@
namespace Maknz\Slack;

use GuzzleHttp\Client as Guzzle;
use RuntimeException;

class Client
{
Expand Down Expand Up @@ -371,6 +372,10 @@ public function sendMessage(Message $message)

$encoded = json_encode($payload, JSON_UNESCAPED_UNICODE);

if ($encoded === false) {
throw new RuntimeException(sprintf('JSON encoding error %s: %s', json_last_error(), json_last_error_msg()));
}

$this->guzzle->post($this->endpoint, ['body' => $encoded]);
}

Expand Down
20 changes: 20 additions & 0 deletions tests/ClientFunctionalTest.php
Expand Up @@ -127,4 +127,24 @@ public function testMessageWithAttachmentsAndFields()

$this->assertEquals($expectedHttpData, $payload);
}

public function testBadEncodingThrowsException()
{
$client = $this->getNetworkStubbedClient();

$this->setExpectedException(RuntimeException::class, 'JSON encoding error');

// Force encoding to ISO-8859-1 so we know we're providing malformed
// encoding to json_encode
$client->send(mb_convert_encoding('æøå', 'ISO-8859-1'));
}

protected function getNetworkStubbedClient()
{
$guzzle = Mockery::mock('GuzzleHttp\Client');

$guzzle->shouldReceive('post');

return new Client('http://fake.endpoint', [], $guzzle);
}
}

0 comments on commit 7d06faa

Please sign in to comment.