Skip to content

Commit

Permalink
Merge pull request #56 from Jarlakxen/master
Browse files Browse the repository at this point in the history
Add DelayResponse support to ResponseStack
  • Loading branch information
donatj authored Sep 7, 2023
2 parents 5929542 + 731b7d2 commit 0153ba7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/ResponseStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* When the stack is empty, the server will return a customizable response defaulting to a 404.
*/
class ResponseStack implements MultiResponseInterface {
class ResponseStack implements InitializingResponseInterface, MultiResponseInterface {

private $ref;

Expand Down Expand Up @@ -39,6 +39,12 @@ public function __construct(ResponseInterface ...$responses) {
$this->pastEndResponse = new Response('Past the end of the ResponseStack', [], 404);
}

public function initialize( RequestInfo $request ) : void {
if( $this->currentResponse instanceof InitializingResponseInterface ) {
$this->currentResponse->initialize($request);
}
}

public function next() : bool {
array_shift($this->responses);
$this->currentResponse = reset($this->responses) ?: null;
Expand Down
21 changes: 21 additions & 0 deletions test/Integration/MockWebServer_IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,27 @@ public function testDelayedMultiResponse() : void {
$this->assertGreaterThan(.9, microtime(true) - $start, 'Delayed response should take ~1 seconds longer than realtime response');
}

public function testMultiResponseWithPartialDelay() : void {
$multi = new ResponseStack(
new Response('Response One', [ 'X-Boop-Bat' => 'Sauce' ], 200),
new DelayedResponse(new Response('Response Two', [ 'X-Slaw-Dawg: FranCran' ], 200), 1000000)
);

$path = self::$server->setResponseOfPath('/delayedMultiPath', $multi);

$start = microtime(true);
$contentOne = file_get_contents($path);
$this->assertSame($contentOne, 'Response One');
$this->assertContains('X-Boop-Bat: Sauce', $http_response_header);
$this->assertLessThan(.2, microtime(true) - $start, 'Delayed response should take less than 200ms');

$start = microtime(true);
$contentTwo = file_get_contents($path);
$this->assertSame($contentTwo, 'Response Two');
$this->assertContains('X-Slaw-Dawg: FranCran', $http_response_header);
$this->assertGreaterThan(.9, microtime(true) - $start, 'Delayed response should take ~1 seconds longer than realtime response');
}

/**
* Regression Test - Was a problem in 1.0.0-beta.2
*/
Expand Down

0 comments on commit 0153ba7

Please sign in to comment.