1<?php
2
3namespace Sabre\HTTP;
4
5class RequestTest extends \PHPUnit_Framework_TestCase {
6
7    function testConstruct() {
8
9        $request = new Request('GET', '/foo', [
10            'User-Agent' => 'Evert',
11        ]);
12        $this->assertEquals('GET', $request->getMethod());
13        $this->assertEquals('/foo', $request->getUrl());
14        $this->assertEquals([
15            'User-Agent' => ['Evert'],
16        ], $request->getHeaders());
17
18    }
19
20    function testGetQueryParameters() {
21
22        $request = new Request('GET', '/foo?a=b&c&d=e');
23        $this->assertEquals([
24            'a' => 'b',
25            'c' => null,
26            'd' => 'e',
27        ], $request->getQueryParameters());
28
29    }
30
31    function testGetQueryParametersNoData() {
32
33        $request = new Request('GET', '/foo');
34        $this->assertEquals([], $request->getQueryParameters());
35
36    }
37
38    /**
39     * @backupGlobals
40     */
41    function testCreateFromPHPRequest() {
42
43        $_SERVER['REQUEST_METHOD'] = 'PUT';
44
45        $request = Sapi::getRequest();
46        $this->assertEquals('PUT', $request->getMethod());
47
48    }
49
50    function testGetAbsoluteUrl() {
51
52        $s = [
53            'HTTP_HOST'   => 'sabredav.org',
54            'REQUEST_URI' => '/foo'
55        ];
56
57        $r = Sapi::createFromServerArray($s);
58
59        $this->assertEquals('http://sabredav.org/foo', $r->getAbsoluteUrl());
60
61        $s = [
62            'HTTP_HOST'   => 'sabredav.org',
63            'REQUEST_URI' => '/foo',
64            'HTTPS'       => 'on',
65        ];
66
67        $r = Sapi::createFromServerArray($s);
68
69        $this->assertEquals('https://sabredav.org/foo', $r->getAbsoluteUrl());
70
71    }
72
73    function testGetPostData() {
74
75        $post = [
76            'bla' => 'foo',
77        ];
78        $r = new Request();
79        $r->setPostData($post);
80        $this->assertEquals($post, $r->getPostData());
81
82    }
83
84    function testGetPath() {
85
86        $request = new Request();
87        $request->setBaseUrl('/foo');
88        $request->setUrl('/foo/bar/');
89
90        $this->assertEquals('bar', $request->getPath());
91
92    }
93
94    function testGetPathStrippedQuery() {
95
96        $request = new Request();
97        $request->setBaseUrl('/foo');
98        $request->setUrl('/foo/bar/?a=b');
99
100        $this->assertEquals('bar', $request->getPath());
101
102    }
103
104    function testGetPathMissingSlash() {
105
106        $request = new Request();
107        $request->setBaseUrl('/foo/');
108        $request->setUrl('/foo');
109
110        $this->assertEquals('', $request->getPath());
111
112    }
113
114    /**
115     * @expectedException \LogicException
116     */
117    function testGetPathOutsideBaseUrl() {
118
119        $request = new Request();
120        $request->setBaseUrl('/foo/');
121        $request->setUrl('/bar/');
122
123        $request->getPath();
124
125    }
126
127    function testToString() {
128
129        $request = new Request('PUT', '/foo/bar', ['Content-Type' => 'text/xml']);
130        $request->setBody('foo');
131
132        $expected = <<<HI
133PUT /foo/bar HTTP/1.1\r
134Content-Type: text/xml\r
135\r
136foo
137HI;
138        $this->assertEquals($expected, (string)$request);
139
140    }
141
142    function testToStringAuthorization() {
143
144        $request = new Request('PUT', '/foo/bar', ['Content-Type' => 'text/xml', 'Authorization' => 'Basic foobar']);
145        $request->setBody('foo');
146
147        $expected = <<<HI
148PUT /foo/bar HTTP/1.1\r
149Content-Type: text/xml\r
150Authorization: Basic REDACTED\r
151\r
152foo
153HI;
154        $this->assertEquals($expected, (string)$request);
155
156    }
157
158    /**
159     * @expectedException \InvalidArgumentException
160     */
161    function testConstructorWithArray() {
162
163        $request = new Request([]);
164
165    }
166
167}
168