2012-03-24 03:19:02 +01:00
|
|
|
<?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
|
2012-06-05 14:22:00 +02:00
|
|
|
$response = $negotiator->respond($request);
|
|
|
|
$this->assertEquals('default response', $response->getBody());
|
2012-03-24 03:19:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2012-06-05 14:22:00 +02:00
|
|
|
$response = $negotiator->respond($request);
|
|
|
|
$this->assertEquals('{"myfragment":"myfragment response"}', $response->getBody());
|
2012-03-24 03:19:02 +01:00
|
|
|
}
|
|
|
|
|
2012-04-18 21:15:31 +02:00
|
|
|
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');
|
2012-06-05 14:22:00 +02:00
|
|
|
$response = $negotiator->respond($request);
|
|
|
|
$json = json_decode( $response->getBody());
|
2012-04-18 21:15:31 +02:00
|
|
|
$this->assertObjectHasAttribute('myfragment', $json);
|
|
|
|
$this->assertEquals('myfragment response', $json->myfragment);
|
|
|
|
$this->assertObjectHasAttribute('otherfragment', $json);
|
|
|
|
$this->assertEquals('otherfragment response', $json->otherfragment);
|
|
|
|
}
|
|
|
|
|
2012-06-07 00:26:27 +02:00
|
|
|
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');
|
|
|
|
|
2012-06-13 10:28:25 +02:00
|
|
|
$response = $negotiator->setFragmentOverride(array('beta'))->respond($request);
|
2012-06-07 00:26:27 +02:00
|
|
|
$json = json_decode( $response->getBody());
|
|
|
|
$this->assertFalse(isset($json->alpha));
|
|
|
|
$this->assertObjectHasAttribute('beta', $json);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|