I am using Module.php onBootstrapfunction to check using authentication. on Postman and with browser everything works fine but when unittesting with zend-test it is not able to access the getHeaders() MY Module.php
public function onBootstrap(MvcEvent $e) { $app = $e->getTarget(); $locator = $app->getServiceManager(); $this->authorize($e); } public function authorize($e){ $sm = $e->getApplication()->getServiceManager(); $request = $sm->get('Request'); if($request->getHeaders('auth_token')){ return; } $response = $e->getResponse(); $model = new JsonModel(array( "httpStatus" => 401, "title" => "Token Not Found", )); $e->getResponse()->setStatusCode(401); $model->detail = "Auth Token not provided"; $model->setTerminal(true); $e->setResult($model); $e->setViewModel($model); } Now i tried to follow this but i couldn't because the code is so difficult to follow.
My test is something like that:
public function testIndexIsAccessibleAction() { $headers = new \Zend\Http\Headers(); $headers->addHeaders([ "auth_token" => 'token' ]); $this->getRequest() ->setMethod('GET') ->getHeaders() ->addHeaders($headers); ; $this->dispatch('/action'); $this->assertResponseStatusCode(200); } Now i am checking every request in onBootstrap because nothing is accessible if user is not authentic. How do i do my unit testing correctly. and how should i change it to using Service or Factory?
getRequestof your test class return?this->authorize($e)with$events->attach(MvcEvent::EVENT_ROUTE, array($this, 'authorize'));and it is working now