Problem/Motivation
Performing outbound HTTP requests with KernelTestBase(TNG) fails. The reason is that in tests TestHttpClientMiddleware calls drupal_generate_test_ua(), which fails, because it is not properly set up.
Proposed resolution
?
Remaining tasks
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #48 | 2571475-48.patch | 1.96 KB | tedbow |
| #48 | interdiff-37-48.txt | 855 bytes | tedbow |
| #37 | 2571475-37.patch | 1.81 KB | mglaman |
| #10 | outbound_http_requests-2571475-10.patch | 2.04 KB | mglaman |
| #3 | 2571475-2.patch | 1.43 KB | tstoeckler |
Comments
Comment #3
tstoecklerDoi, didn't have #2553661: KernelTestBase fails to set up FileCache yet locally...
Comment #7
dawehnermh
Comment #9
mglamanJust found this. Tried testing Commerce integration with a payment gateway's API in kernel test to find it exploded.
I tried unsetting
test.http_client.middlewaredefinition from the container in ::setUp but that failed. I think we need to add a check next todrupal_valid_test_uathat sees ifDRUPAL_TEST_IN_CHILD_SITEis defined.Comment #10
mglamanThis patch is allowing me to send an HTTP request to the service. This is a big blocker with using kernel tests to test integration with third party SDKs.
Comment #13
sebas5384 commentedI have the same issue, but I don't know if Kernel tests are supposed to be used to test third-party APIs for example.
Maybe exists other way of doing this.
Comment #14
dawehnerInteresting issue!
Comment #16
mglamanForgot, here's my workaround for now. Implement
ServiceModifierInterface, thenComment #17
dawehnerI believe #10 is pretty good, we just need to ensure that the constants exists, by probably not having the constant defined in the module.
Comment #18
blake.thompson commentedJust adding my experience with this issue. I tried defining the constant, for kicks, in my test setup, but that just led to this:
Drupal\Tests\mymodule\Kernel\MyTest::testMethod
file_put_contents(/vagrant/web/sites/simpletest/90225451/.htkey): failed to
open stream: No such file or directory
I ended up changing my tests that made HTTP requests to extend the BrowserTestBase instead.
Comment #19
mglamandawehner should we try to handle the constant definition, (I had same issue reported in #18) or just have base kernel test remove the service definition?
Comment #20
slv_ commentedHey there, just jumping in to mention I've experienced this problem too when trying to move convert FileDenormalizeTest (Simpletest) from the "hal" module into a KernelTest.
Not a problem for that one, as it can be simply extend BrowserTestBase instead and keep working as a functional test, but thought I'd mention it!
Comment #21
juampynr commentedPatch at #10 works fine for me.
Comment #22
jazzdrive3 commentedPatch #10 also works for me.
Comment #24
almaudoh commentedHad this issue yesterday. I lost 4 hours trying to fix the test in different ways. I defined constant
DRUPAL_TEST_IN_CHILD_SITEand ended up withI converted to BrowserTest and it still didn't work because
BrowserTestBasedoesn't havedrupalPost(), onlydrupalPostForm(), and I needed to post JSON.Eventually, I had to give up and revert to
WebTestBase- bummer.Can patch in #10 be committed?
Comment #25
ismail cherri commented#16 worked for me actually!
Thanks @mglman
Comment #26
timmillwoodI hit the same issue today.
Queued #10 for retesting.
Comment #27
timmillwoodTest failed
Comment #30
blazey commentedGot this error today while implementing kernel tests for webpack. The workaround from #16 solved it.
Comment #31
Jaesin commentedUn-needed re-roll
Comment #32
Jaesin commentednever mind pointless patch re-roll.
Comment #33
longmtran commentedHit the same problem. Temporary workaround in #16 works for me!
Thanks @mglman
Comment #34
dakku commented++ workaround in #16 works for me too!
Comment #35
millerrs commented#16 works for me.
Comment #37
mglamanRerolled against 8.8.x.
Comment #38
arlina commented#37 Worked for me.
Comment #39
eiriksmWorks for me, and patch looks good.
Uploading a test only patch (just copied from 37) to prove the bug. Will mark as RTBC if that fails.
Comment #41
eiriksm🎊️
Comment #43
eiriksmThat was just the test only patch
Comment #44
alexpottUnfortunately the comment makes no sense. We're do nothing and we're in a test environment :)
The reason we have this problem is that under kernel tests
\Drupal\Core\DrupalKernel::bootEnvironment()is never called and thereforeDRUPAL_TEST_IN_CHILD_SITEis not defined. I spent some time considering if we should defineDRUPAL_TEST_IN_CHILD_SITEin\Drupal\KernelTests\KernelTestBase::bootEnvironment()or\Drupal\KernelTests\KernelTestBase::bootKernel()and what that would look like. However I don't think that's right.So I think we have 2 options here. The simple one is document this properly... i.e add something like
The comment probably can be improved but I think being explicit is useful.
The other option is to somehow check the Kernel environment in \Drupal\Core\CoreServiceProvider::registerTest() because in kernel tests we set it to 'testing' whereas in other tests its 'prod'. That said this solution also feels more fragile than the DRUPAL_TEST_IN_CHILD_SITE solution so let's do that - but with a more explicit comment.
Comment #46
otteverbeek commentedAny updates on this?
Comment #47
mxr576I am using the workaround from #16
Comment #48
tedbowRan into this problem in #3041885: Display relevant Security Advisories data for Drupal when we just moved this functionality to the system module.
This bug makes
\Drupal\KernelTests\Core\File\FileSystemRequirementsTestfail because we fetch the security advisories feed insystem_requirementsUpdated #37 with @alexpott's suggestion in #44 I agree it is better without it's own comment. I think the comment suggested is fine.
The test only patch in #39 can still work as the test only patch since the test has not changed. Will re-queue it.
Should we fix this first in 9.2.x now?
Comment #49
phenaproximaThat should be "...in this case".
That should end with a period, not a comma.
Both of these are fixable on commit, so I'm sending this right to RTBC on the assumption that the tests will come back green. Otherwise, this patch makes sense to me. I'm also re-titling the issue because I don't think we have the "old" KernelTestBase anymore anyway.
I would guess this will land in 9.2.x first and be backported, so I'm also changing the target version and queuing the latest patch up against 9.2.x.
Comment #50
alexpottCommitted 51e0c38 and pushed to 9.2.x. Thanks!
Will backport once bugfix releases are out and we're unfrozen.
Fixed on commit.
Comment #52
mxr576I'm glad that this finally fixed, but testOutboundHttpRequest() does not perform any actual assertion. Isn't that an antipattern that PHPunit also warns when it runs the test?
Comment #53
alexpott@mxr576 good point - can you create a follow-up issue to add an assertion. It doesn't fail because we have an assertion in \Drupal\KernelTests\KernelTestBaseTest::tearDown()
Comment #54
mxr576Added KernelTestBaseTest::testOutboundHttpRequest() does not perform any assertion
Comment #55
alexpottBackported to 9.1.x.
Comment #57
phenaproximaWhilst debugging a test, @tedbow and I found a flaw in this logic: it doesn't account for DRUPAL_TEST_IN_CHILD_SITE being explicitly defined as FALSE. If the constant exists and is FALSE, then it registers the test middleware anyway, and things can get weird.
We only have been encountering this while working on #3041885: Display relevant Security Advisories data for Drupal, which adds code to the System module that uses the http_client service to make outbound HTTP requests. Adding this in System, which is a very foundational module that's close the bootstrapping and installation process, surfaces these sorts of things.
So I think we'll need to either reopen this issue, or open a follow-up, to handle the case where DRUPAL_TEST_IN_CHILD_SITE is FALSE.
Comment #58
tedbowGot some help from @mglaman on the problem in #57. Turns out we were able to solve it a different way
https://git.drupalcode.org/issue/drupal-3041885/-/compare/991f955548ef61...
I think in our case
DRUPAL_TEST_IN_CHILD_SITEwas actually to be TRUE anyways. So probably makes sense to open up a follow up for checking DRUPAL_TEST_IN_CHILD_SITE is FALSE but that won't have solved our problemComment #60
dave reidIt looks like this should be able to be backported to Drupal 8.9 as well since it's a bug fix that touches no APIs. I've run into this several times with making HTTP requests in tests.
Comment #61
matroskeenJust ran into this as well (was writing a custom kernel test for 8.9.x project).
AFAIK, only Major bugs are eligible to be backported to 8.9.x. I'm gonna bump the priority because it's terrible for developer experience and would be great to have fixed in the next release.
I've applied a patch from commit to 9.1.x and it did the trick for me, therefore moving to RTBC.
Here is a link for quick access: https://git.drupalcode.org/project/drupal/commit/ba2c6fb.patch
Comment #63
benjifisherPlease do not ask the testbot to try again until #3207086: [HEAD BROKEN] Consistent failure in MonthDatePluginTest is fixed.
Comment #64
alexpott#3207086: [HEAD BROKEN] Consistent failure in MonthDatePluginTest is fixed.
Comment #65
spokjeThe fix for #3207086: [HEAD BROKEN] Consistent failure in MonthDatePluginTest still needs to be backported to the 8.9.x branch, so currently tests are still failing.
Comment #66
spokje#3207086: [HEAD BROKEN] Consistent failure in MonthDatePluginTest has now been backported to branch
8.9.x.Ordered a retest on the latest patch, which should come back green.
Comment #68
catchBackported to 8.9.x, thanks!