Skip to content

Commit

Permalink
Flesh out HTTP\RedirectResponseTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-parry committed Oct 10, 2018
1 parent f8624ec commit b3ae918
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
3 changes: 3 additions & 0 deletions system/HTTP/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,13 @@ protected function ensureSession()
$session = Services::session();

// Ensure we have the session started up.
// true for travis-ci, so not coverable
// @codeCoverageIgnoreStart
if ( ! isset($_SESSION))
{
$session->start();
}
// @codeCoverageIgnoreEnd

return $session;
}
Expand Down
56 changes: 50 additions & 6 deletions tests/system/HTTP/RedirectResponseTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace CodeIgniter\HTTP;
<?php

namespace CodeIgniter\HTTP;

use Config\App;
use Config\Autoload;
Expand All @@ -10,11 +12,10 @@

class RedirectResponseTest extends \CIUnitTestCase
{

/** @var RouteCollection */
protected $routes;

protected $request;

protected $config;

public function setUp()
Expand Down Expand Up @@ -47,14 +48,25 @@ public function testRedirectRoute()
{
$response = new RedirectResponse(new App());

$this->routes->add( 'exampleRoute', 'Home::index' );
$this->routes->add('exampleRoute', 'Home::index');

$response->route( 'exampleRoute' );
$response->route('exampleRoute');

$this->assertTrue($response->hasHeader('Location'));
$this->assertEquals('http://example.com/exampleRoute', $response->getHeaderLine('Location'));
}

public function testRedirectRouteBad()
{
$this->expectException(Exceptions\HTTPException::class);

$response = new RedirectResponse(new App());

$this->routes->add('exampleRoute', 'Home::index');

$response->route('differentRoute');
}

public function testRedirectRelativeConvertsToFullURI()
{
$response = new RedirectResponse($this->config);
Expand Down Expand Up @@ -97,7 +109,7 @@ public function testWithValidationErrors()

$validation = $this->createMock(Validation::class);
$validation->method('getErrors')
->willReturn(['foo' =>'bar']);
->willReturn(['foo' => 'bar']);

Services::injectMock('validation', $validation);

Expand Down Expand Up @@ -154,4 +166,36 @@ public function testRedirectWithQueryOnly()
$this->assertTrue($response->hasHeader('Location'));
$this->assertEquals('http://example.com/foo?foo=bar', $response->getHeaderLine('Location'));
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testRedirectBack()
{
$_SERVER['HTTP_REFERER'] = 'http://somewhere.com';
$this->request = new MockIncomingRequest($this->config, new URI('http://somewhere.com'), null, new UserAgent());
Services::injectMock('request', $this->request);

$response = new RedirectResponse(new App());

$returned = $response->back();
$this->assertEquals('http://somewhere.com', $returned->getHeader('location')->getValue());
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testRedirectBackMissing()
{
$_SESSION = [];

$response = new RedirectResponse(new App());

$returned = $response->back();

$this->assertSame($response, $returned);
}

}

0 comments on commit b3ae918

Please sign in to comment.