public function testAddCacheHeaders() { $body = "<html><head></head><body><h1>Mysite</h1></body></html>"; $response = new HTTPResponse($body, 200); $this->assertEmpty($response->getHeader('Cache-Control')); HTTP::set_cache_age(30); HTTP::add_cache_headers($response); $this->assertNotEmpty($response->getHeader('Cache-Control')); // Ensure max-age is zero for development. Director::config()->update('environment_type', 'dev'); $response = new HTTPResponse($body, 200); HTTP::add_cache_headers($response); $this->assertContains('max-age=0', $response->getHeader('Cache-Control')); // Ensure max-age setting is respected in production. Director::config()->update('environment_type', 'live'); $response = new HTTPResponse($body, 200); HTTP::add_cache_headers($response); $this->assertContains('max-age=30', explode(', ', $response->getHeader('Cache-Control'))); $this->assertNotContains('max-age=0', $response->getHeader('Cache-Control')); // Still "live": Ensure header's aren't overridden if already set (using purposefully different values). $headers = array('Vary' => '*', 'Pragma' => 'no-cache', 'Cache-Control' => 'max-age=0, no-cache, no-store'); $response = new HTTPResponse($body, 200); foreach ($headers as $name => $value) { $response->addHeader($name, $value); } HTTP::add_cache_headers($response); foreach ($headers as $name => $value) { $this->assertEquals($value, $response->getHeader($name)); } }
/** * If the last request was a 3xx response, then follow the redirection * * @return HTTPResponse The response given, or null if no redirect occurred */ public function followRedirection() { if ($this->lastResponse->getHeader('Location')) { $url = Director::makeRelative($this->lastResponse->getHeader('Location')); $url = strtok($url, '#'); return $this->get($url); } }
/** * Returns true if negotiation is enabled for the given response. By default, negotiation is only * enabled for pages that have the xml header. * * @param HTTPResponse $response * @return bool */ public static function enabled_for($response) { $contentType = $response->getHeader("Content-Type"); // Disable content negotiation for other content types if ($contentType && substr($contentType, 0, 9) != 'text/html' && substr($contentType, 0, 21) != 'application/xhtml+xml') { return false; } if (static::config()->enabled) { return true; } else { return substr($response->getBody(), 0, 5) == '<' . '?xml'; } }
/** * Assert that a response matches the given parameters * * @param int $code HTTP code * @param string $body Body expected for 200 responses * @param HTTPResponse $response */ protected function assertResponseEquals($code, $body, HTTPResponse $response) { $this->assertEquals($code, $response->getStatusCode()); if ($code === 200) { $this->assertFalse($response->isError()); $this->assertEquals($body, $response->getBody()); $this->assertEquals('text/plain', $response->getHeader('Content-Type')); } else { $this->assertTrue($response->isError()); } }