FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Better accepts implementation · JavaDevl/framework@2e92db2 · GitHub

Commit 2e92db2

Browse files
Graham Campbell
committed
Better accepts implementation
1 parent 5294372 commit 2e92db2

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

‎src/Illuminate/Http/Request.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,14 +604,14 @@ public function accepts($contentTypes)
604604

605605
foreach ((array) $contentTypes as $type)
606606
{
607-
$split = explode('/', $type);
608-
609-
if ($accept === $type || $accept === $split[0].'/*')
607+
if ($accept === $type || $accept === strtok('/', $type).'/*')
610608
{
611609
return true;
612610
}
613611

614-
if (preg_match('/'.$split[0].'\/.+\+'.$split[1].'/', $accept))
612+
$split = explode('/', $accept);
613+
614+
if (preg_match('/'.$split[0].'\/.+\+'.$split[1].'/', $type))
615615
{
616616
return true;
617617
}

‎tests/Http/HttpRequestTest.php‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,14 @@ public function testFormatReturnsAcceptsJson()
450450
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'application/json']);
451451
$this->assertEquals('json', $request->format());
452452
$this->assertTrue($request->accepts('application/json'));
453+
$this->assertTrue($request->accepts('application/baz+json'));
453454
$this->assertTrue($request->acceptsJson());
454455
$this->assertFalse($request->acceptsHtml());
455456

456457
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'application/foo+json']);
457458
$this->assertTrue($request->accepts('application/foo+json'));
458-
$this->assertTrue($request->accepts('application/json'));
459+
$this->assertFalse($request->accepts('application/bar+json'));
460+
$this->assertFalse($request->accepts('application/json'));
459461

460462
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'application/*']);
461463
$this->assertTrue($request->accepts('application/xml'));
@@ -477,13 +479,26 @@ public function testFormatReturnsAcceptsHtml()
477479
}
478480

479481

482+
public function testFormatReturnsAcceptsAll()
483+
{
484+
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => '*/*']);
485+
$this->assertEquals('html', $request->format());
486+
$this->assertTrue($request->accepts('text/html'));
487+
$this->assertTrue($request->accepts('foo/bar'));
488+
$this->assertTrue($request->accepts('application/baz+xml'));
489+
$this->assertTrue($request->acceptsHtml());
490+
$this->assertTrue($request->acceptsJson());
491+
}
492+
493+
480494
public function testFormatReturnsAcceptsMultiple()
481495
{
482496
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'application/json,text/*']);
483497
$this->assertTrue($request->accepts(['text/html', 'application/json']));
484498
$this->assertTrue($request->accepts('text/html'));
485499
$this->assertTrue($request->accepts('text/foo'));
486500
$this->assertTrue($request->accepts('application/json'));
501+
$this->assertTrue($request->accepts('application/baz+json'));
487502
}
488503

489504

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL