Completed
Push — master ( b27762...ac741e )
by Tobias
01:40
created

RequestTest::testSupportNumericHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Tests\Nyholm\Psr7;
4
5
use Nyholm\Psr7\Request;
6
use Nyholm\Psr7\Uri;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\StreamInterface;
9
10
/**
11
 * @covers \Nyholm\Psr7\Request
12
 */
13
class RequestTest extends TestCase
14
{
15
    public function testRequestUriMayBeString()
16
    {
17
        $r = new Request('GET', '/');
18
        $this->assertEquals('/', (string) $r->getUri());
19
    }
20
21
    public function testRequestUriMayBeUri()
22
    {
23
        $uri = new Uri('/');
24
        $r = new Request('GET', $uri);
25
        $this->assertSame($uri, $r->getUri());
26
    }
27
28
    /**
29
     * @expectedException \InvalidArgumentException
30
     */
31
    public function testValidateRequestUri()
32
    {
33
        new Request('GET', '///');
34
    }
35
36
    public function testCanConstructWithBody()
37
    {
38
        $r = new Request('GET', '/', [], 'baz');
39
        $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
40
        $this->assertEquals('baz', (string) $r->getBody());
41
    }
42
43
    public function testNullBody()
44
    {
45
        $r = new Request('GET', '/', [], null);
46
        $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
47
        $this->assertSame('', (string) $r->getBody());
48
    }
49
50
    public function testFalseyBody()
51
    {
52
        $r = new Request('GET', '/', [], '0');
53
        $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
54
        $this->assertSame('0', (string) $r->getBody());
55
    }
56
57 View Code Duplication
    public function testConstructorDoesNotReadStreamBody()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $body = $this->getMockBuilder(StreamInterface::class)->getMock();
60
        $body->expects($this->never())
61
            ->method('__toString');
62
63
        $r = new Request('GET', '/', [], $body);
0 ignored issues
show
Documentation introduced by
$body is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a string|null|resource|obj...essage\StreamInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
        $this->assertSame($body, $r->getBody());
65
    }
66
67
    public function testWithUri()
68
    {
69
        $r1 = new Request('GET', '/');
70
        $u1 = $r1->getUri();
71
        $u2 = new Uri('http://www.example.com');
72
        $r2 = $r1->withUri($u2);
73
        $this->assertNotSame($r1, $r2);
74
        $this->assertSame($u2, $r2->getUri());
75
        $this->assertSame($u1, $r1->getUri());
76
    }
77
78
    public function testSameInstanceWhenSameUri()
79
    {
80
        $r1 = new Request('GET', 'http://foo.com');
81
        $r2 = $r1->withUri($r1->getUri());
82
        $this->assertSame($r1, $r2);
83
    }
84
85
    public function testWithRequestTarget()
86
    {
87
        $r1 = new Request('GET', '/');
88
        $r2 = $r1->withRequestTarget('*');
89
        $this->assertEquals('*', $r2->getRequestTarget());
90
        $this->assertEquals('/', $r1->getRequestTarget());
91
    }
92
93
    /**
94
     * @expectedException \InvalidArgumentException
95
     */
96
    public function testRequestTargetDoesNotAllowSpaces()
97
    {
98
        $r1 = new Request('GET', '/');
99
        $r1->withRequestTarget('/foo bar');
100
    }
101
102
    public function testRequestTargetDefaultsToSlash()
103
    {
104
        $r1 = new Request('GET', '');
105
        $this->assertEquals('/', $r1->getRequestTarget());
106
        $r2 = new Request('GET', '*');
107
        $this->assertEquals('*', $r2->getRequestTarget());
108
        $r3 = new Request('GET', 'http://foo.com/bar baz/');
109
        $this->assertEquals('/bar%20baz/', $r3->getRequestTarget());
110
    }
111
112
    public function testBuildsRequestTarget()
113
    {
114
        $r1 = new Request('GET', 'http://foo.com/baz?bar=bam');
115
        $this->assertEquals('/baz?bar=bam', $r1->getRequestTarget());
116
    }
117
118
    public function testBuildsRequestTargetWithFalseyQuery()
119
    {
120
        $r1 = new Request('GET', 'http://foo.com/baz?0');
121
        $this->assertEquals('/baz?0', $r1->getRequestTarget());
122
    }
123
124
    public function testHostIsAddedFirst()
125
    {
126
        $r = new Request('GET', 'http://foo.com/baz?bar=bam', ['Foo' => 'Bar']);
127
        $this->assertEquals([
128
            'Host' => ['foo.com'],
129
            'Foo' => ['Bar'],
130
        ], $r->getHeaders());
131
    }
132
133
    public function testCanGetHeaderAsCsv()
134
    {
135
        $r = new Request('GET', 'http://foo.com/baz?bar=bam', [
136
            'Foo' => ['a', 'b', 'c'],
137
        ]);
138
        $this->assertEquals('a, b, c', $r->getHeaderLine('Foo'));
139
        $this->assertEquals('', $r->getHeaderLine('Bar'));
140
    }
141
142 View Code Duplication
    public function testHostIsNotOverwrittenWhenPreservingHost()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144
        $r = new Request('GET', 'http://foo.com/baz?bar=bam', ['Host' => 'a.com']);
145
        $this->assertEquals(['Host' => ['a.com']], $r->getHeaders());
146
        $r2 = $r->withUri(new Uri('http://www.foo.com/bar'), true);
147
        $this->assertEquals('a.com', $r2->getHeaderLine('Host'));
148
    }
149
150 View Code Duplication
    public function testOverridesHostWithUri()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $r = new Request('GET', 'http://foo.com/baz?bar=bam');
153
        $this->assertEquals(['Host' => ['foo.com']], $r->getHeaders());
154
        $r2 = $r->withUri(new Uri('http://www.baz.com/bar'));
155
        $this->assertEquals('www.baz.com', $r2->getHeaderLine('Host'));
156
    }
157
158
    public function testAggregatesHeaders()
159
    {
160
        $r = new Request('GET', '', [
161
            'ZOO' => 'zoobar',
162
            'zoo' => ['foobar', 'zoobar'],
163
        ]);
164
        $this->assertEquals(['ZOO' => ['zoobar', 'foobar', 'zoobar']], $r->getHeaders());
165
        $this->assertEquals('zoobar, foobar, zoobar', $r->getHeaderLine('zoo'));
166
    }
167
168 View Code Duplication
    public function testSupportNumericHeaders()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        $r = new Request('GET', '', [
171
            'Content-Length' => 200,
172
        ]);
173
        $this->assertSame(['Content-Length' => ['200']], $r->getHeaders());
174
        $this->assertSame('200', $r->getHeaderLine('Content-Length'));
175
    }
176
177
    public function testAddsPortToHeader()
178
    {
179
        $r = new Request('GET', 'http://foo.com:8124/bar');
180
        $this->assertEquals('foo.com:8124', $r->getHeaderLine('host'));
181
    }
182
183
    public function testAddsPortToHeaderAndReplacePreviousPort()
184
    {
185
        $r = new Request('GET', 'http://foo.com:8124/bar');
186
        $r = $r->withUri(new Uri('http://foo.com:8125/bar'));
187
        $this->assertEquals('foo.com:8125', $r->getHeaderLine('host'));
188
    }
189
}
190