Skip to content

set_status_header in controller constructor gets overwritten. #194

Description

@luckydonald

When setting a status code, e.g. 404 it will be overwritten by this line

Edit: To illustrate, here, have some code:

class SomeController extends CI_Controller {
	/** @var bool To indicate the routing to stop executing any functions because the startup failed already. */
	private $startup_failed = false;

	public function __construct() {
		parent::__construct();
		if (true) {  // some pre checks
			$this->output->set_status_header(404);
			echo("OH GAWD!");
			$startup_failed = true;
		}
	}

	public function _remap($object_called, $arguments = []) {
		if ($this->startup_failed) {
			return;
		}
		parent::_remap($object_called, $arguments);
	}
}
class SomeTest extends TestCase {
	public function test_response() {
		$output = $this->request("GET", "route/to/somecontroller", []);
		$expected = "OH GAWD!";
		$this->assertEquals($expected, $output);  // works correctly
		$this->assertResponseCode(404, "not found");  // fails
	}
}

Basically an workaround for using exit() before.

Noticably assertResponseCode(404) fails because the code is overwritten to 200.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions