Fix integration tests for domain API (#294)

* Send multipart/form-data requests by default

* Fix integration tests for Domain API
This commit is contained in:
z38 2017-04-08 01:24:48 +02:00 committed by Sean Johnson
parent 0bc0a3b1a5
commit b713364dc0
2 changed files with 38 additions and 32 deletions

View File

@ -129,18 +129,16 @@ abstract class HttpApi
} }
/** /**
* Send a POST request with JSON-encoded parameters. * Send a POST request with parameters.
* *
* @param string $path Request path * @param string $path Request path
* @param array $parameters POST parameters to be JSON encoded * @param array $parameters POST parameters
* @param array $requestHeaders Request headers * @param array $requestHeaders Request headers
* *
* @return ResponseInterface * @return ResponseInterface
*/ */
protected function httpPost($path, array $parameters = [], array $requestHeaders = []) protected function httpPost($path, array $parameters = [], array $requestHeaders = [])
{ {
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
return $this->httpPostRaw($path, $this->createRequestBody($parameters), $requestHeaders); return $this->httpPostRaw($path, $this->createRequestBody($parameters), $requestHeaders);
} }
@ -167,18 +165,16 @@ abstract class HttpApi
} }
/** /**
* Send a PUT request with JSON-encoded parameters. * Send a PUT request.
* *
* @param string $path Request path * @param string $path Request path
* @param array $parameters POST parameters to be JSON encoded * @param array $parameters PUT parameters
* @param array $requestHeaders Request headers * @param array $requestHeaders Request headers
* *
* @return ResponseInterface * @return ResponseInterface
*/ */
protected function httpPut($path, array $parameters = [], array $requestHeaders = []) protected function httpPut($path, array $parameters = [], array $requestHeaders = [])
{ {
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
try { try {
$response = $this->httpClient->sendRequest( $response = $this->httpClient->sendRequest(
$this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createRequestBody($parameters)) $this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createRequestBody($parameters))
@ -191,18 +187,16 @@ abstract class HttpApi
} }
/** /**
* Send a DELETE request with JSON-encoded parameters. * Send a DELETE request.
* *
* @param string $path Request path * @param string $path Request path
* @param array $parameters POST parameters to be JSON encoded * @param array $parameters DELETE parameters
* @param array $requestHeaders Request headers * @param array $requestHeaders Request headers
* *
* @return ResponseInterface * @return ResponseInterface
*/ */
protected function httpDelete($path, array $parameters = [], array $requestHeaders = []) protected function httpDelete($path, array $parameters = [], array $requestHeaders = [])
{ {
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
try { try {
$response = $this->httpClient->sendRequest( $response = $this->httpClient->sendRequest(
$this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createRequestBody($parameters)) $this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createRequestBody($parameters))
@ -215,14 +209,27 @@ abstract class HttpApi
} }
/** /**
* Create a JSON encoded version of an array of parameters. * Prepare a set of key-value-pairs to be encoded as multipart/form-data.
* *
* @param array $parameters Request parameters * @param array $parameters Request parameters
* *
* @return null|string * @return array
*/ */
protected function createRequestBody(array $parameters) protected function createRequestBody(array $parameters)
{ {
return (count($parameters) === 0) ? null : http_build_query($parameters); $resources = [];
foreach ($parameters as $key => $values) {
if (!is_array($values)) {
$values = [$values];
}
foreach ($values as $value) {
$resources[] = [
'name' => $key,
'content' => $value,
];
}
}
return $resources;
} }
} }

View File

@ -10,7 +10,6 @@
namespace Mailgun\Tests\Integration; namespace Mailgun\Tests\Integration;
use Mailgun\Api\Domain; use Mailgun\Api\Domain;
use Mailgun\Model\ErrorResponse;
use Mailgun\Model\Domain\CreateCredentialResponse; use Mailgun\Model\Domain\CreateCredentialResponse;
use Mailgun\Model\Domain\DeleteCredentialResponse; use Mailgun\Model\Domain\DeleteCredentialResponse;
use Mailgun\Model\Domain\DeleteResponse; use Mailgun\Model\Domain\DeleteResponse;
@ -70,15 +69,15 @@ class DomainApiTest extends TestCase
/** /**
* Performs `DELETE /v3/domains/<domain>` on a non-existent domain. * Performs `DELETE /v3/domains/<domain>` on a non-existent domain.
*
* @expectedException \Mailgun\Exception\HttpClientException
* @expectedExceptionCode 404
*/ */
public function testRemoveDomainNoExist() public function testRemoveDomainNoExist()
{ {
$mg = $this->getMailgunClient(); $mg = $this->getMailgunClient();
$ret = $mg->domains()->delete('example.notareal.tld'); $mg->domains()->delete('example.notareal.tld');
$this->assertNotNull($ret);
$this->assertInstanceOf(DeleteResponse::class, $ret);
$this->assertEquals('Domain not found', $ret->getMessage());
} }
/** /**
@ -104,20 +103,20 @@ class DomainApiTest extends TestCase
/** /**
* Performs `POST /v3/domains` to attempt to create a domain with duplicate * Performs `POST /v3/domains` to attempt to create a domain with duplicate
* values. * values.
*
* @expectedException \Mailgun\Exception\HttpClientException
* @expectedExceptionCode 400
*/ */
public function testDomainCreateDuplicateValues() public function testDomainCreateDuplicateValues()
{ {
$mg = $this->getMailgunClient(); $mg = $this->getMailgunClient();
$domain = $mg->domains()->create( $mg->domains()->create(
'example.notareal.tld', // domain name 'example.notareal.tld', // domain name
'exampleOrgSmtpPassword12', // smtp password 'exampleOrgSmtpPassword12', // smtp password
'tag', // default spam action 'tag', // default spam action
false // wildcard domain? false // wildcard domain?
); );
$this->assertNotNull($domain);
$this->assertInstanceOf(ErrorResponse::class, $domain);
$this->assertEquals('This domain name is already taken', $domain->getMessage());
} }
/** /**
@ -214,15 +213,15 @@ class DomainApiTest extends TestCase
/** /**
* Performs `GET /v3/domains/<domain>/credentials` on a non-existent domain. * Performs `GET /v3/domains/<domain>/credentials` on a non-existent domain.
*
* @expectedException \Mailgun\Exception\HttpClientException
* @expectedExceptionCode 404
*/ */
public function testListCredentialsBadDomain() public function testListCredentialsBadDomain()
{ {
$mg = $this->getMailgunClient(); $mg = $this->getMailgunClient();
$ret = $mg->domains()->credentials('mailgun.org'); $mg->domains()->credentials('mailgun.org');
$this->assertNotNull($ret);
$this->assertInstanceOf(ErrorResponse::class, $ret);
$this->assertEquals('Domain not found: mailgun.org', $ret->getMessage());
} }
/** /**
@ -306,6 +305,9 @@ class DomainApiTest extends TestCase
/** /**
* Performs `DELETE /v3/domains/<domain>/credentials/<login>` to remove an invalid * Performs `DELETE /v3/domains/<domain>/credentials/<login>` to remove an invalid
* credential pair from a domain. * credential pair from a domain.
*
* @expectedException \Mailgun\Exception\HttpClientException
* @expectedExceptionCode 404
*/ */
public function testRemoveCredentialNoExist() public function testRemoveCredentialNoExist()
{ {
@ -313,13 +315,10 @@ class DomainApiTest extends TestCase
$mg = $this->getMailgunClient(); $mg = $this->getMailgunClient();
$ret = $mg->domains()->deleteCredential( $mg->domains()->deleteCredential(
$this->testDomain, $this->testDomain,
$login $login
); );
$this->assertNotNull($ret);
$this->assertInstanceOf(DeleteCredentialResponse::class, $ret);
$this->assertEquals('Credentials not found', $ret->getMessage());
} }
/** /**