1<?php
2
3namespace OAuthTest\Unit\Common\Http\Client;
4
5use OAuth\Common\Http\Client\StreamClient;
6
7class StreamClientTest extends \PHPUnit_Framework_TestCase
8{
9    /**
10     *
11     */
12    public function testConstructCorrectInstance()
13    {
14        $client = new StreamClient();
15
16        $this->assertInstanceOf('\\OAuth\\Common\\Http\\Client\\AbstractClient', $client);
17    }
18
19    /**
20     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
21     */
22    public function testRetrieveResponseThrowsExceptionOnGetRequestWithBody()
23    {
24        $this->setExpectedException('\\InvalidArgumentException');
25
26        $client = new StreamClient();
27
28        $client->retrieveResponse(
29            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),
30            'foo',
31            array(),
32            'GET'
33        );
34    }
35
36    /**
37     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
38     */
39    public function testRetrieveResponseThrowsExceptionOnGetRequestWithBodyMethodConvertedToUpper()
40    {
41        $this->setExpectedException('\\InvalidArgumentException');
42
43        $client = new StreamClient();
44
45        $client->retrieveResponse(
46            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),
47            'foo',
48            array(),
49            'get'
50        );
51    }
52
53    /**
54     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
55     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
56     */
57    public function testRetrieveResponseDefaultUserAgent()
58    {
59        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
60        $endPoint->expects($this->any())
61            ->method('getHost')
62            ->will($this->returnValue('httpbin.org'));
63        $endPoint->expects($this->any())
64            ->method('getAbsoluteUri')
65            ->will($this->returnValue('http://httpbin.org/get'));
66
67        $client = new StreamClient();
68
69        $response = $client->retrieveResponse(
70            $endPoint,
71            '',
72            array(),
73            'get'
74        );
75
76        $response = json_decode($response, true);
77
78        $this->assertSame('PHPoAuthLib', $response['headers']['User-Agent']);
79    }
80
81    /**
82     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
83     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
84     */
85    public function testRetrieveResponseCustomUserAgent()
86    {
87        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
88        $endPoint->expects($this->any())
89            ->method('getHost')
90            ->will($this->returnValue('httpbin.org'));
91        $endPoint->expects($this->any())
92            ->method('getAbsoluteUri')
93            ->will($this->returnValue('http://httpbin.org/get'));
94
95        $client = new StreamClient('My Super Awesome Http Client');
96
97        $response = $client->retrieveResponse(
98            $endPoint,
99            '',
100            array(),
101            'get'
102        );
103
104        $response = json_decode($response, true);
105
106        $this->assertSame('My Super Awesome Http Client', $response['headers']['User-Agent']);
107    }
108
109    /**
110     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
111     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
112     */
113    public function testRetrieveResponseWithCustomContentType()
114    {
115        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
116        $endPoint->expects($this->any())
117            ->method('getHost')
118            ->will($this->returnValue('httpbin.org'));
119        $endPoint->expects($this->any())
120            ->method('getAbsoluteUri')
121            ->will($this->returnValue('http://httpbin.org/get'));
122
123        $client = new StreamClient();
124
125        $response = $client->retrieveResponse(
126            $endPoint,
127            '',
128            array('Content-Type' => 'foo/bar'),
129            'get'
130        );
131
132        $response = json_decode($response, true);
133
134        $this->assertSame('foo/bar', $response['headers']['Content-Type']);
135    }
136
137    /**
138     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
139     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
140     */
141    public function testRetrieveResponseWithFormUrlEncodedContentType()
142    {
143        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
144        $endPoint->expects($this->any())
145            ->method('getHost')
146            ->will($this->returnValue('httpbin.org'));
147        $endPoint->expects($this->any())
148            ->method('getAbsoluteUri')
149            ->will($this->returnValue('http://httpbin.org/post'));
150
151        $client = new StreamClient();
152
153        $response = $client->retrieveResponse(
154            $endPoint,
155            array('foo' => 'bar', 'baz' => 'fab'),
156            array(),
157            'POST'
158        );
159
160        $response = json_decode($response, true);
161
162        $this->assertSame('application/x-www-form-urlencoded', $response['headers']['Content-Type']);
163        $this->assertEquals(array('foo' => 'bar', 'baz' => 'fab'), $response['form']);
164    }
165
166    /**
167     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
168     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
169     */
170    public function testRetrieveResponseHost()
171    {
172        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
173        $endPoint->expects($this->any())
174            ->method('getHost')
175            ->will($this->returnValue('httpbin.org'));
176        $endPoint->expects($this->any())
177            ->method('getAbsoluteUri')
178            ->will($this->returnValue('http://httpbin.org/post'));
179
180        $client = new StreamClient();
181
182        $response = $client->retrieveResponse(
183            $endPoint,
184            array('foo' => 'bar', 'baz' => 'fab'),
185            array(),
186            'POST'
187        );
188
189        $response = json_decode($response, true);
190
191        $this->assertSame('httpbin.org', $response['headers']['Host']);
192    }
193
194    /**
195     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
196     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
197     */
198    public function testRetrieveResponsePostRequestWithRequestBodyAsString()
199    {
200        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
201        $endPoint->expects($this->any())
202            ->method('getHost')
203            ->will($this->returnValue('httpbin.org'));
204        $endPoint->expects($this->any())
205            ->method('getAbsoluteUri')
206            ->will($this->returnValue('http://httpbin.org/post'));
207
208        $client = new StreamClient();
209
210        $response = $client->retrieveResponse(
211            $endPoint,
212            'foo',
213            array(),
214            'POST'
215        );
216
217        $response = json_decode($response, true);
218
219        $this->assertSame('foo', $response['data']);
220    }
221
222    /**
223     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
224     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
225     */
226    public function testRetrieveResponsePutRequestWithRequestBodyAsString()
227    {
228        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
229        $endPoint->expects($this->any())
230            ->method('getHost')
231            ->will($this->returnValue('httpbin.org'));
232        $endPoint->expects($this->any())
233            ->method('getAbsoluteUri')
234            ->will($this->returnValue('http://httpbin.org/put'));
235
236        $client = new StreamClient();
237
238        $response = $client->retrieveResponse(
239            $endPoint,
240            'foo',
241            array(),
242            'PUT'
243        );
244
245        $response = json_decode($response, true);
246
247        $this->assertSame('foo', $response['data']);
248    }
249
250    /**
251     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
252     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
253     */
254    public function testRetrieveResponseThrowsExceptionOnInvalidRequest()
255    {
256        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
257
258        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
259        $endPoint->expects($this->any())
260            ->method('getHost')
261            ->will($this->returnValue('dskjhfckjhekrsfhkehfkreljfrekljfkre'));
262        $endPoint->expects($this->any())
263            ->method('getAbsoluteUri')
264            ->will($this->returnValue('dskjhfckjhekrsfhkehfkreljfrekljfkre'));
265
266        $client = new StreamClient();
267
268        $response = $client->retrieveResponse(
269            $endPoint,
270            '',
271            array('Content-Type' => 'foo/bar'),
272            'get'
273        );
274
275        $response = json_decode($response, true);
276
277        $this->assertSame('foo/bar', $response['headers']['Content-Type']);
278    }
279}
280