protected function _callService($method, $class = 'Zend_Amf_Resource_testclass') { $request = new Request(); $request->setObjectEncoding(0x3); $this->_server->setClass($class); $newBody = new MessageBody("{$class}.{$method}", "/1", array("test")); $request->addAmfBody($newBody); $this->_server->handle($request); $response = $this->_server->getResponse(); return $response; }
/** * Test Amf0 credentials sent to the server * */ public function testAmf0CredentialsInHeader() { $myRequest = file_get_contents(dirname(__FILE__) . '/Request/mock/credentialsheaderAmf0.bin'); // send the mock object request to be deserialized $this->_request->initialize($myRequest); // Make sure that no headers where recieved $this->assertEquals(1, sizeof($this->_request->getAmfHeaders())); $requestHeaders = $this->_request->getAmfHeaders(); $this->assertTrue($requestHeaders[0] instanceof MessageHeader); $this->assertEquals('Credentials', $requestHeaders[0]->name); $this->assertFalse($requestHeaders[0]->mustRead); $data = $requestHeaders[0]->data; // Check the resulting header $this->assertEquals('admin', $data->userid); $this->assertEquals('pw123', $data->password); }
public function testLogout() { \fproject\amf\session\Session::$_unitTestEnabled = true; $this->_server->setAuth(new RightPassword("testuser", "testrole")); $this->_acl->addRole(new \fproject\amf\acl\Role("testrole")); $this->_acl->allow("testrole", null, null); $this->_server->setAcl($this->_acl); $resp = $this->_callServiceAuth("testuser", ""); $this->assertTrue($resp[0]->getData() instanceof AcknowledgeMessage); $this->assertContains("hello", $resp[1]->getData()); // After logout same request should not be allowed $this->setUp(); $this->_server->setAuth(new RightPassword("testuser", "testrole")); $this->_server->setAcl($this->_acl); $request = new Request(); $request->setObjectEncoding(0x3); $this->_addLogout($request); $this->_addServiceCall($request); $this->_server->handle($request); $resp = $this->_server->getResponse()->getAmfBodies(); $this->assertTrue($resp[0]->getData() instanceof AcknowledgeMessage); $data = $resp[1]->getData(); $this->assertTrue($data instanceof ErrorMessage); $this->assertContains("not allowed", $data->faultString); }
/** * @group ZF-6130 */ public function testServerShouldCastObjectArgumentsToAppropriateType() { $server = new Server(); $server->addDirectory(dirname(__FILE__) . '/_files/zf-6130/services'); // Create a mock message $message = new RemotingMessage(); $message->operation = 'createEmployee'; $message->source = 'EmployeeService'; // original raw request used "destination" $message->body = array(array('office' => 322, 'departmentid' => 3, 'street' => 32, 'zipcode' => 32, 'state' => 32, 'lastname' => 4, 'firstname' => 2, 'photofile' => 322, 'city' => 32, 'id' => 1, 'title' => 4, 'officephone' => 233, 'email' => 32, 'cellphone' => 22)); $body = new MessageBody(null, "", $message); $request = new Request(); $request->addAmfBody($body); $request->setObjectEncoding(0x3); $response = $server->handle($request); $employee = EmployeeService::$employee; $this->assertNotNull($employee); $this->assertNotEquals(1, $employee->id); $this->assertRegexp('/[a-z0-9]{3,}/', $employee->id); }
/** * Takes the de_errorMessageserialized AMF request and performs any operations. * * @todo should implement and SPL observer pattern for custom AMF headers * @todo DescribeService support * @param Request $request * @return Response * @throws AmfException|Exception */ protected function _handle(Request $request) { // Get the object encoding of the request. $objectEncoding = $request->getObjectEncoding(); // create a response object to place the output from the services. $response = $this->getResponse(); // set response encoding $response->setObjectEncoding($objectEncoding); // Authenticate, if we have credential headers $error = false; $headers = $request->getAmfHeaders(); if (isset($headers[Constants::CREDENTIALS_HEADER]) && isset($headers[Constants::CREDENTIALS_HEADER]->password)) { try { $authResult = $this->_handleAuth($headers[Constants::CREDENTIALS_HEADER]->userid, $headers[Constants::CREDENTIALS_HEADER]->password); if ($authResult === true || $authResult->getCode() == AuthResult::SUCCESS) { // use RequestPersistentHeader to clear credentials $response->addAmfHeader(new MessageHeader(Constants::PERSISTENT_HEADER, false, new MessageHeader(Constants::CREDENTIALS_HEADER, false, null))); } } catch (Exception $e) { // Error during authentication; report it $error = $this->_errorMessage($objectEncoding, '', $e->getMessage(), $e->getTraceAsString(), $e->getCode(), $e->getLine()); $responseType = Constants::STATUS_METHOD; } } // Iterate through each of the service calls in the AMF request foreach ($request->getAmfBodies() as $body) { if ($error) { // Error during authentication; just report it and be done $responseURI = $body->getResponseURI() . $responseType; $newBody = new MessageBody($responseURI, null, $error); $response->addAmfBody($newBody); continue; } try { switch ($objectEncoding) { case Constants::AMF0_OBJECT_ENCODING: // AMF0 Object Encoding $targetURI = $body->getTargetURI(); $message = ''; // Split the target string into its values. $source = substr($targetURI, 0, strrpos($targetURI, '.')); if ($source) { // Break off method name from namespace into source $method = substr(strrchr($targetURI, '.'), 1); $return = $this->_dispatch($method, $body->getData(), $source); } else { // Just have a method name. $return = $this->_dispatch($targetURI, $body->getData()); } break; case Constants::AMF3_OBJECT_ENCODING: default: // AMF3 read message type $message = $body->getData(); if ($message instanceof CommandMessage) { // async call with command message $return = $this->_loadCommandMessage($message); } elseif ($message instanceof RemotingMessage) { $return = new AcknowledgeMessage($message); $return->body = $this->_dispatch($message->operation, $message->body, $message->source); } else { // Amf3 message sent with netConnection $targetURI = $body->getTargetURI(); // Split the target string into its values. $source = substr($targetURI, 0, strrpos($targetURI, '.')); if ($source) { // Break off method name from namespace into source $method = substr(strrchr($targetURI, '.'), 1); $return = $this->_dispatch($method, $body->getData(), $source); } else { // Just have a method name. $return = $this->_dispatch($targetURI, $body->getData()); } } break; } $responseType = Constants::RESULT_METHOD; } catch (Exception $e) { $return = $this->_errorMessage($objectEncoding, $message, $e->getMessage(), $e->getTraceAsString(), $e->getCode(), $e->getLine()); $responseType = Constants::STATUS_METHOD; } $responseURI = $body->getResponseURI() . $responseType; $newBody = new MessageBody($responseURI, null, $return); $response->addAmfBody($newBody); } // Add a session header to the body if session is requested. if ($this->isSession()) { $currentID = session_id(); $joint = "?"; if (isset($_SERVER['QUERY_STRING'])) { if (!strpos($_SERVER['QUERY_STRING'], $currentID) !== FALSE) { if (strrpos($_SERVER['QUERY_STRING'], "?") !== FALSE) { $joint = "&"; } } } // create a new AMF message header with the session id as a variable. $sessionValue = $joint . $this->_sessionName . "=" . $currentID; $sessionHeader = new MessageHeader(Constants::URL_APPEND_HEADER, false, $sessionValue); $response->addAmfHeader($sessionHeader); } // serialize the response and return serialized body. $response->finalize(); }
public function testResponseHeadersShouldBeSerializedWhenWritingMessage() { $this->testResponseShouldAggregateMessageHeaders(); $this->_response->finalize(); $response = $this->_response->getResponse(); $request = new Request(); $request->initialize($response); $headers = $request->getAmfHeaders(); $this->assertEquals(2, count($headers)); }