Problem/Motivation
While working on expanding the HTTP POST test coverage in #2737719: EntityResource: Provide comprehensive test coverage: for every entity type, every format, every method, I noticed that when the developer forgets to specify the Content-Type request header that they get this rather cryptic message:
{"message":"No route found that matches \u0022Content-Type: \u0022"}
Those \u0022 should be interpreted as "double quote". So it's repeating the literal Content-Type request header: both the key and its value, and the value just happens to be NULL, which PHP casts to the empty string.
Proposed resolution
Detect when the Content-Type request header is not set and provide a more helpful error response:
No "Content-Type" request header specified.
Remaining tasks
Do it.
User interface changes
None.
API changes
None.
Data model changes
None.
Comments
Comment #2
wim leersComment #3
wim leersComment #4
dawehnerWe use
setExpectedException()now, see https://thephp.cc/news/2016/02/questioning-phpunit-best-practices. Mh ideally we would have a @covers hereWe really don't need this with set expected exception
Comment #5
wim leers#4: all of that is just copy/pasted from the existing test at
\Drupal\Tests\Core\Routing\ContentTypeHeaderMatcherTest::testNoRouteFound(). You're okay with me fixing both?Comment #7
dawehnerWell, feel free to choose :) I just don't believe in adding bad new code. I know you value consistency more
Comment #8
neclimdulre #5
1) Is new. Megh but it is the new way. If you're going to do it just put it right before the method going to throw the exception so you test only the method you're testing not some other code that might have thrown the exception.
2) Well ContentTypeHeaderMatcherTest is wrong then. fail() will bypass the expected exception expectations because it runs after the test method finishes. This means the failure message doesn't include the exception type unless it throws the wrong exception type.
Comment #9
wim leersAgreed. So let's fix both then.
Comment #10
dawehnerYeah exactly, its also describing for users where the exception is thrown. All in all just improvements.
Everytime someone comes up with that, similar (it would not be worth to test), its suspicious :P
Comment #11
neclimdulIs this what we're looking for?
Comment #12
neclimdulAlso writing this I found it interesting that one of these 2 exception messages has a period and one doesn't. I don't actually know what our standard is for such things.
Comment #13
wim leers#11: yes!
I would RTBC but there's still #12. I don't care either way, but the pedant in me screams .
Comment #14
dawehnerI personally don't care about the dots :) The discussion might better end up in its own issue + a fix for all the exception messages.
Comment #15
wim leers+1
Comment #16
alexpottCommitted 97f0d43 and pushed to 8.3.x. Thanks!
Also I think this is actually a bug and should be backported to 8.2.x - but I'll leave that to those on the issue to decide.
Remove dot on commit for consistency. Also we've already discussed and agreed no dots.
Comment #19
catchThis broke 8.3.x tests: https://www.drupal.org/pift-ci-job/503665
Rolled back for now. Late here so didn't look into why.
Comment #20
cilefen commentedFor me, it's the period, but weird.
Comment #21
cilefen commentedOh, it's simple. #16 removed the period from the logic but not from the test.
Comment #22
catchThat'd do it!
Comment #23
alexpottOkay alexpott--
As per #16
Committed 8bbdbed and pushed to 8.3.x. Thanks!
Comment #25
wim leers+1
Comment #26
alexpottDiscussed with a @catch and we both agreed this is fine for 8.2.x
Committed 4d97701 and pushed to 8.2.x. Thanks!
Comment #28
wim leersYay! Better DX for 8.2!