Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

58 lines (50 sloc) 2.179 kB
<?php
class PjaxResponseNegotiatorTest extends SapphireTest {
function testDefaultCallbacks() {
$negotiator = new PjaxResponseNegotiator(array(
'default' => function() {return 'default response';},
));
$request = new SS_HTTPRequest('GET', '/'); // not setting pjax header
$response = $negotiator->respond($request);
$this->assertEquals('default response', $response->getBody());
}
function testSelectsFragmentByHeader() {
$negotiator = new PjaxResponseNegotiator(array(
'default' => function() {return 'default response';},
'myfragment' => function() {return 'myfragment response';},
));
$request = new SS_HTTPRequest('GET', '/');
$request->addHeader('X-Pjax', 'myfragment');
$response = $negotiator->respond($request);
$this->assertEquals('{"myfragment":"myfragment response"}', $response->getBody());
}
function testMultipleFragments() {
$negotiator = new PjaxResponseNegotiator(array(
'default' => function() {return 'default response';},
'myfragment' => function() {return 'myfragment response';},
'otherfragment' => function() {return 'otherfragment response';},
));
$request = new SS_HTTPRequest('GET', '/');
$request->addHeader('X-Pjax', 'myfragment,otherfragment');
$request->addHeader('Accept', 'text/json');
$response = $negotiator->respond($request);
$json = json_decode( $response->getBody());
$this->assertObjectHasAttribute('myfragment', $json);
$this->assertEquals('myfragment response', $json->myfragment);
$this->assertObjectHasAttribute('otherfragment', $json);
$this->assertEquals('otherfragment response', $json->otherfragment);
}
function testFragmentsOverride() {
$negotiator = new PjaxResponseNegotiator(array(
'alpha' => function() {return 'alpha response';},
'beta' => function() {return 'beta response';}
));
$request = new SS_HTTPRequest('GET', '/');
$request->addHeader('X-Pjax', 'alpha');
$request->addHeader('Accept', 'text/json');
$response = $negotiator->overrideFragments(array('beta'))->respond($request);
$json = json_decode( $response->getBody());
$this->assertFalse(isset($json->alpha));
$this->assertObjectHasAttribute('beta', $json);
}
}
Jump to Line
Something went wrong with that request. Please try again.