Skip to content

Commit

Permalink
Merge pull request #15 from OpenBuildings/secure-uri
Browse files Browse the repository at this point in the history
Use https:// protocol for Postmark API
  • Loading branch information
hkdobrev committed Sep 26, 2014
2 parents 7b83413 + afaa69b commit 415873b
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
32 changes: 30 additions & 2 deletions src/Openbuildings/Postmark/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class Api
{
const SEND_URI = 'http://api.postmarkapp.com/email';

const SEND_URI_SECURE = 'https://api.postmarkapp.com/email';

protected $_token;

protected $secure = true;

public function __construct($token = null)
{
Expand Down Expand Up @@ -70,7 +73,7 @@ public function send(array $data)
curl_setopt_array(
$curl,
array(
CURLOPT_URL => static::SEND_URI,
CURLOPT_URL => $this->get_send_uri(),
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => $this->headers(),
CURLOPT_POSTFIELDS => json_encode($data),
Expand All @@ -81,7 +84,8 @@ public function send(array $data)
$response = curl_exec($curl);

if (!$response) {
throw new \Exception('Postmark delivery failed: ' . curl_error($curl));
$curl_error = curl_error($curl) ?: 'unknown cURL error';
throw new \Exception('Postmark delivery failed: ' . $curl_error);
}

$response = @json_decode($response, true);
Expand All @@ -101,4 +105,28 @@ public function send(array $data)

return $response;
}

public function is_secure()
{
return $this->secure;
}

public function set_secure($secure)
{
$this->secure = $secure;

return $this;
}

public function get_send_uri()
{
if ($this->is_secure())
{
return static::SEND_URI_SECURE;
}
else
{
return static::SEND_URI;
}
}
}
83 changes: 82 additions & 1 deletion tests/src/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Openbuildings\Postmark\Test;

use Openbuildings\Postmark\Api;
use Openbuildings\Postmark\Test\Mock;
use PHPUnit_Framework_TestCase;

/**
Expand Down Expand Up @@ -147,4 +146,86 @@ public function test_send()
)
);
}

/**
* @covers Openbuildings\Postmark\Api::is_secure
*/
public function test_is_secure()
{
$api = new Api();
$this->assertTrue($api->is_secure());

$api->set_secure(false);
$this->assertFalse($api->is_secure());

$api->set_secure(true);
$this->assertTrue($api->is_secure());
}

/**
* @covers Openbuildings\Postmark\Api::set_secure
*/
public function test_set_secure()
{
$api = new Api();
$this->assertSame($api, $api->set_secure(false));
$this->assertFalse($api->is_secure());

$api->set_secure(true);
$this->assertTrue($api->is_secure());
}

/**
* @covers Openbuildings\Postmark\Api::get_send_uri
*/
public function test_get_send_uri()
{
$api = new Api();
$this->assertEquals(Api::SEND_URI_SECURE, $api->get_send_uri());

$api->set_secure(false);
$this->assertEquals(Api::SEND_URI, $api->get_send_uri());

$api->set_secure(true);
$this->assertEquals(Api::SEND_URI_SECURE, $api->get_send_uri());
}

/**
* @covers Openbuildings\Postmark\Api::send
*/
public function test_send_wrong_json()
{
$api_mock = $this->getMock(
'Openbuildings\Postmark\Api',
array(
'get_send_uri'
),
array(
'POSTMARK_API_TEST'
)
);

$path_to_wrong_json = 'file://'.realpath(__DIR__.'/../test_data/wrong-json.json');

$api_mock
->expects($this->once())
->method('get_send_uri')
->will($this->returnValue($path_to_wrong_json));

$this->setExpectedException(
'Exception',
'Postmark delivery failed: wrong json response'
);

$response = $api_mock->send(
array(
'From' => 'Mark Smith <[email protected]>',
'To' => '[email protected],[email protected],[email protected]',
'Subject' => 'Test',
'HtmlBody' => '<b>Hello</b>',
'TextBody' => 'Hello',
'ReplyTo' => '[email protected]',
)
);
}
}
1 change: 1 addition & 0 deletions tests/test_data/wrong-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'["}

0 comments on commit 415873b

Please sign in to comment.