-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
echo 0;
(or 0.0, or "0") not triggering the development console
- Loading branch information
Showing
3 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php namespace web\unittest\server; | ||
|
||
use test\{Assert, Test, Values}; | ||
use web\io\{TestInput, TestOutput}; | ||
use web\{Filters, Request, Response}; | ||
use xp\web\dev\Console; | ||
|
||
class ConsoleTest { | ||
|
||
/** | ||
* Handles a request with the given headers and handler function, | ||
* returning the response. | ||
* | ||
* @param function(web.Request, web.Response): var $handler | ||
* @return web.Response | ||
*/ | ||
private function handle($handler) { | ||
$compress= new Filters([new Console()], $handler); | ||
$req= new Request(new TestInput('GET', '/?test=true')); | ||
$res= new Response(new TestOutput()); | ||
|
||
foreach ($compress->handle($req, $res) ?? [] as $_) { } | ||
return $res; | ||
} | ||
|
||
#[Test] | ||
public function can_create() { | ||
new Console(); | ||
} | ||
|
||
#[Test] | ||
public function send() { | ||
$res= $this->handle(function($req, $res) { | ||
$res->send('Test', 'text/plain; charset=utf-8'); | ||
}); | ||
|
||
Assert::equals( | ||
"HTTP/1.1 200 OK\r\n". | ||
"Content-Type: text/plain; charset=utf-8\r\n". | ||
"Content-Length: 4\r\n". | ||
"\r\n". | ||
"Test", | ||
$res->output()->bytes() | ||
); | ||
} | ||
|
||
#[Test] | ||
public function stream() { | ||
$res= $this->handle(function($req, $res) { | ||
$res->header('Content-Type', 'text/plain; charset=utf-8'); | ||
$stream= $res->stream(); | ||
$stream->write('Test'); | ||
$stream->close(); | ||
}); | ||
|
||
Assert::equals( | ||
"HTTP/1.1 200 OK\r\n". | ||
"Content-Type: text/plain; charset=utf-8\r\n". | ||
"Transfer-Encoding: chunked\r\n". | ||
"\r\n". | ||
"4\r\nTest\r\n0\r\n\r\n", | ||
$res->output()->bytes() | ||
); | ||
} | ||
|
||
#[Test] | ||
public function empty_echo_does_not_trigger() { | ||
$res= $this->handle(function($req, $res) { | ||
echo ''; | ||
$res->send('Test', 'text/plain; charset=utf-8'); | ||
}); | ||
|
||
Assert::equals( | ||
"HTTP/1.1 200 OK\r\n". | ||
"Content-Type: text/plain; charset=utf-8\r\n". | ||
"Content-Length: 4\r\n". | ||
"\r\n". | ||
"Test", | ||
$res->output()->bytes() | ||
); | ||
} | ||
|
||
#[Test, Values(['true', '0', 0, 0.0])] | ||
public function echo_output_appears_in_console($arg) { | ||
$res= $this->handle(function($req, $res) use($arg) { | ||
echo $arg; | ||
$res->send('Test', 'text/plain; charset=utf-8'); | ||
}); | ||
|
||
Assert::matches( | ||
'/<pre id="output">'.(string)$arg.'<\/pre>/', | ||
$res->output()->bytes() | ||
); | ||
} | ||
|
||
#[Test] | ||
public function var_dump_output_appears_in_console() { | ||
$res= $this->handle(function($req, $res) { | ||
var_dump($req->param('test')); | ||
$res->send('Test', 'text/plain; charset=utf-8'); | ||
}); | ||
|
||
Assert::matches( | ||
'/<pre id="output">string\(4\) "true"\n<\/pre>/', | ||
$res->output()->bytes() | ||
); | ||
} | ||
|
||
#[Test] | ||
public function status_appears_in_console() { | ||
$res= $this->handle(function($req, $res) { | ||
echo 'Test'; | ||
$res->answer(204); | ||
}); | ||
|
||
Assert::matches( | ||
'/HTTP\/1.1 <span id="status">204 No Content<\/span>/', | ||
$res->output()->bytes() | ||
); | ||
} | ||
|
||
#[Test] | ||
public function headers_appear_in_console() { | ||
$res= $this->handle(function($req, $res) { | ||
echo $req->param('test'); | ||
$res->send('Test', 'text/plain; charset=utf-8'); | ||
}); | ||
|
||
Assert::matches( | ||
'/<td class="name">Content-Type<\/td>.*<td class="value">text\/plain; charset=utf-8<\/td>/s', | ||
$res->output()->bytes() | ||
); | ||
} | ||
} |