/** * @test */ public function shouldSetContentType() { //given $_SERVER['CONTENT_TYPE'] = 'application/json'; //when ContentType::set('text/plain'); //then $this->assertEquals('text/plain', ContentType::value()); }
public static function resolve() { $accept = array_keys(RequestHeaders::accept()) ?: array('*/*'); $supported = array('application/json' => 'application/json', 'application/xml' => 'application/xml', 'application/*' => 'application/json', 'text/html' => 'text/html', 'text/*' => 'text/html'); $intersection = array_intersect($accept, array_keys($supported)); if ($intersection) { return $supported[Arrays::first($intersection)]; } return Arrays::getValue($supported, ContentType::value(), 'text/html'); }
/** * @test */ public function shouldReturnHtmlForUnsupportedAcceptAndNoRequestContentType() { //given $_SERVER['HTTP_ACCEPT'] = 'application/unsupported'; ContentType::set(null); //when $resolved = ResponseTypeResolve::resolve(); //then $this->assertEquals('text/html', $resolved); }
/** * @test */ public function shouldAcceptEmptyJson() { //given StreamStub::register('json'); StreamStub::$body = ''; ContentType::set('application/json'); //when $parameters = $this->getRequestParameters('json://input'); //then ArrayAssert::that($parameters)->hasSize(0); StreamStub::unregister(); }
private static function _putRequestParameters($content) { if (Strings::equal(Arrays::getValue($_SERVER, 'REQUEST_METHOD'), 'PUT') && Strings::equalsIgnoreCase(ContentType::value(), 'application/x-www-form-urlencoded')) { parse_str($content, $parameters); return Arrays::toArray($parameters); } return false; }