Problem
-
For outbound HTTP requests performed in a test, a request needs to use the "simpletest" User-Agent.
-
#1447736: Adopt Guzzle library to replace drupal_http_request() converted the legacy code of
drupal_http_request()
into an event subscriber for thehttp_default_client
(Guzzle) service. -
However, this event subscriber is added always — even though it is only needed when a test is executed.
Proposed solution
-
Add a service provider for tests, which only adds the request event subscriber service definition and method call when a test is executed.
API changes
- None.
Comment | File | Size | Author |
---|---|---|---|
#31 | 2168809-31.patch | 3.34 KB | damiankloip |
#16 | test.services.16.patch | 3.34 KB | sun |
#10 | interdiff.txt | 2.08 KB | sun |
#10 | test.services.10.patch | 3.91 KB | sun |
#8 | interdiff.txt | 4.16 KB | sun |
Comments
Comment #2
damiankloip CreditAttribution: damiankloip commentedLet's move the subscriber outside of simpletest module. Haven't looked at the failures yet, they seem to all be coming from when cron is invoked from a test.
Comment #4
Damien Tournoud CreditAttribution: Damien Tournoud commentedI assume this is the passthrough of the web test user-agent. It definitely belongs in the standard container, as it belongs in the site-under-test (which is an unaltered Drupal site), not in the test runner.
We could have a submodule of simpletest that we only enable in the site-under-test. But at this point, it's probably easier not to fix what's not broken.
Comment #5
damiankloip CreditAttribution: damiankloip commentedWe are altering things for the test environment anyway. So that is not really true.
It works, yes. But it's not right having a test specific subscriber for all http requests made just because it's easier to leave it in the standard container.
Comment #6
damiankloip CreditAttribution: damiankloip commentedComment #7
damiankloip CreditAttribution: damiankloip commentedComment #8
sunAs outlined more in-depth in #2165549: Reduce number of password hashing iterations in all tests to improve test performance, I think it makes more sense to do this instead:
Introduce dedicated Test component and conditionally register services/event-subscribers.
Comment #9
damiankloip CreditAttribution: damiankloip commentedI am good with this approach. My main quibble is that we don't want crap like this registered in the container all the time.
Yes! Although if we are already in the Test namespace can we just call the subscriber HttpRequestSubscriber?
Comment #10
sunRenamed to HttpRequestSubscriber.
Adjusted issue title + summary accordingly.
Comment #11
sunComment #12
damiankloip CreditAttribution: damiankloip commentedThis might as well wait for Guzzle 4 now? This patch will not work with Guzzle 4.
Comment #13
sunAfter reviewing and studying that patch, it doesn't really look like these two patches will conflict in serious ways with each other.
That said, the Guzzle4 upgrade seems to hard-code this class as a before-send event emitter — other service providers are no longer able to adjust/extend the http client service through the service definition...? :-/
Perhaps I shouldn't have RTBC'd just yet... OTOH, it's going to be much easier to revise the new code once the main upgrade changes + Guzzle4 is in core.
Comment #14
damiankloip CreditAttribution: damiankloip commentedAgreed, we need to just get Guzzle 4 in before we can really decide what we want to do IMO.
Guzzle 3 had an addSubscriber proxy method on the client to register things, Guzzle 4 does not have this unfortunately, hence the hardcoded call in the constructor.
So I think that is not true, as we will no longer be able to just add a method call like that anymore :/
Comment #15
sunYeah, let's discuss that in #2237681: Revise Guzzle client configuration and event registration :-)
Comment #16
sunUpdated for new http_client_subscriber compiler pass.
Comment #17
sunGreat. This is the same patch as before, just adapted for Guzzle 4.
Once this in, we can move forward with #2165549: Reduce number of password hashing iterations in all tests to improve test performance + look into further clean-ups and optimizations.
Comment #18
damiankloip CreditAttribution: damiankloip commentedIt really would be nicer if we only added this pass when in a test env (like earlier patches were doing) but understand this is a good compromise. Still calling this subscriber code but not registering this guzzle subscriber all the time.
Minor, but I think the service namespacing should be the other way round. i.e. 'http_client.subscriber.test' ?
Better use of the namespace. Removing Simpletest from the class name is a good idea IMO.
Comment #19
sunregisterTest()
contains the check itself and early-returns. I see where you're coming from, though the counter-argument would be that the code is encapsulated in a single spot instead of multiple places. I could go either way, don't really have a preference.Test
, notHttp\Client
, so more correct would be:test.http_client.request_subscriber
Comment #20
sunRenamed service ID to test.http_client.request_subscriber.
Comment #21
sun20: test.services.20.patch queued for re-testing.
Comment #22
damiankloip CreditAttribution: damiankloip commentedThis seems fair to me. Let's do it!
Comment #24
neclimdul20: test.services.20.patch queued for re-testing.
Comment #26
sunMerged 8.x.
Comment #28
sun26: test.services.26.patch queued for re-testing.
Comment #29
damiankloip CreditAttribution: damiankloip commentedand again.
Comment #31
damiankloip CreditAttribution: damiankloip commentedRerolled.
Comment #32
jibranBack to RTBC.
Comment #34
damiankloip CreditAttribution: damiankloip commented31: 2168809-31.patch queued for re-testing.
Comment #35
sunComment #36
catchNot doing this outside of tests seems sensible.
However I can't remember, at all, why we need to do this in tests either? This is only for requests from the tested site during a test run which we should not be doing much if at all.
Comment #37
BerdirWe are doing it, the most obvious example is aggregator test which create feeds based on nodes, and if we run cron as a request or test the UI, that needs to go back to the test environment.
Comment #38
sunThe two prime examples are Aggregator tests (in which the child site performs outbound HTTP requests to itself) and SimpleTest's very own functional tests (in which the test runs a test in a child-child-site).
It's fine to remove it from the production container, but it should be enabled by default whenever a kernel is built in a test environment.
Comment #40
catchOK I think I get this just about.
I think it'd be better if we added a custom header and checked that, rather than messing with the user agent. I can see a test relying on the standard Drupal user agent, which would then not be here. However this issue isn't introducing that problem.
Committed/pushed to 8.x, thanks!