1<?php
2
3namespace Sabre\HTTP;
4
5class SapiTest extends \PHPUnit_Framework_TestCase {
6
7    function testConstructFromServerArray() {
8
9        $request = Sapi::createFromServerArray([
10            'REQUEST_URI'     => '/foo',
11            'REQUEST_METHOD'  => 'GET',
12            'HTTP_USER_AGENT' => 'Evert',
13            'CONTENT_TYPE'    => 'text/xml',
14            'CONTENT_LENGTH'  => '400',
15            'SERVER_PROTOCOL' => 'HTTP/1.0',
16        ]);
17
18        $this->assertEquals('GET', $request->getMethod());
19        $this->assertEquals('/foo', $request->getUrl());
20        $this->assertEquals([
21            'User-Agent'     => ['Evert'],
22            'Content-Type'   => ['text/xml'],
23            'Content-Length' => ['400'],
24        ], $request->getHeaders());
25
26        $this->assertEquals('1.0', $request->getHttpVersion());
27
28        $this->assertEquals('400', $request->getRawServerValue('CONTENT_LENGTH'));
29        $this->assertNull($request->getRawServerValue('FOO'));
30
31    }
32
33    function testConstructPHPAuth() {
34
35        $request = Sapi::createFromServerArray([
36            'REQUEST_URI'     => '/foo',
37            'REQUEST_METHOD'  => 'GET',
38            'PHP_AUTH_USER'   => 'user',
39            'PHP_AUTH_PW'     => 'pass',
40        ]);
41
42        $this->assertEquals('GET', $request->getMethod());
43        $this->assertEquals('/foo', $request->getUrl());
44        $this->assertEquals([
45            'Authorization' => ['Basic ' . base64_encode('user:pass')],
46        ], $request->getHeaders());
47
48    }
49
50    function testConstructPHPAuthDigest() {
51
52        $request = Sapi::createFromServerArray([
53            'REQUEST_URI'     => '/foo',
54            'REQUEST_METHOD'  => 'GET',
55            'PHP_AUTH_DIGEST' => 'blabla',
56        ]);
57
58        $this->assertEquals('GET', $request->getMethod());
59        $this->assertEquals('/foo', $request->getUrl());
60        $this->assertEquals([
61            'Authorization' => ['Digest blabla'],
62        ], $request->getHeaders());
63
64    }
65
66    function testConstructRedirectAuth() {
67
68        $request = Sapi::createFromServerArray([
69            'REQUEST_URI'                 => '/foo',
70            'REQUEST_METHOD'              => 'GET',
71            'REDIRECT_HTTP_AUTHORIZATION' => 'Basic bla',
72        ]);
73
74        $this->assertEquals('GET', $request->getMethod());
75        $this->assertEquals('/foo', $request->getUrl());
76        $this->assertEquals([
77            'Authorization' => ['Basic bla'],
78        ], $request->getHeaders());
79
80    }
81
82    /**
83     * @runInSeparateProcess
84     *
85     * Unfortunately we have no way of testing if the HTTP response code got
86     * changed.
87     */
88    function testSend() {
89
90        if (!function_exists('xdebug_get_headers')) {
91            $this->markTestSkipped('XDebug needs to be installed for this test to run');
92        }
93
94        $response = new Response(204, ['Content-Type' => 'text/xml;charset=UTF-8']);
95
96        // Second Content-Type header. Normally this doesn't make sense.
97        $response->addHeader('Content-Type', 'application/xml');
98        $response->setBody('foo');
99
100        ob_start();
101
102        Sapi::sendResponse($response);
103        $headers = xdebug_get_headers();
104
105        $result = ob_get_clean();
106        header_remove();
107
108        $this->assertEquals(
109            [
110                "Content-Type: text/xml;charset=UTF-8",
111                "Content-Type: application/xml",
112            ],
113            $headers
114        );
115
116        $this->assertEquals('foo', $result);
117
118    }
119
120    /**
121     * @runInSeparateProcess
122     * @depends testSend
123     */
124    function testSendLimitedByContentLengthString() {
125
126        $response = new Response(200);
127
128        $response->addHeader('Content-Length', 19);
129        $response->setBody('Send this sentence. Ignore this one.');
130
131        ob_start();
132
133        Sapi::sendResponse($response);
134
135        $result = ob_get_clean();
136        header_remove();
137
138        $this->assertEquals('Send this sentence.', $result);
139
140    }
141
142    /**
143     * @runInSeparateProcess
144     * @depends testSend
145     */
146    function testSendLimitedByContentLengthStream() {
147
148        $response = new Response(200, ['Content-Length' => 19]);
149
150        $body = fopen('php://memory', 'w');
151        fwrite($body, 'Ignore this. Send this sentence. Ignore this too.');
152        rewind($body);
153        fread($body, 13);
154        $response->setBody($body);
155
156        ob_start();
157
158        Sapi::sendResponse($response);
159
160        $result = ob_get_clean();
161        header_remove();
162
163        $this->assertEquals('Send this sentence.', $result);
164
165    }
166
167}
168