Skip to content

Commit

Permalink
Add Request::toString()
Browse files Browse the repository at this point in the history
Implements #30
  • Loading branch information
thekid committed Jan 20, 2018
1 parent 2a61f0c commit 7439238
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
9 changes: 6 additions & 3 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion src/main/php/web/Request.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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= [];
Expand Down Expand Up @@ -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; }
}
11 changes: 11 additions & 0 deletions src/test/php/web/unittest/RequestTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<http://localhost/>)@[\n".
" Host => [\"localhost\"]\n".
"]",
$req->toString()
);
}
}

0 comments on commit 7439238

Please sign in to comment.