From 74392388bbc7c05d9c085f0a84249ab0bb271a45 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 20 Jan 2018 18:51:07 +0100 Subject: [PATCH] Add Request::toString() Implements #30 --- ChangeLog.md | 9 ++++++--- src/main/php/web/Request.class.php | 20 ++++++++++++++++++- .../php/web/unittest/RequestTest.class.php | 11 ++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 21afdb3f..6e107034 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -5,9 +5,12 @@ Web change log ## 0.10.0 / 2018-01-20 -* Exposed "TestInput" and "TestOutput" classes in `web.io` package. - This way, people wishing to test their filters and handlers can easily - unittest them. +* Implemented #30: Request::toString(). The output includes method, URI + and the HTTP headers sent with the request. + (@thekid) +* Implemented #29: Exposed "TestInput" and "TestOutput" classes in `web.io` + package. This way, people wishing to test their filters and handlers can + easily unittest them. (@thekid) ## 0.9.0 / 2017-12-22 diff --git a/src/main/php/web/Request.class.php b/src/main/php/web/Request.class.php index 0cfa982e..704be01a 100755 --- a/src/main/php/web/Request.class.php +++ b/src/main/php/web/Request.class.php @@ -6,8 +6,10 @@ use web\io\ReadChunks; use io\streams\MemoryInputStream; use io\streams\Streams; +use lang\Value; +use util\Objects; -class Request { +class Request implements Value { private $stream= null; private $lookup= []; private $headers= []; @@ -279,4 +281,20 @@ public function consume() { return $r; } } + + /** @return string */ + public function toString() { + return nameof($this).'('.$this->method.' '.$this->uri->toString().')@'.Objects::stringOf($this->headers); + } + + /** @return string */ + public function hashCode() { return uniqid(microtime(true)); } + + /** + * Compares this request + * + * @param var $value + * @return int + */ + public function compareTo($value) { return $value === $this ? 0 : 1; } } \ No newline at end of file diff --git a/src/test/php/web/unittest/RequestTest.class.php b/src/test/php/web/unittest/RequestTest.class.php index 2ec1210f..c47b2c9a 100755 --- a/src/test/php/web/unittest/RequestTest.class.php +++ b/src/test/php/web/unittest/RequestTest.class.php @@ -266,4 +266,15 @@ public function consume_chunked_after_partial_read() { $partial= $req->stream()->read(50); $this->assertEquals(100 - strlen($partial), $req->consume()); } + + #[@test] + public function string_representation() { + $req= new Request(new TestInput('GET', '/', ['Host' => 'localhost'])); + $this->assertEquals( + "web.Request(GET util.URI)@[\n". + " Host => [\"localhost\"]\n". + "]", + $req->toString() + ); + } } \ No newline at end of file