Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing3/http #1306

Merged
merged 3 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions system/HTTP/Negotiate.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace CodeIgniter\HTTP;
<?php

namespace CodeIgniter\HTTP;

/**
* CodeIgniter
Expand Down Expand Up @@ -35,7 +37,6 @@
* @since Version 3.0.0
* @filesource
*/

use CodeIgniter\HTTP\Exceptions\HTTPException;

/**
Expand Down Expand Up @@ -211,13 +212,6 @@ protected function getBestMatch(array $supported, string $header = null, bool $e

$acceptable = $this->parseHeader($header);

// If no acceptable values exist, return the
// first that we support.
if (count($acceptable) === 0)
{
return $supported[0];
}

foreach ($acceptable as $accept)
{
// if acceptable quality is zero, skip it.
Expand Down Expand Up @@ -290,8 +284,8 @@ public function parseHeader(string $header)
}

$results[] = [
'value' => trim($value),
'q' => (float) $quality,
'value' => trim($value),
'q' => (float) $quality,
'params' => $parameters
];
}
Expand Down
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
32 changes: 24 additions & 8 deletions tests/system/HTTP/NegotiateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,28 +146,44 @@ public function testBestMatchEmpty()

public function testBestMatchNoHeader()
{
$this->request->setHeader('Accept','');
$this->request->setHeader('Accept', '');
$this->assertEquals('', $this->negotiate->media(['apple', 'banana'], true));
$this->assertEquals('apple/mac', $this->negotiate->media(['apple/mac', 'banana/yellow'], false));
}

public function testBestMatchNotAcceptable()
{
$this->request->setHeader('Accept','popcorn/cheddar');
$this->assertEquals('apple/mac', $this->negotiate->media(['apple/mac', 'banana/yellow'],false));
$this->request->setHeader('Accept', 'popcorn/cheddar');
$this->assertEquals('apple/mac', $this->negotiate->media(['apple/mac', 'banana/yellow'], false));
$this->assertEquals('banana/yellow', $this->negotiate->media(['banana/yellow', 'apple/mac'], false));
}

public function testBestMatchFirstSupported()
{
$this->request->setHeader('Accept','popcorn/cheddar, */*');
$this->assertEquals('apple/mac', $this->negotiate->media(['apple/mac', 'banana/yellow'],false));
$this->request->setHeader('Accept', 'popcorn/cheddar, */*');
$this->assertEquals('apple/mac', $this->negotiate->media(['apple/mac', 'banana/yellow'], false));
}

public function testBestMatchLowQuality()
{
$this->request->setHeader('Accept','popcorn/cheddar;q=0, apple/mac, */*');
$this->assertEquals('apple/mac', $this->negotiate->media(['apple/mac', 'popcorn/cheddar'],false));
$this->assertEquals('apple/mac', $this->negotiate->media(['popcorn/cheddar','apple/mac'],false));
$this->request->setHeader('Accept', 'popcorn/cheddar;q=0, apple/mac, */*');
$this->assertEquals('apple/mac', $this->negotiate->media(['apple/mac', 'popcorn/cheddar'], false));
$this->assertEquals('apple/mac', $this->negotiate->media(['popcorn/cheddar', 'apple/mac'], false));
}

public function testBestMatchOnlyLowQuality()
{
$this->request->setHeader('Accept', 'popcorn/cheddar;q=0');
// the first supported should be returned, since nothing will make us happy
$this->assertEquals('apple/mac', $this->negotiate->media(['apple/mac', 'popcorn/cheddar'], false));
$this->assertEquals('popcorn/cheddar', $this->negotiate->media(['popcorn/cheddar', 'apple/mac'], false));
}

public function testParameterMatching()
{
$this->request->setHeader('Accept', 'popcorn/cheddar;a=0;b=1');
$this->assertEquals('popcorn/cheddar;a=2', $this->negotiate->media(['popcorn/cheddar;a=2'], false));
$this->assertEquals('popcorn/cheddar;a=0', $this->negotiate->media(['popcorn/cheddar;a=0', 'popcorn/cheddar;a=2;b=1'], false));
}

}
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);
}

}