Comments (9)
I wouldn’t mind throwing an exception for non-string values, the same way we do in withHeader
:
Lines 87 to 91 in b6b62a4
In essence I guess this is what is happening now, when the anonymous function within trimHeaderValues
throws a TypeError. But it may be better if we throw it ourselves?
Did you prefer any specific option, @ncou?
from psr7.
yes it may be better to throw it ourself, the controls are already presents in the withHeader and withAddedHeader so the controls should also be applyed on the setHeaders(), IMHO.
perhaps you could try to add a new function to do the checks, something like : "assertHeaderValues(string $header, array $values)" and this function could do the following asserts/verifications :
if (empty($values)) {
throw new \InvalidArgumentException('Header values must be non-empty strings');
}
if (!is_string($header) || empty($header)) {
throw new \InvalidArgumentException('Header name must be non-empty strings');
}
foreach ( array_values($values) as $value) {
if (!is_string($value)) {
throw new \InvalidArgumentException('Header values must be non-empty strings');
}
}
and you keep the convertion from string to array for the value variable before calling this assert method. and after you call the trimHeaderValues. This new function could be used/called in setHeaders() / withHeader() and withAddedHeader() and avoid some duplication code.
What do you think ?
from psr7.
@Zegnat : so what is your opinion ?
from psr7.
I do think we should use the same exception throwing in those 3 methods, for sure.
I am ambivalent on creating an extra private method for it. It is good DRY to abstract it. On the other hand, I like it when I am looking at an exception and it originates from the method I called rather than having to go further up in the stack trace. But that is a personal quirk.
from psr7.
@Nyholm : and your opinion ?
from psr7.
no news ?
from psr7.
Sorry for being late on replying.
I agree with Martin, lets duplicate the exception throwing for now.
I agree that doing new Response(200, ['Age' => 60]);
should throw an exception just like (new Response(200))->withHeader('Age', 60)
.
Could you send a PR?
from psr7.
When I closer inspected this. We use the same validation 3(!) times. I used a separate function. See #48
from psr7.
Wohoo. This is all fixed now.
from psr7.
Related Issues (20)
- Broken PSR-7 immutability HOT 2
- argument name of withHeader() is inconsistent with PSR-7 definition. HOT 2
- feature request for a non-buffered stream (NonBufferedStream) HOT 3
- Empty body when using psr17factory's createRequest HOT 3
- Filter Uri->withUserInfo() input HOT 2
- Axios get request gives [AxiosError:Network Error] & Error: header name must be a non-empty string
- Response::__toString() HOT 2
- Integration tests errors HOT 2
- Escaping of user info breaks NTLM authentication HOT 9
- "Site not found" for https://tnyholm.se HOT 2
- psr/http-message 版本问题 HOT 4
- PSR/HTTP message incompatibility 2.0 on nyholm/psr7 server HOT 1
- Breaking change from 1.5.1 => 1.6.0 due to fseek on new streams? HOT 3
- https://packagist.org/packages/php-http/message-factory dependand package abandoned HOT 1
- Segmentation Error while using Elasticsearch php's library bulk method HOT 2
- php-http/message-factory HOT 2
- Empty host value not allowed HOT 1
- For what reason message trait contains original header names?
- Support for "Early Hints" / HTTP Code 103 HOT 1
- Request getSize() returns null for php://input stream HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from psr7.