/** * @test */ public function convertSuffixToContentTypeTest() { $this->assertEquals(ContentType::JPEG_IMAGE, ContentTypeUtility::convertSuffixToContentType('jpg')); $this->assertEquals(ContentType::JPEG_IMAGE, ContentTypeUtility::convertSuffixToContentType('jpeg')); $this->assertEquals(ContentType::PNG_IMAGE, ContentTypeUtility::convertSuffixToContentType('png')); $this->assertEquals(ContentType::GIF_IMAGE, ContentTypeUtility::convertSuffixToContentType('gif')); $this->assertEquals(ContentType::JSON_APPLICATION, ContentTypeUtility::convertSuffixToContentType('json')); $this->assertEquals(ContentType::JAVASCRIPT_APPLICATION, ContentTypeUtility::convertSuffixToContentType('js')); $this->assertEquals(ContentType::XML_TEXT, ContentTypeUtility::convertSuffixToContentType('xml')); $this->assertEquals(ContentType::HTML_TEXT, ContentTypeUtility::convertSuffixToContentType('html')); $this->assertEquals(ContentType::PLAIN_TEXT, ContentTypeUtility::convertSuffixToContentType('plain')); $this->assertEquals(ContentType::CSV_TEXT, ContentTypeUtility::convertSuffixToContentType('csv')); $this->assertEquals(ContentType::CSS_TEXT, ContentTypeUtility::convertSuffixToContentType('css')); }
/** * Handle the given request result * * @param HandlerResultInterface $result * @param Request $request * @param Response $response */ public function handleResult($result, $request, $response) { $formatter = $this->getFormatterForRequest($request); if ($result instanceof HandlerResultInterface) { $response->writeHead($result->getStatusCode(), array('Content-Type' => ContentTypeUtility::convertSuffixToContentType($formatter->getContentSuffix()) . '; charset=utf-8')); $responseData = $result->getData(); if ($responseData) { $response->end($formatter->format($result->getData())); } else { $response->end(); } } else { if ($result === NULL) { $response->writeHead(204, array('Content-Type' => ContentTypeUtility::convertSuffixToContentType($formatter->getContentSuffix()) . '; charset=utf-8')); $response->end($formatter->format('No content')); } else { throw new \UnexpectedValueException('Handler result is of type ' . gettype($result), 1413210970); } } }