Trigger onResponses on non-replacement request too in VirtualRESTServiceClient
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 19 Dec 2014 19:46:10 +0000 (11:46 -0800)
committerOri.livneh <ori@wikimedia.org>
Tue, 6 Jan 2015 21:54:20 +0000 (21:54 +0000)
Change-Id: I1e550812f3c021c2e2f4246345df2f9dd47d15b8

includes/libs/virtualrest/VirtualRESTServiceClient.php

index 46be144..e8bb38d 100644 (file)
@@ -208,6 +208,9 @@ class VirtualRESTServiceClient {
                        if ( ++$rounds > 5 ) { // sanity
                                throw new Exception( "Too many replacement rounds detected. Aborting." );
                        }
+                       // Track requests executed this round that have a prefix/service.
+                       // Note that this also includes requests where 'response' was forced.
+                       $checkReqIndexesByPrefix = array();
                        // Resolve the virtual URLs valid and qualified HTTP(S) URLs
                        // and add any required authentication headers for the backend.
                        // Services can also replace requests with new ones, either to
@@ -220,7 +223,7 @@ class VirtualRESTServiceClient {
                                        if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
                                                // A current or original request which was not modified
                                        } else {
-                                               // Replacement requests with pre-set responses should not execute
+                                               // Replacement request that will convert to original requests
                                                $newReplaceReqsByService[$prefix][$index] = $req;
                                        }
                                        if ( isset( $req['response'] ) ) {
@@ -232,6 +235,7 @@ class VirtualRESTServiceClient {
                                                // Original or mangled request included
                                                $executeReqs[$index] = $req;
                                        }
+                                       $checkReqIndexesByPrefix[$prefix][$index] = 1;
                                }
                        }
                        // Update index of requests to inspect for replacement
@@ -246,12 +250,12 @@ class VirtualRESTServiceClient {
                        // defer the original or to set a proxy response to the original.
                        // Any replacement requests executed above will need to be replaced
                        // with new requests (eventually the original). The responses can be
-                       // forced instead of having the request sent over the wire.
+                       // forced by setting 'response' rather than actually be sent over the wire.
                        $newReplaceReqsByService = array();
-                       foreach ( $replaceReqsByService as $prefix => $servReqs ) {
+                       foreach ( $checkReqIndexesByPrefix as $prefix => $servReqIndexes ) {
                                $service = $this->instances[$prefix];
-                               // Only the request copies stored in $doneReqs actually have the response
-                               $servReqs = array_intersect_key( $doneReqs, $servReqs );
+                               // $doneReqs actually has the requests (with 'response' set)
+                               $servReqs = array_intersect_key( $doneReqs, $servReqIndexes );
                                foreach ( $service->onResponses( $servReqs, $idFunc ) as $index => $req ) {
                                        // Services use unique IDs for replacement requests
                                        if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {