Skip to content

Commit 94cc919

Browse files
committed
Add stream encoding
1 parent a301d77 commit 94cc919

20 files changed

+861
-0
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"require-dev": {
1919
"zendframework/zend-diactoros": "^1.0",
2020
"guzzlehttp/psr7": "^1.0",
21+
"ext-zlib": "*",
22+
"clue/stream-filter": "^1.3",
2123
"phpspec/phpspec": "^2.4",
2224
"henrikbjorn/phpspec-code-coverage" : "^1.0"
2325
},

spec/Encoding/ChunkStreamSpec.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class ChunkStreamSpec extends ObjectBehavior
9+
{
10+
function let(StreamInterface $stream)
11+
{
12+
$this->beConstructedWith($stream);
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Message\Encoding\ChunkStream');
18+
}
19+
20+
function it_is_a_stream()
21+
{
22+
$this->shouldImplement('Psr\Http\Message\StreamInterface');
23+
}
24+
25+
function it_chunks_content()
26+
{
27+
$stream = new MemoryStream('This is a stream');
28+
$this->beConstructedWith($stream, 6);
29+
30+
$this->getContents()->shouldReturn("10\r\nThis is a stream\r\n0\r\n\r\n");
31+
}
32+
33+
function it_chunks_in_multiple()
34+
{
35+
$stream = new MemoryStream('This is a stream', 6);
36+
$this->beConstructedWith($stream, 6);
37+
38+
$this->getContents()->shouldReturn("6\r\nThis i\r\n6\r\ns a st\r\n4\r\nream\r\n0\r\n\r\n");
39+
}
40+
}

spec/Encoding/CompressStreamSpec.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class CompressStreamSpec extends ObjectBehavior
9+
{
10+
function let(StreamInterface $stream)
11+
{
12+
$this->beConstructedWith($stream);
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Message\Encoding\CompressStream');
18+
}
19+
20+
function it_is_a_stream()
21+
{
22+
$this->shouldImplement('Psr\Http\Message\StreamInterface');
23+
}
24+
25+
function it_reads()
26+
{
27+
$stream = new MemoryStream('This is a test stream');
28+
$this->beConstructedWith($stream);
29+
30+
$stream->rewind();
31+
$this->read(4)->shouldReturn(substr(gzcompress('This is a test stream'), 0, 4));
32+
}
33+
34+
function it_gets_content()
35+
{
36+
$stream = new MemoryStream('This is a test stream');
37+
$this->beConstructedWith($stream);
38+
39+
$stream->rewind();
40+
$this->getContents()->shouldReturn(gzcompress('This is a test stream'));
41+
}
42+
}

spec/Encoding/DechunkStreamSpec.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class DechunkStreamSpec extends ObjectBehavior
9+
{
10+
function let(StreamInterface $stream)
11+
{
12+
$this->beConstructedWith($stream);
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Message\Encoding\DechunkStream');
18+
}
19+
20+
function it_is_a_stream()
21+
{
22+
$this->shouldImplement('Psr\Http\Message\StreamInterface');
23+
}
24+
25+
function it_reads()
26+
{
27+
$stream = new MemoryStream("4\r\ntest\r\n4\r\ntest\r\n0\r\n\r\n\0");
28+
$this->beConstructedWith($stream);
29+
30+
$this->read(6)->shouldReturn('testte');
31+
$this->read(6)->shouldReturn('st');
32+
}
33+
34+
function it_gets_content()
35+
{
36+
$stream = new MemoryStream("4\r\ntest\r\n0\r\n\r\n\0");
37+
$this->beConstructedWith($stream);
38+
39+
$this->getContents()->shouldReturn('test');
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class DecompressStreamSpec extends ObjectBehavior
9+
{
10+
function let(StreamInterface $stream)
11+
{
12+
$this->beConstructedWith($stream);
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Message\Encoding\DecompressStream');
18+
}
19+
20+
function it_is_a_stream()
21+
{
22+
$this->shouldImplement('Psr\Http\Message\StreamInterface');
23+
}
24+
25+
function it_reads()
26+
{
27+
// "This is a test stream" | deflate
28+
$stream = new MemoryStream(gzcompress('This is a test stream'));
29+
$this->beConstructedWith($stream);
30+
31+
$this->read(4)->shouldReturn('This');
32+
}
33+
34+
function it_gets_content()
35+
{
36+
// "This is a test stream" | deflate
37+
$stream = new MemoryStream(gzcompress('This is a test stream'));
38+
$this->beConstructedWith($stream);
39+
40+
$this->getContents()->shouldReturn('This is a test stream');
41+
}
42+
}

spec/Encoding/DeflateStreamSpec.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class DeflateStreamSpec extends ObjectBehavior
9+
{
10+
function let(StreamInterface $stream)
11+
{
12+
$this->beConstructedWith($stream);
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Message\Encoding\DeflateStream');
18+
}
19+
20+
function it_is_a_stream()
21+
{
22+
$this->shouldImplement('Psr\Http\Message\StreamInterface');
23+
}
24+
25+
function it_reads()
26+
{
27+
$stream = new MemoryStream('This is a test stream');
28+
$this->beConstructedWith($stream);
29+
30+
$stream->rewind();
31+
$this->read(4)->shouldReturn(substr(gzdeflate('This is a test stream'),0, 4));
32+
}
33+
34+
function it_gets_content()
35+
{
36+
$stream = new MemoryStream('This is a test stream');
37+
$this->beConstructedWith($stream);
38+
39+
$stream->rewind();
40+
$this->getContents()->shouldReturn(gzdeflate('This is a test stream'));
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class GzipDecodeStreamSpec extends ObjectBehavior
9+
{
10+
function let(StreamInterface $stream)
11+
{
12+
$this->beConstructedWith($stream);
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Message\Encoding\GzipDecodeStream');
18+
}
19+
20+
function it_is_a_stream()
21+
{
22+
$this->shouldImplement('Psr\Http\Message\StreamInterface');
23+
}
24+
25+
function it_reads()
26+
{
27+
// "This is a test stream" | deflate
28+
$stream = new MemoryStream(gzencode('This is a test stream'));
29+
$this->beConstructedWith($stream);
30+
31+
$this->read(4)->shouldReturn('This');
32+
}
33+
34+
function it_gets_content()
35+
{
36+
// "This is a test stream" | deflate
37+
$stream = new MemoryStream(gzencode('This is a test stream'));
38+
$this->beConstructedWith($stream);
39+
40+
$this->getContents()->shouldReturn('This is a test stream');
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class GzipEncodeStreamSpec extends ObjectBehavior
9+
{
10+
function let(StreamInterface $stream)
11+
{
12+
$this->beConstructedWith($stream);
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Message\Encoding\GzipEncodeStream');
18+
}
19+
20+
function it_is_a_stream()
21+
{
22+
$this->shouldImplement('Psr\Http\Message\StreamInterface');
23+
}
24+
25+
function it_reads()
26+
{
27+
$stream = new MemoryStream('This is a test stream');
28+
$this->beConstructedWith($stream);
29+
30+
$stream->rewind();
31+
$this->read(4)->shouldReturn(substr(gzencode('This is a test stream'),0, 4));
32+
}
33+
34+
function it_gets_content()
35+
{
36+
$stream = new MemoryStream('This is a test stream');
37+
$this->beConstructedWith($stream);
38+
39+
$stream->rewind();
40+
$this->getContents()->shouldReturn(gzencode('This is a test stream'));
41+
}
42+
}

spec/Encoding/InflateStreamSpec.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class InflateStreamSpec extends ObjectBehavior
9+
{
10+
function let(StreamInterface $stream)
11+
{
12+
$this->beConstructedWith($stream);
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Message\Encoding\InflateStream');
18+
}
19+
20+
function it_is_a_stream()
21+
{
22+
$this->shouldImplement('Psr\Http\Message\StreamInterface');
23+
}
24+
25+
function it_reads()
26+
{
27+
// "This is a test stream" | deflate
28+
$stream = new MemoryStream(gzdeflate('This is a test stream'));
29+
$this->beConstructedWith($stream);
30+
31+
$this->read(4)->shouldReturn('This');
32+
}
33+
34+
function it_gets_content()
35+
{
36+
// "This is a test stream" | deflate
37+
$stream = new MemoryStream(gzdeflate('This is a test stream'));
38+
$this->beConstructedWith($stream);
39+
40+
$this->getContents()->shouldReturn('This is a test stream');
41+
}
42+
}

0 commit comments

Comments
 (0)