/** * @covers WSHelper::extractParameters * @expectedException WebServiceException */ public function testextractParameters_WithEmptyModuleAndMethod() { $requestMock = $this->getMock('sfWebRequest', array('getMethod', 'getHttpHeader', 'getRequestParameters'), array(new sfEventDispatcher())); $requestMock->expects($this->once())->method('getMethod')->will($this->returnValue('GET')); $requestMock->expects($this->any())->method('getHttpHeader')->will($this->onConsecutiveCalls('bad json', json_encode(array('page' => 2, 'limit' => 50)))); $requestMock->expects($this->once())->method('getRequestParameters')->will($this->returnValue(array('module' => 'api', 'action' => 'wsCall', '_sf_route' => null))); $this->helper->extractParameters($requestMock); }
public function execute($request) { $logger = $this->getWebServiceLogger(); $wsHelper = new WSHelper(); $wsManager = new WSManager(); $result = ''; $status = 'INITIAL'; $contentType = 'text/plain'; $httpStatus = 200; $httpStatusText = null; try { $paramObj = $wsHelper->extractParameters($request); $isMethodAvailable = $wsManager->isMethodAvailable($paramObj->getMethod(), $paramObj->getRequestMethod()); $logger->debug(print_r($paramObj, true)); $logger->debug("MethodAvailable:" . $isMethodAvailable); if ($isMethodAvailable) { $isAuthenticated = $wsManager->isAuthenticated($paramObj); $isAuthorized = $wsManager->isAuthorized($paramObj); if ($isAuthenticated && $isAuthorized) { $result = $wsManager->callMethod($paramObj); $logger->debug(print_r($result, true)); $result = $wsHelper->formatResult($result, WSHelper::FORMAT_JSON); $logger->debug(print_r($result, true)); $status = 'SUCCESS'; $contentType = 'text/json'; } else { $result = 'NOT ALLOWED'; $status = 'ERROR'; $httpStatus = 401; } } else { $result = 'INVALID REQUEST'; $status = 'ERROR'; $httpStatus = 404; $httpStatusText = 'Webservice Method Not Found (' . $paramObj->getMethod() . ')'; } } catch (WebServiceException $e) { $result = $e->getCode() . ': ' . $e->getMessage(); $status = 'ERROR'; $httpStatus = $e->getCode(); $httpStatusText = $e->getMessage(); } catch (Exception $e) { $logger = $this->getWebServiceLogger(); $logger->info('Uncaught Exception: ' . $e->getMessage()); $result = $e->getMessage() . ' ' . $e->getCode(); $status = 'ERROR'; } $response = $this->getResponse(); $response->setContent($result); $response->setHttpHeader('Content-type', $contentType); $response->setHttpHeader('ohrm_ws_call_status', $status); $response->setStatusCode($httpStatus, $httpStatusText); return sfView::NONE; }