2010-11-23 20:38:15 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-11-23 20:38:15 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class HTTPResponseTest extends SapphireTest {
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testStatusDescriptionStripsNewlines() {
|
2010-11-23 20:38:15 +01:00
|
|
|
$r = new SS_HTTPResponse('my body', 200, "my description \nwith newlines \rand carriage returns");
|
|
|
|
$this->assertEquals(
|
|
|
|
"my description with newlines and carriage returns",
|
|
|
|
$r->getStatusDescription()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testContentLengthHeader() {
|
2011-07-26 08:25:09 +02:00
|
|
|
$r = new SS_HTTPResponse('123ü');
|
2012-09-29 17:23:05 +02:00
|
|
|
$r->fixContentLength();
|
2011-07-26 08:25:09 +02:00
|
|
|
$this->assertNotNull($r->getHeader('Content-Length'), 'Content-length header is added');
|
|
|
|
$this->assertEquals(
|
|
|
|
5,
|
|
|
|
$r->getHeader('Content-Length'),
|
|
|
|
'Header matches actual content length in bytes'
|
|
|
|
);
|
|
|
|
|
|
|
|
$r->setBody('1234ü');
|
2012-09-29 17:23:05 +02:00
|
|
|
$r->fixContentLength();
|
2011-07-26 08:25:09 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
6,
|
|
|
|
$r->getHeader('Content-Length'),
|
|
|
|
'Header is updated when body is changed'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-27 02:26:25 +02:00
|
|
|
public function testHTTPResponseException() {
|
|
|
|
$response = new SS_HTTPResponse("Test", 200, 'OK');
|
|
|
|
|
|
|
|
// Confirm that the exception's statusCode and statusDescription take precedence
|
|
|
|
try {
|
|
|
|
throw new SS_HTTPResponse_Exception($response, 404, 'not even found');
|
|
|
|
|
|
|
|
} catch(SS_HTTPResponse_Exception $e) {
|
|
|
|
$this->assertEquals(404, $e->getResponse()->getStatusCode());
|
|
|
|
$this->assertEquals('not even found', $e->getResponse()->getStatusDescription());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Fail if we get to here
|
|
|
|
$this->assertFalse(true, 'Something went wrong with our test exception');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExceptionContentPlainByDefault() {
|
|
|
|
|
|
|
|
// Confirm that the exception's statusCode and statusDescription take precedence
|
|
|
|
try {
|
|
|
|
throw new SS_HTTPResponse_Exception("Some content that may be from a hacker", 404, 'not even found');
|
|
|
|
|
|
|
|
} catch(SS_HTTPResponse_Exception $e) {
|
|
|
|
$this->assertEquals("text/plain", $e->getResponse()->getHeader("Content-Type"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Fail if we get to here
|
|
|
|
$this->assertFalse(true, 'Something went wrong with our test exception');
|
|
|
|
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|