File tree Expand file tree Collapse file tree 2 files changed +44
-1
lines changed
src/Illuminate/Http/Client Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,13 @@ class Factory
28
28
*/
29
29
protected $ dispatcher ;
30
30
31
+ /**
32
+ * The callbacks that should execute before every request is sent.
33
+ *
34
+ * @var array
35
+ */
36
+ protected $ beforeSendingCallbacks = [];
37
+
31
38
/**
32
39
* The stub callables that will handle requests.
33
40
*
@@ -76,6 +83,19 @@ public function __construct(Dispatcher $dispatcher = null)
76
83
$ this ->stubCallbacks = collect ();
77
84
}
78
85
86
+ /**
87
+ * Add a new "before sending" callback to every request.
88
+ *
89
+ * @param callable $callback
90
+ * @return $this
91
+ */
92
+ public function beforeSending ($ callback )
93
+ {
94
+ $ this ->beforeSendingCallbacks [] = $ callback ;
95
+
96
+ return $ this ;
97
+ }
98
+
79
99
/**
80
100
* Create a new response instance for use during stubbing.
81
101
*
@@ -353,7 +373,11 @@ public function recorded($callback = null)
353
373
*/
354
374
protected function newPendingRequest ()
355
375
{
356
- return new PendingRequest ($ this );
376
+ return tap (new PendingRequest ($ this ), function ($ request ) {
377
+ foreach ($ this ->beforeSendingCallbacks as $ callback ) {
378
+ $ request ->beforeSending ($ callback );
379
+ }
380
+ });
357
381
}
358
382
359
383
/**
Original file line number Diff line number Diff line change @@ -2316,4 +2316,23 @@ public function testTheTransferStatsAreCustomizableOnFake(): void
2316
2316
2317
2317
$ this ->assertTrue ($ onStatsFunctionCalled );
2318
2318
}
2319
+
2320
+ public function testItCanMakeGlobalChangesToNewPendingRequests ()
2321
+ {
2322
+ $ request = null ;
2323
+ $ this ->factory ->beforeSending (function (Request $ request , array $ options , PendingRequest $ pending ) {
2324
+ return $ request ->toPsrRequest ()->withHeader ('User-Agent ' , 'Laravel Framework/1.0 ' );
2325
+ });
2326
+ $ this ->factory ->fake (function ($ r ) use (&$ request ) {
2327
+ $ request = $ r ;
2328
+
2329
+ return $ this ->factory ::response ('expected content ' );
2330
+ });
2331
+
2332
+ $ response = $ this ->factory ->post ('http://vapor.laravel.com ' );
2333
+
2334
+ $ this ->assertSame ('expected content ' , $ response ->body ());
2335
+ $ this ->assertNotNull ($ request );
2336
+ $ this ->assertSame (['Laravel Framework/1.0 ' ], $ request ->header ('User-Agent ' ));
2337
+ }
2319
2338
}
You can’t perform that action at this time.
0 commit comments