Skip to content

Commit

Permalink
Rename upstream test const to accommodate second upstream test const,…
Browse files Browse the repository at this point in the history
… add Toxiproxy->update() and test.
  • Loading branch information
ihsw committed Jan 6, 2018
1 parent 9a7031a commit 7e53f26
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/Test/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
abstract class AbstractTest extends TestCase
{
const TEST_NAME = "ihsw_test_redis_master";
const TEST_UPSTREAM = "127.0.0.1:6379";
const TEST_UPSTREAM_REDIS = "127.0.0.1:6379";
const TEST_UPSTREAM_PSQL = "127.0.0.1:5432";

use AssertionHelpers;

Expand Down Expand Up @@ -42,7 +43,7 @@ protected function getListen()
protected function createProxy(Toxiproxy $toxiproxy)
{
$toxiproxy = $this->createToxiproxy();
return $toxiproxy->create(self::TEST_NAME, self::TEST_UPSTREAM, $this->getListen());
return $toxiproxy->create(self::TEST_NAME, self::TEST_UPSTREAM_REDIS, $this->getListen());
}

/**
Expand Down
22 changes: 12 additions & 10 deletions src/Toxiproxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,18 @@ public function delete(Proxy $proxy)
try {
$this->httpClient->delete(sprintf("/proxies/%s", $proxy->getName()));
} catch (HttpClientException $e) {
switch ($e->getResponse()->getStatusCode()) {
case self::NOT_FOUND:
throw new NotFoundException(sprintf("Proxy not found: %s", $proxy->getName()));
default:
throw new UnexpectedStatusCodeException(
sprintf("Unexpected status code"),
$e->getResponse()->getStatusCode(),
$e
);
}
throw $this->handleHttpClientException($e);
}
}

public function update(Proxy $proxy)
{
try {
return $this->responseToProxy($this->httpClient->post(sprintf("/proxies/%s", $proxy->getName()), [
"body" => json_encode($proxy)
]));
} catch (HttpClientException $e) {
throw $this->handleHttpClientException($e);
}
}
}
16 changes: 14 additions & 2 deletions tests/ToxiproxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public function testGetHttpClient()
public function testCreate()
{
$toxiproxy = $this->createToxiproxy();
$proxy = $toxiproxy->create(self::TEST_NAME, self::TEST_UPSTREAM, $this->getListen());
$proxy = $toxiproxy->create(self::TEST_NAME, self::TEST_UPSTREAM_REDIS, $this->getListen());
$this->assertTrue($proxy instanceof Proxy);

$this->assertEquals(self::TEST_NAME, $proxy->getName());
$this->assertEquals(self::TEST_UPSTREAM, $proxy->getUpstream());
$this->assertEquals(self::TEST_UPSTREAM_REDIS, $proxy->getUpstream());
list($ip, $port) = explode(":", $this->getListen());
$listen = sprintf("%s:%s", gethostbyname($ip), $port);
$this->assertEquals($listen, $proxy->getListen());
Expand Down Expand Up @@ -117,4 +117,16 @@ public function testHandleHttpClientException()
$this->assertInstanceOf($item["expected"], $toxiproxy->handleHttpClientException($e));
}
}

public function testUpdate()
{
$toxiproxy = $this->createToxiproxy();
$proxy = $this->createProxy($toxiproxy);

$proxy->setUpstream(self::TEST_UPSTREAM_PSQL);
$updatedProxy = $toxiproxy->update($proxy);
$this->assertEquals($proxy->jsonSerialize(), $updatedProxy->jsonSerialize());

$toxiproxy->delete($updatedProxy);
}
}

0 comments on commit 7e53f26

Please sign in to comment.