8000 Added support for getting default values in Accept headers · symfony/symfony@7e31fd9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e31fd9

Browse files
javiereguiluzfabpot
authored andcommitted
Added support for getting default values in Accept headers
1 parent 9be03d8 commit 7e31fd9

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/Symfony/Component/HttpFoundation/AcceptHeader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function has($value)
9191
*/
9292
public function get($value)
9393
{
94-
return isset($this->items[$value]) ? $this->items[$value] : null;
94+
return $this->items[$value] ?? $this->items[explode('/', $value)[0].'/*'] ?? $this->items['*/*'] ?? $this->items['*'] ?? null;
9595
}
9696

9797
/**

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ CHANGELOG
99

1010
* The `getClientSize()` method of the `UploadedFile` class is deprecated. Use `getSize()` instead.
1111

12+
* The `get()` method of the `AcceptHeader` class now takes into account the
13+
`*` and `*/*` default values (if they are present in the Accept HTTP header)
14+
when looking for items.
15+
1216
4.0.0
1317
-----
1418

@@ -26,8 +30,8 @@ CHANGELOG
2630
method (by not passing `false` as its argument) is not supported anymore and
2731
throws a `\BadMethodCallException`
2832
* the `WriteCheckSessionHandler`, `NativeSessionHandler` and `NativeProxy` classes have been removed
29-
* setting session save handlers that do not implement `\SessionHandlerInterface` in
30-
`NativeSessionStorage::setSaveHandler()` is not supported anymore and throws a
33+
* setting session save handlers that do not implement `\SessionHandlerInterface` in
34+
`NativeSessionStorage::setSaveHandler()` is not supported anymore and throws a
3135
`\TypeError`
3236

3337
3.4.0

src/Symfony/Component/HttpFoundation/Tests/AcceptHeaderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,31 @@ public function provideSortingData()
100100
'order matters when q is equal2' => array('*;q=0.3,utf-8;q=0.7,ISO-8859-1;q=0.7', array('utf-8', 'ISO-8859-1', '*')),
101101
);
102102
}
103+
104+
/**
105+
* @dataProvider provideDefaultValueData
106+
*/
107+
public function testDefaultValue($acceptHeader, $value, $expectedQuality)
108+
{
109+
$header = AcceptHeader::fromString($acceptHeader);
110+
$this->assertSame($expectedQuality, $header->get($value)->getQuality());
111+
}
112+
113+
public function provideDefaultValueData()
114+
{
115+
yield array('text/plain;q=0.5, text/html, text/x-dvi;q=0.8, *;q=0.3', 'text/xml', 0.3);
116+
yield array('text/plain;q=0.5, text/html, text/x-dvi;q=0.8, */*;q=0.3', 'text/xml', 0.3);
117+
yield array('text/plain;q=0.5, text/html, text/x-dvi;q=0.8, */*;q=0.3', 'text/html', 1.0);
118+
yield array('text/plain;q=0.5, text/html, text/x-dvi;q=0.8, */*;q=0.3', 'text/plain', 0.5);
119+
yield array('text/plain;q=0.5, text/html, text/x-dvi;q=0.8, */*;q=0.3', '*', 0.3);
120+
yield array('text/plain;q=0.5, text/html, text/x-dvi;q=0.8, */*', '*', 1.0);
121+
yield array('text/plain;q=0.5, text/html, text/x-dvi;q=0.8, */*', 'text/xml', 1.0);
122+
yield array('text/plain;q=0.5, text/html, text/x-dvi;q=0.8, */*', 'text/*', 1.0);
123+
yield array('text/plain;q=0.5, text/html, text/*;q=0.8, */*', 'text/*', 0.8);
124+
yield array('text/plain;q=0.5, text/html, text/*;q=0.8, */*', 'text/html', 1.0);
125+
yield array('text/plain;q=0.5, text/html, text/*;q=0.8, */*', 'text/x-dvi', 0.8);
126+
yield array('*;q=0.3, ISO-8859-1;q=0.7, utf-8;q=0.7', '*', 0.3);
127+
yield array('*;q=0.3, ISO-8859-1;q=0.7, utf-8;q=0.7', 'utf-8', 0.7);
128+
yield array('*;q=0.3, ISO-8859-1;q=0.7, utf-8;q=0.7', 'SHIFT_JIS', 0.3);
129+
}
103130
}

0 commit comments

Comments
 (0)
0