Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to check that response has not been already sent in phalcon/mvc/micro.zep #12668

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Added `prepend` parameter to `Phalcon\Loader::register` to specify autoloader's loading order to top most
- Fixed `Phalcon\Session\Bag::remove` to initialize the bag before removing a value [#12647](https://github.com/phalcon/cphalcon/pull/12647)
- Fixed `Phalcon\Mvc\Model::getChangedFields` to correct detect changes from NULL to Zero [#12628](https://github.com/phalcon/cphalcon/issues/12628)
- Fixed when `Micro::getReturnedValue()` is `string` or `ResponseInterface`, if response is handled in `Micro::after($handle)` or `Micro::finish($handle)`, `Phalcon` attemps to send response again, and faces `Response Already Sent` error.
- Fixed `Phalcon\Mvc\Model` to create/refresh snapshot after create/update/refresh operation [#11007](https://github.com/phalcon/cphalcon/issues/11007), [#11818](https://github.com/phalcon/cphalcon/issues/11818), [#11424](https://github.com/phalcon/cphalcon/issues/11424)

# [3.0.4](https://github.com/phalcon/cphalcon/releases/tag/v3.0.4) (2017-02-20)
Expand Down
10 changes: 7 additions & 3 deletions phalcon/mvc/micro.zep
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,10 @@ class Micro extends Injectable implements \ArrayAccess
*/
if typeof returnedValue == "string" {
let response = <ResponseInterface> dependencyInjector->getShared("response");
response->setContent(returnedValue);
response->send();
if !response->isSent() {
response->setContent(returnedValue);
response->send();
}
}

/**
Expand All @@ -997,7 +999,9 @@ class Micro extends Injectable implements \ArrayAccess
/**
* Automatically send the response
*/
returnedValue->send();
if !returnedValue->isSent() {
returnedValue->send();
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/Mvc/MicroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,27 @@ function () {
}
);
}

public function testMicroResponseAlreadySentError()
{
$this->specify(
"Micro::handle method doesn't work as expected",
function () {
$app = new Micro();
$app->after(
function () use ($app) {
$content = $app->getReturnedValue();
$app->response->setJsonContent($content)->send();
}
);
$app->map(
"/api",
function () {
return "success";
}
);
expect($app->handle('/api'))->equals("success");
}
);
}
}