forked from sctr/FOSRestBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatNegotiatorTest.php
More file actions
175 lines (142 loc) · 6.32 KB
/
FormatNegotiatorTest.php
File metadata and controls
175 lines (142 loc) · 6.32 KB
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
172
173
174
175
<?php
/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\RestBundle\Tests\Negotiatior;
use FOS\RestBundle\Negotiation\FormatNegotiator;
use FOS\RestBundle\Util\StopFormatListenerException;
use Negotiation\Accept;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
/**
* FormatNegotiatorTest.
*
* @author Ener-Getick <egetick@gmail.com>
*/
class FormatNegotiatorTest extends TestCase
{
private $requestStack;
private $request;
private $negotiator;
protected function setUp(): void
{
$this->requestStack = new RequestStack();
$this->request = new Request();
$this->requestStack->push($this->request);
$this->negotiator = new FormatNegotiator($this->requestStack, ['json' => ['application/json;version=1.0']]);
}
public function testEmptyRequestMatcherMap()
{
$this->assertNull($this->negotiator->getBest(''));
}
public function testStopException()
{
$this->expectException(StopFormatListenerException::class);
$this->expectExceptionMessage('Stopped');
$this->addRequestMatcher(false);
$this->addRequestMatcher(true, ['stop' => true]);
$this->negotiator->getBest('');
}
public function testFallbackFormat()
{
$this->addRequestMatcher(true, ['fallback_format' => null]);
$this->assertNull($this->negotiator->getBest(''));
$this->addRequestMatcher(true, ['fallback_format' => 'html']);
$this->assertEquals(new Accept('text/html'), $this->negotiator->getBest(''));
}
public function testFallbackFormatWithPriorities()
{
$this->addRequestMatcher(true, ['priorities' => ['json', 'xml'], 'fallback_format' => null]);
$this->assertNull($this->negotiator->getBest(''));
$this->addRequestMatcher(true, ['priorities' => ['json', 'xml'], 'fallback_format' => 'json']);
$this->assertEquals(new Accept('application/json'), $this->negotiator->getBest(''));
}
public function testGetBest()
{
$this->request->headers->set('Accept', 'application/xhtml+xml, text/html, application/xml;q=0.9, */*;q=0.8');
$priorities = ['text/html; charset=UTF-8', 'html', 'application/json'];
$this->addRequestMatcher(true, ['priorities' => $priorities]);
$this->assertEquals(
new Accept('text/html;charset=utf-8'),
$this->negotiator->getBest('')
);
$this->request->headers->set('Accept', 'application/xhtml+xml, application/xml;q=0.9, */*;q=0.8');
$this->assertEquals(
new Accept('application/xhtml+xml'),
$this->negotiator->getBest('', ['html', 'json'])
);
}
public function testGetBestFallback()
{
$this->request->headers->set('Accept', 'text/html');
$priorities = ['application/json'];
$this->addRequestMatcher(true, ['priorities' => $priorities, 'fallback_format' => 'xml']);
$this->assertEquals(new Accept('text/xml'), $this->negotiator->getBest(''));
}
public function testGetBestWithFormat()
{
$this->request->headers->set('Accept', 'application/json;version=1.0');
$priorities = ['json'];
$this->addRequestMatcher(true, ['priorities' => $priorities, 'fallback_format' => 'xml']);
$this->assertEquals(new Accept('application/json;version=1.0'), $this->negotiator->getBest(''));
}
public function testGetBestWithPreferExtension()
{
$priorities = ['text/html', 'application/json'];
$this->addRequestMatcher(true, ['priorities' => $priorities, 'prefer_extension' => '2.0']);
$reflectionClass = new \ReflectionClass(get_class($this->request));
$reflectionProperty = $reflectionClass->getProperty('pathInfo');
if (PHP_VERSION_ID < 80100) {
$reflectionProperty->setAccessible(true);
}
$reflectionProperty->setValue($this->request, '/file.json');
// Without extension mime-type in Accept header
$this->request->headers->set('Accept', 'text/html; q=1.0');
$this->assertEquals(new Accept('application/json'), $this->negotiator->getBest(''));
// With low q extension mime-type in Accept header
$this->request->headers->set('Accept', 'text/html; q=1.0, application/json; q=0.1');
$this->assertEquals(new Accept('application/json'), $this->negotiator->getBest(''));
$reflectionProperty->setValue($this->request, null);
}
public function testGetBestWithPreferExtensionAndUnknownExtension()
{
$priorities = ['text/html', 'application/json'];
$this->addRequestMatcher(true, ['priorities' => $priorities, 'prefer_extension' => '2.0']);
$reflectionClass = new \ReflectionClass(get_class($this->request));
$reflectionProperty = $reflectionClass->getProperty('pathInfo');
if (PHP_VERSION_ID < 80100) {
$reflectionProperty->setAccessible(true);
}
$reflectionProperty->setValue($this->request, '/file.123456789');
$this->request->headers->set('Accept', 'text/html, application/json');
$this->assertEquals(new Accept('text/html'), $this->negotiator->getBest(''));
$reflectionProperty->setValue($this->request, null);
}
public function testGetBestWithFormatWithRequestMimeTypeFallback()
{
$negotiator = new FormatNegotiator($this->requestStack);
$this->request->headers->set('Accept', 'application/json');
$priorities = ['json'];
$this->addRequestMatcher(true, ['priorities' => $priorities, 'fallback_format' => 'xml']);
$this->assertEquals(new Accept('application/json'), $this->negotiator->getBest(''));
}
/**
* @param bool $match
*/
private function addRequestMatcher($match, array $options = [])
{
$matcher = $this->getMockBuilder(RequestMatcherInterface::class)->getMock();
$matcher->expects($this->any())
->method('matches')
->with($this->request)
->willReturn($match);
$this->negotiator->add($matcher, $options);
}
}