Completed
Push — master ( 2f8690...a5753d )
by Chad
14s
created

RequestBridgeTest::toOAuth2JsonContentType()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace ChadicusTest\Slim\OAuth2\Http;
4
5
use Chadicus\Slim\OAuth2\Http\RequestBridge;
6
use Zend\Diactoros\ServerRequest;
7
use Zend\Diactoros\UploadedFile;
8
9
/**
10
 * Unit tests for the \Chadicus\Slim\OAuth2\Http\RequestBridge class.
11
 *
12
 * @coversDefaultClass \Chadicus\Slim\OAuth2\Http\RequestBridge
13
 * @covers ::<private>
14
 */
15
final class RequestBridgeTest extends \PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * Verify basic behavior of toOAuth2()
19
     *
20
     * @test
21
     * @covers ::toOAuth2
22
     *
23
     * @return void
24
     */
25
    public function toOAuth2()
26
    {
27
        $uri = 'https://example.com/foo/bar';
28
        $headers = ['Host' => ['example.com'], 'Accept' => ['application/json', 'text/json']];
29
        $cookies = ['PHPSESSID' => uniqid()];
30
        $server = ['SCRIPT_NAME'     => __FILE__, 'SCRIPT_FILENAME' => __FILE__];
31
        $json = json_encode(['foo' => 'bar', 'abc' => '123']);
32
33
        $stream = fopen('php://memory', 'r+');
34
        fwrite($stream, $json);
35
        rewind($stream);
36
37
        $files = [
38
            'foo' => new UploadedFile(
39
                __FILE__,
40
                100,
41
                UPLOAD_ERR_OK,
42
                'foo.txt',
43
                'text/plain'
44
            ),
45
        ];
46
47
        $psr7Request = new ServerRequest($server, $files, $uri, 'PATCH', $stream, $headers, $cookies, ['baz' => 'bat']);
48
49
        $oauth2Request = RequestBridge::toOauth2($psr7Request);
50
51
        $this->assertInstanceOf('\OAuth2\Request', $oauth2Request);
52
        $this->assertSame('bat', $oauth2Request->query('baz'));
53
        $this->assertSame('example.com', $oauth2Request->headers('Host'));
54
        $this->assertSame('application/json, text/json', $oauth2Request->headers('Accept'));
55
        $this->assertSame($cookies, $oauth2Request->cookies);
56
57
        $this->assertSame(__FILE__, $oauth2Request->server('SCRIPT_NAME'));
58
59
        $this->assertSame($json, $oauth2Request->getContent());
60
61
        $this->assertSame(
62
            [
63
                'foo' => [
64
                    'name' => 'foo.txt',
65
                    'type' => 'text/plain',
66
                    'size' => 100,
67
                    'tmp_name' => __FILE__,
68
                    'error' => UPLOAD_ERR_OK,
69
                ],
70
            ],
71
            $oauth2Request->files
72
        );
73
    }
74
75
    /**
76
     * Verify behavior of toOAuth2() with application/json content type
77
     *
78
     * @test
79
     * @covers ::toOAuth2
80
     *
81
     * @return void
82
     */
83
    public function toOAuth2JsonContentType()
84
    {
85
        $uri = 'https://example.com/foos';
86
87
        $data = ['foo' => 'bar', 'abc' => '123'];
88
89
        $json = json_encode($data);
90
        $stream = fopen('php://memory', 'r+');
91
        fwrite($stream, $json);
92
        rewind($stream);
93
94
        $headers = [
95
            'Content-Type' => ['application/json'],
96
            'Content-Length' => [strlen($json)],
97
        ];
98
99
        $psr7Request = new ServerRequest([], [], $uri, 'POST', $stream, $headers, [], [], $data);
100
101
        $oauth2Request = RequestBridge::toOAuth2($psr7Request);
102
103
        $this->assertSame((string)strlen($json), $oauth2Request->headers('Content-Length'));
104
        $this->assertSame('application/json', $oauth2Request->headers('Content-Type'));
105
        $this->assertSame('bar', $oauth2Request->request('foo'));
106
        $this->assertSame('123', $oauth2Request->request('abc'));
107
    }
108
109
    /**
110
     * Verify behavior of replacing bad header key names
111
     *
112
     * @test
113
     * @covers ::toOAuth2
114
     *
115
     * @return void
116
     */
117
    public function toOAuth2HeaderKeyNames()
118
    {
119
        $uri = 'https://example.com/foos';
120
121
        $headers = [
122
            'Php-Auth-User' => ['test_client_id'],
123
            'Php-Auth-Pw' => ['test_secret'],
124
        ];
125
126
        $psr7Request = new ServerRequest([], [], $uri, 'GET', 'php://input', $headers);
127
128
        $oauth2Request = RequestBridge::toOAuth2($psr7Request);
129
130
        $this->assertSame('test_client_id', $oauth2Request->headers('PHP_AUTH_USER'));
131
        $this->assertSame('test_secret', $oauth2Request->headers('PHP_AUTH_PW'));
132
        $this->assertNull($oauth2Request->headers('Php-Auth-User'));
133
        $this->assertNull($oauth2Request->headers('Php-Auth-Pw'));
134
    }
135
136
    /**
137
     * Verify behavior of PSR-7 request with Authorization header.
138
     *
139
     * @test
140
     * @covers ::toOAuth2
141
     *
142
     * @return void
143
     */
144
    public function toOAuth2WithAuthorization()
145
    {
146
        $uri = 'https://example.com/foos';
147
148
        $headers = ['HTTP_AUTHORIZATION' => ['Bearer abc123']];
149
150
        $psr7Request = new ServerRequest([], [], $uri, 'GET', 'php://input', $headers);
151
152
        $oauth2Request = RequestBridge::toOAuth2($psr7Request);
153
154
        $this->assertSame('Bearer abc123', $oauth2Request->headers('AUTHORIZATION'));
155
    }
156
157
    /**
158
     * Verify that steam contents of a passed request is preserved (read and rewound).
159
     *
160
     * @test
161
     * @covers ::toOAuth2
162
     *
163
     * @return void
164
     */
165
    public function toOAuth2BodyContentsOfRequestPreserved()
166
    {
167
        $uri = 'https://example.com/foos';
168
169
        $temp = tmpfile();
170
        fwrite($temp, 'foo');
171
        rewind($temp);
172
173
        $psr7Request = new ServerRequest([], [], $uri, 'POST', $temp);
174
175
        $oauth2Request = RequestBridge::toOAuth2($psr7Request);
176
177
        $this->assertSame('foo', $psr7Request->getBody()->getContents());
178
        $this->assertSame('foo', $oauth2Request->getContent());
179
    }
180
}
181