|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Http\Client\Plugin; |
| 4 | + |
| 5 | +use Http\Client\Utils\Promise\FulfilledPromise; |
| 6 | +use Psr\Http\Message\RequestInterface; |
| 7 | +use Psr\Http\Message\ResponseInterface; |
| 8 | +use Psr\Http\Message\StreamInterface; |
| 9 | +use PhpSpec\Exception\Example\SkippingException; |
| 10 | +use PhpSpec\ObjectBehavior; |
| 11 | +use Prophecy\Argument; |
| 12 | + |
| 13 | +class DecoderPluginSpec extends ObjectBehavior |
| 14 | +{ |
| 15 | + function it_is_initializable() |
| 16 | + { |
| 17 | + $this->shouldHaveType('Http\Client\Plugin\DecoderPlugin'); |
| 18 | + } |
| 19 | + |
| 20 | + function it_is_a_plugin() |
| 21 | + { |
| 22 | + $this->shouldImplement('Http\Client\Plugin\Plugin'); |
| 23 | + } |
| 24 | + |
| 25 | + function it_decodes(RequestInterface $request, ResponseInterface $response, StreamInterface $stream) |
| 26 | + { |
| 27 | + if(defined('HHVM_VERSION')) { |
| 28 | + throw new SkippingException('Skipping test on hhvm, as there is no chunk encoding on hhvm'); |
| 29 | + } |
| 30 | + |
| 31 | + $request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request); |
| 32 | + $request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request); |
| 33 | + $next = function () use($response) { |
| 34 | + return new FulfilledPromise($response->getWrappedObject()); |
| 35 | + }; |
| 36 | + |
| 37 | + $response->hasHeader('Transfer-Encoding')->willReturn(true); |
| 38 | + $response->getHeader('Transfer-Encoding')->willReturn(['chunked']); |
| 39 | + $response->getBody()->willReturn($stream); |
| 40 | + $response->withBody(Argument::type('Http\Encoding\DechunkStream'))->willReturn($response); |
| 41 | + $response->withHeader('Transfer-Encoding', [])->willReturn($response); |
| 42 | + $response->hasHeader('Content-Encoding')->willReturn(false); |
| 43 | + |
| 44 | + $stream->isReadable()->willReturn(true); |
| 45 | + $stream->isWritable()->willReturn(false); |
| 46 | + $stream->eof()->willReturn(false); |
| 47 | + |
| 48 | + $this->handleRequest($request, $next, function () {}); |
| 49 | + } |
| 50 | + |
| 51 | + function it_decodes_gzip(RequestInterface $request, ResponseInterface $response, StreamInterface $stream) |
| 52 | + { |
| 53 | + $request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request); |
| 54 | + $request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request); |
| 55 | + $next = function () use($response) { |
| 56 | + return new FulfilledPromise($response->getWrappedObject()); |
| 57 | + }; |
| 58 | + |
| 59 | + $response->hasHeader('Transfer-Encoding')->willReturn(false); |
| 60 | + $response->hasHeader('Content-Encoding')->willReturn(true); |
| 61 | + $response->getHeader('Content-Encoding')->willReturn(['gzip']); |
| 62 | + $response->getBody()->willReturn($stream); |
| 63 | + $response->withBody(Argument::type('Http\Encoding\GzipDecodeStream'))->willReturn($response); |
| 64 | + $response->withHeader('Content-Encoding', [])->willReturn($response); |
| 65 | + |
| 66 | + $stream->isReadable()->willReturn(true); |
| 67 | + $stream->isWritable()->willReturn(false); |
| 68 | + $stream->eof()->willReturn(false); |
| 69 | + |
| 70 | + $this->handleRequest($request, $next, function () {}); |
| 71 | + } |
| 72 | + |
| 73 | + function it_decodes_deflate(RequestInterface $request, ResponseInterface $response, StreamInterface $stream) |
| 74 | + { |
| 75 | + $request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request); |
| 76 | + $request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request); |
| 77 | + $next = function () use($response) { |
| 78 | + return new FulfilledPromise($response->getWrappedObject()); |
| 79 | + }; |
| 80 | + |
| 81 | + $response->hasHeader('Transfer-Encoding')->willReturn(false); |
| 82 | + $response->hasHeader('Content-Encoding')->willReturn(true); |
| 83 | + $response->getHeader('Content-Encoding')->willReturn(['deflate']); |
| 84 | + $response->getBody()->willReturn($stream); |
| 85 | + $response->withBody(Argument::type('Http\Encoding\InflateStream'))->willReturn($response); |
| 86 | + $response->withHeader('Content-Encoding', [])->willReturn($response); |
| 87 | + |
| 88 | + $stream->isReadable()->willReturn(true); |
| 89 | + $stream->isWritable()->willReturn(false); |
| 90 | + $stream->eof()->willReturn(false); |
| 91 | + |
| 92 | + $this->handleRequest($request, $next, function () {}); |
| 93 | + } |
| 94 | + |
| 95 | + function it_decodes_inflate(RequestInterface $request, ResponseInterface $response, StreamInterface $stream) |
| 96 | + { |
| 97 | + $request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request); |
| 98 | + $request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request); |
| 99 | + $next = function () use($response) { |
| 100 | + return new FulfilledPromise($response->getWrappedObject()); |
| 101 | + }; |
| 102 | + |
| 103 | + $response->hasHeader('Transfer-Encoding')->willReturn(false); |
| 104 | + $response->hasHeader('Content-Encoding')->willReturn(true); |
| 105 | + $response->getHeader('Content-Encoding')->willReturn(['compress']); |
| 106 | + $response->getBody()->willReturn($stream); |
| 107 | + $response->withBody(Argument::type('Http\Encoding\DecompressStream'))->willReturn($response); |
| 108 | + $response->withHeader('Content-Encoding', [])->willReturn($response); |
| 109 | + |
| 110 | + $stream->isReadable()->willReturn(true); |
| 111 | + $stream->isWritable()->willReturn(false); |
| 112 | + $stream->eof()->willReturn(false); |
| 113 | + |
| 114 | + $this->handleRequest($request, $next, function () {}); |
| 115 | + } |
| 116 | + |
| 117 | + function it_does_not_decode_with_content_encoding(RequestInterface $request, ResponseInterface $response) |
| 118 | + { |
| 119 | + $this->beConstructedWith(false); |
| 120 | + |
| 121 | + $request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request); |
| 122 | + $request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldNotBeCalled(); |
| 123 | + $next = function () use($response) { |
| 124 | + return new FulfilledPromise($response->getWrappedObject()); |
| 125 | + }; |
| 126 | + |
| 127 | + $response->hasHeader('Transfer-Encoding')->willReturn(false); |
| 128 | + $response->hasHeader('Content-Encoding')->shouldNotBeCalled(); |
| 129 | + |
| 130 | + $this->handleRequest($request, $next, function () {}); |
| 131 | + } |
| 132 | +} |
0 commit comments