Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpbin: use a local instance for tests and in CI #224

Merged
merged 4 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ jobs:
- "3306:3306"
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

# https://github.com/postmanlabs/httpbin
# https://httpbin.org/
httpbin:
image: kennethreitz/httpbin
ports:
- "5555:80"

steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ nano
## Testing

```
docker run -d -p 6379:6379 redis:5.0.9-alpine redis-server --requirepass qwerty --port 6379
docker run -d -p 6379:6379 --name redis-test redis:5.0.9-alpine redis-server --requirepass qwerty --port 6379
docker run -d -p 5555:80 --name httpin kennethreitz/httpbin
composer run test
```

Expand Down
3 changes: 3 additions & 0 deletions classes/NanoBaseTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class NanoBaseTest extends TestCase
/* @var $app \NanoApp */
protected $app;

/* @see https://github.com/postmanlabs/httpbin */
const HTTPBIN_HOST = 'http://0.0.0.0:5555';

protected function setUp(): void
{
// use the current working directory where "./composer.phar test" is run
Expand Down
10 changes: 5 additions & 5 deletions classes/utils/HttpClient.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function setProxy(string $proxy, int $type = CURLPROXY_HTTP)
/**
* Set user agent identification used by HTTP client
*/
public function setUserAgent(string $userAgent)
public function setUserAgent(string $userAgent): void
{
$this->userAgent = $userAgent;

Expand All @@ -143,7 +143,7 @@ public function getUserAgent()
/**
* Set request headers
*/
public function setRequestHeader(string $header, string $value)
public function setRequestHeader(string $header, string $value): void
{
$this->reqHeaders[] = "$header: $value";

Expand All @@ -153,7 +153,7 @@ public function setRequestHeader(string $header, string $value)
/**
* Set timeout for a single request
*/
public function setTimeout(int $timeout)
public function setTimeout(int $timeout): void
{
$this->timeout = $timeout;

Expand All @@ -163,7 +163,7 @@ public function setTimeout(int $timeout)
/**
* Use given cookie jar file
*/
public function useCookieJar(string $jarFile)
public function useCookieJar(string $jarFile): void
{
$this->logger->debug(__METHOD__, ['jar' => $jarFile]);

Expand All @@ -176,7 +176,7 @@ public function useCookieJar(string $jarFile)
/**
* Manually sets request cookie
*/
public function setCookie(string $name, string $value)
public function setCookie(string $name, string $value): void
{
$this->cookies[$name] = $value;

Expand Down
7 changes: 3 additions & 4 deletions tests/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,19 @@ public function testCookiesJar()
$client->setCookie('another-one', 'tasty');

// set a cookie via HTTP response header
$resp = $client->get('https://httpbin.org/cookies/set', ['cookie' => 'yummy']);
$resp = $client->get(self::HTTPBIN_HOST . '/cookies/set/the_cookie/is_yummy');
$this->assertEquals(200, $resp->getResponseCode());
$this->assertStringContainsString('cookie=yummy', $resp->getHeader('set-cookie'), 'Cookie is set');

// check cookies
$this->assertFileExists($this->jarFile, 'Jar file is created');

// are cookies kept within the session?
$resp = $client->get('https://httpbin.org/cookies');
$resp = $client->get(self::HTTPBIN_HOST . '/cookies');

$body = json_decode($resp->getContent(), true);
$this->assertCount(2, $body['cookies'], 'Two cookies are set');
$this->assertEquals('tasty', $body['cookies']['another-one']);
$this->assertEquals('yummy', $body['cookies']['cookie']);
$this->assertEquals('is_yummy', $body['cookies']['the_cookie']);

// close HTTP session
$client->close();
Expand Down
15 changes: 6 additions & 9 deletions tests/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ class HttpTest extends NanoBaseTest
*/
public function testGet()
{
$url = 'https://httpbin.org/get';
$url = self::HTTPBIN_HOST . '/get';

$resp = Http::get($url);

$this->assertEquals(200, $resp->getResponseCode());
$this->assertEquals($url, $resp->getLocation());
$this->assertEquals('application/json', $resp->getHeader('content-type'));
$this->assertNull($resp->getHeader('Content-Type'), 'Headers are case sensitive!');

$json = json_decode($resp->getContent(), true);
$this->assertEquals($url, $json['url']);
Expand All @@ -42,7 +40,7 @@ public function testGet()
*/
public function testGetWithParams()
{
$url = 'https://httpbin.org/get';
$url = self::HTTPBIN_HOST . '/get';

$resp = Http::get($url, ['foo' => 42]);
$this->assertEquals(200, $resp->getResponseCode());
Expand All @@ -56,11 +54,10 @@ public function testGetWithParams()
*/
public function testPost()
{
$resp = Http::post('https://httpbin.org/post', ['foo' => 'bar']);
$resp = Http::post(self::HTTPBIN_HOST . '/post', ['foo' => 'bar']);

$this->assertEquals(200, $resp->getResponseCode());
$this->assertEquals('https://httpbin.org/post', $resp->getLocation());
$this->assertEquals('application/json', $resp->getHeader('content-type'));
$this->assertEquals(self::HTTPBIN_HOST . '/post', $resp->getLocation());

$json = json_decode($resp->getContent(), true);
$this->assertEquals(['foo' => 'bar'], $json['form']);
Expand All @@ -72,7 +69,7 @@ public function testPost()
*/
public function testHead()
{
$resp = Http::head('https://httpbin.org');
$resp = Http::head(self::HTTPBIN_HOST);
$this->assertEquals(200, $resp->getResponseCode());
$this->assertEquals('', $resp->getContent(), 'No content is returned');
}
Expand All @@ -91,7 +88,7 @@ public function testFailingRequest()
*/
public function testResponseWithCode(int $responseCode)
{
$resp = Http::head("https://httpbin.org/status/${responseCode}");
$resp = Http::head(self::HTTPBIN_HOST . "/status/{$responseCode}");
$this->assertEquals($responseCode, $resp->getResponseCode());
}

Expand Down
9 changes: 4 additions & 5 deletions tests/ImageTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

abstract class ImageTestBase extends NanoBaseTest
{
/* @var string $file */
private $file;
private $url;
private string $file;
private string $url;

const DEBUG = false;

Expand All @@ -20,8 +19,8 @@ public function setUp(): void
// 578x406
$this->file = __DIR__ . '/app/statics/php-logo.jpg';

/* @see https://httpbin.org/image/jpeg */
$this->url = 'https://httpbin.org/image/jpeg';
/* @see http://0.0.0.0:5555/image/jpeg */
$this->url = self::HTTPBIN_HOST . '/image/jpeg';
}

public function testNewFromFile()
Expand Down