Skip to content

Commit

Permalink
Fix actions running after response in init
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Feb 24, 2025
1 parent 81f4a3f commit 2d29fbd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 38 deletions.
76 changes: 38 additions & 38 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,8 @@ public function send(string $body = ''): void
$this->end();
}

$this->sent = true;

$this->disablePayload();
}

Expand Down
13 changes: 13 additions & 0 deletions tests/e2e/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,17 @@ public function testFile()
$response = $this->client->call(Client::METHOD_GET, '/humans.txt');
$this->assertEquals(204, $response['headers']['status-code']);
}

public function testEarlyResponse()
{
// Ensure response from action is not recieved
$response = $this->client->call(Client::METHOD_GET, '/early-response');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEquals('Action response', $response['body']);

// 2nd request would catch if action from first ran
$response = $this->client->call(Client::METHOD_GET, '/early-response');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('Init response. Actioned before: no', $response['body']);
}
}
19 changes: 19 additions & 0 deletions tests/e2e/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@
$response->noContent();
});


// Endpoints for early response
// Meant to run twice, so init hook can know if action ran
$earlyResponseAction = 'no';
App::init()
->groups(['early-response'])
->inject('response')
->action(function (Response $response) use ($earlyResponseAction) {
$response->send('Init response. Actioned before: ' . $earlyResponseAction);
});

App::get('/early-response')
->groups(['early-response'])
->inject('response')
->action(function (Response $response) use (&$earlyResponseAction) {
$earlyResponseAction = 'yes';
$response->send('Action response');
});

$request = new Request();
$response = new Response();

Expand Down

0 comments on commit 2d29fbd

Please sign in to comment.