10000 feature #26036 Added support for getting default values in Accept hea… · symfony/symfony@c557327 · GitHub
[go: up one dir, main page]

Skip to content

Commit c557327

Browse files
committed
feature #26036 Added support for getting default values in Accept headers (javiereguiluz)
This PR was squashed before being merged into the 4.1-dev branch (closes #26036). Discussion ---------- Added support for getting default values in Accept headers | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25877 | License | MIT | Doc PR | - Commits ------- 7e31fd9 Added support for getting default values in Accept headers
2 parents 4e97b97 + 7e31fd9 commit c557327

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
@@ -10,6 +10,10 @@ CHANGELOG
1010
* The `getClientSize()` method of the `UploadedFile` class is deprecated. Use `getSize()` instead.
1111
* added `RedisSessionHandler` to use Redis as a session storage
1212

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

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

3438
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