/** * Returns the current request * * @return \Cundd\Rest\Request */ public function getRequest() { if (!$this->request) { return $this->requestFactory->getRequest(); } return $this->request; }
/** * @test */ public function createSuccessResponseTest() { $_GET['u'] = 'MyExt-MyModel/1.json'; $response = $this->fixture->createSuccessResponse('Everything ok', 200); $this->assertEquals(200, $response->status()); $this->assertEquals('{"message":"Everything ok"}', $response->content()); $this->requestFactory->getRequest()->format('html'); $response = $this->fixture->createSuccessResponse('HTML format is currently not supported', 200); $this->assertEquals(200, $response->status()); $this->assertEquals('Unsupported format: html. Please set the Accept header to application/json', $response->content()); $this->requestFactory->getRequest()->format('blur'); $response = $this->fixture->createSuccessResponse('This will default to JSON', 200); $this->assertEquals(200, $response->status()); $this->assertEquals('{"message":"This will default to JSON"}', $response->content()); $response = $this->fixture->createSuccessResponse(NULL, 200); $this->assertEquals(200, $response->status()); $this->assertEquals('{"message":"OK"}', $response->content()); // This will be an error $response = $this->fixture->createSuccessResponse(NULL, 404); $this->assertEquals(404, $response->status()); $this->assertEquals('{"error":"Not Found"}', $response->content()); }
/** * Returns a response with the given message and status code * * @param string|array $data Data to send * @param int $status Status code of the response * @param bool $forceError If TRUE the response will be treated as an error, otherwise any status below 400 will be a normal response * @return Response * @internal */ public function createResponse($data, $status, $forceError = FALSE) { $body = NULL; $response = new Response(NULL, $status); $format = $this->requestFactory->getRequest()->format(); if (!$format) { $format = 'json'; } $messageKey = 'message'; if ($forceError || $status >= 400) { $messageKey = 'error'; } switch ($format) { case 'json': switch (gettype($data)) { case 'string': $body = array($messageKey => $data); break; case 'array': $body = $data; break; case 'NULL': $body = array($messageKey => $response->statusText($status)); break; } $response->contentType('application/json'); $response->content(json_encode($body)); break; case 'xml': // TODO: support more response formats // TODO: support more response formats default: $body = sprintf('Unsupported format: %s. Please set the Accept header to application/json', $format); $response->content($body); } return $response; }
/** * Returns the key to use for the root object if addRootObjectForCollection * is enabled * * @return string * @deprecated use the request's getRootObjectKey() */ public function getRootObjectKey() { \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction(); return $this->requestFactory->getRequest()->getRootObjectKey(); }
/** * @test */ public function getFormatWithNotExistingFormatTest() { $_GET['u'] = 'MyExt-MyModel/1.blur'; $request = $this->fixture->getRequest(); $this->assertEquals('json', $request->format()); }