summaryrefslogtreecommitdiff
path: root/vendor/lusitanian/oauth/tests/Unit/Common/Http/HttpClientsTest.php
blob: 6fa9eace182df8da8489e7bb739340617da09bd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php

/**
 * @category   OAuth
 * @package    Tests
 * @author     David Desberg <david@daviddesberg.com>
 * @copyright  Copyright (c) 2012 The authors
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 */

namespace OAuth\Unit\Common\Http;

use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Http\Uri\UriInterface;
use OAuth\Common\Http\Client;

class HttpClientsTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var object|\OAuth\Common\Http\Client\ClientInterface[]
     */
    protected $clients;

    public function setUp()
    {
        $streamClient = new Client\StreamClient();
        $streamClient->setTimeout(3);

        $curlClient = new Client\CurlClient();
        $curlClient->setTimeout(3);

        $this->clients[] = $streamClient;
        $this->clients[] = $curlClient;
    }

    public function tearDown()
    {
        foreach ($this->clients as $client) {
            unset($client);
        }
    }

    /**
     * Test that extra headers are passed properly
     */
    public function testHeaders()
    {
        $testUri = new Uri('http://httpbin.org/get');

        $me = $this;
        $headerCb = function ($response) use ($me) {
            $data = json_decode($response, true);
            $me->assertEquals('extraheadertest', $data['headers']['Testingheader']);
        };

        $this->__doTestRetrieveResponse($testUri, array(), array('Testingheader' => 'extraheadertest'), 'GET', $headerCb);
    }

    /**
     * Tests that we get an exception for a >= 400 status code
     */
    public function testException()
    {
        // sending a post here should get us a 405 which should trigger an exception
        $testUri = new Uri('http://httpbin.org/delete');
        foreach ($this->clients as $client) {
            $this->setExpectedException('OAuth\Common\Http\Exception\TokenResponseException');
            $client->retrieveResponse($testUri, array('blah' => 'blih'));
        }
    }

    /**
     * Tests the DELETE method
     */
    public function testDelete()
    {
        $testUri = new Uri('http://httpbin.org/delete');

        $me = $this;
        $deleteTestCb = function ($response) use ($me) {
            $data = json_decode($response, true);
            $me->assertEquals('', $data['data']);
        };

        $this->__doTestRetrieveResponse($testUri, array(), array(), 'DELETE', $deleteTestCb);
    }

    /**
     * Tests the PUT method
     */
    public function testPut()
    {
        $testUri = new Uri('http://httpbin.org/put');

        $me = $this;
        $putTestCb = function ($response) use ($me) {
            // verify the put response
            $data = json_decode($response, true);
            $me->assertEquals(json_encode(array('testKey' => 'testValue')), $data['data']);
        };

        $this->__doTestRetrieveResponse($testUri, json_encode(array('testKey' => 'testValue')), array('Content-Type' => 'application/json'), 'PUT', $putTestCb);
    }

    /**
     * Tests the POST method
     */
    public function testPost()
    {
        // http test server
        $testUri = new Uri('http://httpbin.org/post');

        $me = $this;
        $postTestCb = function ($response) use ($me) {
            // verify the post response
            $data = json_decode($response, true);
            // note that we check this because the retrieveResponse wrapper function automatically adds a content-type
            // if there isn't one and it
            $me->assertEquals('testValue', $data['form']['testKey']);
        };

        $this->__doTestRetrieveResponse($testUri, array('testKey' => 'testValue'), array(), 'POST', $postTestCb);
    }

    /**
     * Expect exception when we try to send a GET request with a body
     */
    public function testInvalidGet()
    {
        $testUri =  new Uri('http://site.net');

        foreach ($this->clients as $client) {
            $this->setExpectedException('InvalidArgumentException');
            $client->retrieveResponse($testUri, array('blah' => 'blih'), array(), 'GET');
        }
    }

    /**
     * Tests the GET method
     */
    public function testGet()
    {
        // test uri
        $testUri = new Uri('http://httpbin.org/get?testKey=testValue');

        $me = $this;
        $getTestCb = function ($response) use ($me) {
            $data = json_decode($response, true);
            $me->assertEquals('testValue', $data['args']['testKey']);
        };

        $this->__doTestRetrieveResponse($testUri, array(), array(), 'GET', $getTestCb);
    }

    /**
     * Test on all HTTP clients.
     *
     * @param UriInterface $uri
     * @param array        $param
     * @param array        $header
     * @param string       $method
     * @param \Closure     $responseCallback
     */
    protected function __doTestRetrieveResponse(UriInterface $uri, $param, array $header, $method, $responseCallback)
    {
        foreach ($this->clients as $client) {
            $response = $client->retrieveResponse($uri, $param, $header, $method);
            $responseCallback($response, $client);
        }
    }
}