Skip to content

Commit

Permalink
Make ReadLength consistent with ReadChunks on EOF handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jan 18, 2025
1 parent f5d8504 commit d4c3b59
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main/php/web/io/ReadLength.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function __construct(Input $input, $length) {
* @return string
*/
public function read($limit= 8192) {
if (0 === $this->remaininig) return '';

$chunk= $this->input->read(min($limit, $this->remaininig));
if ('' === $chunk) {
$this->remaininig= 0;
Expand Down
15 changes: 10 additions & 5 deletions src/test/php/web/unittest/io/ReadLengthTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@ public function available_after_read() {
Assert::equals(0, $fixture->available());
}

#[Test]
public function raises_exception_on_eof_before_length_reached() {
$fixture= new ReadLength($this->input('Test'), 10);
$fixture->read();

Assert::throws(IOException::class, fn() => $fixture->read(1));
}

#[Test, Values([4, 8192])]
public function reading_after_eof_raises_exception($length) {
public function reading_after_eof_returns_empty_string($length) {
$fixture= new ReadLength($this->input('Test'), 4);
$fixture->read($length);

try {
$fixture->read(1);
$this->fail('No exception raised', null, IOException::class);
} catch (IOException $expected) { }
Assert::equals('', $fixture->read(1));
}

#[Test]
Expand Down

0 comments on commit d4c3b59

Please sign in to comment.