Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\HttpFoundation\Tests;
13
 
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\StreamedResponse;
16
 
17
class StreamedResponseTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testConstructor()
20
    {
21
        $response = new StreamedResponse(function () { echo 'foo'; }, 404, array('Content-Type' => 'text/plain'));
22
 
23
        $this->assertEquals(404, $response->getStatusCode());
24
        $this->assertEquals('text/plain', $response->headers->get('Content-Type'));
25
    }
26
 
27
    public function testPrepareWith11Protocol()
28
    {
29
        $response = new StreamedResponse(function () { echo 'foo'; });
30
        $request = Request::create('/');
31
        $request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
32
 
33
        $response->prepare($request);
34
 
35
        $this->assertEquals('1.1', $response->getProtocolVersion());
36
        $this->assertNotEquals('chunked', $response->headers->get('Transfer-Encoding'), 'Apache assumes responses with a Transfer-Encoding header set to chunked to already be encoded.');
37
    }
38
 
39
    public function testPrepareWith10Protocol()
40
    {
41
        $response = new StreamedResponse(function () { echo 'foo'; });
42
        $request = Request::create('/');
43
        $request->server->set('SERVER_PROTOCOL', 'HTTP/1.0');
44
 
45
        $response->prepare($request);
46
 
47
        $this->assertEquals('1.0', $response->getProtocolVersion());
48
        $this->assertNull($response->headers->get('Transfer-Encoding'));
49
    }
50
 
51
    public function testPrepareWithHeadRequest()
52
    {
53
        $response = new StreamedResponse(function () { echo 'foo'; });
54
        $request = Request::create('/', 'HEAD');
55
 
56
        $response->prepare($request);
57
    }
58
 
59
    public function testPrepareWithCacheHeaders()
60
    {
61
        $response = new StreamedResponse(function () { echo 'foo'; }, 200, array('Cache-Control' => 'max-age=600, public'));
62
        $request = Request::create('/', 'GET');
63
 
64
        $response->prepare($request);
65
        $this->assertEquals('max-age=600, public', $response->headers->get('Cache-Control'));
66
    }
67
 
68
    public function testSendContent()
69
    {
70
        $called = 0;
71
 
72
        $response = new StreamedResponse(function () use (&$called) { ++$called; });
73
 
74
        $response->sendContent();
75
        $this->assertEquals(1, $called);
76
 
77
        $response->sendContent();
78
        $this->assertEquals(1, $called);
79
    }
80
 
81
    /**
82
     * @expectedException \LogicException
83
     */
84
    public function testSendContentWithNonCallable()
85
    {
86
        $response = new StreamedResponse(null);
87
        $response->sendContent();
88
    }
89
 
90
    /**
91
     * @expectedException \LogicException
92
     */
93
    public function testSetContent()
94
    {
95
        $response = new StreamedResponse(function () { echo 'foo'; });
96
        $response->setContent('foo');
97
    }
98
 
99
    public function testGetContent()
100
    {
101
        $response = new StreamedResponse(function () { echo 'foo'; });
102
        $this->assertFalse($response->getContent());
103
    }
104
 
105
    public function testCreate()
106
    {
107
        $response = StreamedResponse::create(function () {}, 204);
108
 
109
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
110
        $this->assertEquals(204, $response->getStatusCode());
111
    }
112
}