Skip to content

Commit 73fac6f

Browse files
authored
Merge pull request #164 from utopia-php/fix-actions-after-response
Fix: Init response to avoid action
2 parents 81f4a3f + 2d29fbd commit 73fac6f

File tree

4 files changed

+72
-38
lines changed

4 files changed

+72
-38
lines changed

composer.lock

Lines changed: 38 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Response.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,8 @@ public function send(string $body = ''): void
639639
$this->end();
640640
}
641641

642+
$this->sent = true;
643+
642644
$this->disablePayload();
643645
}
644646

tests/e2e/ResponseTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,17 @@ public function testFile()
4343
$response = $this->client->call(Client::METHOD_GET, '/humans.txt');
4444
$this->assertEquals(204, $response['headers']['status-code']);
4545
}
46+
47+
public function testEarlyResponse()
48+
{
49+
// Ensure response from action is not recieved
50+
$response = $this->client->call(Client::METHOD_GET, '/early-response');
51+
$this->assertEquals(200, $response['headers']['status-code']);
52+
$this->assertNotEquals('Action response', $response['body']);
53+
54+
// 2nd request would catch if action from first ran
55+
$response = $this->client->call(Client::METHOD_GET, '/early-response');
56+
$this->assertEquals(200, $response['headers']['status-code']);
57+
$this->assertEquals('Init response. Actioned before: no', $response['body']);
58+
}
4659
}

tests/e2e/server.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@
4646
$response->noContent();
4747
});
4848

49+
50+
// Endpoints for early response
51+
// Meant to run twice, so init hook can know if action ran
52+
$earlyResponseAction = 'no';
53+
App::init()
54+
->groups(['early-response'])
55+
->inject('response')
56+
->action(function (Response $response) use ($earlyResponseAction) {
57+
$response->send('Init response. Actioned before: ' . $earlyResponseAction);
58+
});
59+
60+
App::get('/early-response')
61+
->groups(['early-response'])
62+
->inject('response')
63+
->action(function (Response $response) use (&$earlyResponseAction) {
64+
$earlyResponseAction = 'yes';
65+
$response->send('Action response');
66+
});
67+
4968
$request = new Request();
5069
$response = new Response();
5170

0 commit comments

Comments
 (0)