Completed
Pull Request — master (#479)
by Andrey
02:39
created

testRetrieveResponseThrowsExceptionOnInvalidRequest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\Common\Http\Client;
4
5
use OAuth\Common\Http\Client\StreamClient;
6
7
class 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
        $formData = array('baz' => 'fab', 'foo' => 'bar');
209
210
        $client = new StreamClient();
211
212
        $response = $client->retrieveResponse(
213
            $endPoint,
214
            $formData,
215
            array(),
216
            'POST'
217
        );
218
219
        $response = json_decode($response, true);
220
221
        $this->assertSame($formData, $response['form']);
222
    }
223
224
    /**
225
     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
226
     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
227
     */
228
    public function testRetrieveResponsePutRequestWithRequestBodyAsString()
229
    {
230
        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
231
        $endPoint->expects($this->any())
232
            ->method('getHost')
233
            ->will($this->returnValue('httpbin.org'));
234
        $endPoint->expects($this->any())
235
            ->method('getAbsoluteUri')
236
            ->will($this->returnValue('http://httpbin.org/put'));
237
238
        $formData = array('baz' => 'fab', 'foo' => 'bar');
239
240
        $client = new StreamClient();
241
242
        $response = $client->retrieveResponse(
243
            $endPoint,
244
            $formData,
245
            array(),
246
            'PUT'
247
        );
248
249
        $response = json_decode($response, true);
250
251
        $this->assertSame($formData, $response['form']);
252
    }
253
254
    /**
255
     * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse
256
     * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext
257
     */
258
    public function testRetrieveResponseThrowsExceptionOnInvalidRequest()
259
    {
260
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
261
262
        $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
263
        $endPoint->expects($this->any())
264
            ->method('getHost')
265
            ->will($this->returnValue('dskjhfckjhekrsfhkehfkreljfrekljfkre'));
266
        $endPoint->expects($this->any())
267
            ->method('getAbsoluteUri')
268
            ->will($this->returnValue('dskjhfckjhekrsfhkehfkreljfrekljfkre'));
269
270
        $client = new StreamClient();
271
272
        $response = $client->retrieveResponse(
273
            $endPoint,
274
            '',
275
            array('Content-Type' => 'foo/bar'),
276
            'get'
277
        );
278
279
        $response = json_decode($response, true);
280
281
        $this->assertSame('foo/bar', $response['headers']['Content-Type']);
282
    }
283
}
284